Example #1
0
        private Expr parseMulExpr()
        {
            Expr expr = parseParenExpr();

            while (true)
            {
                int token = parseToken();
                switch (token)
                {
                case '%':
                    expr = new ModExpr(expr, parseParenExpr());
                    break;

                case '*':
                    expr = new MulExpr(expr, parseParenExpr());
                    break;

                case '/':
                    expr = new DivExpr(expr, parseParenExpr());
                    break;

                default:
                    _peekToken = token;
                    return(expr);
                }
            }
        }
Example #2
0
 public Value Visit(DivExpr expr, Scope scope)
 {
     return(PerformOperation(expr.Left.Accept(this, scope),
                             expr.Right.Accept(this, scope),
                             (a, b) => a / b,
                             (a, b) => a / b,
                             (a, b) => { throw new InvalidOperationException(); },
                             (a, b) => { throw new InvalidOperationException(); }));
 }
Example #3
0
            public void RightPropertyReturnsPassedRhsInstance()
            {
                var lhs = new ConstantExpr(1);
                var rhs = new ConstantExpr(2);

                var target = new DivExpr(lhs, rhs);

                Assert.AreSame(rhs, target.Right);
            }
Example #4
0
            public void RightPropertyReturnsPassedRhsInstance()
            {
                var lhs = new ConstantExpr(1);
                var rhs = new ConstantExpr(2);

                var target = new DivExpr(lhs, rhs);

                Assert.AreSame(rhs, target.Right);
            }
Example #5
0
            public void VisitDivExpr()
            {
                var target = new TypeCheckingVisitor();

                var lhs        = new ConstantExpr(10);
                var rhs        = new ConstantExpr(2);
                var expression = new DivExpr(lhs, rhs);

                var actual = target.Visit(expression, scope);

                Assert.AreEqual(ValueType.Int, actual);
            }
Example #6
0
            public void AcceptMethodCallsOnlyVisitOnVisitorWithThisAndNoOtherVisitMethods()
            {
                var lhs = new ConstantExpr(1);
                var rhs = new ConstantExpr(2);

                var target = new DivExpr(lhs, rhs);
                // throw exception is any other methods called other than the DivExpr overload.
                var visitor = new Mock<IExpressionVisitor<string, int>>(MockBehavior.Strict);
                visitor.Setup(x => x.Visit(It.IsAny<DivExpr>(), It.IsAny<int>())).Returns("");

                target.Accept(visitor.Object, 2);
            }
Example #7
0
            public void AcceptMethodCallsVisitOnVisitorWithThis()
            {
                var lhs = new ConstantExpr(1);
                var rhs = new ConstantExpr(2);

                var target = new DivExpr(lhs, rhs);
                var visitor = new Mock<IExpressionVisitor<string, int>>();

                target.Accept(visitor.Object,5);

                visitor.Verify(x => x.Visit(target,5), Times.Once);
            }
Example #8
0
        static void Main(string[] args)
        {
            Console.WriteLine("1_______________________________________________________________________________");
            // (3*x^2 + 7) / (x + 5)   82 / 10 = 8.2
            Expression exp = new DivExpr(
                new AddExpr(
                    MonomialExpr.createObject(3, 2),
                    ConstExpr.createObject(7)),
                new AddExpr(
                    MonomialExpr.createObject(1, 1),
                    ConstExpr.createObject(5)));

            Console.WriteLine("f(x)=" + exp.ToString());
            Console.WriteLine("f(5)=" + exp.Evaluate(5));
            Console.WriteLine("f'(x)=" + exp.Derive().ToString());

            Console.WriteLine("2_______________________________________________________________________________");
            // (0 + 10) - 0 * (x^1 + 0)
            Expression exp5 = new AddExpr(MonomialExpr.createObject(0, 2),
                                          ConstExpr.createObject(10));

            Expression exp6 = new SubExpr(exp5,
                                          new MulExpr(ConstExpr.createObject(0),
                                                      new AddExpr(MonomialExpr.createObject(1, 1),
                                                                  ConstExpr.createObject(0))));

            Console.WriteLine("f(x)=" + exp6.ToString());
            Console.WriteLine("f(2)=" + exp6.Evaluate(2));
            Console.WriteLine("f'(x)=" + exp6.Derive().ToString());


            Console.WriteLine("3_______________________________________________________________________________");
            //(((x^2 + 2) / (x + 1)) + 10) / (x^2 + 5)
            Expression exp3 = new DivExpr(
                new AddExpr(
                    MonomialExpr.createObject(1, 2),
                    ConstExpr.createObject(2)),
                new AddExpr(
                    MonomialExpr.createObject(1, 1),
                    ConstExpr.createObject(1)));

            Expression exp4 = new DivExpr(
                new AddExpr(
                    exp3,
                    ConstExpr.createObject(10)),
                new AddExpr(
                    MonomialExpr.createObject(1, 2),
                    ConstExpr.createObject(5)));

            Console.WriteLine("f(x)=" + exp4.ToString());
            Console.WriteLine("f(1)=" + exp4.Evaluate(1));
            Console.WriteLine("f'(x)=" + exp4.Derive().ToString());
        }
Example #9
0
            public void AcceptMethodCallsVisitOnVisitorWithThis()
            {
                var lhs = new ConstantExpr(1);
                var rhs = new ConstantExpr(2);

                var target  = new DivExpr(lhs, rhs);
                var visitor = new Mock <IExpressionVisitor <string, int> >();

                target.Accept(visitor.Object, 5);

                visitor.Verify(x => x.Visit(target, 5), Times.Once);
            }
Example #10
0
            public void VisitDivExpr()
            {
                var target = new PrintVisitor();

                var lhs        = new ConstantExpr(1);
                var rhs        = new ConstantExpr(2);
                var expression = new DivExpr(lhs, rhs);

                var actual = target.Visit(expression, scope);

                Assert.AreEqual("1/2", actual);
            }
Example #11
0
            public void VisitDivExpr()
            {
                var target = new EvaluateVisitor();

                var lhs        = new ConstantExpr(10);
                var rhs        = new ConstantExpr(2);
                var expression = new DivExpr(lhs, rhs);

                var actual = target.Visit(expression, _scope);

                Assert.AreEqual("5", actual.ToString());
            }
Example #12
0
            public void AcceptMethodCallsOnlyVisitOnVisitorWithThisAndNoOtherVisitMethods()
            {
                var lhs = new ConstantExpr(1);
                var rhs = new ConstantExpr(2);

                var target = new DivExpr(lhs, rhs);
                // throw exception is any other methods called other than the DivExpr overload.
                var visitor = new Mock <IExpressionVisitor <string, int> >(MockBehavior.Strict);

                visitor.Setup(x => x.Visit(It.IsAny <DivExpr>(), It.IsAny <int>())).Returns("");

                target.Accept(visitor.Object, 2);
            }
Example #13
0
            public void VisitMathExpressionUsingFloatingPointDivisionTree()
            {
                var target = new EvaluateVisitor();

                var one   = new ConstantExpr(1.0);
                var two   = new ConstantExpr(2);
                var three = new ConstantExpr(3);
                var four  = new ConstantExpr(4);
                var five  = new ConstantExpr(5);
                var six   = new ConstantExpr(6);

                var expr = new DivExpr(new MultExpr(three, six), new MultExpr(new MinusExpr(five, one), new PlusExpr(four, two)));

                var actual = target.Visit(expr, _scope);

                Assert.AreEqual("0.75", actual.ToString());
            }
Example #14
0
            public void VisitMathExpressionTree()
            {
                var target = new PrintVisitor();

                var one   = new ConstantExpr(1);
                var two   = new ConstantExpr(2);
                var three = new ConstantExpr(3);
                var four  = new ConstantExpr(4);
                var five  = new ConstantExpr(5);
                var six   = new ConstantExpr(6);

                var expression = new DivExpr(new MultExpr(three, six), new MultExpr(new MinusExpr(five, one), new PlusExpr(four, two)));

                var actual = target.Visit(expression, scope);

                Assert.AreEqual("3*6/5-1*4+2", actual);
            }
Example #15
0
            public void VisitMathExpressionTree()
            {
                var target = new TypeCheckingVisitor();

                var one   = new ConstantExpr(1);
                var two   = new ConstantExpr(2);
                var three = new ConstantExpr(3);
                var four  = new ConstantExpr(4);
                var five  = new ConstantExpr(5);
                var six   = new ConstantExpr(6);

                var expr = new DivExpr(new MultExpr(three, six), new MultExpr(new MinusExpr(five, one), new PlusExpr(four, two)));

                var actual = target.Visit(expr, scope);

                Assert.AreEqual(ValueType.Int, actual);
            }
Example #16
0
            public void VisitAlgebraicExpressionTree()
            {
                var target = new PrintVisitor();

                var a     = new IdentifierExpr("a");
                var b     = new IdentifierExpr("b");
                var c     = new IdentifierExpr("c");
                var one   = new ConstantExpr(1);
                var two   = new ConstantExpr(2);
                var three = new ConstantExpr(3);
                var four  = new ConstantExpr(4);
                var five  = new ConstantExpr(5);
                var six   = new ConstantExpr(6);

                var expression = new DivExpr(new MultExpr(three, new MultExpr(a, two)), new MultExpr(new MinusExpr(new PowExpr(five, b), one), new PlusExpr(new MinusExpr(six, four), c)));

                var actual = target.Visit(expression, scope);

                Assert.AreEqual("3*a*2/5^b-1*6-4+c", actual);
            }
Example #17
0
        public void Visit(DivExpr divExpr, object[] args)
        {
            divExpr.FirstOp.Accept(this);
            divExpr.SecondOp.Accept(this);

            RightValue v1 = readRightValue(divExpr.FirstOp);
            RightValue v2 = readRightValue(divExpr.SecondOp);


            DataType resultType = readAlgoOperand(v1, v2, divExpr.Location);

            if (resultType == DataType.Int)
            {
                IntConst result = new IntConst();
                if (Convert.ToInt32(v2.GetValueObject()) == 0)
                {
                    throw new RuntimeException(new Error(ErrorType.DivisorZero, divExpr.Location));
                }
                result.Value       = Convert.ToInt32(v1.GetValueObject()) / Convert.ToInt32(v2.GetValueObject());
                divExpr.RightValue = result;
                //divExpr.DataType = DataType.Int;
            }
            else if (resultType == DataType.Float)
            {
                FloatConst result = new FloatConst();
                if (Convert.ToDouble(v2.GetValueObject()) == 0)
                {
                    throw new RuntimeException(new Error(ErrorType.DivisorZero, divExpr.Location));
                }
                result.Value       = Convert.ToDouble(v1.GetValueObject()) / Convert.ToDouble(v2.GetValueObject());
                divExpr.RightValue = result;
                //divExpr.DataType = DataType.Float;
            }
            else
            {
                throw new Exception();
            }
        }
Example #18
0
 /// <summary>
 /// Validate a division expression.
 /// </summary>
 /// <param name="mulex">
 ///            is the expression. </param>
 /// <returns> null. </returns>
 public virtual object visit(DivExpr mulex)
 {
     printBinExpr("DIV", mulex);
     return(null);
 }
        public void Visit(DivExpr divExpr, object[] args)
        {
            divExpr.FirstOp.Accept(this);
            divExpr.SecondOp.Accept(this);

            RightValue v1 = readRightValue(divExpr.FirstOp);
            RightValue v2 = readRightValue(divExpr.SecondOp);

            DataType resultType = readAlgoOperand(v1, v2, divExpr.Location);
            if (resultType == DataType.Int)
            {
                IntConst result = new IntConst();
                if (Convert.ToInt32(v2.GetValueObject()) == 0)
                {
                    throw new RuntimeException(new Error(ErrorType.DivisorZero, divExpr.Location));
                }
                result.Value = Convert.ToInt32(v1.GetValueObject()) / Convert.ToInt32(v2.GetValueObject());
                divExpr.RightValue = result;
                //divExpr.DataType = DataType.Int;
            }
            else if (resultType == DataType.Float)
            {
                FloatConst result = new FloatConst();
                if (Convert.ToDouble(v2.GetValueObject()) == 0)
                {
                    throw new RuntimeException(new Error(ErrorType.DivisorZero, divExpr.Location));
                }
                result.Value = Convert.ToDouble(v1.GetValueObject()) / Convert.ToDouble(v2.GetValueObject());
                divExpr.RightValue = result;
                //divExpr.DataType = DataType.Float;
            }
            else
            {
                throw new Exception();
            }
        }
Example #20
0
 /// <param name="mulex">
 ///            is the division expression. </param>
 /// <returns> a new function. </returns>
 public virtual object visit(DivExpr mulex)
 {
     return(make_ArithOp(mulex, new QName("fs", "div", OpFunctionLibrary.XPATH_OP_NS)));
 }
            public void VisitMathExpressionUsingFloatingPointDivisionTree()
            {
                var target = new EvaluateVisitor();

                var one = new ConstantExpr(1.0);
                var two = new ConstantExpr(2);
                var three = new ConstantExpr(3);
                var four = new ConstantExpr(4);
                var five = new ConstantExpr(5);
                var six = new ConstantExpr(6);

                var expr = new DivExpr(new MultExpr(three, six), new MultExpr(new MinusExpr(five, one), new PlusExpr(four, two)));

                var actual = target.Visit(expr, _scope);

                Assert.AreEqual("0.75", actual.ToString());
            }
            public void VisitDivExpr()
            {
                var target = new EvaluateVisitor();

                var lhs = new ConstantExpr(10);
                var rhs = new ConstantExpr(2);
                var expression = new DivExpr(lhs, rhs);

                var actual = target.Visit(expression, _scope);

                Assert.AreEqual("5", actual.ToString());
            }
Example #23
0
            public void VisitAlgebraicExpressionTree()
            {
                var target = new PrintVisitor();

                var a = new IdentifierExpr("a");
                var b = new IdentifierExpr("b");
                var c = new IdentifierExpr("c");
                var one = new ConstantExpr(1);
                var two = new ConstantExpr(2);
                var three = new ConstantExpr(3);
                var four = new ConstantExpr(4);
                var five = new ConstantExpr(5);
                var six = new ConstantExpr(6);

                var expression = new DivExpr(new MultExpr(three,new MultExpr(a, two)), new MultExpr(new MinusExpr(new PowExpr(five,b), one), new PlusExpr(new MinusExpr(six, four), c)));

                var actual = target.Visit(expression, scope);

                Assert.AreEqual("3*a*2/5^b-1*6-4+c", actual);
            }
Example #24
0
 public string Visit(DivExpr expr, Scope scope)
 {
     return(expr.Left.Accept(this, scope) + "/" + expr.Right.Accept(this, scope));
 }
Example #25
0
            public void VisitMathExpressionTree()
            {
                var target = new PrintVisitor();

                var one = new ConstantExpr(1);
                var two = new ConstantExpr(2);
                var three = new ConstantExpr(3);
                var four = new ConstantExpr(4);
                var five = new ConstantExpr(5);
                var six = new ConstantExpr(6);

                var expression = new DivExpr(new MultExpr(three, six), new MultExpr(new MinusExpr(five, one), new PlusExpr(four, two)));

                var actual = target.Visit(expression, scope);

                Assert.AreEqual("3*6/5-1*4+2", actual);
            }
Example #26
0
            public void VisitDivExpr()
            {
                var target = new PrintVisitor();

                var lhs = new ConstantExpr(1);
                var rhs = new ConstantExpr(2);
                var expression = new DivExpr(lhs, rhs);

                var actual = target.Visit(expression, scope);

                Assert.AreEqual("1/2", actual);
            }
Example #27
0
 public ValueType Visit(DivExpr expr, Scope scope)
 {
     return(BinaryOperatorTypeCheck(expr, scope, numericTypesOnly: true));
 }
Example #28
0
 /// <param name="ex">
 ///            is the division expression. </param>
 /// <returns> a new function. </returns>
 public virtual object visit(DivExpr ex)
 {
     ex.left().accept(this);
     ex.right().accept(this);
     return(null);
 }
            public void VisitDivExpr()
            {
                var target = new TypeCheckingVisitor();

                var lhs = new ConstantExpr(10);
                var rhs = new ConstantExpr(2);
                var expression = new DivExpr(lhs, rhs);

                var actual = target.Visit(expression, scope);

                Assert.AreEqual(ValueType.Int, actual);
            }
            public void VisitMathExpressionTree()
            {
                var target = new TypeCheckingVisitor();

                var one = new ConstantExpr(1);
                var two = new ConstantExpr(2);
                var three = new ConstantExpr(3);
                var four = new ConstantExpr(4);
                var five = new ConstantExpr(5);
                var six = new ConstantExpr(6);

                var expr = new DivExpr(new MultExpr(three, six), new MultExpr(new MinusExpr(five, one), new PlusExpr(four, two)));

                var actual = target.Visit(expr, scope);

                Assert.AreEqual(ValueType.Int, actual);
            }
Example #31
0
 static void Main(string[] args)
 {
     DivExpr add = new DivExpr(new MonomialExpr(5, 2), new MonomialExpr(4, 3));
     //Console.WriteLine(add.derive().toString());
 }