package chocopy.pa2;

import chocopy.common.analysis.SymbolTable;
import chocopy.common.analysis.types.SymbolType;
import chocopy.common.astnodes.Program;

/** Top-level class for performing semantic analysis. */
public class StudentAnalysis {

    /** Perform semantic analysis on PROGRAM, adding error messages and
     *  type annotations. Provide debugging output iff DEBUG. Returns modified
     *  tree. */
    public static Program process(Program program, boolean debug) {
        if (program.hasErrors()) {
            return program;
        }

        //DeclarationAnalyzer declarationAnalyzer =
        //    new DeclarationAnalyzer(program.errors);
        //program.dispatch(declarationAnalyzer);
        //SymbolTable<SymbolType> globalSym =
        //    declarationAnalyzer.getGlobals();
        SymbolTable<SymbolType> globalSym = new SymbolTable<>();

        //if (!program.hasErrors()) {
        TypeChecker typeChecker = new TypeChecker(globalSym, program.errors, true);
        program.dispatch(typeChecker);
        //}

        if(!program.hasErrors()) {
            TypeChecker next_time = new TypeChecker(typeChecker.getGlobals(), program.errors, false);
            program.dispatch(next_time);
        }

        return program;
    }
}
