Beispiel #1
0
        public static void Main(string[] args)
        {
            // Use a try/catch block for parser exceptions
            try
            {
                string inputFileName;
                string templateFileName;

                if ((args.Length == 1) || (args.Length == 2))
                {
                    if (args.Length == 1)
                    {
                        templateFileName = "Java.stg";
                        inputFileName = args[0];
                    }
                    else
                    {
                        templateFileName = args[0];
                        inputFileName = args[1];
                    }

                    // Ensure full pathnames
                    if (!Path.IsPathRooted(templateFileName))
                    {
                        //templateFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, templateFileName);
                        templateFileName = Path.Combine(Environment.CurrentDirectory, templateFileName);
                    }
                    if (!Path.IsPathRooted(inputFileName))
                    {
                        inputFileName = Path.Combine(Environment.CurrentDirectory, inputFileName);
                    }

                    templates = new StringTemplateGroup(new StreamReader(templateFileName),
                                        typeof(AngleBracketTemplateLexer));

                    ICharStream input = new ANTLRFileStream(inputFileName);
                    CMinusLexer lexer = new CMinusLexer(input);
                    CommonTokenStream tokens = new CommonTokenStream(lexer);
                    CMinusParser parser = new CMinusParser(tokens);
                    parser.TemplateLib = templates;
                    RuleReturnScope r = parser.program();
                    Console.Out.WriteLine(r.Template.ToString());
                }
                else
                    Console.Error.WriteLine("Usage: cminus [<output-template-file>] <input-file>");
            }
            catch (System.Exception e)
            {
                Console.Error.WriteLine("exception: " + e);
                Console.Error.WriteLine(e.StackTrace); // so we can get stack trace
            }
        }
Beispiel #2
0
        static void Do(bool dot)
        {
            var input = new ANTLRStringStream(
            @"
            char c;
            int x;

            int foo(int y, char d) {
            int i;
            for (i=0; i!=3; i=i+1) {
            x=3;
            y=5;
            }
            }");
            var lexer = new CMinusLexer(input);
            var tokens = new CommonTokenStream(lexer);
            var parser = new CMinusParser(tokens);
            var r = parser.program();
            var t = (CommonTree)r.Tree;

            if (dot) {
                DotTreeGenerator gen = new DotTreeGenerator();
                var st = gen.ToDot(t);

                Console.WriteLine(st);
            }
            else {

                Console.WriteLine(t.ToStringTree());
            }

            var nodes = new CommonTreeNodeStream(t);

            nodes.TokenStream = tokens;

            var walker = new CMinusWalker(nodes);

            walker.program();
        }
Beispiel #3
0
        static int Main(string[] args)
        {
            Console.Write("Input path: ");
            string path = Console.ReadLine().Trim('\"');

            Console.Write("Output path: ");
            string outputPath = Console.ReadLine().Trim('\"');

            string input = File.ReadAllText(path);
            string code  = PrependCommonFunctions(input);

            Console.WriteLine(code);

            AntlrInputStream  inputStream = new AntlrInputStream(new StringReader(code));
            CMinusLexer       lexer       = new CMinusLexer(inputStream);
            CommonTokenStream tokens      = new CommonTokenStream(lexer);
            CMinusParser      parser      = new CMinusParser(tokens);

            SyntaxErrorListener syntaxErrorListener = new SyntaxErrorListener();

            parser.AddErrorListener(syntaxErrorListener);

            CMinusParser.CompileUnitContext tree = parser.compileUnit();

            if (lexer.errors > 0)
            {
                Console.WriteLine("Lexical analysis failure.");
            }

            if (syntaxErrorListener.errors > 0)
            {
                Console.WriteLine("Syntax analysis failure.");
            }

            if (lexer.errors > 0 || syntaxErrorListener.errors > 0)
            {
                Console.ReadKey();
                return(-1);
            }

            GlobalAnalysisVisitor globalVisitor = new GlobalAnalysisVisitor();

            globalVisitor.Visit(tree);

            if (globalVisitor.errors > 0)
            {
                Console.Error.WriteLine("Global semantic analysis failure.");
                Console.ReadKey();
                return(-1);
            }

            Console.WriteLine(globalVisitor.symbolTable.ToString());

            InternalAnalysisVisitor internalVisitor = new InternalAnalysisVisitor(globalVisitor.symbolTable);

            internalVisitor.Visit(tree);

            if (internalVisitor.errors > 0)
            {
                Console.Error.WriteLine("Internal semantic analysis failure.");
                Console.ReadKey();
                return(-1);
            }

            Console.WriteLine("\n----------\n");

            // ILVisitor ilVisitor = new ILVisitor(globalVisitor.symbolTable);
            // ilVisitor.Visit(tree);

            CrimsonForthVisitor CFVisitor = new CrimsonForthVisitor();

            CFVisitor.Visit(tree);

            string assemblyCode = CFVisitor.writer.Finalize();

            Console.WriteLine(assemblyCode);

            Console.WriteLine("\n----------\n");

            File.WriteAllText(outputPath, assemblyCode.Replace("\r", ""));

            Console.ReadKey();

            return(0);
        }