Пример #1
0
        static void Main(string[] args)
        {
            try
            {
                string path;
                if (args.Length == 0)
                {
                    Console.WriteLine("Please enter a filepath: ");
                    path = Console.ReadLine();
                }
                else
                {
                    path = args[0];
                }

                ErrorManager em    = new ErrorManager();
                Lexer        lexer = new Lexer(new FileSource(path));

                Parser parser = new Parser(lexer);
                parser.HookTo(em);
                AST program = parser.parse();

                SymbolTableBuildingVisitor tableBuilder = new SymbolTableBuildingVisitor();
                tableBuilder.HookTo(em);
                SymbolTableManager stm = tableBuilder.buildTables(program);

                IdentifierUsageCheckingVisitor idChecker = new IdentifierUsageCheckingVisitor(stm);
                idChecker.HookTo(em);
                idChecker.check(program);

                TypeCheckingVisitor typeChecker = new TypeCheckingVisitor(stm);
                typeChecker.HookTo(em);
                typeChecker.check(program);

                if (!em.areErrors())
                {
                    CodeGeneratingVisitor cgv = new CodeGeneratingVisitor(stm);
                    CProgram cProgram         = cgv.generate(program);
                    cProgram.generateFile();
                    Console.WriteLine("Build successful.");
                }
                else
                {
                    Console.WriteLine("Build failed.");
                }

                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return;
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            try
            {
                //Initialize
                string path;
                if (args.Length == 0)
                {
                    Console.WriteLine("Please enter a filepath: ");
                    path = Console.ReadLine();
                }
                else
                {
                    path = args[0];
                }

                FileSource   source       = new FileSource(path);
                ErrorManager errorManager = new ErrorManager();
                Scanner      scanner      = new Scanner(source);
                Parser       parser       = new Parser(scanner);
                SymbolTableBuildingVisitor symbolTableBuilder = new SymbolTableBuildingVisitor();
                TypeCheckingVisitor        typeChecker        = new TypeCheckingVisitor(symbolTableBuilder.table);
                Interpreter interpreter = new Interpreter();

                //Hook to error manager
                parser.HookTo(errorManager);
                symbolTableBuilder.HookTo(errorManager);
                typeChecker.HookTo(errorManager);

                AST program = parser.parse();
                symbolTableBuilder.build(program);
                typeChecker.check(program);
                if (!errorManager.areErrors())
                {
                    interpreter.run(program);
                }
                else
                {
                    Console.WriteLine("Program couldn't be executed due to errors.");
                }

                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return;
            }
        }