[jlox] Match error messages to official impl

.... For easier automated testing
This commit is contained in:
ctsk
2024-09-05 09:46:13 +02:00
parent 8d524a05e1
commit 7d0a7b072a
6 changed files with 9 additions and 9 deletions

View File

@@ -338,6 +338,6 @@ public class Interpreter implements Expr.Visitor<Object>, Stmt.Visitor<Void> {
private void checkNumberOperands(Token operator, Object left, Object right) { private void checkNumberOperands(Token operator, Object left, Object right) {
if (left instanceof Double && right instanceof Double) return; if (left instanceof Double && right instanceof Double) return;
throw new RuntimeError(operator, "Operands must be numbers"); throw new RuntimeError(operator, "Operands must be numbers.");
} }
} }

View File

@@ -28,6 +28,6 @@ public class LoxInstance {
@Override @Override
public String toString() { public String toString() {
return "<" + clazz.name + " instance>"; return clazz.name + " instance";
} }
} }

View File

@@ -121,7 +121,7 @@ public class Parser {
params.add(consume(IDENTIFIER, "Expect parameter name.")); params.add(consume(IDENTIFIER, "Expect parameter name."));
} while (match(COMMA)); } while (match(COMMA));
} }
consume(RIGHT_PAREN, "Expect ')' after parameters"); consume(RIGHT_PAREN, "Expect ')' after parameters.");
consume(LEFT_BRACE, "Expect '{' before %s body.".formatted(kind.toString().toLowerCase())); consume(LEFT_BRACE, "Expect '{' before %s body.".formatted(kind.toString().toLowerCase()));
List<Stmt> body = blockStatement(); List<Stmt> body = blockStatement();
@@ -382,7 +382,7 @@ public class Parser {
} while (match(COMMA)); } while (match(COMMA));
} }
Token paren = consume(RIGHT_PAREN, "Expect ')' after arguments"); Token paren = consume(RIGHT_PAREN, "Expect ')' after arguments.");
return new Expr.Call(callee, paren, arguments); return new Expr.Call(callee, paren, arguments);
} }
@@ -397,7 +397,7 @@ public class Parser {
if (match(SUPER)) { if (match(SUPER)) {
Token keyword = previous(); Token keyword = previous();
consume(DOT, "Expect ',' after 'super'."); consume(DOT, "Expect '.' after 'super'.");
Token method = consume(IDENTIFIER, "Expect superclass method name."); Token method = consume(IDENTIFIER, "Expect superclass method name.");
return new Expr.Super(keyword, method); return new Expr.Super(keyword, method);
} }

View File

@@ -155,7 +155,7 @@ public class Resolver implements Expr.Visitor<Void>, Stmt.Visitor<Void> {
@Override @Override
public Void visitReturnStmt(Stmt.Return stmt) { public Void visitReturnStmt(Stmt.Return stmt) {
if (currentFunction == FunctionType.NONE) { if (currentFunction == FunctionType.NONE) {
Lox.error(stmt.keyword(), "Can't return fom top-level code."); Lox.error(stmt.keyword(), "Can't return from top-level code.");
} }
if (stmt.value() != null) { if (stmt.value() != null) {
@@ -242,7 +242,7 @@ public class Resolver implements Expr.Visitor<Void>, Stmt.Visitor<Void> {
if (currentClass == ClassType.NONE) { if (currentClass == ClassType.NONE) {
Lox.error(expr.keyword(), "Can't use 'super' outside of a class."); Lox.error(expr.keyword(), "Can't use 'super' outside of a class.");
} else if (currentClass != ClassType.SUBCLASS) { } else if (currentClass != ClassType.SUBCLASS) {
Lox.error(expr.keyword(), "Can't use 'super' in a class with no subclass."); Lox.error(expr.keyword(), "Can't use 'super' in a class with no superclass.");
} }
resolveLocal(expr, expr.keyword()); resolveLocal(expr, expr.keyword());
return null; return null;