aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/craftinginterpreters/lox/Interpreter.java
diff options
context:
space:
mode:
authorGravatar Tom Willemse2021-06-14 23:36:10 -0700
committerGravatar Tom Willemse2021-06-14 23:36:10 -0700
commitdb403da4b2ed543100dc42fd3f0036fd64ed76a9 (patch)
tree1bfba97f0580d8274173642897148eb68566ea2a /src/com/craftinginterpreters/lox/Interpreter.java
parent958ba22a570594ed2ce60d24cd082ce3b8a6b89a (diff)
downloadcrafting-interpreters-db403da4b2ed543100dc42fd3f0036fd64ed76a9.tar.gz
crafting-interpreters-db403da4b2ed543100dc42fd3f0036fd64ed76a9.zip
13.1 Superclasses and Subclasses
Diffstat (limited to 'src/com/craftinginterpreters/lox/Interpreter.java')
-rw-r--r--src/com/craftinginterpreters/lox/Interpreter.java9
1 files changed, 8 insertions, 1 deletions
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<Object>, Stmt.Visitor<Void> {
@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<String, LoxFunction> methods = new HashMap<>();
@@ -189,7 +196,7 @@ class Interpreter implements Expr.Visitor<Object>, Stmt.Visitor<Void> {
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;
}