public static EvaluationResult Evaluate(ParseTreeNode node, IRootEvaluator rootEvaluator) { var arg1 = rootEvaluator.Evaluate(node.ChildNodes[0]); var operNode = node.ChildNodes[1]; var oper = operNode.Token.Value as string; var arg2 = rootEvaluator.Evaluate(node.ChildNodes[2]); if (arg1.HasError) return arg1; if (arg2.HasError) return arg2; try { if (oper == "+" && arg1.Result is int && arg2.Result is int) return DoOperAdd((int)arg1.Result, (int)arg2.Result); if (oper == "-" && arg1.Result is int && arg2.Result is int) return DoOperMinus((int)arg1.Result, (int)arg2.Result); if (oper == "*" && arg1.Result is int && arg2.Result is int) return DoOperMul((int)arg1.Result, (int)arg2.Result); if (oper == "/" && arg1.Result is int && arg2.Result is int) return DoOperDiv((int)arg1.Result, (int)arg2.Result, operNode); } catch (ArithmeticException) { return new EvaluationResult(SimpleExpressionEvaluator.OverflowMessage, operNode.Span.Location.Position); } throw new NotSupportedException(string.Format(SimpleExpressionEvaluator.OperationNotSupported, oper)); }
public static EvaluationResult Evaluate(ParseTreeNode node, IRootEvaluator rootEvaluator) { var sing = node.ChildNodes[0].Token.Value as string; var arg = rootEvaluator.Evaluate(node.ChildNodes[1]); if (arg.HasError) return arg; if (sing == "-") return new EvaluationResult(-(int)arg.Result); if (sing == "+") return arg; throw new NotSupportedException(string.Format(SimpleExpressionEvaluator.OperationNotSupported, sing)); }