Beispiel #1
0
        /// <summary>
        /// Compiles the received input to an executable file.
        /// </summary>
        /// <param name="input"></param>
        public void Compile(string input)
        {
            // CREATE LEXER FROM SOURCE CODE, GET TOKENS.
            // Create char stream from input source code.
            AntlrInputStream inputStream = new AntlrInputStream(input);
            // Create lexer that will use the char stream.
            CLUBSLexer CLUBSLexer = new CLUBSLexer(inputStream);
            // Create token stream using the lexer.
            CommonTokenStream commonTokenStream = new CommonTokenStream(CLUBSLexer);

            // CREATE PARSER.
            ErrorLogger errorLogger = new ErrorLogger();
            // Create parser that will use the tokens.
            CLUBSParser CLUBSParser = new CLUBSParser(commonTokenStream);

            ;
            // Add own error handling.
            CLUBSParser.RemoveErrorListeners();
            CLUBSParser.AddErrorListener(errorLogger);
            Console.WriteLine("\n\nCOMPILER STAGE: Parser generated");
            ;
            // GENERATE CST/Parse tree from tokens.
            // Begin parsing from prog rule (the start rule)
            CLUBSParser.ProgContext cst = CLUBSParser.prog();
            Console.WriteLine("COMPILER STAGE: Parsing done (CST built)");
            // Check any parser errrors. Print these and dont proceed to next stage.
            //PrintErrorsAndExitIfAny(errorLogger); // XXX: Vi kan ikke fortsætte med at compile hvis syntax errors. Er det et problem?

            //ParseTreeWalker walker = new ParseTreeWalker(); // NOTE: Do not remove. Prints the parse tree (ish..)
            //.Walk(new ParseTreeListener(), cst);
            //Console.ReadKey();

            // GENERATE AST.
            ASTNode ast = new BuildASTVisitor().VisitProg(cst);

            ;

            Console.WriteLine("COMPILER STAGE: AST built.");
            ;
            // PRINT AST
            //new PrintVisitor().Visit(ast);
            // DECORATE AST
            Checker checker = new Checker(errorLogger);

            checker.Visit(ast);

            Console.WriteLine("COMPILER STAGE: Checker finished.");

            // SHOW ACE COMPILER ERRORS
            // Print any compiler errors and do not translate to IR (C#).
            PrintErrorsAndExitIfAny(errorLogger);

            string sourceCode = new CodeGenerator().GenerateCode(ast);

            CompileToExe(sourceCode, "AceProgram.exe");

            Console.WriteLine("All done.");
        }
Beispiel #2
0
        public ASTNode ParseInput(string input)
        {
            StringBuilder builder = new StringBuilder();

            builder.Append("setup {\n");
            builder.Append(input);
            builder.Append("\n}");

            AntlrInputStream  inputStream       = new AntlrInputStream(builder.ToString());
            CLUBSLexer        CLUBSLexer        = new CLUBSLexer(inputStream);
            CommonTokenStream commonTokenStream = new CommonTokenStream(CLUBSLexer);

            CLUBSParser CLUBSParser = new CLUBSParser(commonTokenStream);

            BlockNode block = (BuildASTVisitor.VisitProg(CLUBSParser.prog()) as ProgNode).Children.First() as BlockNode;

            return(block.Statements.FirstOrDefault());
        }