package com.davidmogar.njc;

import com.davidmogar.njc.code.ExecVisitor;
import com.davidmogar.njc.lexicon.Lexicon;
import com.davidmogar.njc.semantic.LinkerVisitor;
import com.davidmogar.njc.semantic.SemanticVisitor;
import com.davidmogar.njc.syntactic.Parser;
import com.davidmogar.njc.code.OffsetVisitor;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

public class Main {

    private static void showErrors() {
        List<TypeError> errors = ErrorHandler.getInstance().getTypeErrors();
        if (errors.size() > 0) {
            errors.forEach(System.err::println);
            System.exit(-1);
        }
    }

    public static void main(String[] args) throws IOException {
        if (args.length < 1) {
            System.err.println("Input file expected.");
            System.exit(0);
        }

        File inputFile = null;
        File outputFile = null;
        FileReader fileReader = null;
        try {
            inputFile = new File(args[0]);
            outputFile = new File(inputFile.getPath() + "o");
            fileReader = new FileReader(inputFile);
        } catch (IOException e) {
            System.err.println(e.getMessage());
            System.exit(-1);
        }

        Lexicon lexicon = new Lexicon(fileReader);
        Parser parser = new Parser(lexicon);
        parser.run();

        showErrors();

        if (parser.ast != null) {
            parser.ast.accept(new LinkerVisitor(), null);
            showErrors();
            parser.ast.accept(new SemanticVisitor(), null);
            parser.ast.accept(new OffsetVisitor(), null);
            parser.ast.accept(new ExecVisitor(inputFile, outputFile), null);
        }

        showErrors();

        String s = "";
        s += 'h';
        s += 'o';
        s += 'l';
        s += 'a';
        s += '\\';
        s += 'n';
        System.out.println(s.replaceAll("\\\\n", "\n"));
    }

}

@@ -1625,32 +1625,62 @@
 r(), null);%0A    
+    %7D%0A%0A        showErrors();%0A%0A
         parser.a
@@ -1731,34 +1731,24 @@
 ile), null);
-%0A        %7D
 %0A%0A        sh
@@ -1762,198 +1762,8 @@
 s();
-%0A%0A        String s = %22%22;%0A        s += 'h';%0A        s += 'o';%0A        s += 'l';%0A        s += 'a';%0A        s += '%5C%5C';%0A        s += 'n';%0A        System.out.println(s.replaceAll(%22%5C%5C%5C%5Cn%22, %22%5Cn%22));
 %0A   

Fix an error on main function