Chapter 24.6

This commit is contained in:
Tom Willemse 2022-03-22 16:45:48 -07:00
parent 410f30ef93
commit baedf99f63

View file

@ -444,6 +444,20 @@ static void printStatement() {
emitByte(OP_PRINT); 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() { static void whileStatement() {
int loopStart = currentChunk()->count; int loopStart = currentChunk()->count;
consume(TOKEN_LEFT_PAREN, "Expect '(' after 'while'."); consume(TOKEN_LEFT_PAREN, "Expect '(' after 'while'.");
@ -503,6 +517,8 @@ static void statement() {
forStatement(); forStatement();
} else if (match(TOKEN_IF)) { } else if (match(TOKEN_IF)) {
ifStatement(); ifStatement();
} else if (match(TOKEN_RETURN)) {
returnStatement();
} else if (match(TOKEN_WHILE)) { } else if (match(TOKEN_WHILE)) {
whileStatement(); whileStatement();
} else if (match(TOKEN_LEFT_BRACE)) { } else if (match(TOKEN_LEFT_BRACE)) {