From db403da4b2ed543100dc42fd3f0036fd64ed76a9 Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Mon, 14 Jun 2021 23:36:10 -0700 Subject: 13.1 Superclasses and Subclasses --- src/com/craftinginterpreters/lox/Interpreter.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src/com/craftinginterpreters/lox/Interpreter.java') diff --git a/src/com/craftinginterpreters/lox/Interpreter.java b/src/com/craftinginterpreters/lox/Interpreter.java index 7dfd0fb..101c33c 100644 --- a/src/com/craftinginterpreters/lox/Interpreter.java +++ b/src/com/craftinginterpreters/lox/Interpreter.java @@ -181,6 +181,13 @@ class Interpreter implements Expr.Visitor, Stmt.Visitor { @Override public Void visitClassStmt(Stmt.Class stmt) { + Object superclass = null; + if (stmt.superclass != null) { + superclass = evaluate(stmt.superclass); + if (!(superclass instanceof LoxClass)) { + throw new RuntimeError(stmt.superclass.name, "Superclass must be a class."); + } + } environment.define(stmt.name.lexeme, null); Map methods = new HashMap<>(); @@ -189,7 +196,7 @@ class Interpreter implements Expr.Visitor, Stmt.Visitor { methods.put(method.name.lexeme, function); } - LoxClass klass = new LoxClass(stmt.name.lexeme, methods); + LoxClass klass = new LoxClass(stmt.name.lexeme, (LoxClass)superclass, methods); environment.assign(stmt.name, klass); return null; } -- cgit v1.2.3-54-g00ecf