Пример #1
0
    public static void Main( string[] args )
    {
        string filename = null;
        bool printAST = false;
        bool printASTtc = false;

        foreach( string arg in args ) {
            if (arg.StartsWith("-")) {
                switch(arg) {
                case "-ast":
                    printAST = true;  break;
                case "-tc":
                    printASTtc = true;  break;
                default:
                    Console.WriteLine("Unknown option {0}, ignored", arg);
                    break;
                }
            } else {
                if (filename != null)
                    Usage();
                filename = arg;
            }
        }
        // require exactly one input file to be provided
        if (filename == null)
            Usage();
        // require a filename suffix of .cb or .cs
        if (!filename.EndsWith(".cb") && !filename.EndsWith(".cs"))
            Usage();

        AST tree = DoParse(filename);

        if (tree == null) {
            Console.WriteLine("\n-- no tree constructed");
            return;
        }

        if (printAST) {
            PrVisitor printVisitor = new PrVisitor();
            tree.Accept(printVisitor);
        }

        int numErrors = 0;

        // perform typechecking ...
        TcVisitor typeCheckVisitor = new TcVisitor();
        tree.Accept(typeCheckVisitor);
        numErrors = typeCheckVisitor.NumErrors;

        if (printASTtc) {
            PrVisitor printVisitor = new PrVisitor();
            tree.Accept(printVisitor);    // print AST with datatype annotations
        }

        // generate intermediate code

        if (numErrors > 0) {
            Console.WriteLine("\n{0} errors reported, compilation halted", numErrors);
            return;
        }
    }
Пример #2
0
    public static void Main( string[] args )
    {
        string filename = null;
        bool printAST = false;
        bool printASTtc = false;

        foreach( string arg in args ) {
            if (arg.StartsWith("-")) {
                switch(arg) {
                case "-ast":
                    printAST = true;  break;
                case "-tc":
                    printASTtc = true;  break;
                default:
                    Console.WriteLine("Unknown option {0}, ignored", arg);
                    break;
                }
            } else {
                if (filename != null)
                    Usage();
                filename = arg;
            }
        }
        // require exactly one input file to be provided
        if (filename == null)
            Usage();
        // require a filename suffix of .cb or .cs
        if (!filename.EndsWith(".cb") && !filename.EndsWith(".cs"))
            Usage();

        AST tree = DoParse(filename);

        if (tree == null) {
            Console.WriteLine("\n-- no tree constructed");
            return;
        }

        if (printAST) {
            PrVisitor printVisitor = new PrVisitor();
            tree.Accept(printVisitor);
        }

        int numErrors = 0;

        // perform typechecking ...
        TcVisitor typeCheckVisitor = new TcVisitor();
        tree.Accept(typeCheckVisitor);
        numErrors = typeCheckVisitor.NumErrors;

        if (printASTtc) {
            PrVisitor printVisitor = new PrVisitor();
            tree.Accept(printVisitor);    // print AST with datatype annotations
        }

        string asmFileName = filename.Substring(0,filename.Length-2)+"s";
        BackEnd.GenCode cgen = new BackEnd.GenCode();
        cgen.GenProgram(tree);

        if (numErrors > 0) {
            Console.WriteLine("\n{0} errors reported, compilation halted", numErrors);
            return;
        }

        if (cgen.Asm != null) {
            cgen.Asm.WriteCode(asmFileName);
            Console.WriteLine("Assembler code written to {0}", asmFileName);
        }
    }