Skip to content

Commit

Permalink
Support for the binary and operation
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterderycke committed Aug 15, 2018
1 parent f3712bc commit 196dbec
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 13 deletions.
16 changes: 16 additions & 0 deletions Jace.Tests/CalculationEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -823,5 +823,21 @@ public void TestCustomFunctionDynamicFuncNestedDynamicCompiled()
double expected = (11 * (11 + 1)) / 2.0;
Assert.AreEqual(expected, result);
}

[TestMethod]
public void TestAndCompiled()
{
CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Compiled, true, false);
double result = engine.Calculate("(1&0)");
Assert.AreEqual(0, result);
}

[TestMethod]
public void TestAndInterpreted()
{
CalculationEngine engine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Interpreted, true, false);
double result = engine.Calculate("(1&0)");
Assert.AreEqual(0, result);
}
}
}
32 changes: 19 additions & 13 deletions Jace/AstBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,20 @@ public AstBuilder(IFunctionRegistry functionRegistry)
this.functionRegistry = functionRegistry;

operationPrecedence.Add('(', 0);
operationPrecedence.Add('<', 1);
operationPrecedence.Add('>', 1);
operationPrecedence.Add('≤', 1);
operationPrecedence.Add('≥', 1);
operationPrecedence.Add('≠', 1);
operationPrecedence.Add('=', 1);
operationPrecedence.Add('+', 2);
operationPrecedence.Add('-', 2);
operationPrecedence.Add('*', 3);
operationPrecedence.Add('/', 3);
operationPrecedence.Add('%', 3);
operationPrecedence.Add('_', 5);
operationPrecedence.Add('^', 4);
operationPrecedence.Add('&', 1);
operationPrecedence.Add('<', 2);
operationPrecedence.Add('>', 2);
operationPrecedence.Add('≤', 2);
operationPrecedence.Add('≥', 2);
operationPrecedence.Add('≠', 2);
operationPrecedence.Add('=', 2);
operationPrecedence.Add('+', 3);
operationPrecedence.Add('-', 3);
operationPrecedence.Add('*', 4);
operationPrecedence.Add('/', 4);
operationPrecedence.Add('%', 4);
operationPrecedence.Add('_', 6);
operationPrecedence.Add('^', 5);
}

public Operation Build(IList<Token> tokens)
Expand Down Expand Up @@ -213,6 +214,11 @@ private Operation ConvertOperation(Token operationToken)
Operation @base = resultStack.Pop();

return new Exponentiation(DataType.FloatingPoint, @base, exponent);
case '&':
argument2 = resultStack.Pop();
argument1 = resultStack.Pop();
dataType = RequiredDataType(argument1, argument2);
return new And(dataType, argument1, argument2);
case '<':
argument2 = resultStack.Pop();
argument1 = resultStack.Pop();
Expand Down
10 changes: 10 additions & 0 deletions Jace/Execution/DynamicCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ public double Execute(Operation operation, IFunctionRegistry functionRegistry)
Expression argument = GenerateMethodBody(unaryMinus.Argument, contextParameter, functionRegistry);
return Expression.Negate(argument);
}
else if (operation.GetType() == typeof(And))
{
And and = (And)operation;
Expression argument1 = Expression.NotEqual(GenerateMethodBody(and.Argument1, contextParameter, functionRegistry), Expression.Constant(0.0));
Expression argument2 = Expression.NotEqual(GenerateMethodBody(and.Argument2, contextParameter, functionRegistry), Expression.Constant(0.0));

return Expression.Condition(Expression.And(argument1, argument2),
Expression.Constant(1.0),
Expression.Constant(0.0));
}
else if (operation.GetType() == typeof(LessThan))
{
LessThan lessThan = (LessThan)operation;
Expand Down
5 changes: 5 additions & 0 deletions Jace/Execution/Interpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public double Execute(Operation operation, IFunctionRegistry functionRegistry)
UnaryMinus unaryMinus = (UnaryMinus)operation;
return -Execute(unaryMinus.Argument, functionRegistry, variables);
}
else if (operation.GetType() == typeof(And))
{
And lessThan = (And)operation;
return ((Execute(lessThan.Argument1, functionRegistry, variables) != 0) && (Execute(lessThan.Argument2, functionRegistry, variables) != 0)) ? 1.0 : 0.0;
}
else if(operation.GetType() == typeof(LessThan))
{
LessThan lessThan = (LessThan)operation;
Expand Down
19 changes: 19 additions & 0 deletions Jace/Operations/And.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Jace.Operations
{
public class And : Operation
{
public And(DataType dataType, Operation argument1, Operation argument2)
: base(dataType, argument1.DependsOnVariables || argument2.DependsOnVariables)
{
this.Argument1 = argument1;
this.Argument2 = argument2;
}

public Operation Argument1 { get; internal set; }
public Operation Argument2 { get; internal set; }
}
}
1 change: 1 addition & 0 deletions Jace/Tokenizer/TokenReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public List<Token> Read(string formula)
case '≤':
case '≥':
case '≠':
case '&':
if (IsUnaryMinus(characters[i], tokens))
{
// We use the token '_' for a unary minus in the AST builder
Expand Down

0 comments on commit 196dbec

Please sign in to comment.