예제 #1
0
        /// <summary>
        /// Creates an AST from the given CST (<see cref="ParserRuleContext"/>).
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override ASTNode VisitProg(CLUBSParser.ProgContext context)
        {
            ProgNode node = new ProgNode(new SourcePosition(context.start));

            node.Children.Add(Visit(context.setupBlock));

            return(node);
        }
예제 #2
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.");
        }