Пример #1
0
        private static void RunFile(string filename)
        {
            if (File.Exists(filename))
            {
                string fileContents = File.ReadAllText(filename);

                Scanner      scanner = new Scanner(fileContents);
                List <Token> tokens  = scanner.ScanTokens();
                Parser       parser  = new Parser(tokens);
                List <Stmt>  ast     = parser.ParseFile();

                if (debugMode)
                {
                    AstPrinter astp = new AstPrinter();
                    foreach (var stmt in ast)
                    {
                        Console.WriteLine(astp.Print(stmt));
                    }
                    Console.WriteLine();
                }

                if (hadError)
                {
                    System.Environment.Exit(1);
                }

                interpreter.Interpret(ast);

                if (hadRuntimeError)
                {
                    System.Environment.Exit(2);
                }
            }
            else
            {
                Console.WriteLine("File \"{0}\" doesn't exist!", filename);
            }
        }
Пример #2
0
        private static void RunPrompt()
        {
            if (!debugMode)
            {
                Console.WriteLine("Feebl v.0.1.");
            }
            else
            {
                Console.WriteLine("Feebl v.0.1. AST print mode on.");
            }


            while (true)
            {
                hadError = false;
                Console.Write("> ");

                string       source  = Console.ReadLine();
                Scanner      scanner = new Scanner(source);
                List <Token> tokens  = scanner.ScanTokens();
                Parser       parser  = new Parser(tokens);
                object       ast     = parser.ParseRepl();

                if (hadError)
                {
                    continue;
                }

                if (ast is List <Stmt> )
                {
                    if (hadError)
                    {
                        continue;
                    }

                    if (debugMode)
                    {
                        AstPrinter astp = new AstPrinter();
                        foreach (var stmt in (ast as List <Stmt>))
                        {
                            Console.WriteLine(astp.Print(stmt));
                        }
                    }

                    interpreter.Interpret((List <Stmt>)ast);
                }
                else if (ast is Expr)
                {
                    if (debugMode)
                    {
                        AstPrinter astp = new AstPrinter();
                        Console.WriteLine(astp.Print(ast as Expr));
                    }

                    string result = interpreter.Interpret((Expr)ast);
                    if (result != null)
                    {
                        Console.WriteLine("-> {0}", result);
                    }
                }
            }
        }