public static IExpression CreateFromExpression(string expression) { IExpression result = null; if (ConstantExpression.IsMatch(expression)) { result = new ConstantExpression(); } else if (AddExpression.IsMatch(expression)) { result = new AddExpression(); } else if (SubExpression.IsMatch(expression)) { result = new SubExpression(); } else if (MulExpression.IsMatch(expression)) { result = new MulExpression(); } else if (DivExpression.IsMatch(expression)) { result = new DivExpression(); } else if (LetExpression.IsMatch(expression)) { result = new LetExpression(); } else if (VariableExpression.IsMatch(expression)) { result = new VariableExpression(); } return(result); }
private static RawExpression Exp2(char **input) { RawExpression e = Exp1(input); while (true) { if (Char(input, '*')) { e = new MulExpression { Left = e, Right = Exp1(input), }; } else if (Char(input, '/')) { e = new DivExpression { Left = e, Right = Exp1(input), }; } else { break; } } return(e); }
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); }
public void EvaluateTests(string expression, int expectedResult) { var divExpression = new DivExpression(); int result = divExpression.Evaluate(expression); Assert.That(result, Is.EqualTo(expectedResult)); }
public void ValidateExpressionTests(string expression, bool expectedResult, string expectedError) { var divExpression = new DivExpression(); bool result = divExpression.ValidateExpression(expression, out string error); Assert.That(result, Is.EqualTo(expectedResult)); Assert.That(error, Is.EqualTo(expectedError)); }
public void interpreterTest() { DivExpression divExpression = new DivExpression(new VarExpression(0), new VarExpression(2)); List <string> list = new List <string>(); list.Add("1"); list.Add("+"); list.Add("3"); list.Add("-"); list.Add("1"); int i = divExpression.interpreter(list); Assert.AreEqual(0, i); }
static int InterpretInput(String input) { Expression expression = null; if (input.Contains("nhân")) { expression = new MulExpression(input); } else if (input.Contains("chia")) { expression = new DivExpression(input); } return(expression.Interpret(new InterpreterEngine())); }
public void RaiseIfDivideRealByInteger() { DivExpression expr = new DivExpression(new ConstantExpression(6.5), new ConstantExpression(2)); try { expr.Evaluate(null); Assert.Fail(); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(InvalidCastException)); } }
public void RaiseIfDivideRealByInteger() { DivExpression expr = new DivExpression(new ConstantExpression(6.5), new ConstantExpression(2)); try { expr.Evaluate(null); Assert.Fail(); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(InvalidCastException)); } }
public static int Evaluate(Expression exp, int var) { if (exp is ConstantExpression) { ConstantExpression tExp = exp as ConstantExpression; return((int)(exp as ConstantExpression).Value); } else if (exp is VariableExpression) { return(var); } else if (exp is AddExpression) { AddExpression nExp = (AddExpression)exp; return(Evaluate(nExp.OperandA, var) + Evaluate(nExp.OperandB, var)); } else if (exp is SubExpression) { SubExpression nExp = (SubExpression)exp; return(Evaluate(nExp.OperandA, var) - Evaluate(nExp.OperandB, var)); } else if (exp is MulExpression) { MulExpression nExp = (MulExpression)exp; return(Evaluate(nExp.OperandA, var) * Evaluate(nExp.OperandB, var)); } else if (exp is DivExpression) { DivExpression nExp = (DivExpression)exp; return(Evaluate(nExp.OperandA, var) / Evaluate(nExp.OperandB, var)); } else if (exp is NegExpression) { NegExpression nExp = (NegExpression)exp; return(-Evaluate(nExp.Value, var)); } else if (exp is InvExpression) { InvExpression nExp = (InvExpression)exp; return(~Evaluate(nExp.Value, var)); } else if (exp is XorExpression) { XorExpression nExp = (XorExpression)exp; return(Evaluate(nExp.OperandA, var) ^ Evaluate(nExp.OperandB, var)); } throw new NotSupportedException(); }
public void ParseSimpleDiv() { Parser parser = new Parser("10 div 20."); IExpression expression = parser.ParseExpression(); Assert.IsNotNull(expression); Assert.IsInstanceOfType(expression, typeof(DivExpression)); DivExpression divexpression = (DivExpression)expression; Assert.IsInstanceOfType(divexpression.LeftExpression, typeof(ConstantExpression)); Assert.IsInstanceOfType(divexpression.RightExpression, typeof(ConstantExpression)); Assert.AreEqual(10, ((ConstantExpression)divexpression.LeftExpression).Value); Assert.AreEqual(20, ((ConstantExpression)divexpression.RightExpression).Value); }
public void ErrorEvaluateTests(string expression) { var divExpression = new DivExpression(); Assert.Throws(typeof(ArithmeticException), () => divExpression.Evaluate(expression)); }
public void DivideTwoIntegersWithTruncation() { DivExpression expr = new DivExpression(new ConstantExpression(5), new ConstantExpression(2)); Assert.AreEqual(2, expr.Evaluate(null)); }
public void DivideTwoIntegers() { DivExpression expr = new DivExpression(new ConstantExpression(6), new ConstantExpression(2)); Assert.AreEqual(3, expr.Evaluate(null)); }
public void IsMatchTests(string expression, bool expectedResult) { bool result = DivExpression.IsMatch(expression); Assert.That(result, Is.EqualTo(expectedResult)); }
public static double ReverseEvaluate(Expression exp, double val) { if (exp is ConstantExpression) { ConstantExpression tExp = exp as ConstantExpression; return((exp as ConstantExpression).Value); } else if (exp is VariableExpression) { return(val); } else if (exp is AddExpression) { AddExpression nExp = (AddExpression)exp; if (nExp.OperandB.HasVariable) { return(ReverseEvaluate(nExp.OperandB, val - ReverseEvaluate(nExp.OperandA, val))); } else if (nExp.OperandA.HasVariable) { return(ReverseEvaluate(nExp.OperandA, val - ReverseEvaluate(nExp.OperandB, val))); } else { return(Evaluate(nExp, val)); } } else if (exp is SubExpression) { SubExpression nExp = (SubExpression)exp; if (nExp.OperandB.HasVariable) { return(ReverseEvaluate(nExp.OperandB, ReverseEvaluate(nExp.OperandA, val) - val)); } else if (nExp.OperandA.HasVariable) { return(ReverseEvaluate(nExp.OperandA, val + ReverseEvaluate(nExp.OperandB, val))); } else { return(Evaluate(nExp, val)); } } else if (exp is MulExpression) { MulExpression nExp = (MulExpression)exp; if (nExp.OperandB.HasVariable) { return(ReverseEvaluate(nExp.OperandB, val / ReverseEvaluate(nExp.OperandA, val))); } else if (nExp.OperandA.HasVariable) { return(ReverseEvaluate(nExp.OperandA, val / ReverseEvaluate(nExp.OperandB, val))); } else { return(Evaluate(nExp, val)); } } else if (exp is NegExpression) { NegExpression nExp = (NegExpression)exp; return(-ReverseEvaluate(nExp.Value, val)); } else if (exp is DivExpression) { DivExpression nExp = (DivExpression)exp; if (nExp.OperandB.HasVariable) { return(ReverseEvaluate(nExp.OperandB, ReverseEvaluate(nExp.OperandA, val) / val)); } else if (nExp.OperandA.HasVariable) { return(ReverseEvaluate(nExp.OperandA, val * ReverseEvaluate(nExp.OperandB, val))); } else { return(Evaluate(nExp, val)); } } throw new NotSupportedException(); }
public void DivideTwoIntegers() { DivExpression expr = new DivExpression(new ConstantExpression(6), new ConstantExpression(2)); Assert.AreEqual(3, expr.Evaluate(null)); }
public void DivideTwoIntegersWithTruncation() { DivExpression expr = new DivExpression(new ConstantExpression(5), new ConstantExpression(2)); Assert.AreEqual(2, expr.Evaluate(null)); }
public void EvaluateDivideByZero() { var divExpression = new DivExpression(); Assert.Throws(typeof(DivideByZeroException), () => divExpression.Evaluate("div(1,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; }
public void DivExpressionTest_1() { DivExpression divExpression = new DivExpression(new VarExpression(1), new VarExpression(2)); Assert.IsNotNull(divExpression); }