aboutsummaryrefslogtreecommitdiffstats
path: root/clox/src/compiler.c
diff options
context:
space:
mode:
Diffstat (limited to 'clox/src/compiler.c')
-rw-r--r--clox/src/compiler.c27
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},