Exemple #1
0
        public static int Compile(string inputPath, string outputPath)
        {
            try
            {
                ANTLRFileStream input = new ANTLRFileStream(inputPath);
                //LexicalAnalysis
                TigerLexer        lexer       = new TigerLexer(input);
                CommonTokenStream tokenStream = new CommonTokenStream(lexer);

                //SintaxAnalysis
                TigerParser parser = new TigerParser(tokenStream); //Use tokens obtained by lexer
                parser.TreeAdaptor = new ASTFactory();             //Use my ASTFactory
                TigerParser.tiger_program_return tigerProgram = parser.tiger_program();

                //**Check lexical and sintax errors

                if (lexer.Errors.Count == 0 && parser.Errors.Count == 0)
                {
                    //SemanticAnalysis
                    List <Error>    semanticErrors = new List <Error>();
                    SymbolTable     symbolTable    = new SymbolTable();
                    InstructionNode decoratedExpr  = tigerProgram.Tree as InstructionNode;
                    decoratedExpr.CheckSemantic(semanticErrors, symbolTable);
                    if (semanticErrors.Count == 0)
                    {
                        TypeExpression programReturnType = decoratedExpr.ReturnType.PrimitiveType;
                        if (programReturnType is BuiltInType || programReturnType is NilType)
                        {
                            ProgramGenerator.BuildTigerProgram(outputPath, decoratedExpr);
                            return(ReportCompilationSuccesful(inputPath));
                        }
                        string message = "If program isn't a valueless expression its return type has to be a built-in type";
                        semanticErrors.Add(new Error(message, decoratedExpr.Line, decoratedExpr.CharPositionInLine));
                    }
                    return(ReportCompilationFailed(semanticErrors));
                }
                IEnumerable <Error> antlrErrors = lexer.Errors.Union(parser.Errors);
                return(ReportCompilationFailed(antlrErrors));
            }
            catch (Exception e)
            {
                IEnumerable <Error> exeption = new[] { new Error(e.Message, 0, 0, ErrorKind.UserError) };
                return(ReportCompilationFailed(exeption));
            }
        }