Skip to content

Commit

Permalink
Fix #284 - Implicit mul and exponentiation cannot be parsed (#285)
Browse files Browse the repository at this point in the history
* Fix #284 - Implicit mul and exponentiation cannot be parsed

* Update ReportGenerator
  • Loading branch information
sys27 committed Aug 29, 2020
1 parent 59e04ad commit e50b5e1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
20 changes: 18 additions & 2 deletions xFunc.Maths/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,7 @@ private IExpression MulImplicitLeftUnary(TokenReader tokenReader)
var number = Number(tokenReader);
if (number != null)
{
var rightUnary = Function(tokenReader) ??
Variable(tokenReader) ??
var rightUnary = MulImplicitExponentiation(tokenReader) ??
ParenthesesExpression(tokenReader) ??
Matrix(tokenReader) ??
Vector(tokenReader);
Expand All @@ -539,6 +538,23 @@ private IExpression MulImplicitLeftUnary(TokenReader tokenReader)
return null;
}

private IExpression MulImplicitExponentiation(TokenReader tokenReader)
{
var left = Function(tokenReader) ??
Variable(tokenReader);
if (left == null)
return null;

var @operator = tokenReader.Operator(OperatorToken.Exponentiation);
if (@operator == null)
return left;

var right = Exponentiation(tokenReader) ??
throw new ParseException(Resource.ExponentParseException);

return CreateExponentiation(left, right);
}

private IExpression LeftUnary(TokenReader tokenReader)
{
var token = (tokenReader.Operator(OperatorToken.Not) ??
Expand Down
41 changes: 41 additions & 0 deletions xFunc.Tests/ParserTests/ParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2151,5 +2151,46 @@ public void ParseCosWithoutOperator()

ParseErrorTest(tokens);
}

[Fact]
public void ImplicitMulAndPowerFunction()
{
var tokens = Builder()
.Number(2)
.Id("sin")
.OpenParenthesis()
.VariableX()
.CloseParenthesis()
.Operation(OperatorToken.Exponentiation)
.Number(2)
.Tokens;

var exp = parser.Parse(tokens);
var expected = new Mul(
Number.Two,
new Pow(new Sin(Variable.X), Number.Two)
);

Assert.Equal(expected, exp);
}

[Fact]
public void ImplicitMulAndPowerVariable()
{
var tokens = Builder()
.Number(2)
.VariableX()
.Operation(OperatorToken.Exponentiation)
.Number(2)
.Tokens;

var exp = parser.Parse(tokens);
var expected = new Mul(
Number.Two,
new Pow(Variable.X, Number.Two)
);

Assert.Equal(expected, exp);
}
}
}
2 changes: 1 addition & 1 deletion xFunc.Tests/xFunc.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</ItemGroup>

<ItemGroup>
<DotNetCliToolReference Include="dotnet-reportgenerator-cli" Version="4.4.7" />
<DotNetCliToolReference Include="dotnet-reportgenerator-cli" Version="4.6.5" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit e50b5e1

Please sign in to comment.