aboutsummaryrefslogtreecommitdiffstats
path: root/clox/src/vm.c
diff options
context:
space:
mode:
Diffstat (limited to 'clox/src/vm.c')
-rw-r--r--clox/src/vm.c35
1 files changed, 23 insertions, 12 deletions
diff --git a/clox/src/vm.c b/clox/src/vm.c
index bf7078c..0ed46b4 100644
--- a/clox/src/vm.c
+++ b/clox/src/vm.c
@@ -30,7 +30,7 @@ static void runtimeError(const char *format, ...) {
for (int i = vm.frameCount; i >= 0; i--) {
CallFrame *frame = &vm.frames[i];
- ObjFunction *function = frame->function;
+ ObjFunction *function = frame->closure->function;
size_t instruction = frame->ip - function->chunk.code - 1;
fprintf(stderr, "[line %d] in ", function->chunk.lines[instruction]);
if (function->name == NULL) {
@@ -79,9 +79,9 @@ Value pop() {
static Value peek(int distance) { return vm.stackTop[-1 - distance]; }
-static bool call(ObjFunction *function, int argCount) {
- if (argCount != function->arity) {
- runtimeError("Expected %d argumetns but got %d.", function->arity,
+static bool call(ObjClosure *closure, int argCount) {
+ if (argCount != closure->function->arity) {
+ runtimeError("Expected %d arguments but got %d.", closure->function->arity,
argCount);
return false;
}
@@ -92,8 +92,8 @@ static bool call(ObjFunction *function, int argCount) {
}
CallFrame *frame = &vm.frames[vm.frameCount++];
- frame->function = function;
- frame->ip = function->chunk.code;
+ frame->closure = closure;
+ frame->ip = closure->function->chunk.code;
frame->slots = vm.stackTop - argCount - 1;
return true;
}
@@ -101,8 +101,8 @@ static bool call(ObjFunction *function, int argCount) {
static bool callValue(Value callee, int argCount) {
if (IS_OBJ(callee)) {
switch (OBJ_TYPE(callee)) {
- case OBJ_FUNCTION:
- return call(AS_FUNCTION(callee), argCount);
+ case OBJ_CLOSURE:
+ return call(AS_CLOSURE(callee), argCount);
case OBJ_NATIVE: {
NativeFn native = AS_NATIVE(callee);
Value result = native(argCount, vm.stackTop - argCount);
@@ -144,7 +144,8 @@ static InterpretResult run() {
#define READ_SHORT() \
(frame->ip += 2, (uint16_t)((frame->ip[-2] << 8) | frame->ip[-1]))
-#define READ_CONSTANT() (frame->function->chunk.constants.values[READ_BYTE()])
+#define READ_CONSTANT() \
+ (frame->closure->function->chunk.constants.values[READ_BYTE()])
#define READ_STRING() AS_STRING(READ_CONSTANT())
#define BINARY_OP(valueType, op) \
do { \
@@ -165,8 +166,9 @@ static InterpretResult run() {
printf(" ]");
}
printf("\n");
- disassembleInstruction(&frame->function->chunk,
- (int)(frame->ip - frame->function->chunk.code));
+ disassembleInstruction(
+ &frame->closure->function->chunk,
+ (int)(frame->ip - frame->closure->function->chunk.code));
#endif
uint8_t instruction;
@@ -296,6 +298,12 @@ static InterpretResult run() {
frame = &vm.frames[vm.frameCount - 1];
break;
}
+ case OP_CLOSURE: {
+ ObjFunction *function = AS_FUNCTION(READ_CONSTANT());
+ ObjClosure *closure = newClosure(function);
+ push(OBJ_VAL(closure));
+ break;
+ }
case OP_RETURN: {
Value result = pop();
vm.frameCount--;
@@ -325,7 +333,10 @@ InterpretResult interpret(const char *source) {
return INTERPRET_COMPILE_ERROR;
push(OBJ_VAL(function));
- call(function, 0);
+ ObjClosure *closure = newClosure(function);
+ pop();
+ push(OBJ_VAL(closure));
+ call(closure, 0);
return run();
}