|
18 | 18 | public final class Main {
|
19 | 19 |
|
20 | 20 | public static void main(String[] args) throws IOException {
|
21 |
| - final String file = "program.own"; |
22 |
| - final String input = new String( Files.readAllBytes(Paths.get(file)), "UTF-8"); |
| 21 | + if (args.length == 0) { |
| 22 | + run(readFile("program.own"), true, true); |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + boolean showTokens = false, showAst = false; |
| 27 | + String input = null; |
| 28 | + for (int i = 0; i < args.length; i++) { |
| 29 | + switch (args[i]) { |
| 30 | + case "-a": |
| 31 | + case "--showast": |
| 32 | + showAst = true; |
| 33 | + break; |
| 34 | + |
| 35 | + case "-t": |
| 36 | + case "--showtokens": |
| 37 | + showTokens = true; |
| 38 | + break; |
| 39 | + |
| 40 | + case "-f": |
| 41 | + case "--file": |
| 42 | + if (i + 1 < args.length) { |
| 43 | + input = readFile(args[i + 1]); |
| 44 | + i++; |
| 45 | + } |
| 46 | + break; |
| 47 | + |
| 48 | + default: |
| 49 | + input = args[i]; |
| 50 | + } |
| 51 | + } |
| 52 | + if (input == null) { |
| 53 | + throw new IllegalArgumentException("Empty input"); |
| 54 | + } |
| 55 | + run(input, showTokens, showAst); |
| 56 | + } |
| 57 | + |
| 58 | + private static String readFile(String file) throws IOException { |
| 59 | + return new String( Files.readAllBytes(Paths.get(file)), "UTF-8"); |
| 60 | + } |
| 61 | + |
| 62 | + private static void run(String input, boolean showTokens, boolean showAst) { |
23 | 63 | final List<Token> tokens = new Lexer(input).tokenize();
|
24 |
| - for (int i = 0; i < tokens.size(); i++) { |
25 |
| - System.out.println(i + " " + tokens.get(i)); |
| 64 | + if (showTokens) { |
| 65 | + for (int i = 0; i < tokens.size(); i++) { |
| 66 | + System.out.println(i + " " + tokens.get(i)); |
| 67 | + } |
26 | 68 | }
|
27 |
| -// for (Token token : tokens) { |
28 |
| -// System.out.println(token); |
29 |
| -// } |
30 | 69 |
|
31 | 70 | final Statement program = new Parser(tokens).parse();
|
32 |
| - System.out.println(program.toString()); |
| 71 | + if (showAst) { |
| 72 | + System.out.println(program.toString()); |
| 73 | + } |
33 | 74 | program.accept(new FunctionAdder());
|
34 | 75 | // program.accept(new VariablePrinter());
|
35 | 76 | program.accept(new AssignValidator());
|
|
0 commit comments