Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for wrong precedence order #62 #63

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion Jace.Tests/AstBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public void TestSinFunction3()
}

[TestMethod]
public void TestUnaryMinus()
public void TestUnaryMinus1()
{
IFunctionRegistry registry = new MockFunctionRegistry();

Expand All @@ -304,6 +304,29 @@ public void TestUnaryMinus()
Assert.AreEqual(new IntegerConstant(42), addition.Argument2);
}

[TestMethod]
public void TestUnaryMinus2()
{
IFunctionRegistry registry = new MockFunctionRegistry();

AstBuilder builder = new AstBuilder(registry, false);
Operation operation = builder.Build(new List<Token>() {
new Token() { Value = '_', TokenType = TokenType.Operation },
new Token() { Value = '(', TokenType = TokenType.LeftBracket },
new Token() { Value = 1, TokenType = TokenType.Integer },
new Token() { Value = ')', TokenType = TokenType.RightBracket },
new Token() { Value = '^', TokenType = TokenType.Operation },
new Token() { Value = 2, TokenType = TokenType.Integer },
});

UnaryMinus unaryMinus = (UnaryMinus)operation;

Exponentiation exponentiation = (Exponentiation)unaryMinus.Argument;
Assert.AreEqual(new IntegerConstant(1), exponentiation.Base);
Assert.AreEqual(new IntegerConstant(2), exponentiation.Exponent);
}


[TestMethod]
public void TestBuildInvalidFormula1()
{
Expand Down
18 changes: 18 additions & 0 deletions Jace.Tests/CalculationEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,24 @@ public void TestUnaryMinus5Interpreted()
Assert.AreEqual(-8.0, result);
}

[TestMethod]
public void TestUnaryMinus6Compiled()
{
CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Compiled, true, false, true);
double result = engine.Calculate("-(1*2)^2");

Assert.AreEqual(-4.0, result);
}

[TestMethod]
public void TestUnaryMinus6Interpreted()
{
CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Interpreted, true, false, true);
double result = engine.Calculate("-(1*2)^2");

Assert.AreEqual(-4.0, result);
}

[TestMethod]
public void TestBuild()
{
Expand Down
37 changes: 37 additions & 0 deletions Jace.Tests/TokenReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -574,5 +574,42 @@ public void TestTokenReader26()
Assert.AreEqual(7, tokens[2].StartPosition);
Assert.AreEqual(6, tokens[2].Length);
}

[TestMethod]
public void TestTokenReader27()
{
TokenReader reader = new TokenReader(CultureInfo.InvariantCulture);
List<Token> tokens = reader.Read("-(1)^2");

Assert.AreEqual(6, tokens.Count);

Assert.AreEqual('_', tokens[0].Value);
Assert.AreEqual(0, tokens[0].StartPosition);
Assert.AreEqual(1, tokens[0].Length);
Assert.AreEqual(TokenType.Operation, tokens[0].TokenType);

Assert.AreEqual('(', tokens[1].Value);
Assert.AreEqual(1, tokens[1].StartPosition);
Assert.AreEqual(1, tokens[1].Length);
Assert.AreEqual(TokenType.LeftBracket, tokens[1].TokenType);

Assert.AreEqual(1, tokens[2].Value);
Assert.AreEqual(2, tokens[2].StartPosition);
Assert.AreEqual(1, tokens[2].Length);

Assert.AreEqual(')', tokens[3].Value);
Assert.AreEqual(3, tokens[3].StartPosition);
Assert.AreEqual(1, tokens[3].Length);
Assert.AreEqual(TokenType.RightBracket, tokens[3].TokenType);

Assert.AreEqual('^', tokens[4].Value);
Assert.AreEqual(4, tokens[4].StartPosition);
Assert.AreEqual(1, tokens[4].Length);
Assert.AreEqual(TokenType.Operation, tokens[4].TokenType);

Assert.AreEqual(2, tokens[5].Value);
Assert.AreEqual(5, tokens[5].StartPosition);
Assert.AreEqual(1, tokens[5].Length);
}
}
}
4 changes: 2 additions & 2 deletions Jace/AstBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public AstBuilder(IFunctionRegistry functionRegistry, bool caseSensitive, IConst
operationPrecedence.Add('*', 4);
operationPrecedence.Add('/', 4);
operationPrecedence.Add('%', 4);
operationPrecedence.Add('_', 6);
operationPrecedence.Add('^', 5);
operationPrecedence.Add('_', 5);
operationPrecedence.Add('^', 6);
}

public Operation Build(IList<Token> tokens)
Expand Down