private void Compare(object left, object right, Operator op, bool expected) { // LESS THAN var exp = new CompareExpression(new ConstantExpression(left), op, new ConstantExpression(right)); Assert.AreEqual(expected, exp.EvaluateAs <bool>()); }
private IExpression CompileBinaryLevel0Expression() { IExpression expression = this.CompileBinaryLevel1Expression(); if (expression == null) { return(null); } Token token = this.lexer.NextToken(); while (IsLevel0Operator(token)) { IExpression expression2 = this.CompileBinaryLevel1Expression(); expression = new CompareExpression(CompileCompareOperator(token.Value), expression, expression2); token = this.lexer.NextToken(); } if (token != null) { this.lexer.PushToken(token); } return(expression); }
public void ExecuteForCommand() { ICommand setX = new SetVariableCommand("x", new ConstantExpression(0)); ICommand setY = new SetVariableCommand("y", new ConstantExpression(0)); List <ICommand> commands = new List <ICommand>(); commands.Add(setX); commands.Add(setY); ICommand initialCommand = new CompositeCommand(commands); IExpression condition = new CompareExpression(ComparisonOperator.Less, new VariableExpression("x"), new ConstantExpression(6)); IExpression addXtoY = new ArithmeticBinaryExpression(ArithmeticOperator.Add, new VariableExpression("y"), new VariableExpression("x")); ICommand addToY = new SetVariableCommand("y", addXtoY); ICommand endCommand = new SetVariableCommand("x", new ArithmeticBinaryExpression(ArithmeticOperator.Add, new VariableExpression("x"), new ConstantExpression(1))); ForCommand forcmd = new ForCommand(initialCommand, condition, endCommand, addToY); Context context = new Context(); context.SetValue("y", null); forcmd.Execute(context); Assert.AreEqual(15, context.GetValue("y")); }
public void EvaluateCompareExpression() { CompareExpression expression = new CompareExpression(ComparisonOperator.Equal, new ConstantExpression(1), new ConstantExpression(2)); var result = expression.Evaluate(null); Assert.IsInstanceOfType(result, typeof(bool)); Assert.IsFalse((bool)result); Assert.AreEqual(ComparisonOperator.Equal, expression.Operation); }
public static ValidateLogic FirstTestLogic() { ValidateLogic VL = new ValidateLogic(); DeclareVariableStatement dvs = new DeclareVariableStatement("Times", typeof(INumber)); SetVariableStatement svs = new SetVariableStatement(new LongVar("Times"), new LongConst(0)); Execute initialEx = new Execute(); initialEx.Statements.Add(dvs); initialEx.Statements.Add(svs); SetVariableStatement svs2 = new SetVariableStatement(new LongVar("Times"), new ArithmeticExpression(new LongVar("Times"), null, Operator.PlusOne)); Execute ex2 = new Execute(svs2); CompareExpression AtLeast2 = new CompareExpression(new LongVar("Times"), new LongConst(2), Operator.GreaterThanOrEqualTo); Area ar1 = new Area(null, null, VL); VL.Areas.Add(ar1); AreaStart ap1 = new AreaStart(ar1); VL.StartNode = initialEx; initialEx.NextNode = ap1; CharsToIntegerPart stip = new CharsToIntegerPart(); ar1.StartNode = stip; UnitSet us1 = new UnitSet(CharUnits.Comma); us1.Units.Add(CharUnits.WhiteSpace); stip.NextNode = us1; CharsToIntegerPart stip2 = new CharsToIntegerPart(); us1.NextNode = stip2; UnitSet us2 = new UnitSet(CharUnits.WhiteSpace); stip2.NextNode = us2; CharsToIntegerPart stip3 = new CharsToIntegerPart(); us2.NextNode = stip3; stip3.NextNode = EndNode.Instance; UnitSet us3 = " CH".ToUnitSet(); us3.Units.Add(CharUnits.AtoZ); Status st = new Status(); ap1.NextNode = us3; UnitSet CRLF = "\r\n".ToUnitSet(); st.Choices.Add(new Choice(CRLF)); st.Choices.Add(new Choice(EndNode.Instance, AtLeast2)); us3.NextNode = st; CRLF.NextNode = ex2; ex2.NextNode = ap1; //12, 56 70 CHA //08, 32 45 CHR //98, -3 45 CHD return(VL); }
public Expression VisitAndConvert(IExpression expression) { return(expression switch { AndExpression andExpression => VisitAndConvert(andExpression), CompareExpression compareExpression => VisitAndConvert(compareExpression), InExpression inExpression => VisitAndConvert(inExpression), NotExpression notExpression => VisitAndConvert(notExpression), OrExpression orExpression => VisitAndConvert(orExpression), AssignExpression assignExpression => VisitAndConvert(assignExpression), _ => null });
private IExpression ParseBinaryExpressionZerothLevel() { IExpression expression = this.ParseBinaryExpressionFirstLevel(); if (expression == null) { return(null); } while (this.TryParse(TokenType.Operator, "<", ">", "==", ">=", "<=", "!=", "===", "!==")) { Token oper = this.lexer.NextToken(); IExpression right = this.ParseBinaryExpressionFirstLevel(); ComparisonOperator op = ComparisonOperator.Unknown; if (oper.Value == "<") { op = ComparisonOperator.Less; } if (oper.Value == ">") { op = ComparisonOperator.Greater; } if (oper.Value == "<=") { op = ComparisonOperator.LessEqual; } if (oper.Value == ">=") { op = ComparisonOperator.GreaterEqual; } if (oper.Value == "===") { op = ComparisonOperator.Equal; } if (oper.Value == "!==") { op = ComparisonOperator.NotEqual; } if (op == ComparisonOperator.Unknown) { throw new ParserException(string.Format("Unknown operator '{0}'", oper.Value)); } expression = new CompareExpression(op, expression, right); } return(expression); }
private static ICallable BuildFactorialFunction() { IExpression condition = new CompareExpression(ComparisonOperator.LessEqual, new VariableExpression("n"), new ConstantExpression(1)); ICommand return1 = new ReturnCommand(new ConstantExpression(1)); ICommand return2 = new ReturnCommand(new ArithmeticBinaryExpression(ArithmeticOperator.Multiply, new VariableExpression("n"), new InvokeExpression("Factorial", new IExpression[] { new ArithmeticBinaryExpression(ArithmeticOperator.Subtract, new VariableExpression("n"), new ConstantExpression(1)) }))); ICommand ifcmd = new IfCommand(condition, return1, return2); ICallable factorial = new Function(new string[] { "n" }, ifcmd); return(factorial); }
public void Equals() { CompareExpression expr1 = new CompareExpression(new ConstantExpression(1), new ConstantExpression(2), CompareOperator.Equal); CompareExpression expr2 = new CompareExpression(new ConstantExpression(1), new ConstantExpression(2), CompareOperator.NotEqual); CompareExpression expr3 = new CompareExpression(new ConstantExpression(1), new ConstantExpression(2), CompareOperator.Equal); Assert.IsTrue(expr1.Equals(expr3)); Assert.IsTrue(expr3.Equals(expr1)); Assert.AreEqual(expr1.GetHashCode(), expr3.GetHashCode()); Assert.IsFalse(expr1.Equals(null)); Assert.IsFalse(expr1.Equals(123)); Assert.IsFalse(expr1.Equals(expr2)); Assert.IsFalse(expr2.Equals(expr1)); }
public void ParseSimpleCompareExpression() { IExpression expression = ParseExpression("x <= 1"); Assert.IsNotNull(expression); Assert.IsInstanceOfType(expression, typeof(CompareExpression)); CompareExpression operation = (CompareExpression)expression; Assert.AreEqual(ComparisonOperator.LessEqual, operation.Operation); Assert.IsNotNull(operation.LeftExpression); Assert.IsInstanceOfType(operation.LeftExpression, typeof(VariableExpression)); Assert.IsNotNull(operation.RightExpression); Assert.IsInstanceOfType(operation.RightExpression, typeof(ConstantExpression)); }
public void ParseEqualsOperator() { Parser parser = new Parser("a == 1"); IExpression expr = parser.ParseExpression(); Assert.IsNotNull(expr); Assert.That(expr is CompareExpression); CompareExpression cexpr = (CompareExpression)expr; Assert.AreEqual(ComparisonOperator.Equal, cexpr.Operation); Assert.That(cexpr.LeftExpression is VariableExpression); Assert.That(cexpr.RightExpression is ConstantExpression); Assert.IsNull(parser.ParseExpression()); }
public void ParseLessOperator() { Parser parser = new Parser("a < 1"); IExpression expr = parser.ParseExpression(); Assert.IsNotNull(expr); Assert.IsInstanceOfType(expr, typeof(CompareExpression)); CompareExpression cexpr = (CompareExpression)expr; Assert.AreEqual(ComparisonOperator.Less, cexpr.Operation); Assert.IsInstanceOfType(cexpr.LeftExpression, typeof(VariableExpression)); Assert.IsInstanceOfType(cexpr.RightExpression, typeof(ConstantExpression)); Assert.IsNull(parser.ParseExpression()); }
public void CreateAndEvaluateSimpleWhileCommand() { BindingEnvironment environment = new BindingEnvironment(); environment.SetValue("a", 1); IExpression condition = new CompareExpression(ComparisonOperator.Less, new NameExpression("a"), new ConstantExpression(10)); ICommand body = new SetCommand("a", new BinaryOperatorExpression(new NameExpression("a"), new ConstantExpression(1), BinaryOperator.Add)); WhileCommand command = new WhileCommand(condition, body); command.Execute(environment); Assert.AreEqual(condition, command.Condition); Assert.AreEqual(body, command.Command); Assert.AreEqual(10, environment.GetValue("a")); }
private IExpression ParseBinaryExpressionLevel0() { IExpression expression = this.ParseBinaryExpressionLevel1(); Token token = this.lexer.NextToken(); while (token != null && token.TokenType == TokenType.Operator && (token.Value == ">" || token.Value == "<" || token.Value == "<=" || token.Value == ">=" || token.Value == "<>" || token.Value == "!=" || token.Value == "==")) { if (token.Value == ">") { expression = new CompareExpression(expression, this.ParseBinaryExpressionLevel1(), CompareOperator.Greater); } else if (token.Value == "<") { expression = new CompareExpression(expression, this.ParseBinaryExpressionLevel1(), CompareOperator.Less); } else if (token.Value == ">=") { expression = new CompareExpression(expression, this.ParseBinaryExpressionLevel1(), CompareOperator.GreaterEqual); } else if (token.Value == "<=") { expression = new CompareExpression(expression, this.ParseBinaryExpressionLevel1(), CompareOperator.LessEqual); } else if (token.Value == "<>" || token.Value == "!=") { expression = new CompareExpression(expression, this.ParseBinaryExpressionLevel1(), CompareOperator.NotEqual); } else if (token.Value == "==") { expression = new CompareExpression(expression, this.ParseBinaryExpressionLevel1(), CompareOperator.Equal); } token = this.lexer.NextToken(); } if (token != null) { this.lexer.PushToken(token); } return(expression); }
public void IfFalseThen() { var condition = new CompareExpression(ComparisonOperator.Less, new VariableExpression("a"), new ConstantExpression(10)); var thencommand = new SetVariableCommand("k", new ConstantExpression(1)); var ifcommand = new IfCommand(condition, thencommand); Context context = new Context(); context.SetValue("a", 10); context.SetValue("k", 0); var result = ifcommand.Execute(context); Assert.IsNull(result); Assert.IsNotNull(ifcommand.Condition); Assert.IsNotNull(ifcommand.ThenCommand); Assert.IsNull(ifcommand.ElseCommand); Assert.AreEqual(0, context.GetValue("k")); }
public void WhileWithAddExpression() { var expression = new CompareExpression(ComparisonOperator.Less, new VariableExpression("a"), new ConstantExpression(10)); var inccommand = new SetVariableCommand("k", new ArithmeticBinaryExpression(ArithmeticOperator.Add, new VariableExpression("k"), new ConstantExpression(1))); var addcommand = new SetVariableCommand("a", new ArithmeticBinaryExpression(ArithmeticOperator.Add, new VariableExpression("k"), new VariableExpression("a"))); var command = new CompositeCommand(new ICommand[] { inccommand, addcommand }); var whilecommand = new WhileCommand(expression, command); Context context = new Context(); context.SetValue("a", 0); context.SetValue("k", 0); var result = whilecommand.Execute(context); Assert.IsNull(result); Assert.IsNotNull(whilecommand.Condition); Assert.IsNotNull(whilecommand.Command); Assert.AreEqual(4, context.GetValue("k")); Assert.AreEqual(10, context.GetValue("a")); }
public void CompareExpression() { FakeVariableLinker fvl = new FakeVariableLinker(); LongConst a = new LongConst(0304); NumberConst b = new DoubleConst(56.8); ObjectConst c = new DoubleConst(690.8); Assert.IsFalse((a < b).Value); Assert.IsTrue((a < c).Value); CompareExpression ce = new CompareExpression(new LongConst(30), new LongConst(30)); Assert.IsTrue(ce.GetResult(fvl).Value); CompareExpression ce2 = new CompareExpression(new LongConst(30), new LongVar(FakeVariableLinker.IntA)); Assert.IsFalse(ce2.GetResult(fvl).Value); ArithmeticExpression ae = new ArithmeticExpression(new LongConst(30), new DoubleConst(30d)); CompareExpression ce3 = new CompareExpression(ae, new LongVar(FakeVariableLinker.IntA)); Assert.IsTrue(ce3.GetResult(fvl).Value); CompareExpression ce4 = new CompareExpression(new LongConst(40), new DoubleConst(30), Operator.GreaterThan); Assert.IsTrue(ce4.GetResult(fvl).Value); CompareExpression ce5 = new CompareExpression(new BooleanConst(true), new DoubleConst(30), Operator.GreaterThan); Assert.ThrowsException <ArithmeticException>(() => ce5.GetResult(fvl)); CompareExpression ce6 = new CompareExpression(new LongConst(20), new DoubleConst(30), Operator.LessThan); Assert.IsTrue(ce6.GetResult(fvl).Value); CompareExpression ce7 = new CompareExpression(new LongConst(20), new DoubleConst(20), Operator.LessThanOrEqualTo); Assert.IsTrue(ce7.GetResult(fvl).Value); CompareExpression ce8 = new CompareExpression(new DoubleVar(FakeVariableLinker.DoubleA), new DoubleConst(2.55), Operator.NotEqualTo); Assert.IsFalse(ce8.GetResult(fvl).Value); }
public void CompareGreaterIntegers() { CompareExpression expr = new CompareExpression(new ConstantExpression(1), new ConstantExpression(2), CompareOperator.Greater); Assert.AreEqual(false, expr.Evaluate(null)); }
private IExpression ParseBinaryExpression(int level) { if (level >= binaryoperators.Length) { return(this.ParseTerm()); } IExpression expr = this.ParseBinaryExpression(level + 1); if (expr == null) { return(null); } Token token; for (token = this.lexer.NextToken(); token != null && this.IsBinaryOperator(level, token); token = this.lexer.NextToken()) { if (token.Value == "+") { expr = new AddExpression(expr, this.ParseBinaryExpression(level + 1)); } if (token.Value == "-") { expr = new SubtractExpression(expr, this.ParseBinaryExpression(level + 1)); } if (token.Value == "*") { expr = new MultiplyExpression(expr, this.ParseBinaryExpression(level + 1)); } if (token.Value == "/") { expr = new DivideExpression(expr, this.ParseBinaryExpression(level + 1)); } if (token.Value == "==") { expr = new CompareExpression(expr, this.ParseBinaryExpression(level + 1), CompareOperator.Equal); } if (token.Value == "!=") { expr = new CompareExpression(expr, this.ParseBinaryExpression(level + 1), CompareOperator.NotEqual); } if (token.Value == "<") { expr = new CompareExpression(expr, this.ParseBinaryExpression(level + 1), CompareOperator.Less); } if (token.Value == ">") { expr = new CompareExpression(expr, this.ParseBinaryExpression(level + 1), CompareOperator.Greater); } if (token.Value == "<=") { expr = new CompareExpression(expr, this.ParseBinaryExpression(level + 1), CompareOperator.LessOrEqual); } if (token.Value == ">=") { expr = new CompareExpression(expr, this.ParseBinaryExpression(level + 1), CompareOperator.GreaterOrEqual); } if (token.Value == "..") { expr = new RangeExpression(expr, this.ParseBinaryExpression(level + 1)); } } if (token != null) { this.lexer.PushToken(token); } return(expr); }
public void CompareLessOrEqualIntegers() { CompareExpression expr = new CompareExpression(new ConstantExpression(1), new ConstantExpression(2), CompareOperator.LessOrEqual); Assert.AreEqual(true, expr.Evaluate(null)); }
internal static LogicalExpression <TSource> CompileLogicalExpression <TSource, TToken>(string text, Func <string, TToken> pathToken) where TToken : Token <TSource> { LogicalExpression <TSource> result = null; var whereMatch = parser.Match(text); if (whereMatch == null || !whereMatch.Success) { throw new ArgumentException("Invalid expression \"" + text + "\"."); } var exprCaptures = whereMatch.Groups["expr"].Captures; var nextCaptures = whereMatch.Groups["next"].Captures; AndExpressionGroup <TSource> andGroup = null; for (var i = 0; i < exprCaptures.Count; i++) { var exprText = exprCaptures[i].Value.Trim(); var nextText = nextCaptures[i].Value.Trim(); IEvaluate <TSource> expr; if (Regex.IsMatch(exprText, @"^\(.*\)$")) { expr = CompileLogicalExpression <TSource, TToken>(exprText.Substring(1, exprText.Length - 2), pathToken); } else { var exprMatch = exprParser.Match(exprText); if (exprMatch == null || !exprMatch.Success) { throw new Exception("Invalid expression \"" + exprText + "\"."); } var leftText = exprMatch.Groups["left"].Value.Trim(); var opText = exprMatch.Groups["op"].Value.Trim(); var rightText = exprMatch.Groups["right"].Value.Trim(); if (string.IsNullOrEmpty(opText)) { // No operator, so this is a truthy property or constant check. object value; if (JavaScriptHelpers.TryParseConstant(leftText, out value)) { expr = new TruthyConstantExpression <TSource>(new ConstantToken <TSource>(value)); } else { expr = new PathExpression <TSource>(pathToken(leftText)); } } else { // Parse the comparison operator. LogicalOperator op = ParseLogicalOperator(opText); // Parse the left-hand token. Token <TSource> leftToken; object leftValue; if (JavaScriptHelpers.TryParseConstant(leftText, out leftValue)) { leftToken = new ConstantToken <TSource>(leftValue); } else { leftToken = pathToken(leftText); } // Parse the right-hand token. Token <TSource> rightToken; object rightValue; if (JavaScriptHelpers.TryParseConstant(rightText, out rightValue)) { rightToken = new ConstantToken <TSource>(rightValue); } else { rightToken = pathToken(rightText); } // Create the expression from "left op right". expr = new CompareExpression <TSource>(leftToken, op, rightToken); } } if (nextText == "&&" && andGroup == null) { // There is currently no active and group and the next expression // will be "ANDed", so start a new and group, beginning with this expression. andGroup = AndExpressionGroup <TSource> .Begin(expr); } else if (andGroup != null) { // There is an active and group expression, so add this expression to it. andGroup.And(expr); } else if (result != null) { // There is currently a result, so or it with this expression. result = result.Or(expr); } else { // There is currently no result, so use this expression as the result. result = new TruthyExpressionWrapper <TSource>(expr); } // Add the existing group if we have reached the end of the expression, or the next expression will be "ORed". if ((string.IsNullOrEmpty(nextText) || nextText == "||") && andGroup != null) { if (result == null) { result = andGroup; } else { result = result.Or(andGroup); } andGroup = null; } } return(result); }
/// <summary> /// build a XPT Node out of a selection condition /// </summary> /// <param name="ctx"></param> /// <returns></returns> public bool BuildXPTNode(SelectConditionContext ctx) { string entryName; //| RPAREN selectConditions [$ClassName, $keypos] LPAREN if (ctx.selectConditions() != null) { if (ctx.NOT() == null) { ctx.XPTreeNode = ctx.selectConditions().XPTreeNode; } else { ctx.XPTreeNode = LogicalExpression.NOT((IExpression)ctx.selectConditions().XPTreeNode); } // set the max priority for disabling reshuffle ((OperationExpression)ctx.XPTreeNode).Priority = uint.MaxValue; return(true); } else { // determine the key name with the key is not provided by the key position // if (ctx.dataObjectEntry == null) { string aClassName = GetDefaultClassName(ctx); if (this.Engine.Repository.HasDataObjectDefinition(aClassName)) { iObjectDefinition aObjectDefinition = this.Engine.GetDataObjectDefinition(aClassName); if (ctx.keypos <= aObjectDefinition.Keys.Count()) { entryName = aClassName + "." + aObjectDefinition.Keys[ctx.keypos - 1]; } else { this.NotifyErrorListeners(String.Format(Messages.RCM_8, aClassName, aObjectDefinition.Keys.Count(), ctx.keypos)); return(false); } } else { this.NotifyErrorListeners(String.Format(Messages.RCM_9, aClassName)); return(false); } } else { entryName = ctx.dataObjectEntry.entryname; } // get the symbol DataObjectEntrySymbol aSymbol = new DataObjectEntrySymbol(entryName, engine: this.Engine); // Operator Operator anOperator; // default operator is the EQ operator if (ctx.Operator == null) { anOperator = Engine.GetOperator(new Token(Token.EQ)); } else { anOperator = ctx.Operator.Operator; } // build the comparer expression CompareExpression aCompare = null; if (aSymbol != null && ctx.select.XPTreeNode != null) { aCompare = new CompareExpression(anOperator, aSymbol, (IExpression)ctx.select.XPTreeNode); } else { return(false); } // set it ctx.XPTreeNode = aCompare; return(true); } return(false); }
/// <summary> /// Checks if the source supports the expression. /// </summary> /// <param name="expression"> /// The expression to check for. /// </param> /// <returns> /// <c>true</c> if the expression is supported, false otherwise. /// </returns> public bool SupportsExpression(CompareExpression expression) { var field = (expression.Left as FieldExpression)?.FieldName; return((field == "RowKey" || field == "PartitionKey") && !expression.Right.GetFields().Any()); }
private static bool EvaluateComparisonOperator(ComparisonOperator operation, object left, object right) { IExpression expression = new CompareExpression(operation, new ConstantExpression(left), new ConstantExpression(right)); return((bool)expression.Evaluate(null)); }