package MentalCore;

/**
 * Created by Songyu on 16/3/28.
 */

import MentalAST.BuildTreeListener;
import MentalParser.*;
import org.antlr.v4.runtime.ANTLRFileStream;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.tree.ParseTreeWalker;

import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        TokenStream tokens = null;
        try {
            if (args.length >= 1) {
                tokens = new CommonTokenStream(new MentalLexer(new ANTLRFileStream(args[0])));
            } else {
                tokens = new CommonTokenStream(new MentalLexer(new ANTLRInputStream(System.in)));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        MentalParser parser = new MentalParser(tokens);
        ParseTreeWalker walker = new ParseTreeWalker();
        MentalParser.ProgramContext programContext = parser.program();
        BuildTreeListener listener = new BuildTreeListener();
        walker.walk(listener, programContext);
        if (listener.existError) {
            System.exit(1);
        }
        System.out.println();
        System.out.println(listener.tree.get(programContext).toPrintString());
    }
}

@@ -1206,30 +1206,36 @@
     
-System.out.println();%0A
+if (args.length %3E= 1) %7B%0A    
     
@@ -1305,20 +1305,30 @@
 tString());%0A
+        %7D%0A
     %7D%0A%7D%0A

print AST if only if read from file given by args[0]