Example #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;
            }
        }
Example #2
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;
            }
        }
Example #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);
                /*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;
            }
        }
Example #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);
                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;
            }
        }
Example #5
0
        void Run(string[] args)
        {
            // Console.WriteLine ("Don't panic, use deep lingo");
            Console.WriteLine();

            if (args.Length < 1)
            {
                Console.Error.WriteLine(
                    "Please specify the name of the input file.");
                Environment.Exit(1);
            }
            if (args[args.Length - 1] == "DEBUG")
            {
                DEBUG = true;
            }

            if (args[0] == "test")
            {
                ScannerTest tests = new ScannerTest();
                tests.RunTests();
            }
            else
            {
                try {
                    var    inputPath = args[0];
                    String input     = File.ReadAllText(inputPath);
                    if (DEBUG)
                    {
                        foreach (var tok in new Scanner(input).Start())
                        {
                            int count = 1;
                            if (tok.Lexeme == "42" && tok.Category != TokenType.TRUE)
                            {
                                Console.WriteLine("NO PUSISTE 42 como TRUE :'v");
                                throw new Exception();
                            }
                            Console.WriteLine(String.Format("[{0}] {1}",
                                                            count++, tok));
                        }
                    }
                    var parser  = new Parser(new Scanner(input).Start().GetEnumerator());
                    var program = parser.Program();

                    // Console.WriteLine ("Syntax OK.");
                    if (DEBUG)
                    {
                        Console.Write(program.ToStringTree());
                    }

                    var semanticFirst = new SemanticFirst(DEBUG);
                    semanticFirst.Visit((dynamic)program);

                    if (DEBUG)
                    {
                        Console.WriteLine("Global Function Table");
                        Console.WriteLine("============");
                        foreach (var entry in semanticFirst.globalFunctions)
                        {
                            Console.Write(entry.Key + "\t");
                            Console.WriteLine(entry.Value.arity);
                        }
                        Console.WriteLine("Global Variable Table");
                        Console.WriteLine("============");
                        foreach (var entry in semanticFirst.globalVariables)
                        {
                            Console.WriteLine(entry.Key + "\t");
                        }
                        Console.WriteLine("Second Pass BOIS");
                        Console.WriteLine("============");
                    }

                    var semanticSecond = new SemanticSecond(DEBUG, semanticFirst.globalFunctions, semanticFirst.globalVariables);
                    semanticSecond.Visit((dynamic)program);
                    // Console.WriteLine ("Semantic OK.");
                    // Console.WriteLine ("Generating Code.");
                    var cilGenerator = new CILGenerator(semanticFirst.globalFunctions, semanticFirst.globalVariables);
                    Console.Write(cilGenerator.Visit((dynamic)program));
                    Thread.Sleep(1000);
                } catch (FileNotFoundException e) {
                    Console.Error.WriteLine(e.Message);
                    Environment.Exit(1);
                } catch (SyntaxError s) {
                    Console.WriteLine(s);
                } catch (SemanticError c) {
                    Console.WriteLine("Semantic not correct.");
                    Console.WriteLine(c.Message);
                }
            }
        }