Finish chapter 6

This commit is contained in:
Michael Smith
2022-07-06 14:50:11 +02:00
parent 472a589502
commit bdf3a3db34
3 changed files with 194 additions and 6 deletions

View File

@@ -46,11 +46,14 @@ 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));
}
static void error(int line, String message) {
@@ -63,4 +66,11 @@ public class Lox {
hadError = true;
}
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);
}
}
}