public List <Stmt> ConvertRepl(StannumParser.ReplContext context, string name) { var statements = new List <Stmt>(); for (var i = 0; i < context._Stmts.Count; i += 1) { if (!(Visit(context._Stmts[i]) is Stmt statement)) { throw new Exception("Unrecognized statement!"); } statements.Add(statement); } if (context.Value == null) { return(statements); } if (!(Visit(context.Value) is Expr expression)) { throw new Exception("Unrecognized expression!"); } statements.Add(new DefStmt(name, expression)); statements.Add(new ExprStmt(new CallExpr(new Literal(new Builtin(1, arguments => { Console.WriteLine(Interpreter.Stringify(arguments[0])); return(null); })), new List <Expr> { new Identifier(name) }))); return(statements); }
/// <summary> /// Visit a parse tree produced by <see cref="StannumParser.repl"/>. /// <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 VisitRepl([NotNull] StannumParser.ReplContext context) { return(VisitChildren(context)); }