Closed quipa closed 3 years ago
Lox.runtimeError places the message before the [line n], while the Scanner and Parser place it afterwards.
Lox.runtimeError
[line n]
> 1 @ 2 // Scanner error [line 1] Error: Unexpected character. > 1 + * 2 // Parser error [line 1] Error at '*': Expect expression. > 1 + "hello" // Interpreter error Operands must be two numbers or two strings. [line 1]
Could be replaced by following code to keep errors consistent.
static void runtimeError(RuntimeError error) { System.err.println( "[line " + error.token.line + "] " + error.getMessage()); hadRuntimeError = true; }
This would give the following error messages:
> 1 @ 2 // Scanner error [line 1] Error: Unexpected character. > 1 + * 2 // Parser error [line 1] Error at '*': Expect expression. > 1 + "hello" // Interpreter error [line 1] Operands must be two numbers or two strings.
Also couldn't it be renamed static void error(RuntimeError error) and use report like for the Scanner and Parser?
static void error(RuntimeError error)
static void error(int line, String message) { report(line, "", message); 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); } hadError = true; } static void error(RuntimeError error) { report(error.token.line, "", error.getMessage()); hadRuntimeError = true; } private static void report(int line, String where, String message) { System.err.println( "[line " + line + "] Error" + where + ": " + message); }
This would be even more consistent (notice Error: Operands ...):
Error: Operands ...
> 1 @ 2 [line 1] Error: Unexpected character. > 1 + * 2 [line 1] Error at '*': Expect expression. > 1 + "hello" [line 1] Error: Operands must be two numbers or two strings. >
Even nicer would be to have an equivalent ScannerError and ParserError to organise things (probably overkill though).
This is deliberate. Eventually, you'll get to a point in the book where runtime errors print stack traces, and at that point, it makes sense for the error message to come before the line information. :)
Lox.runtimeError
places the message before the[line n]
, while the Scanner and Parser place it afterwards.Could be replaced by following code to keep errors consistent.
This would give the following error messages:
Also couldn't it be renamed
static void error(RuntimeError error)
and use report like for the Scanner and Parser?This would be even more consistent (notice
Error: Operands ...
):Even nicer would be to have an equivalent ScannerError and ParserError to organise things (probably overkill though).