diff options
author | Tom Willemse | 2022-03-22 16:45:48 -0700 |
---|---|---|
committer | Tom Willemse | 2022-03-22 16:45:48 -0700 |
commit | baedf99f635735c50936bb555e4a77058ec9e2e0 (patch) | |
tree | d7c96e035c04386078264895612df6d4c06dd9c3 /clox/src | |
parent | 410f30ef93aebab85238e470253aefdd5f0348c1 (diff) | |
download | crafting-interpreters-baedf99f635735c50936bb555e4a77058ec9e2e0.tar.gz crafting-interpreters-baedf99f635735c50936bb555e4a77058ec9e2e0.zip |
Chapter 24.6
Diffstat (limited to 'clox/src')
-rw-r--r-- | clox/src/compiler.c | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/clox/src/compiler.c b/clox/src/compiler.c index ea40d7e..b1e4811 100644 --- a/clox/src/compiler.c +++ b/clox/src/compiler.c @@ -444,6 +444,20 @@ static void printStatement() { emitByte(OP_PRINT); } +static void returnStatement() { + if (current->type == TYPE_SCRIPT) { + error("Can't return from top-level code."); + } + + if (match(TOKEN_SEMICOLON)) { + emitReturn(); + } else { + expression(); + consume(TOKEN_SEMICOLON, "Expect ';' after return value."); + emitByte(OP_RETURN); + } +} + static void whileStatement() { int loopStart = currentChunk()->count; consume(TOKEN_LEFT_PAREN, "Expect '(' after 'while'."); @@ -503,6 +517,8 @@ static void statement() { forStatement(); } else if (match(TOKEN_IF)) { ifStatement(); + } else if (match(TOKEN_RETURN)) { + returnStatement(); } else if (match(TOKEN_WHILE)) { whileStatement(); } else if (match(TOKEN_LEFT_BRACE)) { |