diff options
author | Tom Willemse | 2020-11-11 21:07:49 -0800 |
---|---|---|
committer | Tom Willemse | 2020-11-11 21:07:49 -0800 |
commit | a78fe97f60c2ad39d3d6335c1d058bd8ee4921bb (patch) | |
tree | bee881d28f01db046e01813e2066e23c47d3cc88 /src/com/craftinginterpreters/lox/Lox.java | |
parent | 09600801287ee5f219c4389688f81ec3b5ab5404 (diff) | |
download | crafting-interpreters-a78fe97f60c2ad39d3d6335c1d058bd8ee4921bb.tar.gz crafting-interpreters-a78fe97f60c2ad39d3d6335c1d058bd8ee4921bb.zip |
Add the Parser
Diffstat (limited to 'src/com/craftinginterpreters/lox/Lox.java')
-rw-r--r-- | src/com/craftinginterpreters/lox/Lox.java | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/src/com/craftinginterpreters/lox/Lox.java b/src/com/craftinginterpreters/lox/Lox.java index 51d72c7..586442d 100644 --- a/src/com/craftinginterpreters/lox/Lox.java +++ b/src/com/craftinginterpreters/lox/Lox.java @@ -27,7 +27,8 @@ public class Lox { run(new String(bytes, Charset.defaultCharset())); // Indicate an error in the exit code. - if (hadError) System.exit(65); + if (hadError) + System.exit(65); } private static void runPrompt() throws IOException { @@ -37,7 +38,8 @@ public class Lox { for (;;) { System.out.print("> "); String line = reader.readLine(); - if (line == null) break; + if (line == null) + break; run(line); hadError = false; } @@ -46,11 +48,13 @@ public class Lox { private static void run(String source) { Scanner scanner = new Scanner(source); List<Token> tokens = scanner.scanTokens(); + Parser parser = new Parser(tokens); + Expr expression = parser.parse(); - // For now, just print the tokens. - for (Token token : tokens) { - System.out.println(token); - } + // Stop if there was a syntax error + if (hadError) return; + + System.out.println(new AstPrinter().print(expression)); } public static void error(int line, String message) { @@ -61,4 +65,12 @@ public class Lox { System.err.println("[line " + line + "] Error" + where + ": " + message); hadError = true; } + + public static void error(Token token, String message) { + if (token.type == TokenType.EOF) { + report(token.line, " at end", message); + } else { + report(token.line, " at '" + token.lexeme + "'", message); + } + } } |