Ejemplo n.º 1
0
        public void GetAndEvaluateWhileExpression()
        {
            WhileExpression expression = WhileExpression.Instance;

            Assert.IsNotNull(expression);

            Machine machine = new Machine();

            List <Expression> listcond = new List <Expression>();

            listcond.Add(DupExpression.Instance);
            listcond.Add(new IntegerExpression(0));
            listcond.Add(EqualsExpression.Instance);
            listcond.Add(NotExpression.Instance);

            List <Expression> listblock = new List <Expression>();

            listblock.Add(IntegerDecrementOperation.Instance);

            CompositeExpression cond  = new CompositeExpression(listcond);
            CompositeExpression block = new CompositeExpression(listblock);

            machine.Push(10);
            machine.Push(block);
            machine.Push(cond);

            expression.Evaluate(machine);

            Assert.AreEqual(1, machine.StackCount);
            Assert.IsInstanceOfType(machine.Top(), typeof(int));
            Assert.AreEqual(0, machine.Pop());
        }
Ejemplo n.º 2
0
        public void ExecuteSimpleWhileWhenConditionIsTrue()
        {
            Parser      cmdparser  = new Parser("a = a + 1");
            IExpression body       = cmdparser.ParseCommand();
            Parser      exprparser = new Parser("a < 6");
            IExpression expr       = exprparser.ParseExpression();

            Context context = new Context();

            context.SetLocalValue("a", 1);

            WhileExpression cmd = new WhileExpression(expr, body);

            Assert.IsNull(cmd.Evaluate(context));

            Assert.AreEqual(6, context.GetValue("a"));
        }