示例#1
0
        //call an ICallable object directly
        public static object Run(Environment env, ICallable callable, List <object> arguments)
        {
            try {
                //transform the argument list
                List <Expr> exprArguments = new List <Expr>();
                foreach (object arg in arguments)
                {
                    exprArguments.Add(new Literal(arg));
                }

                //build the call object
                Token token = new Token(TokenType.EOF, "internal", null, -1);
                Call  call  = new Call(((ScriptFunction)callable).GetDeclaration(), token, exprArguments);

                //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(interpreter.Interpret(new List <Stmt>()
                {
                    new Expression(call)
                }));

                //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);
        }
示例#2
0
文件: runner.cs 项目: FireofGods/Toy
        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);
        }