public void CreateAndExecuteDefineFunctionCommand() { IEnumerable<ICommand> commands = new ICommand[] { new SetVariableCommand("a", new ConstantExpression(1)), new SetVariableCommand("b", new ConstantExpression(2)) }; CompositeCommand body = new CompositeCommand(commands); DefineFunctionCommand command = new DefineFunctionCommand("foo", null, body); Context context = new Context(); Assert.IsNull(command.Execute(context)); Assert.AreEqual("foo", command.Name); Assert.IsNull(command.ArgumentNames); Assert.AreEqual(body, command.Command); var result = context.GetValue("foo"); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(DefinedFunction)); DefinedFunction dfunc = (DefinedFunction)result; Assert.AreEqual(2, dfunc.Call(context, null)); }
public void CreateAndExecuteCompositeCommand() { IEnumerable<ICommand> commands = new ICommand[] { new SetVariableCommand("a", new ConstantExpression(1)), new SetVariableCommand("b", new ConstantExpression(2)) }; CompositeCommand command = new CompositeCommand(commands); Context context = new Context(); Assert.IsNotNull(command.Commands); Assert.AreEqual(2, command.Commands.Count()); Assert.AreEqual(2, command.Execute(context)); Assert.AreEqual(1, context.GetValue("a")); Assert.AreEqual(2, context.GetValue("b")); }
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")); }
private static void EvaluateCommands(string text, Context context) { Parser parser = new Parser(text); var result = parser.ParseCommands(); var command = new CompositeCommand(result); command.Execute(context); }
public ICommand ParseCommand() { Token token = this.NextToken(); if (token == null) return null; if (token.Type == TokenType.Separator) { if (token.Value == ";") return NullCommand.Instance; if (token.Value == "{") { ICommand commands = new CompositeCommand(this.ParseCommands()); this.ParseToken("}", TokenType.Separator); return commands; } } if (token.Type == TokenType.Name) { if (token.Value == "if") return this.ParseIfCommand(); if (token.Value == "while") return this.ParseWhileCommand(); if (token.Value == "class") return this.ParseDefineClassCommand(); if (token.Value == "return") { var expr = this.ParseExpression(); this.ParseToken(";", TokenType.Separator); return new ReturnCommand(expr); } if (token.Value == "void") return this.ParseDefineFunction(this.ParseName()); if (token.Value == "main") return this.ParseDefineFunction(token.Value); } this.PushToken(token); IExpression expression = this.ParseExpression(); if (expression == null) return null; token = this.NextToken(); if (token != null && token.Type == TokenType.Name && this.IsTypeExpression(expression)) { if (this.TryPeekToken("(", TokenType.Separator)) return this.ParseDefineFunction(token.Value); return this.ParseDefineVariableCommand(expression, token.Value); } ICommand command = null; if (expression is VariableExpression && token.Type == TokenType.Operator && token.Value == "=") { string name = ((VariableExpression)expression).Name; IExpression expr = this.ParseExpression(); command = new SetVariableCommand(name, expr); token = this.NextToken(); } else command = new ExpressionCommand(expression); if (token == null || token.Type != TokenType.Separator || token.Value != ";") throw new ParserException("Expected ';'"); return command; }