diff options
Diffstat (limited to 'clox/src/compiler.c')
-rw-r--r-- | clox/src/compiler.c | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/clox/src/compiler.c b/clox/src/compiler.c index bfa0a96..f14666c 100644 --- a/clox/src/compiler.c +++ b/clox/src/compiler.c @@ -474,6 +474,10 @@ static void namedVariable(Token name, bool canAssign) { } } +static void variable(bool canAssign) { + namedVariable(parser.previous, canAssign); +} + static void classDeclaration() { consume(TOKEN_IDENTIFIER, "Expect class name"); Token className = parser.previous; @@ -487,6 +491,18 @@ static void classDeclaration() { classCompiler.enclosing = currentClass; currentClass = &classCompiler; + if (match(TOKEN_LESS)) { + consume(TOKEN_IDENTIFIER, "Expect superclass name."); + variable(false); + + if (identifiersEqual(&className, &parser.previous)) { + error("A class can't inherit from itself."); + } + + namedVariable(className, false); + emitByte(OP_INHERIT); + } + namedVariable(className, false); consume(TOKEN_LEFT_BRACE, "Expect '{' before class body."); while (!check(TOKEN_RIGHT_BRACE) && !check(TOKEN_EOF)) { @@ -722,10 +738,6 @@ static void string(bool canAssign) { copyString(parser.previous.start + 1, parser.previous.length - 2))); } -static void variable(bool canAssign) { - namedVariable(parser.previous, canAssign); -} - static void this_(bool canAssign) { if (currentClass == NULL) { error("Can't use 'this' outside of a class."); |