| @@ -3,6 +3,7 @@ import interpreter.InterpreterException; | |||||
| import parser.Parser; | import parser.Parser; | ||||
| import parser.SyntaxException; | import parser.SyntaxException; | ||||
| import program.Program; | import program.Program; | ||||
| import printer.ProgramPrinter; | |||||
| import java.io.IOException; | import java.io.IOException; | ||||
| import java.nio.charset.StandardCharsets; | import java.nio.charset.StandardCharsets; | ||||
| @@ -14,12 +15,20 @@ public class Main { | |||||
| public static void main(String[] args) { | public static void main(String[] args) { | ||||
| if (args.length == 0) { | if (args.length == 0) { | ||||
| System.err.println("No file given"); | System.err.println("No file given"); | ||||
| } else if (args.length > 1) { | |||||
| System.err.println("Too many arguments"); | |||||
| } else { | } else { | ||||
| try { | try { | ||||
| String code = readFile(args[0]); | String code = readFile(args[0]); | ||||
| run(code); | |||||
| if (args.length == 2) { | |||||
| if ("--print".equals(args[1])) { | |||||
| print(code); | |||||
| } else { | |||||
| System.err.println("Invalid argument " + args[1]); | |||||
| } | |||||
| } else if (args.length == 1) { | |||||
| run(code); | |||||
| } else { | |||||
| System.err.println("Too many arguments"); | |||||
| } | |||||
| } catch (IOException e) { | } catch (IOException e) { | ||||
| e.printStackTrace(); | e.printStackTrace(); | ||||
| } | } | ||||
| @@ -31,6 +40,17 @@ public class Main { | |||||
| return new String(encoded, StandardCharsets.UTF_8); | return new String(encoded, StandardCharsets.UTF_8); | ||||
| } | } | ||||
| private static void print(String code) { | |||||
| try { | |||||
| Parser parser = new Parser(code); | |||||
| Program program = parser.parse(); | |||||
| ProgramPrinter printer = new ProgramPrinter(); | |||||
| System.out.println(printer.print(program)); | |||||
| } catch (SyntaxException se) { | |||||
| System.err.println(se); | |||||
| } | |||||
| } | |||||
| private static void run(String code) { | private static void run(String code) { | ||||
| try { | try { | ||||
| Parser parser = new Parser(code); | Parser parser = new Parser(code); | ||||