Beispiel #1
0
        public void Compile()
        {
            lexer.GetText();
            try
            {
                lexer.StartLexer();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                //throw;
                return;
            }

            parser = new Parser(lexer.GetTokens(), lexer.GetScriptName());
            Root root;

            try
            {
                root = parser.ParseProgram();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                //throw;
                return;
            }

            foreach (var decl in root.Decls.Where(decl => decl.ReturnName().Value.ToString().ToLower() == "main" && decl.getParams().Count == 0))
            {
                mainFound = true;
            }

            if (!mainFound)
            {
                Console.WriteLine($"{lexer.GetScriptName()}:{lexer.GetTokens().Last().LineN}: error: function main must exist and have zero parameters");
                return;
            }
            var printer = new AstPrinter();
            //printer.Print("root", root);

            var rootScope = new Scope(null, lexer.GetScriptName());

            try
            {
                root.ResolveNames(rootScope);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                GlobalVars.running = false;
                //throw;
            }
            try
            {
                root.CheckTypes();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                //throw;
            }

            if (!GlobalVars.running)
            {
                return;
            }
            PushInstructions();
            var writer = new CodeWriter();

            try
            {
                root.GenCode(writer);
                writer.DumpCode();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                //throw;
                return;
            }

            var interpreter = new Interpreter.Interpreter(writer.Code, CodeWriter.StringStorage);

            try
            {
                interpreter.Exec();
            }
            catch (Exception e)
            {
                /*if (e is System.FormatException)
                 * {
                 *  //Console.WriteLine($"{GlobalVars.FileName}:{}:Error :Input was not an integer");
                 *  //throw;
                 *  return;
                 * }*/
                Console.WriteLine(e.Message);
                //throw;
            }
        }