summaryrefslogtreecommitdiffstats
path: root/src/com/craftinginterpreters/lox/Lox.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/craftinginterpreters/lox/Lox.java')
-rw-r--r--src/com/craftinginterpreters/lox/Lox.java24
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);
+ }
+ }
}