Exemplo n.º 1
0
        //-----------------------------------------------------------
        void Run(string[] args)
        {
            PrintAppHeader();
            Console.WriteLine();
            PrintReleaseIncludes();
            Console.WriteLine();

            if (args.Length != 2)
            {
                Console.Error.WriteLine("Please specify the name of the input and output files.");
                Environment.Exit(1);
            }

            try {
                var inputPath  = args[0];
                var outputPath = args[1];
                var input      = File.ReadAllText(inputPath);

                var parser  = new Parser(new Scanner(input).Start().GetEnumerator());
                var program = parser.Program();
                Console.WriteLine("Syntax OK");
                Console.Write(program.ToStringTree());

                //pruebas de la table symboltable (globales)
                var semantic = new SemanticAnalyzer();
                semantic.Visit((dynamic)program);
                semantic.Visit((dynamic)program);
                if (semantic.isSemanticCorrect())
                {
                    //Console.WriteLine();
                    Console.WriteLine("____________________________________________");
                    Console.WriteLine();
                    Console.WriteLine("Semantic OK");
                    Console.WriteLine("____________________________________________");
                    Console.WriteLine();
                    Console.WriteLine();
                }

                var    CILgeneration = new CILGenerator();
                string finalCode     = CILgeneration.Visit((dynamic)program);

                Console.WriteLine(finalCode);

                //se guarda el archivo que se desea
                File.WriteAllText(
                    outputPath,
                    finalCode);
                //se termina de guardar el archivo
            } catch (Exception e) {
                if (e is FileNotFoundException || e is SyntaxError)
                {
                    Console.Error.WriteLine(e.Message);
                    Environment.Exit(1);
                }

                throw;
            }
        }
Exemplo n.º 2
0
        //-----------------------------------------------------------
        void Run(string[] args)
        {
            PrintAppHeader();
            Console.WriteLine();
            PrintReleaseIncludes();
            Console.WriteLine();

            if (args.Length != 1)
            {
                Console.Error.WriteLine("Please specify the name of the input file.");
                Environment.Exit(1);
            }

            try {
                var inputPath = args[0];
                var input     = File.ReadAllText(inputPath);

                var parser  = new Parser(new Scanner(input).Start().GetEnumerator());
                var program = parser.Program();
                Console.WriteLine("Syntax OK");
                Console.Write(program.ToStringTree());

                //pruebas de la table symboltable (globales)
                var semantic = new SemanticAnalyzer();
                semantic.Visit((dynamic)program);
                semantic.Visit((dynamic)program);
                if (semantic.isSemanticCorrect())
                {
                    Console.WriteLine();
                    Console.WriteLine("____________________________________________");
                    Console.WriteLine();
                    Console.WriteLine("Semantic OK");
                    Console.WriteLine("____________________________________________");
                    Console.WriteLine();
                    Console.WriteLine();
                    Console.WriteLine(semantic.GlobalSymbols.ToString());
                    Console.WriteLine(semantic.Functions.ToString());
                }
            } catch (Exception e) {
                if (e is FileNotFoundException || e is SyntaxError)
                {
                    Console.Error.WriteLine(e.Message);
                    Environment.Exit(1);
                }

                throw;
            }
        }
Exemplo n.º 3
0
        //-----------------------------------------------------------
        void Run(string[] args)
        {
            PrintAppHeader();
            Console.WriteLine();
            PrintReleaseIncludes();
            Console.WriteLine();

            if (args.Length != 2)
            {
                Console.Error.WriteLine(
                    "Please specify the name of the input and output files.");
                Environment.Exit(1);
            }

            try {
                var inputPath  = args[0];
                var outputPath = args[1];
                var input      = File.ReadAllText(inputPath);
                var parser     = new Parser(new Scanner(input).Start().GetEnumerator());
                var ast        = parser.Program();
                //Console.Write(ast.ToStringTree());
                Console.WriteLine("Syntax OK.");

                var semantic = new SemanticAnalyzer();
                semantic.Visit((dynamic)ast);

                //Console.WriteLine(semantic.FancyPrint());

                var codeGenerator = new CILGenerator(semantic);
                File.WriteAllText(
                    outputPath,
                    codeGenerator.Visit((dynamic)ast));
                Console.WriteLine(
                    "Generated CIL code to '" + outputPath + "'.");
                Console.WriteLine();
            } catch (Exception e) {
                if (e is FileNotFoundException ||
                    e is SyntaxError ||
                    e is SemanticError)
                {
                    Console.Error.WriteLine(e.Message);
                    Environment.Exit(1);
                }

                throw;
            }
        }
Exemplo n.º 4
0
        //-----------------------------------------------------------
        void Run(string[] args)
        {
            PrintAppHeader();
            Console.WriteLine();
            PrintReleaseIncludes();
            Console.WriteLine();

            if (args.Length != 2)
            {
                Console.Error.WriteLine("Please specify the name of the input and output files.");
                Environment.Exit(1);
            }

            try {
                var inputPath  = args[0];
                var outputPath = args[1];
                var input      = File.ReadAllText(inputPath);
                /*Lexical Analysis START*/
                Console.WriteLine("****** Lexical Analysis ******");
                Console.WriteLine(String.Format("===== Tokens Identification"));
                Console.WriteLine(String.Format("===== Tokens from: \"{0}\" =====", inputPath));
                var count = 1;
                foreach (var tok in new Scanner(input).Start())
                {
                    Console.WriteLine(String.Format("[{0}] {1}", count++, tok));
                }
                /*Lexical Analysis END*/

                /*Syntactic Analysis START*/
                Console.WriteLine("****** Syntactic Analysis ******");
                var parser  = new Parser(new Scanner(input).Start().GetEnumerator());
                var program = parser.Program();
                Console.WriteLine("===== Syntax OK =====");
                /*Syntactic Analysis END*/

                /*AST construction START*/
                Console.WriteLine("===================");
                Console.WriteLine("===== AST Tree =====");
                Console.Write(program.ToStringTree());
                /*AST construction END*/

                /*Semantic analysis START*/
                Console.WriteLine("===================");
                var semantic = new SemanticAnalyzer();
                semantic.Visit((dynamic)program);

                Console.WriteLine("Semantics OK.");
                Console.WriteLine();
                Console.WriteLine("Global Symbol Table");
                Console.WriteLine("============");
                foreach (var entry in semantic.Global_Symbol_Table)
                {
                    Console.WriteLine(entry);
                }
                Console.WriteLine("============");
                Console.WriteLine("Global Function Table");
                Console.WriteLine("============");
                foreach (var entry in semantic.Global_Function_Table)
                {
                    Console.WriteLine(entry);
                }

                Console.WriteLine("============");
                Console.WriteLine("Local Symbol Tables");
                foreach (var entry in semantic.localSymbolTables)
                {
                    Console.WriteLine("");
                    Console.WriteLine(entry);
                }

                /*Semantic analysis END*/

                /* CIL Code Generation START*/
                Console.WriteLine("============");

                var codeGenerator = new CILGenerator();
                File.WriteAllText(outputPath, codeGenerator.Visit((dynamic)program));
                Console.WriteLine("Generated CIL code to '" + outputPath + "'."); Console.WriteLine();

                /* CIL Code Generation END*/
            }
            catch (Exception e) {
                if (e is FileNotFoundException || e is SyntaxError || e is SemanticError)
                {
                    Console.Error.WriteLine(e.Message);
                    Environment.Exit(1);
                }
                throw;
            }
        }
Exemplo n.º 5
0
        //-----------------------------------------------------------
        void Run(string[] args)
        {
            PrintAppHeader();
            Console.WriteLine();
            PrintReleaseIncludes();
            Console.WriteLine();

            if (args.Length != 2)
            {
                Console.Error.WriteLine("Please specify the name of the input and output files.");
                Environment.Exit(1);
            }

            try
            {
                var inputPath  = args[0];
                var outputPath = args[1];
                var input      = File.ReadAllText(inputPath);
                var parser     = new Parser(new Scanner(input).Start().GetEnumerator());
                var program    = parser.Program();
                //Console.Write(program.ToStringTree());
                Console.WriteLine("Syntax OK.");

                var semantic = new SemanticAnalyzer();
                //First Run
                semantic.Visit((dynamic)program, 1);
                if (!semantic.FunctionTable.Contains("main"))
                {
                    throw new SemanticError("There must be a main function on the program");
                }
                //Second Run
                semantic.Visit((dynamic)program);
                //fill the refs
                semantic.FillTheRefs();
                Console.WriteLine("Semantics OK.");


                //  Console.WriteLine();
                //   Console.WriteLine(semantic.GlobalVarsTable.ToString());
                //   Console.WriteLine(semantic.FunctionTable.ToString());
                //   Console.WriteLine(semantic.FunMethods.ToString());


                var codeGenerator = new CILGenerator(semantic.GlobalVarsTable, semantic.FunctionTable);
                File.WriteAllText(
                    outputPath,
                    codeGenerator.Visit((dynamic)program));
                Console.WriteLine(
                    "Generated CIL code to '" + outputPath + "'.");
                Console.WriteLine();
            }
            catch (Exception e)
            {
                if (e is FileNotFoundException ||
                    e is SyntaxError ||
                    e is SemanticError)
                {
                    Console.Error.WriteLine(e.Message);
                    Environment.Exit(1);
                }

                throw;
            }
        }