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); } } }
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(); })); }
public void RightPropertyReturnsPassedRhsInstance() { var lhs = new ConstantExpr(1); var rhs = new ConstantExpr(2); var target = new DivExpr(lhs, rhs); Assert.AreSame(rhs, target.Right); }
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 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); }
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); }
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()); }
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); }
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); }
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()); }
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); }
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 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); }
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); }
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); }
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(); } }
/// <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); }
/// <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 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); }
public string Visit(DivExpr expr, Scope scope) { return(expr.Left.Accept(this, scope) + "/" + expr.Right.Accept(this, scope)); }
public ValueType Visit(DivExpr expr, Scope scope) { return(BinaryOperatorTypeCheck(expr, scope, numericTypesOnly: true)); }
/// <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); }
static void Main(string[] args) { DivExpr add = new DivExpr(new MonomialExpr(5, 2), new MonomialExpr(4, 3)); //Console.WriteLine(add.derive().toString()); }