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 ExecuteCompositeCommand() { Context context = new Context(); SetVariableCommand command1 = new SetVariableCommand("a", new ConstantExpression("bar")); SetVariableCommand command2 = new SetVariableCommand("b", new ConstantExpression(1)); SetVariableCommand command3 = new SetVariableCommand("c", new VariableExpression("a")); List<ICommand> commands = new List<ICommand>(); commands.Add(command1); commands.Add(command2); commands.Add(command3); CompositeCommand command = new CompositeCommand(commands); command.Execute(context); Assert.AreEqual("bar", context.GetValue("a")); Assert.AreEqual(1, context.GetValue("b")); Assert.AreEqual("bar", context.GetValue("c")); }
private FunctionExpression ParseFunctionExpression() { string name = null; if (this.TryPeekName()) { Token token = this.lexer.NextToken(); name = token.Value; } IList<ICommand> currentHoistedCommands = this.hoistedCommands; this.hoistedCommands = new List<ICommand>(); try { IList<string> arguments = this.ParseArgumentNames(); this.Parse(TokenType.Separator, "{"); CompositeCommand command = this.ParseCompositeCommand(); // TODO Review, should be 0 if (command.HoistedCommandCount > 0) throw new ParserException("Invalid command"); command = new CompositeCommand(this.hoistedCommands, command.Commands); return new FunctionExpression(name, arguments.ToArray(), command); } finally { this.hoistedCommands = currentHoistedCommands; } }
private ICommand ParseForInCommand() { string name = this.ParseName(); bool isvar = false; if (name == "var") { name = this.ParseName(); isvar = true; } this.Parse(TokenType.Name, "in"); IExpression values = this.ParseExpression(); this.Parse(TokenType.Separator, ")"); ICommand command = this.ParseCommand(); ICommand forcmd = new ForEachCommand(name, values, command); if (!isvar) return forcmd; // TODO review if var command should be hoisted ICommand cmds = new CompositeCommand(new List<ICommand>() { new VarCommand(name), forcmd }); return cmds; }
public void ExecuteWhileCommand() { IExpression incrementX = new ArithmeticBinaryExpression(ArithmeticOperator.Add, new ConstantExpression(1), new VariableExpression("a")); IExpression decrementY = new ArithmeticBinaryExpression(ArithmeticOperator.Subtract, new VariableExpression("b"), new ConstantExpression(1)); ICommand setX = new SetVariableCommand("a", incrementX); ICommand setY = new SetVariableCommand("b", decrementY); List<ICommand> commands = new List<ICommand>(); commands.Add(setX); commands.Add(setY); ICommand command = new CompositeCommand(commands); IExpression yexpr = new VariableExpression("b"); WhileCommand whilecmd = new WhileCommand(yexpr, command); Context context = new Context(); context.SetValue("a", 0); context.SetValue("b", 5); whilecmd.Execute(context); Assert.AreEqual(0, context.GetValue("b")); Assert.AreEqual(5, context.GetValue("a")); }