Skip to content

Commit 50df5ee

Browse files
committed
chore: complete method op code
1 parent 377a71a commit 50df5ee

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

src/debug.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ int disassemble_instruction(Chunk* chunk, int offset)
113113
return simple_instruction("OP_RETURN", offset);
114114
case OP_CLASS:
115115
return constant_instruction("OP_CLASS", chunk, offset);
116+
case OP_METHOD:
117+
return constant_instruction("OP_METHOD", chunk, offset);
116118
default:
117119
printf("Uknown opcode %d \n", instruction);
118120
return offset + 1;

src/vm.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ static void close_upvalues(Value* last)
159159
}
160160
}
161161

162+
static void define_method(ObjString* name)
163+
{
164+
Value method = peek(0);
165+
ObjClass* klass = AS_CLASS(peek(1));
166+
table_set(&klass->methods, name, method);
167+
pop();
168+
}
169+
162170
static ObjUpvalue* capture_upvalue(Value* local)
163171
{
164172
ObjUpvalue* prev_upvalue = NULL;
@@ -484,6 +492,9 @@ static InterpretResult run()
484492
case OP_CLASS:
485493
push(OBJ_VAL(new_class(READ_STRING())));
486494
break;
495+
case OP_METHOD:
496+
define_method(READ_STRING());
497+
break;
487498
}
488499
}
489500

0 commit comments

Comments
 (0)