Ejemplo n.º 1
0
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main()
    {
        Variable v = new Variable("machine");
        Command  c = new ForCommand(ExampleMachine.Dublin(), v, new PrintCommand(v));

        c.Execute();
    }
Ejemplo n.º 2
0
        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"));
        }
Ejemplo n.º 3
0
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main()
    {
        MachineComposite dublin = ExampleMachine.Dublin();
        Variable         v      = new Variable("machine");
        Command          c      = new ForCommand(dublin, v, new ShutDownCommand(v));

        c.Execute();
    }
Ejemplo n.º 4
0
        public void CreateForCommand()
        {
            IExpression fromexpr = new ConstantExpression(1);
            IExpression toexpr   = new ConstantExpression(2);
            ICommand    body     = new ExpressionCommand(new ConstantExpression(2));
            ForCommand  fcmd     = new ForCommand("k", fromexpr, toexpr, body);

            Assert.AreEqual("k", fcmd.Name);
            Assert.AreSame(fromexpr, fcmd.FromExpression);
            Assert.AreSame(toexpr, fcmd.ToExpression);
            Assert.AreSame(body, fcmd.Body);
        }
Ejemplo n.º 5
0
        public void ExecuteSimpleForOnEmptyList()
        {
            ICommand           body        = new SetCommand("b", new BinaryOperatorExpression(new NameExpression("a"), new NameExpression("b"), BinaryOperator.Add));
            BindingEnvironment environment = new BindingEnvironment();

            environment.SetValue("b", 0);

            ForCommand command = new ForCommand("a", new ConstantExpression(new object[] { }), body);

            command.Execute(environment);

            Assert.AreEqual(0, environment.GetValue("b"));
        }
Ejemplo n.º 6
0
        public void ParseSimpleFor()
        {
            ICommand command = ParseCommand("for (k=1; k<=5; k++) result=result+k;");

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

            ForCommand forcommand = (ForCommand)command;

            Assert.IsNotNull(forcommand.InitialCommand);
            Assert.IsNotNull(forcommand.Condition);
            Assert.IsNotNull(forcommand.EndCommand);
            Assert.IsNotNull(forcommand.Body);
        }
Ejemplo n.º 7
0
        public void ExecuteForCommand()
        {
            IExpression fromexpr = new ConstantExpression(1);
            IExpression toexpr   = new ConstantExpression(4);
            ICommand    body     = new ExpressionCommand(new AssignExpression("a", new BinaryOperatorExpression(BinaryOperator.Multiply, new NameExpression("a"), new NameExpression("k"))));
            ForCommand  fcmd     = new ForCommand("k", fromexpr, toexpr, body);

            Context context = new Context();

            context.SetValue("a", 1);

            fcmd.Execute(context);

            Assert.AreEqual(24, context.GetValue("a"));
        }
Ejemplo n.º 8
0
        public void ExecuteSimpleForWithContinue()
        {
            ICommand ifcmd  = new IfCommand(new CompareExpression(ComparisonOperator.Equal, new NameExpression("a"), new ConstantExpression(2)), new ContinueCommand());
            ICommand setcmd = new SetCommand("b", new BinaryOperatorExpression(new NameExpression("a"), new NameExpression("b"), BinaryOperator.Add));
            ICommand body   = new CompositeCommand(new ICommand[] { ifcmd, setcmd });

            BindingEnvironment environment = new BindingEnvironment();

            environment.SetValue("b", 0);

            ForCommand command = new ForCommand("a", new ConstantExpression(new object[] { 1, 2, 3 }), body);

            command.Execute(environment);

            Assert.AreEqual(4, environment.GetValue("b"));
        }
Ejemplo n.º 9
0
        public void ForWithAddExpression()
        {
            var command    = new SetVariableCommand("a", new ArithmeticBinaryExpression(ArithmeticOperator.Add, new VariableExpression("k"), new VariableExpression("a")));
            var forcommand = new ForCommand("k", new ConstantExpression(new int[] { 1, 2, 3, 4 }), command);

            Context context = new Context();

            var result = forcommand.Execute(context);

            Assert.IsNull(result);
            Assert.AreEqual("k", forcommand.VariableName);
            Assert.IsNotNull(forcommand.Expression);
            Assert.IsNotNull(forcommand.Command);
            Assert.AreEqual(4, context.GetValue("k"));
            Assert.AreEqual(10, context.GetValue("a"));
        }
Ejemplo n.º 10
0
        public void ParseForCommandMultiline()
        {
            Parser   parser  = new Parser("for k in a \r\n b = k\r\nend");
            ICommand command = parser.ParseCommand();

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

            ForCommand forcommand = (ForCommand)command;

            Assert.AreEqual("k", forcommand.VariableName);
            Assert.IsNotNull(forcommand.Expression);
            Assert.IsNotNull(forcommand.Command);

            Assert.IsNull(parser.ParseCommand());
        }
Ejemplo n.º 11
0
        public void CreateAndExecuteForCommand()
        {
            var command    = new SetVariableCommand("a", new VariableExpression("k"));
            var forcommand = new ForCommand("k", new ConstantExpression((new int[] { 1, 2, 3 })), command);

            Context context = new Context();

            context.SetValue("a", 0);

            var result = forcommand.Execute(context);

            Assert.IsNull(result);
            Assert.AreEqual("k", forcommand.VariableName);
            Assert.IsNotNull(forcommand.Expression);
            Assert.IsNotNull(forcommand.Command);
            Assert.AreEqual(3, context.GetValue("k"));
            Assert.AreEqual(3, context.GetValue("a"));
        }
Ejemplo n.º 12
0
        public void RaiseWhenForOverAnInteger()
        {
            BindingEnvironment environment = new BindingEnvironment();

            environment.SetValue("b", 0);

            ForCommand command = new ForCommand("a", new ConstantExpression(1), null);

            try
            {
                command.Execute(environment);
                Assert.Fail("Exception expected");
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(TypeError));
                Assert.AreEqual("'int' object is not iterable", ex.Message);
            }
        }
Ejemplo n.º 13
0
        public void DashFor()
        {
            IHalationCommand cmd = new ForCommand(Halation.CurrentSelectedLine, this.GetIndent(Halation.CurrentSelectedLine), Halation.currentCodePackage);

            HalationInvoker.Dash(Halation.currentScriptName, cmd);
        }