Ejemplo n.º 1
0
        public Expressions Parse(string sourceText) //API entry point
        {
            ResetParserState();
            sourceText += '\0';  //TOOD: figure out how newline is interpreted in a c# string
            m_Lexer.Lex(sourceText, Lexer.Lexer.IncludeWhiteSpaceTokens.No, Lexer.Lexer.IncludeEofToken.Yes);
            m_tokenList = m_Lexer.GetTokens();

            if (m_Lexer.m_Diagnostics.Count > 0)
            {
                foreach (var errorMessage in m_Lexer.m_Diagnostics)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Lexer: " + errorMessage);
                    Console.ResetColor();
                }

                return(null);
            }

            switch (Peek().TokenType)
            {
            //ParseStatement,....
            default:
                return(ParseExpression());
            }
        }
Ejemplo n.º 2
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;
            }
        }