public void CreateBinaryExpression() { IExpression leftExpression = new ConstantExpression(1); IExpression rightExpression = new ConstantExpression(2); BinaryExpression expression = new ArithmeticBinaryExpression(ArithmeticOperator.Add, leftExpression, rightExpression); Assert.IsTrue(expression.LeftExpression == leftExpression); Assert.IsTrue(expression.RightExpression == rightExpression); }
private static object EvaluateArithmeticBinaryOperator(ArithmeticOperator operation, object left, object right) { IExpression expression = new ArithmeticBinaryExpression(operation, new ConstantExpression(left), new ConstantExpression(right)); return expression.Evaluate(null); }
private IExpression ParseBinaryExpressionSecondLevel() { IExpression expression = this.ParseUnaryExpression(); if (expression == null) return null; while (this.TryParse(TokenType.Operator, "*", "/", @"\", "%")) { Token oper = this.lexer.NextToken(); IExpression right = this.ParseUnaryExpression(); ArithmeticOperator op; if (oper.Value == "*") op = ArithmeticOperator.Multiply; else if (oper.Value == "/") op = ArithmeticOperator.Divide; else if (oper.Value == "\\") op = ArithmeticOperator.IntegerDivide; else if (oper.Value == "%") op = ArithmeticOperator.Modulo; else throw new ParserException(string.Format("Invalid operator '{0}'", oper.Value)); expression = new ArithmeticBinaryExpression(op, expression, right); } return expression; }
private IExpression ParseBinaryExpressionFirstLevel() { IExpression expression = this.ParseBinaryExpressionSecondLevel(); if (expression == null) return null; while (this.TryParse(TokenType.Operator, "+", "-")) { Token oper = this.lexer.NextToken(); IExpression right = this.ParseBinaryExpressionSecondLevel(); ArithmeticOperator op = oper.Value == "+" ? ArithmeticOperator.Add : ArithmeticOperator.Subtract; expression = new ArithmeticBinaryExpression(op, expression, right); } 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); BindingEnvironment environment = new BindingEnvironment(); environment.SetValue("y", null); forcmd.Execute(environment); Assert.AreEqual(15, environment.GetValue("y")); }
public void ExecuteWhileCommand() { IExpression incrementX = new ArithmeticBinaryExpression(ArithmeticOperator.Add, new ConstantExpression(1), new VariableExpression("x")); IExpression decrementY = new ArithmeticBinaryExpression(ArithmeticOperator.Subtract, new VariableExpression("y"), new ConstantExpression(1)); ICommand setX = new SetVariableCommand("x", incrementX); ICommand setY = new SetVariableCommand("y", decrementY); List<ICommand> commands = new List<ICommand>(); commands.Add(setX); commands.Add(setY); ICommand command = new CompositeCommand(commands); IExpression yexpr = new VariableExpression("y"); WhileCommand whilecmd = new WhileCommand(yexpr, command); BindingEnvironment environment = new BindingEnvironment(); environment.SetValue("x", 0); environment.SetValue("y", 5); whilecmd.Execute(environment); Assert.AreEqual(0, environment.GetValue("y")); Assert.AreEqual(5, environment.GetValue("x")); }
public void ExecuteForEachCommand() { IExpression addToX = new ArithmeticBinaryExpression(ArithmeticOperator.Add, new VariableExpression("y"), new VariableExpression("x")); ICommand setX = new SetVariableCommand("x", addToX); IExpression values = new ConstantExpression(new int[] { 1, 2, 3 }); ForEachCommand foreachcmd = new ForEachCommand("y", values, setX); BindingEnvironment environment = new BindingEnvironment(); environment.SetValue("x", 0); foreachcmd.Execute(environment); Assert.AreEqual(6, environment.GetValue("x")); }