示例#1
0
        public void EvaluateCompareExpression()
        {
            CompareExpression expression = new CompareExpression(ComparisonOperator.Equal, new ConstantExpression(1), new ConstantExpression(2));
            var result = expression.Evaluate(null);

            Assert.IsInstanceOfType(result, typeof(bool));
            Assert.IsFalse((bool)result);
            Assert.AreEqual(ComparisonOperator.Equal, expression.Operation);
        }
示例#2
0
        public void CreateAndEvaluateSimpleWhileCommand()
        {
            BindingEnvironment environment = new BindingEnvironment();
            environment.SetValue("a", 1);
            IExpression condition = new CompareExpression(ComparisonOperator.Less, new NameExpression("a"), new ConstantExpression(10));
            ICommand body = new SetCommand("a", new BinaryOperatorExpression(new NameExpression("a"), new ConstantExpression(1), BinaryOperator.Add));

            WhileCommand command = new WhileCommand(condition, body);

            command.Execute(environment);

            Assert.AreEqual(condition, command.Condition);
            Assert.AreEqual(body, command.Command);
            Assert.AreEqual(10, environment.GetValue("a"));
        }
示例#3
0
        private IExpression CompileBinaryLevel0Expression()
        {
            IExpression expression = this.CompileBinaryLevel1Expression();

            if (expression == null)
                return null;

            Token token = this.lexer.NextToken();

            while (IsLevel0Operator(token))
            {
                IExpression expression2 = this.CompileBinaryLevel1Expression();
                expression = new CompareExpression(CompileCompareOperator(token.Value), expression, expression2);
                token = this.lexer.NextToken();
            }

            if (token != null)
                this.lexer.PushToken(token);

            return expression;
        }