//Mike Maxim
//A utility to run the abstract syntax tree printer

package edu.cmu.cs.l3.parse;

import edu.cmu.cs.l3.absyn.*;
import edu.cmu.cs.l3.semant.*;
import java.io.*;
import edu.cmu.cs.l3.general.*;

/** A class for testing out the parser, for debugging purposes. */

public class ParseMain {

    public static void go(String cfile, String ofile, boolean ty) throws Exception {

        Parse p = new Parse();

        ASPrintAbsyn printer = new ASPrintAbsyn(System.out);

        try {
            p.parse(cfile);
        } catch (FileNotFoundException e) {
            System.out.println("PARSE: Error during parsing: Possibly a #include to a missing file in: " + ofile);
            Globals.shutdown(-1);
        }

        p.getProgram().visit(printer);

        if (ty) {
            TypeChecker tychecker = new TypeChecker(p.getErrorHandler(), new Environment());
            tychecker.setupProcedures((ASDeclarationList)p.getProgram());
            p.getProgram().visit(tychecker);
            TyPrinter typrinter = new TyPrinter();
            p.getProgram().visit(typrinter);
            System.out.println("TYCHECK: Completed: " + tychecker.getNumErrors() + " error(s)");
        }

        System.out.println();
        Globals.shutdown(0);
    }
}


