コード例 #1
0
ファイル: Compilator.cs プロジェクト: nardo7/COOLcompiler
        public static Tuple <int, Context> SemantickCheck(COOLLenguage.SemanticCheck.AST1.Program ast)
        {
            var logger = new ErrorLogger(Console.Out);
            TypeCollectorVisitor visit1 = new TypeCollectorVisitor()
            {
                log = logger
            };

            visit1.Visit(ast);
            TypeBuilderVisitor visit2 = new TypeBuilderVisitor()
            {
                Context = visit1.Context, errorLog = logger
            };

            visit2.Visit(ast);
            TypeChekerVisitor visit3 = new TypeChekerVisitor()
            {
                Context = visit2.Context, errorLog = logger
            };

            visit3.Visit(ast);
            return(new Tuple <int, Context>(visit3.errorLog.CountErrors, (Context)visit3.Context));
        }
コード例 #2
0
ファイル: Class1.cs プロジェクト: nardo7/COOLcompiler
        public static void parser(string code)
        {
            System.IO.DirectoryInfo d = new DirectoryInfo("E:\\Universidad\\4to\\compilacion 2\\testing");
            var files = d.GetFiles("*.cl");
            //foreach (var file in files)
            //{


            var               stringStream = new ANTLRFileStream(code);
            COOLLexer         lexer        = new COOLLexer(stringStream);
            CommonTokenStream stream       = new CommonTokenStream(lexer);
            COOLParser        parser       = new COOLParser(stream);
            ITreeAdaptor      adaptor      = new COOLCommonTreeAdaptor();

            parser.TreeAdaptor      = adaptor;
            parser.TraceDestination = Console.Out;
            var tree = parser.program();
            var t    = (COOLCommonTree)tree.Tree;
            var ast1 = t.GetAST1();
            TypeCollectorVisitor visit1 = new TypeCollectorVisitor()
            {
                log = new ErrorLogger(Console.Out)
            };

            visit1.Visit((Program)ast1);
            TypeBuilderVisitor visit2 = new TypeBuilderVisitor()
            {
                Context = visit1.Context, errorLog = new ErrorLogger(Console.Out)
            };

            visit2.Visit((Program)ast1);
            TypeChekerVisitor tcv = new TypeChekerVisitor()
            {
                Context = visit2.Context, errorLog = new ErrorLogger(Console.Out)
            };

            tcv.Visit((Program)ast1);

            //build ast to generate
            var astGenerator = (CoolCompilator.Program)ast1.GetAstCodeGenerator();

            CoolCompilator.AstNodeDisplayer displayer = new CoolCompilator.AstNodeDisplayer(Console.Out, CoolCompilator.AstNodeDisplayer.display_option.DISPLAYNONBASIC);
            displayer.visit((CoolCompilator.Program)astGenerator);
            var rootType = tcv.Context.GetType("Object");
            var IntType  = tcv.Context.GetType("Int");
            var IOType   = tcv.Context.GetType("IO");
            var strType  = tcv.Context.GetType("String");
            var boolType = tcv.Context.GetType("Bool");
            var VoidType = tcv.Context.GetType("Void");
            Dictionary <CoolCompilator.Class, CoolCompilator.Class> dict = new Dictionary <CoolCompilator.Class, CoolCompilator.Class>();
            Dictionary <string, CoolCompilator.Class> codeClass          = new Dictionary <string, CoolCompilator.Class>();


            var classs = astGenerator.classes.ToList();

            classs.AddRange(new CoolCompilator.Class[] { rootType.ToNodeCodeClass(), IntType.ToNodeCodeClass(), strType.ToNodeCodeClass(),
                                                         IOType.ToNodeCodeClass(), boolType.ToNodeCodeClass(), VoidType.ToNodeCodeClass() });
            astGenerator.classes = classs.ToArray();
            for (int i = 0; i < astGenerator.classes.Length; i++)
            {
                codeClass.Add(astGenerator.classes[i].name.value, astGenerator.classes[i]);
            }
            foreach (var name in codeClass.Keys)
            {
                if (name == "Object")
                {
                    dict.Add(codeClass[name], new CoolCompilator.Class(CoolCompilator.Constants.NOCLASS, "", new CoolCompilator.Attribute[0], new CoolCompilator.Method[0]));
                }
                else
                {
                    dict.Add(codeClass[name], codeClass[codeClass[name].parent.value]);
                }
            }
            StreamWriter sw = new StreamWriter("./out.s");

            CoolCompilator.AstNodeCodeGenerator cd = new CoolCompilator.AstNodeCodeGenerator(dict, sw);
            cd.visit(astGenerator);
            //}



            //if (parser.NumberOfSyntaxErrors > 0)
            //{
            //    Console.WriteLine("error");
            //}
        }
コード例 #3
0
 static void Main(string[] args)
 {
     if (args.Length > 0)
     {
         try
         {
             //var streamReader = new StreamReader("Testing/Tipos.cl");
             //var streamReader = new StreamReader("Testing/Dispatch.cl");
             //var streamReader = new StreamReader("Testing/ZAHUIS.cl");
             //var streamReader = new StreamReader("Testing/Comparaciones.cl");
             //var streamReader = new StreamReader("Testing/Expresion Case.cl");
             //var streamReader = new StreamReader("Testing/Bool.cool");
             //var streamReader = new StreamReader("Testing/Nuevo tipo.cl");
             //var streamReader = new StreamReader("Testing/String.cl");
             //var streamReader = new StreamReader("Testing/Tipos.cl");
             //var streamReader = new StreamReader("Testing/Dispatch.cl");
             //var streamReader = new StreamReader("Testing/ZAHUIS.cl");
             //var streamReader = new StreamReader("Testing/Expresion Case.cl");
             //var streamReader = new StreamReader("Testing/Bool.cool");
             //var streamReader = new StreamReader("Testing/Nuevo tipo.cl");
             //var streamReader = new StreamReader("Testing/Comparaciones.cl");
             //var streamReader = new StreamReader("Testing/String.cl");
             //var streamReader = new StreamReader("Testing/Expresion let.cl");
             //var streamReader = new StreamReader("Testing/Tipos.cl");
             //var streamReader = new StreamReader("Testing/While.cl");
             //var streamReader = new StreamReader("Testing/Example.cl");
             //var streamReader = new StreamReader("Testing/Factorial.cl");
             //var streamReader = new StreamReader("Testing/Tipos.cl");
             //var streamReader = new StreamReader("Testing/Test de Cool.cl");
             //var streamReader = new StreamReader("Testing/Basic Types.cl");
             //var streamReader = new StreamReader("bigexample.cl");
             var path         = args[0];
             var streamReader = new StreamReader(path);
             #region Lexer
             var MyLexer = new COOLgrammarLexer(new AntlrInputStream(streamReader.ReadToEnd()));
             MyLexer.RemoveErrorListeners();
             var myErrorsLexer = new MyErrorListenerLexer();
             MyLexer.AddErrorListener(myErrorsLexer);
             streamReader.Close();
             if (myErrorsLexer.errorLoggers.msgs.Count != 0)
             {
                 foreach (var e in myErrorsLexer.errorLoggers.msgs)
                 {
                     Console.WriteLine(e);
                 }
                 return;
             }
             #endregion
             var tokens = new CommonTokenStream(MyLexer);
             #region Parser
             var MyParser = new COOLgrammarParser(tokens);
             MyParser.RemoveErrorListeners();
             var myErrors = new MyErrorListenerParser();
             MyParser.AddErrorListener(myErrors);
             var program = MyParser.program();
             //Si hay errores para la ejecucion del programa
             if (myErrors.errorLoggers.msgs.Count != 0)
             {
                 foreach (var e in myErrors.errorLoggers.msgs)
                 {
                     Console.WriteLine(e);
                 }
                 return;
             }
             //Console.WriteLine(program.ToStringTree(MyParser));
             #endregion
             #region CheckSemantic
             var typeVisitor = new TypeBuilderVisitor();
             typeVisitor.Visit(program);
             var FeatureVisitor = new FeatureBuilderVisitor(typeVisitor);
             FeatureVisitor.Visit(program);
             var CheckSemantic = new CheckSemanticVisitor(FeatureVisitor);
             CheckSemantic.Visit(program);
             if (CheckSemantic.errorLogger.msgs.Count != 0)
             {
                 foreach (var item in CheckSemantic.errorLogger.msgs)
                 {
                     Console.WriteLine(item);
                 }
                 return;
             }
             #endregion
             #region CoolToCil
             var generateCilTypes = new GenerateToCilTypes(CheckSemantic);
             generateCilTypes.Visit(program);
             //var generateCilFeatures = new GenerateToCilFeatures(generateCilTypes);
             //generateCilFeatures.Visit(program);
             var generateCil = new GenerateToCil(CheckSemantic, generateCilTypes);
             //var generateCil = new GenerateToCilOptimization(CheckSemantic, generateCilFeatures);
             generateCil.Visit(program);
             // Console.WriteLine(generateCil.CilAst);
             // StreamWriter streamWriter = new StreamWriter("mips.cil");
             // streamWriter.WriteLine(generateCil.CilAst);
             // streamWriter.Close();
             #endregion
             #region CilToMIPS
             var generateMips = new CilToMips(generateCil);
             var mips         = generateMips.Visit(program);
             var streamWriter = new StreamWriter($"{path.Replace(".cl",".mips")}");
             streamWriter.WriteLine(".data");
             mips.Data.ForEach(x => streamWriter.WriteLine(x));
             streamWriter.WriteLine(".text");
             mips.Text.ForEach(x => streamWriter.WriteLine(x));
             streamWriter.WriteLine("##-----functions-----");
             mips.Functions.ForEach(x => streamWriter.WriteLine(x));
             //Console.WriteLine(".data");
             //mips.Data.ForEach(x => Console.WriteLine(x));
             //Console.WriteLine(".text");
             //mips.Text.ForEach(x => Console.WriteLine(x));
             //Console.WriteLine("##-----functions-----");
             //mips.Functions.ForEach(x => Console.WriteLine(x));
             streamWriter.Close();
             #endregion
             Console.WriteLine("Success!!");
         }
         catch (Exception e)
         {
             Console.WriteLine(e.Message);
         }
     }
     else
     {
         Console.WriteLine("Debe pasar como argumento el fichero a compilar");
     }
 }