Beispiel #1
0
        private ICommand MakeAddCommand(string target, string source, int number)
        {
            IExpression add = new BinaryArithmeticExpression(new VariableExpression(source), new ConstantExpression(number), ArithmeticOperator.Add);
            ICommand    set = new SetCommand(target, add);

            return(set);
        }
Beispiel #2
0
        public void EvaluateDivideExpression()
        {
            ConstantExpression leftExpression  = new ConstantExpression(4);
            ConstantExpression rightExpression = new ConstantExpression(2);

            BinaryArithmeticExpression expression = new BinaryArithmeticExpression(leftExpression, rightExpression, ArithmeticOperator.Divide);

            Assert.AreEqual(2.0, expression.Evaluate(null));
        }
Beispiel #3
0
        public void EvaluateMultiplyExpression()
        {
            ConstantExpression leftExpression  = new ConstantExpression(3);
            ConstantExpression rightExpression = new ConstantExpression(2);

            BinaryArithmeticExpression expression = new BinaryArithmeticExpression(leftExpression, rightExpression, ArithmeticOperator.Multiply);

            Assert.AreEqual(6, expression.Evaluate(null));
        }
Beispiel #4
0
        public void EvaluateSubtractExpression()
        {
            ConstantExpression leftExpression  = new ConstantExpression(1);
            ConstantExpression rightExpression = new ConstantExpression(2);

            BinaryArithmeticExpression expression = new BinaryArithmeticExpression(leftExpression, rightExpression, ArithmeticOperator.Subtract);

            Assert.AreEqual(-1, expression.Evaluate(null));
        }
Beispiel #5
0
        public void CreateBinaryArithmeticExpression()
        {
            ConstantExpression leftExpression  = new ConstantExpression(1);
            ConstantExpression rightExpression = new ConstantExpression(2);

            BinaryArithmeticExpression expression = new BinaryArithmeticExpression(leftExpression, rightExpression, ArithmeticOperator.Add);

            Assert.AreEqual(leftExpression, expression.LeftExpression);
            Assert.AreEqual(rightExpression, expression.RightExpression);
            Assert.AreEqual(ArithmeticOperator.Add, expression.Operator);
        }
Beispiel #6
0
        public void ParseAndEvaluateMultiplyExpression()
        {
            Parser parser = new Parser("2*3");

            IExpression expression = parser.ParseExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(BinaryArithmeticExpression));

            BinaryArithmeticExpression multexpr = (BinaryArithmeticExpression)expression;

            Assert.AreEqual(ArithmeticOperator.Multiply, multexpr.Operator);

            Assert.AreEqual(6, multexpr.Evaluate(null));

            Assert.IsNull(parser.ParseExpression());
        }
Beispiel #7
0
        public void ParseAndEvaluateAddExpression()
        {
            Parser parser = new Parser("1+2");

            IExpression expression = parser.ParseExpression();

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(BinaryArithmeticExpression));

            BinaryArithmeticExpression addexpr = (BinaryArithmeticExpression)expression;

            Assert.AreEqual(ArithmeticOperator.Add, addexpr.Operator);

            Assert.AreEqual(3, addexpr.Evaluate(null));

            Assert.IsNull(parser.ParseExpression());
        }
Beispiel #8
0
        private IExpression ParseFactorExpression()
        {
            IExpression expression = this.ParseSimpleExpression();

            Token token = this.NextToken();

            while (token != null && token.TokenType == TokenType.Operator)
            {
                if (token.Value.Equals("*") || token.Value.Equals("/"))
                {
                    ArithmeticOperator oper = token.Value.Equals("*") ? ArithmeticOperator.Multiply : ArithmeticOperator.Divide;
                    expression = new BinaryArithmeticExpression(expression, this.ParseSimpleExpression(), oper);
                    token      = this.NextToken();
                    continue;
                }

                break;
            }

            this.PushToken(token);

            return(expression);
        }
Beispiel #9
0
        public IExpression ParseExpression()
        {
            IExpression expression = this.ParseFactorExpression();

            Token token = this.NextToken();

            while (token != null && token.TokenType == TokenType.Operator)
            {
                if (token.Value.Equals("+") || token.Value.Equals("-"))
                {
                    ArithmeticOperator oper = token.Value.Equals("+") ? ArithmeticOperator.Add : ArithmeticOperator.Subtract;
                    expression = new BinaryArithmeticExpression(expression, this.ParseFactorExpression(), oper);
                    token      = this.NextToken();
                    continue;
                }

                break;
            }

            this.PushToken(token);

            return(expression);
        }