private static int runLines(Interpreter interp, IEnumerable <string> lines) { var ls = new LocalScope("code-inner"); var obj = new UserObject(); interp.Extend(obj); ls.AddLocalDef("self", obj); interp.ExtendMinor(ls); var memo = interp.Memorise(); bool unfinished; GraceObject result; foreach (var line in lines) { int r = REPL.RunLine(interp, obj, memo, line, out unfinished, out result); if (r != 0) { return(r); } if (unfinished) { return(1); } } return(0); }
private static int repl(string filename, string builtinsExtensionFile) { Console.WriteLine("* Grace REPL with runtime " + Interpreter.GetRuntimeVersion()); Console.WriteLine("Copyright (C) 2015, 2016 its authors."); Console.WriteLine("This program comes with ABSOLUTELY NO WARRANTY;" + " for details type `--about'."); ParseNode module; var ls = new LocalScope("repl-inner"); var obj = new UserObject(); var interp = REPL.CreateInterpreter(obj, ls); if (builtinsExtensionFile != null) { interp.LoadExtensionFile(builtinsExtensionFile); } else { interp.LoadExtensionFile(); } if (filename != null) { if (!File.Exists(filename)) { return(error("File `" + filename + "` does not exist.")); } var dir = Path.GetDirectoryName(Path.GetFullPath(filename)); interp.AddModuleRoot(dir); Console.WriteLine("* Loading " + filename + "..."); using (StreamReader reader = File.OpenText(filename)) { var parser = new Parser( Path.GetFileNameWithoutExtension(filename), reader.ReadToEnd()); module = parser.Parse(); ExecutionTreeTranslator ett = new ExecutionTreeTranslator(); Node eModule = ett.Translate(module as ObjectParseNode); try { obj = (UserObject)eModule.Evaluate(interp); } catch (GraceExceptionPacketException e) { Console.Error.WriteLine("Uncaught exception:"); ErrorReporting.WriteException(e.ExceptionPacket); if (e.ExceptionPacket.StackTrace != null) { foreach (var l in e.ExceptionPacket.StackTrace) { Console.Error.WriteLine(" from " + l); } } return(1); } } Console.WriteLine("* Loaded."); } else { var dir = Path.GetFullPath("."); interp.AddModuleRoot(dir); } Console.WriteLine("* Enter code at the prompt.\n"); ErrorReporting.SilenceError("P1001"); var memo = interp.Memorise(); var edit = new Editor(s => completion(obj, s)); string accum = String.Empty; bool unfinished = false; GraceObject result; string line = edit.GetLine(">>> "); while (line != null) { if (line.StartsWith("--about")) { var spl = line.Split(' '); about(spl.Length > 1 ? spl[1] : null); line = edit.GetLine(">>> "); continue; } accum += line.Replace("\u0000", "") + "\n"; var r = REPL.RunLine(interp, obj, memo, accum, out unfinished, out result); if (result != null) { ls["LAST"] = result; } if (unfinished) { // "Unexpected end of file" is expected here // for unfinished statements. line = edit.GetLine("... "); unfinished = false; continue; } else if (r != 0) { // All other errors are errors, and should // clear the accumulated buffer and let the // user start again. accum = String.Empty; } else { accum = String.Empty; } line = edit.GetLine(">>> "); } return(0); }