public void Visit(AdditionExpression ae) { ae.Left.Accept(this); var left = _result; ae.Right.Accept(this); _result += left; }
public void Visit(AdditionExpression ae) { _sb.Append("("); ae.Left.Accept(this); _sb.Append("+"); ae.Right.Accept(this); _sb.Append(")"); }
// !!! Dangerous approach. Compiler type checking is omitted. !!! public static void Print(this AdditionExpression ae, StringBuilder sb) { sb.Append("("); Print((dynamic)ae.Left, sb); sb.Append("+"); Print((dynamic)ae.Right, sb); sb.Append(")"); }
static void Main(string[] args) { var exp = new AdditionExpression(new DoubleExpression(1), new AdditionExpression(new DoubleExpression(2), new DoubleExpression(3))); var sb = new StringBuilder(); exp.Print(sb); WriteLine(sb.ToString()); }
static void Main(string[] args) { var exp = new AdditionExpression(new DoubleExpression(1), new AdditionExpression(new DoubleExpression(2), new DoubleExpression(3))); var sb = new StringBuilder(); exp.Print(sb); WriteLine($"{sb} = {exp.Calculate()}"); }
static void Main(string[] args) { var exp = new AdditionExpression(new DoubleExpression(1), new AdditionExpression(new DoubleExpression(2), new DoubleExpression(3))); var ev = new ExpressionPrinter(); exp.Accept(ev); var calc = new ExpressionCalculator(); exp.Accept(calc); WriteLine($"{ev.ToString()} = {calc.Result}"); }
public static double Calculate(this AdditionExpression ae) { return(Calculate((dynamic)ae.Left) + Calculate((dynamic)ae.Right)); }