Inheritance: ICommand
        private static ICallable BuildFactorialFunction(IContext context)
        {
            IExpression condition = new CompareExpression(ComparisonOperator.LessEqual, new VariableExpression("n"), new ConstantExpression(1));

            ICommand return1 = new ReturnCommand(new ConstantExpression(1));
            ICommand return2 = new ReturnCommand(new ArithmeticBinaryExpression(ArithmeticOperator.Multiply,
                new VariableExpression("n"),
                new InvokeExpression(new VariableExpression("Factorial"), new IExpression[] { new ArithmeticBinaryExpression(ArithmeticOperator.Subtract, new VariableExpression("n"), new ConstantExpression(1)) })));

            ICommand ifcmd = new IfCommand(condition, return1, return2);
            ICallable factorial = new Function(new string[] { "n" }, ifcmd, context);

            return factorial;
        }
Beispiel #2
0
        public void ExecuteIfCommandWhenTrue()
        {
            IExpression condition = new ConstantExpression(true);
            ICommand setCommand = new SetVariableCommand("a", new ConstantExpression(1));
            IfCommand command = new IfCommand(condition, setCommand);

            Context context = new Context();

            command.Execute(context);

            Assert.AreEqual(1, context.GetValue("a"));
        }
Beispiel #3
0
        public void ExecuteIfCommandElseWhenFalse()
        {
            IExpression condition = new ConstantExpression(false);
            ICommand setXCommand = new SetVariableCommand("a", new ConstantExpression(1));
            ICommand setYCommand = new SetVariableCommand("b", new ConstantExpression(2));
            IfCommand command = new IfCommand(condition, setXCommand, setYCommand);

            Context context = new Context();

            command.Execute(context);

            Assert.AreEqual(Undefined.Instance, context.GetValue("a"));
            Assert.AreEqual(2, context.GetValue("b"));
        }