Finish chapter 6
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user