示例#1
0
        //-----------------------------------------------------------
        void Run(string[] args)
        {
            PrintAppHeader();
            Console.WriteLine();
            PrintReleaseIncludes();
            Console.WriteLine();

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

            foreach (string inputPath in args)
            {
                try
                {
                    // Syntactic Analysis Validation
                    var input  = File.ReadAllText(inputPath);
                    var parser = new Parser(new Scanner(input).Start().GetEnumerator());
                    var ast    = parser.Program();
                    Console.WriteLine("Syntax OK.");

                    // Semantic Analysis Validation
                    var semantic = new SemanticAnalyzer();
                    semantic.Visit((dynamic)ast);
                    Console.WriteLine("Semantics OK.");

                    // Assembly Code Generation & File Validation
                    var codeGenerator = new CILGenerator(semantic.symbolTable, semantic.procedureTable);
                    codeGenerator.Visit((dynamic)ast);
                    var code       = codeGenerator.ToString();
                    var outputPath = inputPath.Replace(".chimera", ".il");

                    File.WriteAllText(
                        outputPath,
                        code);
                    Console.WriteLine(
                        $"Generated CIL code to '{outputPath}'.");
                }
                catch (Exception e)
                {
                    if (e is FileNotFoundException ||
                        e is SyntaxError ||
                        e is SemanticError)
                    {
                        Console.Error.WriteLine(e.Message);
                        Environment.Exit(1);
                    }

                    throw;
                }
            }
        }
示例#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];
                Console.WriteLine("Filename: " + inputPath);
                var input   = File.ReadAllText(inputPath);
                var parser  = new Parser(new Scanner(input).Start().GetEnumerator());
                var program = parser.Program();

                Console.WriteLine(program.ToStringTree());

                Console.WriteLine("Syntax OK.");
                //semantic
                var semantic = new SemanticAnalyzer();
                semantic.Visit((dynamic)program);
                Console.WriteLine("Semantics OK.");

                var tables = semantic.getTables();
                semantic.printTables();

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

                throw;
            }
        }
示例#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 * /
                 * 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 + syntactic analysis* /
                 * var parser = new Parser(new Scanner(input).Start().GetEnumerator());
                 * parser.Program();
                 * Console.WriteLine("Syntax OK.");
                 *
                 * /* Lexical + syntactic analysis + AST construction* /
                 * var parser = new Parser(new Scanner(input).Start().GetEnumerator());
                 * var program = parser.Program();
                 * Console.Write(program.ToStringTree());
                 *
                 * /* Lexical + syntactic analysis + AST construction + Semantic Analyzer*/
                var parser  = new Parser(new Scanner(input).Start().GetEnumerator());
                var program = parser.Program();
                Console.WriteLine("---START SYNTAX TREE---");
                Console.Write(program.ToStringTree());//
                Console.WriteLine("---END SYNTAX TREE---");

                var semantic = new SemanticAnalyzer();
                semantic.Visit((dynamic)program);
                //Console.WriteLine(semantic.GSTable);
                //Console.WriteLine(semantic.GPTable);
                Console.WriteLine("Semantics OK.");


                var codeGenerator = new CILGenerator(semantic.GSTable, semantic.GPTable);
                var code          = codeGenerator.Visit((dynamic)program);
                File.WriteAllText(outputPath, code);
                Console.WriteLine(code);
                Console.WriteLine("Generated CIL code to '" + outputPath + "'.");
                Console.WriteLine();
            }
            catch (FileNotFoundException e)
            {
                Console.Error.WriteLine(e.Message);
                Environment.Exit(1);
            }
            catch (SyntaxError e)
            {
                Console.Error.WriteLine(e.Message);
                Environment.Exit(2);
            }
            catch (SemanticError e)
            {
                Console.Error.WriteLine(e.Message);
                Environment.Exit(3);
            }
        }