public override Expression VisitAdditive_expression(Additive_expressionContext context) { var expression = Visit(context.children[0]); for (int i = 1; i < context.children.Count - 1; i += 2) { var current = Visit(context.children[i + 1]); var op = context.children[i].GetText(); if (op == "+" && expression.Type == typeof(String)) { var mi = typeof(String).GetMethod(nameof(String.Concat), new[] { typeof(String), typeof(String) }); expression = Expression.Add(expression, current, mi); } else if (op == "+") { expression = Expression.Add(expression, current); } else if (op == "-") { expression = Expression.Subtract(expression, current); } else { throw new CompilationException($"Unsupported operation. Operation={op}", context); } } return(expression); }
public Additive_expressionContext additive_expression() { Additive_expressionContext _localctx = new Additive_expressionContext(_ctx, State); EnterRule(_localctx, 230, RULE_additive_expression); int _la; try { EnterOuterAlt(_localctx, 1); { State = 1259; multiplicative_expression(); State = 1264; _errHandler.Sync(this); _la = _input.La(1); while (_la==ADD || _la==SUB) { { { State = 1260; _la = _input.La(1); if ( !(_la==ADD || _la==SUB) ) { _errHandler.RecoverInline(this); } Consume(); State = 1261; multiplicative_expression(); } } State = 1266; _errHandler.Sync(this); _la = _input.La(1); } } } catch (RecognitionException re) { _localctx.exception = re; _errHandler.ReportError(this, re); _errHandler.Recover(this, re); } finally { ExitRule(); } return _localctx; }