Skip to content

Commit

Permalink
Fix #284 - Implicit mul and exponentiation cannot be parsed
Browse files Browse the repository at this point in the history
  • Loading branch information
sys27 committed Aug 29, 2020
1 parent 59e04ad commit b7ab274
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 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
22 changes: 22 additions & 0 deletions xFunc.Tests/ParserTests/ParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2151,5 +2151,27 @@ public void ParseCosWithoutOperator()

ParseErrorTest(tokens);
}

[Fact]
public void ImplicitMulAndPower()
{
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);
}
}
}

0 comments on commit b7ab274

Please sign in to comment.