Beispiel #1
0
        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 ExecuteForEachCommand()
        {
            IExpression addToX = new ArithmeticBinaryExpression(ArithmeticOperator.Add, new VariableExpression("b"), new VariableExpression("a"));
            ICommand    setX   = new SetVariableCommand("a", addToX);
            IExpression values = new ConstantExpression(new int [] { 1, 2, 3 });

            ForEachCommand foreachcmd = new ForEachCommand("b", values, setX);

            Context context = new Context();

            context.SetValue("a", 0);

            foreachcmd.Execute(context);

            Assert.AreEqual(6, context.GetValue("a"));
        }
Beispiel #3
0
        public void ParseSimpleForEachWithLocalVar()
        {
            ICommand command = ParseCommand("foreach (var x in xs) y=y+x;");

            Assert.IsNotNull(command);
            Assert.IsInstanceOfType(command, typeof(ForEachCommand));

            ForEachCommand foreachcmd = (ForEachCommand)command;

            Assert.AreEqual("x", foreachcmd.Name);
            Assert.IsNotNull(foreachcmd.Expression);
            Assert.IsInstanceOfType(foreachcmd.Expression, typeof(VariableExpression));
            Assert.IsNotNull(foreachcmd.Command);
            Assert.IsInstanceOfType(foreachcmd.Command, typeof(SetCommand));
            Assert.IsTrue(foreachcmd.LocalVariable);
        }
Beispiel #4
0
        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"));
        }
Beispiel #5
0
        public void ParseSimpleForIn()
        {
            ICommand command = ParseCommand("for (var k in b) a=a+k;");

            Assert.IsNotNull(command);
            Assert.IsInstanceOfType(command, typeof(CompositeCommand));

            IList <ICommand> cmds = ((CompositeCommand)command).Commands.ToList();

            Assert.IsInstanceOfType(cmds[0], typeof(VarCommand));
            Assert.IsInstanceOfType(cmds[1], typeof(ForEachCommand));

            ForEachCommand foreachcmd = (ForEachCommand)cmds[1];

            Assert.IsNotNull(foreachcmd.Expression);
            Assert.IsInstanceOfType(foreachcmd.Expression, typeof(VariableExpression));
            Assert.IsNotNull(foreachcmd.Command);
            Assert.IsInstanceOfType(foreachcmd.Command, typeof(SetCommand));
        }
Beispiel #6
0
        public void ParseSimpleForInWithLocalVar()
        {
            ICommand command = ParseCommand("for (var x in b) c=c+x;");

            Assert.IsNotNull(command);
            Assert.IsInstanceOfType(command, typeof(CompositeCommand));

            CompositeCommand ccmd = (CompositeCommand)command;

            Assert.AreEqual(2, ccmd.CommandCount);
            IList <ICommand> cmds = ccmd.Commands.ToList();

            Assert.IsInstanceOfType(cmds[0], typeof(VarCommand));
            Assert.IsInstanceOfType(cmds[1], typeof(ForEachCommand));

            ForEachCommand foreachcmd = (ForEachCommand)cmds[1];

            Assert.IsNotNull(foreachcmd.Expression);
            Assert.IsInstanceOfType(foreachcmd.Expression, typeof(VariableExpression));
            Assert.IsNotNull(foreachcmd.Command);
            Assert.IsInstanceOfType(foreachcmd.Command, typeof(SetCommand));
        }