diff options
author | Tom Willemse | 2022-03-15 00:39:11 -0700 |
---|---|---|
committer | Tom Willemse | 2022-03-15 00:39:11 -0700 |
commit | 410f30ef93aebab85238e470253aefdd5f0348c1 (patch) | |
tree | 860c8024ff1028d13a8ef681c25578098a22fe15 /clox/src/compiler.c | |
parent | 44e47b89de9e7d9f7d86e1f7d532b70c952a8ab3 (diff) | |
download | crafting-interpreters-410f30ef93aebab85238e470253aefdd5f0348c1.tar.gz crafting-interpreters-410f30ef93aebab85238e470253aefdd5f0348c1.zip |
Chapter 24.5
Diffstat (limited to 'clox/src/compiler.c')
-rw-r--r-- | clox/src/compiler.c | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/clox/src/compiler.c b/clox/src/compiler.c index bd835f2..ea40d7e 100644 --- a/clox/src/compiler.c +++ b/clox/src/compiler.c @@ -143,7 +143,10 @@ static int emitJump(uint8_t instruction) { return currentChunk()->count - 2; } -static void emitReturn() { emitByte(OP_RETURN); } +static void emitReturn() { + emitByte(OP_NIL); + emitByte(OP_RETURN); +} static uint8_t makeConstant(Value value) { int constant = addConstant(currentChunk(), value); @@ -269,6 +272,26 @@ static void binary(bool canAssign) { } } +static uint8_t argumentList() { + uint8_t argCount = 0; + if (!check(TOKEN_RIGHT_PAREN)) { + do { + expression(); + if (argCount == 255) { + error("Can't have more than 255 arguments."); + } + argCount++; + } while (match(TOKEN_COMMA)); + } + consume(TOKEN_RIGHT_PAREN, "Expect ')' after arguments."); + return argCount; +} + +static void call(bool canAssign) { + uint8_t argCount = argumentList(); + emitBytes(OP_CALL, argCount); +} + static void literal(bool canAssign) { switch (parser.previous.type) { case TOKEN_FALSE: @@ -571,7 +594,7 @@ static void grouping(bool canAssign) { } ParseRule rules[] = { - [TOKEN_LEFT_PAREN] = {grouping, NULL, PREC_NONE}, + [TOKEN_LEFT_PAREN] = {grouping, call, PREC_CALL}, [TOKEN_RIGHT_PAREN] = {NULL, NULL, PREC_NONE}, [TOKEN_LEFT_BRACE] = {NULL, NULL, PREC_NONE}, [TOKEN_RIGHT_BRACE] = {NULL, NULL, PREC_NONE}, |