Example #1
0
        public void DefineCommand()
        {
            IList<ICommand> commandlist = new List<ICommand>();

            commandlist.Add(new SetVariableCommand("a", new ConstantExpression(1)));
            commandlist.Add(new SetVariableCommand("b", new ConstantExpression(2)));

            CompositeCommand commands = new CompositeCommand(commandlist);

            DefineCommand command = new DefineCommand("foo", null, commands);

            Assert.AreEqual("foo", command.Name);
            Assert.AreEqual(commands, command.Command);
        }
Example #2
0
        public void ExecuteDefineCommand()
        {
            Context context = new Context();
            IList<ICommand> commandlist = new List<ICommand>();

            commandlist.Add(new SetVariableCommand("a", new ConstantExpression(1)));
            commandlist.Add(new SetVariableCommand("b", new ConstantExpression(2)));
            commandlist.Add(new ExpressionCommand(new VariableExpression("b")));

            CompositeCommand commands = new CompositeCommand(commandlist);

            DefineCommand command = new DefineCommand("foo", null, commands);

            object result = command.Execute(context);

            Assert.IsNull(result);
            Assert.IsInstanceOfType(context.GetValue("foo"), typeof(DefinedMethod));
        }
Example #3
0
        public void ExecuteCallExpression()
        {
            Context context = new Context();
            IList<ICommand> commandlist = new List<ICommand>();

            commandlist.Add(new SetVariableCommand("a", new ConstantExpression(1)));
            commandlist.Add(new SetVariableCommand("b", new ConstantExpression(2)));
            commandlist.Add(new ExpressionCommand(new VariableExpression("b")));

            CompositeCommand commands = new CompositeCommand(commandlist);

            DefinedMethod method = new DefinedMethod(null, commands);

            CallExpression callexpr = new CallExpression(new ConstantExpression(method), null);

            object result = callexpr.Evaluate(context);

            Assert.AreEqual(2, result);
        }
        public void EvaluateDefinedMethod()
        {
            Context context = new Context();
            IList<ICommand> commandlist = new List<ICommand>();

            commandlist.Add(new SetVariableCommand("a", new ConstantExpression(1)));
            commandlist.Add(new SetVariableCommand("b", new ConstantExpression(2)));
            commandlist.Add(new ExpressionCommand(new VariableExpression("b")));

            CompositeCommand commands = new CompositeCommand(commandlist);

            DefinedMethod method = new DefinedMethod(null, commands);

            context.SetValue("foo", method);

            VariableExpression expr = new VariableExpression("foo");

            Assert.AreEqual(2, expr.Evaluate(context));
        }
Example #5
0
        public void ExecuteDefinedMethod()
        {
            Context context = new Context();
            IList<ICommand> commandlist = new List<ICommand>();

            commandlist.Add(new SetVariableCommand("a", new ConstantExpression(1)));
            commandlist.Add(new SetVariableCommand("b", new ConstantExpression(2)));
            commandlist.Add(new ExpressionCommand(new VariableExpression("b")));

            CompositeCommand commands = new CompositeCommand(commandlist);

            DefinedMethod method = new DefinedMethod(null, commands);

            object result = method.Call(context, null);

            Assert.IsNotNull(result);
            Assert.AreEqual(2, result);

            Assert.IsNull(context.GetValue("a"));
            Assert.IsNull(context.GetValue("b"));
        }
Example #6
0
        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"));
        }
Example #7
0
        private WhileCommand ParseWhileCommand()
        {
            IExpression condition = this.ParseExpression();
            this.ParseEndOfCommand();
            ICommand command = new CompositeCommand(this.ParseCommands());
            this.ParseToken("end", TokenType.Name);

            return new WhileCommand(condition, command);
        }
Example #8
0
        private IfCommand ParseIfCommand()
        {
            IExpression condition = this.ParseExpression();
            if (!this.TryParseToken("then", TokenType.Name))
                this.ParseEndOfCommand();
            ICommand thencommand = new CompositeCommand(this.ParseCommands());
            ICommand elsecommand = null;

            if (this.TryParseToken("else", TokenType.Name))
                elsecommand = new CompositeCommand(this.ParseCommands());

            this.ParseEnd();

            return new IfCommand(condition, thencommand, elsecommand);
        }