public void ExecuteCompositeCommand()
        {
            SetCommand command1 = new SetCommand("foo", new ConstantExpression("bar"));
            SetCommand command2 = new SetCommand("one", new ConstantExpression(1));

            CompositeCommand command = new CompositeCommand();
            command.AddCommand(command1);
            command.AddCommand(command2);

            Machine machine = new Machine();

            command.Execute(machine.Environment);

            Assert.AreEqual("bar", machine.Environment.GetValue("foo"));
            Assert.AreEqual(1, machine.Environment.GetValue("one"));
            Assert.IsNotNull(command.Commands);
        }
        public void ExecuteCompositeCommandWithReturn()
        {
            SetCommand command1 = new SetCommand("foo", new ConstantExpression("bar"));
            ReturnCommand command2 = new ReturnCommand(new ConstantExpression("spam"));
            SetCommand command3 = new SetCommand("one", new ConstantExpression(1));

            CompositeCommand command = new CompositeCommand();
            command.AddCommand(command1);
            command.AddCommand(command2);
            command.AddCommand(command3);

            Machine machine = new Machine();
            BindingEnvironment environment = new BindingEnvironment(machine.Environment);

            command.Execute(environment);

            Assert.AreEqual("bar", environment.GetValue("foo"));
            Assert.IsNull(environment.GetValue("one"));
            Assert.IsTrue(environment.HasReturnValue());
            Assert.AreEqual("spam", environment.GetReturnValue());
            Assert.IsNotNull(command.Commands);
        }
        public void ExecuteFunctionWithReturn()
        {
            IList<Parameter> parameters = new Parameter[] { new Parameter("a", null, false), new Parameter("b", null, false) };
            CompositeCommand body = new CompositeCommand();
            body.AddCommand(new ReturnCommand(new BinaryOperatorExpression(new NameExpression("a"), new NameExpression("b"), BinaryOperator.Add)));

            Machine machine = new Machine();
            StringWriter writer = new StringWriter();
            machine.Output = writer;

            DefinedFunction func = new DefinedFunction("foo", parameters, body, null);

            var result = func.Apply(machine.Environment, new object[] { 1, 2 }, null);

            Assert.IsNotNull(result);
            Assert.AreEqual(3, result);
        }
        public void ExecuteFunctionWithPrint()
        {
            IList<Parameter> parameters = new Parameter[] { new Parameter("a", null, false), new Parameter("b", null, false) };
            CompositeCommand body = new CompositeCommand();
            body.AddCommand(new ExpressionCommand(new CallExpression(new NameExpression("print"), new IExpression[] { new NameExpression("a") })));
            body.AddCommand(new ExpressionCommand(new CallExpression(new NameExpression("print"), new IExpression[] { new NameExpression("b") })));

            Machine machine = new Machine();
            StringWriter writer = new StringWriter();
            machine.Output = writer;

            DefinedFunction func = new DefinedFunction("foo", parameters, body, machine.Environment);

            func.Apply(machine.Environment, new object[] { 1, 2 }, null);
            Assert.AreEqual("1\r\n2\r\n", writer.ToString());
        }