//call an ICallable object directly public static object Run(Environment env, ICallable callable, List <object> arguments) { try { //build the call object Token token = new Token(TokenType.EOF, "internal", null, -1); //build a new interpreter Interpreter interpreter = new Interpreter(env); Resolver resolver = new Resolver(interpreter); resolver.Resolve(((ScriptFunction)callable).GetDeclaration()); //call the ICallable, returning the result return(callable.Call(interpreter, token, arguments)); //WARNING: duplicate code } catch (ErrorHandler.AssertError) { ConsoleOutput.Log("Assert error caught at Run()"); ConsoleOutput.Log("The program will now exit early"); } catch (ErrorHandler.ParserError e) { ConsoleOutput.Log("Parser error caught at Run()"); ConsoleOutput.Log("The following output is for internal debugging only, and will be removed from the final release:\n" + e.ToString()); } catch (ErrorHandler.ResolverError e) { ConsoleOutput.Log("Resolver error caught at Run()"); ConsoleOutput.Log("The following output is for internal debugging only, and will be removed from the final release:\n" + e.ToString()); } catch (ErrorHandler.RuntimeError e) { ConsoleOutput.Log("Runtime error caught at Run()"); ConsoleOutput.Log("The following output is for internal debugging only, and will be removed from the final release:\n" + e.ToString()); } catch (Exception e) { ConsoleOutput.Log("Terminal error caught at Run()"); ConsoleOutput.Log("The following output is for internal debugging only, and will be removed from the final release:\n" + e.ToString()); } return(null); }
public static Environment Run(Environment env, string source) { try { Scanner scanner = new Scanner(source); Parser parser = new Parser(scanner.ScanTokens()); List <Stmt> stmtList = parser.ParseStatements(); if (ErrorHandler.HadError) { return(null); } Interpreter interpreter = new Interpreter(env == null ? new Environment() : env); Resolver resolver = new Resolver(interpreter); resolver.Resolve(stmtList); if (ErrorHandler.HadError) { return(null); } interpreter.Interpret(stmtList); //return the environment context for the import keyword return(interpreter.environment); } catch (ErrorHandler.AssertError) { ConsoleOutput.Log("Assert error caught at Run()"); ConsoleOutput.Log("The program will now exit early"); } catch (ErrorHandler.ParserError e) { ConsoleOutput.Log("Parser error caught at Run()"); ConsoleOutput.Log("The following output is for internal debugging only, and will be removed from the final release:\n" + e.ToString()); } catch (ErrorHandler.ResolverError e) { ConsoleOutput.Log("Resolver error caught at Run()"); ConsoleOutput.Log("The following output is for internal debugging only, and will be removed from the final release:\n" + e.ToString()); } catch (ErrorHandler.RuntimeError e) { ConsoleOutput.Log("Runtime error caught at Run()"); ConsoleOutput.Log("The following output is for internal debugging only, and will be removed from the final release:\n" + e.ToString()); } catch (Exception e) { ConsoleOutput.Log("Terminal error caught at Run()"); ConsoleOutput.Log("The following output is for internal debugging only, and will be removed from the final release:\n" + e.ToString()); } return(null); }
//visitor pattern public object Visit(Print stmt) { object value = Evaluate(stmt.expression); //unescape a string if (value is string) { ConsoleOutput.Log(System.Text.RegularExpressions.Regex.Unescape((string)value)); } else if (value is null) { ConsoleOutput.Log("null"); } else { ConsoleOutput.Log(value); } return(null); }
private static void Report(int line, string where, string message) { ConsoleOutput.Log($"[line {line}] Error {where}: {message}"); HadError = true; }