package mjc;

import java.io.PushbackReader;
import java.io.FileReader;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;

import mjc.lexer.Lexer;
import mjc.parser.Parser;
import mjc.node.Start;
import mjc.analysis.ASTGraphPrinter;
import mjc.analysis.ASTPrinter;
import mjc.analysis.BasicAnalyzer;

public class ARMMain {
    private BasicAnalyzer analyzer = new BasicAnalyzer();
    private ASTPrinter astPrinter = new ASTPrinter();
    private ASTGraphPrinter graphPrinter = new ASTGraphPrinter();

    private void run(String[] args) {
        Options options = new Options();
        options.addOption("S", false, "output assembler code");
        options.addOption("o", true, "output file");
        options.addOption("h", false, "show help message");
        options.addOption("p", false, "print abstract syntax tree");
        options.addOption("g", false, "print abstract syntax tree (GraphViz)");

        CommandLineParser commandLineParser = new GnuParser();
        HelpFormatter helpFormatter = new HelpFormatter();

        try {
            CommandLine commandLine = commandLineParser.parse(options, args);

            if (commandLine.hasOption("h") || commandLine.getArgs().length != 1) {
                helpFormatter.printHelp("java ARMMain <file> <options>", options);
                System.exit(1);
            }

            // Parse input from the specified file.
            PushbackReader in = new PushbackReader(new FileReader(commandLine.getArgs()[0]));
            Parser parser = new Parser(new Lexer(in));
            Start tree = parser.parse();

            // Run basic analysis.
            if (!analyzer.analyze(tree)) {
                System.exit(1);
            }

            // Print AST to log.
            if (commandLine.hasOption("p")) {
                astPrinter.print(tree);
            }

            // Print AST in GraphViz format.
            if (commandLine.hasOption("g")) {
                graphPrinter.print(tree);
            }

        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }

    public static void main(String[] args) {
        ARMMain program = new ARMMain();
        program.run(args);
    }
}

@@ -1414,37 +1414,28 @@
 lp(%22
-java ARMMain %3C
+mjc in
 file
-%3E %3C
+ %5B
 options
-%3E
+%5D
 %22, o

Improve help formatting.