示例#1
0
        //throw new CompilerException(nvae.Message, nvae.Line, nvae.CharPositionInLine);
        public void Compile(string FileName, string Source)
        {
            Tokens = new List <Token>();
            Errors = new List <CompilerError>();
            CoolGrammarLexer  lexer     = null;
            CoolGrammarParser parser    = null;
            CoolCodeGenerator generator = null;

            try
            {
                var stream = new ANTLRStringStream(Source);
                lexer = new CoolGrammarLexer(stream, new RecognizerSharedState()
                {
                    errorRecovery = true
                });

                IToken token;
                token = lexer.NextToken();
                while (token.Type != CoolGrammarLexer.EOF)
                {
                    Tokens.Add(
                        new Token
                    {
                        Name   = CoolTokens.Dictionary[token.Type],
                        Value  = token.Text,
                        Line   = token.Line,
                        Column = token.CharPositionInLine
                    });
                    token = lexer.NextToken();
                }
                lexer.Reset();
                lexer.Line = 0;
                lexer.CharPositionInLine = 0;
            }
            catch (EarlyExitException exception)
            {
                Errors.Add(new EarlyExitErrorLexer(exception.Message,
                                                   Errors.Count(), exception.Line, exception.CharPositionInLine));
            }
            catch (MismatchedSetException exception)
            {
                Errors.Add(new MismatchedSetErrorLexer(exception.Message,
                                                       Errors.Count(), exception.Line, exception.CharPositionInLine));
            }
            catch (NoViableAltException exception)
            {
                Errors.Add(new NoViableAltErrorLexer(exception.Message,
                                                     Errors.Count(), exception.Line, exception.CharPositionInLine));
            }
            catch (CompilerException exception)
            {
                Errors.Add(new CommonLexerError(exception.Message, Errors.Count(), exception.Line, exception.Column));
            }
            catch
            {
            }

            try
            {
                var tokenStream = new CommonTokenStream(lexer);
                parser = new CoolGrammarParser(tokenStream);
                Tree   = parser.program();
            }
            catch (EarlyExitException exception)
            {
                Errors.Add(new EarlyExitErrorParser(exception.Message,
                                                    Errors.Count(), exception.Line, exception.CharPositionInLine));
            }
            catch (MismatchedSetException exception)
            {
                Errors.Add(new MismatchedSetErrorParser(exception.Message,
                                                        Errors.Count(), exception.Line, exception.CharPositionInLine));
            }
            catch (NoViableAltException exception)
            {
                Errors.Add(new NoViableAltErrorParser(exception.Message,
                                                      Errors.Count(), exception.Line, exception.CharPositionInLine));
            }
            catch (RecognitionException exception)
            {
                Errors.Add(new RecognitionError(exception.Message,
                                                Errors.Count(), exception.Line, exception.CharPositionInLine));
            }
            catch (RewriteEarlyExitException exception)
            {
                Errors.Add(new RewriteEarlyExitError(exception.Message,
                                                     Errors.Count()));
            }
            catch (CompilerException exception)
            {
                Errors.Add(new CommonParserError(exception.Message, Errors.Count(), exception.Line, exception.Column));
            }
            catch (Exception exception)
            {
                Errors.Add(new CommonParserError(exception.Message, Errors.Count(), null, null));
            }

            try
            {
                generator = new CoolCodeGenerator(FileName, Tree.Tree);
                generator.Generate();

                GeneratedProgramName = System.IO.Path.GetFileNameWithoutExtension(FileName) + ".exe";

                foreach (var error in generator.CompilerErrors)
                {
                    Errors.Add(error);
                }
            }
            catch (Exception e)
            {
                Errors.Add(new CommonError(e.Message, Errors.Count));
            }


            if (Tree == null)
            {
                Tree = new AstParserRuleReturnScope <CommonTree, CommonToken>();
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            /* Note Comentar estas 2 lineas para ejecutarlo por consola */
            args    = new string[1];
            args[0] = $"{Environment.CurrentDirectory}/examples/cases.cool";

            foreach (var arg in args)
            {
                #region Check file exist + update path

                string current = arg;
                if (!File.Exists(arg))
                {
                    current = Environment.CurrentDirectory + "/examples/" + arg;
                    if (!File.Exists(current))
                    {
                        Console.WriteLine("No such file or directory. Please enter a valid .cool file");
                        break;
                    }
                }

                #endregion

                #region Parser-Lexer

                var input  = new ANTLRFileStream(current);
                var lexer  = new CoolGrammarLexer(input);
                var tokens = new CommonTokenStream(lexer);
                var parser = new CoolGrammarParser(tokens)
                {
                    TreeAdaptor = new TreeAdaptor()
                };
                var programNode = (ProgramNode)parser.program().Tree;

                if (Logger.HasError)
                {
                    Logger.Report();
                    return;
                }

                #endregion

                #region Semantic

                var typeCollectorVisitor = new TypeCollectorVisitor();
                typeCollectorVisitor.CheckSemantic(programNode);
                if (Logger.HasError)
                {
                    Logger.Report();
                    return;
                }

                var typeBuilderVisitor = new TypeBuilderVisitor();
                typeBuilderVisitor.CheckSemantic(programNode);
                if (Logger.HasError)
                {
                    Logger.Report();
                    return;
                }

                var typeCheckerVisitor = new TypeCheckerVisitor();
                typeCheckerVisitor.CheckSemantic(programNode);
                if (Logger.HasError)
                {
                    Logger.Report();
                    return;
                }

                #endregion

                #region Code Generation

                var codeGeneratorVisitor = new CodeGeneratorVisitor();
                codeGeneratorVisitor.GenerateCode(programNode);

                var mipsGenerator = new MIPS_GeneratorVisitor();
                mipsGenerator.GenerateCode(CIL_Factory.Program);

                string mipsCode = $"{Path.GetFileNameWithoutExtension(arg)}.s";
                string path     = arg.Replace(Path.GetFileName(arg), "");
                writeOutput(mipsGenerator.MipsInstructions, path + mipsCode);

                #endregion

                #region qtSpim

                using (Process openQtSpim = new Process())
                {
                    openQtSpim.StartInfo.UseShellExecute = false;
                    openQtSpim.StartInfo.FileName        = "/Applications/QtSpim.app/Contents/MacOS/QtSpim";
                    openQtSpim.StartInfo.Arguments       = mipsCode;
                    openQtSpim.Start();
                }

                #endregion
            }
        }
示例#3
0
        //throw new CompilerException(nvae.Message, nvae.Line, nvae.CharPositionInLine);
        public void Compile(string FileName, string Source)
        {
            Tokens = new List<Token>();
            Errors = new List<CompilerError>();
            CoolGrammarLexer lexer = null;
            CoolGrammarParser parser = null;
            CoolCodeGenerator generator = null;

            try
            {
                var stream = new ANTLRStringStream(Source);
                lexer = new CoolGrammarLexer(stream, new RecognizerSharedState() { errorRecovery = true });

                IToken token;
                token = lexer.NextToken();
                while (token.Type != CoolGrammarLexer.EOF)
                {
                    Tokens.Add(
                        new Token
                        {
                            Name = CoolTokens.Dictionary[token.Type],
                            Value = token.Text,
                            Line = token.Line,
                            Column = token.CharPositionInLine
                        });
                    token = lexer.NextToken();
                }
                lexer.Reset();
                lexer.Line = 0;
                lexer.CharPositionInLine = 0;
            }
            catch (EarlyExitException exception)
            {
                Errors.Add(new EarlyExitErrorLexer(exception.Message,
                    Errors.Count(), exception.Line, exception.CharPositionInLine));
            }
            catch (MismatchedSetException exception)
            {
                Errors.Add(new MismatchedSetErrorLexer(exception.Message,
                    Errors.Count(), exception.Line, exception.CharPositionInLine));
            }
            catch (NoViableAltException exception)
            {
                Errors.Add(new NoViableAltErrorLexer(exception.Message,
                    Errors.Count(), exception.Line, exception.CharPositionInLine));
            }
            catch (CompilerException exception)
            {
                Errors.Add(new CommonLexerError(exception.Message, Errors.Count(), exception.Line, exception.Column));
            }
            catch
            {

            }

            try
            {
                var tokenStream = new CommonTokenStream(lexer);
                parser = new CoolGrammarParser(tokenStream);
                Tree = parser.program();
            }
            catch (EarlyExitException exception)
            {
                Errors.Add(new EarlyExitErrorParser(exception.Message,
                    Errors.Count(), exception.Line, exception.CharPositionInLine));
            }
            catch (MismatchedSetException exception)
            {
                Errors.Add(new MismatchedSetErrorParser(exception.Message,
                    Errors.Count(), exception.Line, exception.CharPositionInLine));
            }
            catch (NoViableAltException exception)
            {
                Errors.Add(new NoViableAltErrorParser(exception.Message,
                    Errors.Count(), exception.Line, exception.CharPositionInLine));
            }
            catch (RecognitionException exception)
            {
                Errors.Add(new RecognitionError(exception.Message,
                    Errors.Count(), exception.Line, exception.CharPositionInLine));
            }
            catch (RewriteEarlyExitException exception)
            {
                Errors.Add(new RewriteEarlyExitError(exception.Message,
                    Errors.Count()));
            }
            catch (CompilerException exception)
            {
                Errors.Add(new CommonParserError(exception.Message, Errors.Count(), exception.Line, exception.Column));
            }
            catch (Exception exception)
            {
                Errors.Add(new CommonParserError(exception.Message, Errors.Count(), null, null));
            }

            try
            {
                generator = new CoolCodeGenerator(FileName, Tree.Tree);
                generator.Generate();

                GeneratedProgramName = System.IO.Path.GetFileNameWithoutExtension(FileName) + ".exe";

                foreach (var error in generator.CompilerErrors)
                    Errors.Add(error);
            }
            catch (Exception e)
            {
                Errors.Add(new CommonError(e.Message, Errors.Count));
            }

            if (Tree == null)
                Tree = new AstParserRuleReturnScope<CommonTree, CommonToken>();
        }