/// <summary> /// Visit a parse tree produced by <see cref="CustomExpressionParser.exp"/>. /// <para> /// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/> /// on <paramref name="context"/>. /// </para> /// </summary> /// <param name="context">The parse tree.</param> /// <return>The visitor result.</return> public virtual Result VisitExp([NotNull] CustomExpressionParser.ExpContext context) { return(VisitChildren(context)); }
public override object VisitExp([NotNull] CustomExpressionParser.ExpContext context) { if (context.lit != null) { return(this.VisitLiteral(context.lit)); } if (context.func != null) { var f = VisitExp(context.func); var args = (context.args != null) ? VisitExpList(context.args) : Array.Empty <object>(); if (context.ptArg != null) { if (args is object[] o) { var update = new object[o.Length + 1]; for (var i = 0; i < o.Length; i++) { update[i] = o[i]; } update[o.Length] = VisitPlainTextArgument(context.ptArg); args = update; } else { throw new ArgumentException(); } } return(new object[] { "call", f, args }); } var unop = context.unop?.Text; if (!string.IsNullOrEmpty(unop)) { var exps = context.exp(); switch (unop) { case "!": case "-": case "+": return(new object[] { unop, VisitExp(exps[0]) }); default: throw new ArgumentException($"exp '{context.GetText()}', '{unop}'"); } } else { var op = context.op?.Text; var exps = context.exp(); switch (op) { case "(": return(VisitExp(exps[0])); case ">=": case "<=": case ">": case "<": case "==": case "!=": case "+": case "-": case "*": case "/": case "%": case ".": return(new object[] { op, VisitExp(exps[0]), VisitExp(exps[1]) }); default: throw new ArgumentException($"exp '{context.GetText()}', '{op}'"); } } }