コード例 #1
0
ファイル: runFile.cs プロジェクト: drewbanas/Monkey-CSC
        public static void Start(string path)
        {
            // Benchmarking variables
            System.DateTime start    = new System.DateTime();
            System.TimeSpan duration = new System.TimeSpan();
            Object.Object   result   = null;

            string input = readFile(path);
            Lexer  l     = Lexer.New(input);
            Parser p     = Parser.New(l);

            ast.Program program = p.ParseProgram();
            if (Parser.Errors().Count != 0)
            {
                repl.repl.printParserErrors(Parser.Errors());
                System.Console.ReadKey();
                System.Environment.Exit(77);
            }

            if (flag.EngineType == flag.engineType.vm)
            {
                Compiler_t comp = Compiler.New();
                Compiler._SetCompiler(ref comp);

                error err = Compiler.Compile(program);
                if (err != null)
                {
                    System.Console.WriteLine("Woops! Compilation failed:\n {0}", err);
                    System.Console.ReadKey();
                    System.Environment.Exit(78);
                }

                VM_t machine = VM.New(Compiler.Bytecode());
                VM._SetVM(ref machine); // work around

                if (flag.EnableBenchmark)
                {
                    start = System.DateTime.Now;
                }


                err = VM.Run();
                if (err != null)
                {
                    System.Console.WriteLine("Woops! Executing bytecode failed:\n {0}", err);
                    System.Console.ReadKey();
                    System.Environment.Exit(79);
                }

                if (flag.EnableBenchmark)
                {
                    duration = System.DateTime.Now.Subtract(start);
                }

                Object.Object lastPopped = VM.LastPoppedStackElem();
                System.Console.Write(lastPopped.Inspect());
                System.Console.WriteLine();

                if (flag.EnableBenchmark)
                {
                    result = lastPopped;
                }
            }
            else
            {
                Object.Environment env = Object.Environment.NewEnvironment();

                if (flag.EnableBenchmark)
                {
                    start = System.DateTime.Now;
                }

                Object.Object evaluated = evaluator.evaluator.Eval(program, env);

                if (flag.EnableBenchmark)
                {
                    duration = System.DateTime.Now.Subtract(start);
                }

                if (evaluated != null)
                {
                    System.Console.Write(evaluated.Inspect());
                    System.Console.WriteLine();
                }

                if (flag.EnableBenchmark)
                {
                    result = evaluated;
                }
            }

            if (flag.EnableBenchmark)
            {
                System.Console.WriteLine
                (
                    "\nengine={0}, result={1}, duration={2}s",
                    flag.EngineType.ToString(),
                    result.Inspect(),
                    duration.TotalSeconds.ToString()
                );

                System.Console.ReadKey();
            }
        }
コード例 #2
0
        public static void Start()
        {
            if (flag.EngineType == flag.engineType.eval)
            {
                _evalRepl();
                return; // unreachable
            }

            List <Object> constants = new List <Object>();
            List <Object> globals   = new List <Object>(new Object[VM.GlobalSize]);

            symbol_table.SymbolTable symbolTable = symbol_table.NewSymbolTable();
            for (int i = 0; i < builtins.Builtins.Length; i++)
            {
                _BuiltinDefinition v = builtins.Builtins[i];
                symbol_table.DefineBuiltin(ref symbolTable, i, v.Name);
            }

            for (;;)
            {
                System.Console.Write(PROMPT);
                string line = System.Console.ReadLine();

                Lexer  l = Lexer.New(line);
                Parser p = Parser.New(l);

                ast.Program program = p.ParseProgram();
                if (Parser.Errors().Count != 0)
                {
                    printParserErrors(Parser.Errors());
                    continue;
                }

                Compiler_t comp = Compiler.NewWithState(ref symbolTable, constants);
                Compiler._SetCompiler(ref comp); // work around

                error err = Compiler.Compile(program);
                if (err != null)
                {
                    System.Console.WriteLine("Woops! Compilation failed:\n {0}", err);
                    continue;
                }

                Bytecode code = Compiler.Bytecode();
                constants = code.Constants;

                VM_t machine = VM.NewWithGlobalStore(code, ref globals);
                VM._SetVM(ref machine); // work around

                err = VM.Run();
                if (err != null)
                {
                    System.Console.WriteLine("Woops! Executing bytecode failed:\n {0}", err);
                    continue;
                }

                Object lastPopped = VM.LastPoppedStackElem();
                System.Console.Write(lastPopped.Inspect());
                System.Console.WriteLine();
            }
        }