Inheritance: BinaryExpression
Esempio n. 1
0
        public void AddTwoIntegers()
        {
            AddExpression expr = new AddExpression(new ConstantExpression(1), new ConstantExpression(2));

            Assert.AreEqual(3, expr.Evaluate(null));
            Assert.IsFalse(expr.HasVariable());
        }
Esempio n. 2
0
        public void AddVariableAndInteger()
        {
            Context context = new Context();
            context.SetValue("One", 1);
            AddExpression expr = new AddExpression(new VariableExpression(new Variable("One")), new ConstantExpression(2));

            Assert.AreEqual(3, expr.Evaluate(context));
            Assert.IsTrue(expr.HasVariable());
        }
Esempio n. 3
0
        public void AddIntegerAndVariable()
        {
            Context context = new Context();
            context.SetValue("Two", 2);
            AddExpression expr = new AddExpression(new ConstantExpression(1), new VariableExpression(new Variable("Two")));

            Assert.AreEqual(3, expr.Evaluate(context));
            Assert.IsTrue(expr.HasVariable());
        }
Esempio n. 4
0
        private static object Sum(Context context, IList<object> arguments)
        {
            if (arguments[0] is EmptyList)
                return 0;

            AddExpression addexpr = new AddExpression(null, null);
            List list = (List)arguments[0];
            object result = 0;

            while (list != null)
            {
                result = addexpr.Apply(result, list.Head);
                list = (List)list.Tail;
            }

            return result;
        }
Esempio n. 5
0
        private IExpression ParseBinaryExpression(int level)
        {
            if (level >= binaryoperators.Length)
                return this.ParseTerm();

            IExpression expr = this.ParseBinaryExpression(level + 1);

            if (expr == null)
                return null;

            Token token;

            for (token = this.lexer.NextToken(); token != null && this.IsBinaryOperator(level, token); token = this.lexer.NextToken())
            {
                if (token.Value == "=:=")
                    expr = new StrictEqualExpression(expr, this.ParseBinaryExpression(level + 1));
                else if (token.Value == "==")
                    expr = new EqualExpression(expr, this.ParseBinaryExpression(level + 1));
                else if (token.Value == "+")
                    expr = new AddExpression(expr, this.ParseBinaryExpression(level + 1));
                else if (token.Value == "-")
                    expr = new SubtractExpression(expr, this.ParseBinaryExpression(level + 1));
                else if (token.Value == "*")
                    expr = new MultiplyExpression(expr, this.ParseBinaryExpression(level + 1));
                else if (token.Value == "/")
                    expr = new DivideExpression(expr, this.ParseBinaryExpression(level + 1));
                else if (token.Value == "div")
                    expr = new DivExpression(expr, this.ParseBinaryExpression(level + 1));
                else if (token.Value == "rem")
                    expr = new RemExpression(expr, this.ParseBinaryExpression(level + 1));
            }

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

            return expr;
        }
Esempio n. 6
0
        public void AddTwoDoubles()
        {
            AddExpression expr = new AddExpression(new ConstantExpression(2.5), new ConstantExpression(3.7));

            Assert.AreEqual(2.5 + 3.7, expr.Evaluate(null));
        }
Esempio n. 7
0
        public void AddIntegerToDouble()
        {
            AddExpression expr = new AddExpression(new ConstantExpression(1), new ConstantExpression(2.5));

            Assert.AreEqual(1 + 2.5, expr.Evaluate(null));
        }
Esempio n. 8
0
        public void AddDoubleToInteger()
        {
            AddExpression expr = new AddExpression(new ConstantExpression(2.5), new ConstantExpression(1));

            Assert.AreEqual(2.5 + 1, expr.Evaluate(null));
        }