Skip to content

Commit

Permalink
Class initializers
Browse files Browse the repository at this point in the history
  • Loading branch information
dils2k committed Jun 25, 2023
1 parent 9878725 commit 008e82c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
10 changes: 5 additions & 5 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
#include <stddef.h>
#include <stdint.h>

// #define DEBUG_PRINT_CODE
// #define DEBUG_TRACE_EXECUTION
//
// #define DEBUG_STRESS_GC
// #define DEBUG_LOG_GC
#define DEBUG_PRINT_CODE
#define DEBUG_TRACE_EXECUTION

#define DEBUG_STRESS_GC
#define DEBUG_LOG_GC

#define UINT8_COUNT (UINT8_MAX + 1)

Expand Down
10 changes: 9 additions & 1 deletion compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ typedef struct {
typedef enum {
TYPE_FUNCTION,
TYPE_METHOD,
TYPE_INITIALIZER,
TYPE_SCRIPT
} FunctionType;

Expand Down Expand Up @@ -162,7 +163,11 @@ static int emitJump(uint8_t instruction) {
}

static void emitReturn() {
emitByte(OP_NIL);
if (current->type == TYPE_INITIALIZER) {
emitBytes(OP_GET_LOCAL, 0);
} else {
emitByte(OP_NIL);
}
emitByte(OP_RETURN);
}

Expand Down Expand Up @@ -469,6 +474,9 @@ static void method() {
uint8_t constant = identifierConstant(&parser.previous);

FunctionType type = TYPE_METHOD;
if (parser.previous.length == 4 && memcmp(parser.previous.start, "init", 4) == 0) {
type = TYPE_INITIALIZER;
}
function(type);
emitBytes(OP_METHOD, constant);
}
Expand Down
1 change: 1 addition & 0 deletions memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ static void markRoots() {

markTable(&vm.globals);
markCompilerRoots();
markObject((Obj*)vm.initString);
}

static void traceReferences() {
Expand Down
10 changes: 10 additions & 0 deletions vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,14 @@ void initVM() {
initTable(&vm.globals);
initTable(&vm.strings);

vm.initString = NULL;
vm.initString = copyString("init", 4);

defineNative("clock", clockNative);
}

void freeVM() {
vm.initString = NULL;
freeTable(&vm.globals);
freeTable(&vm.strings);
freeObjects();
Expand Down Expand Up @@ -119,6 +123,12 @@ static bool callValue(Value callee, int argCount) {
case OBJ_CLASS: {
ObjClass* klass = AS_CLASS(callee);
vm.stackTop[-argCount-1] = OBJ_VAL(newInstance(klass));
Value initializer;
if (tableGet(&klass->methods, vm.initString, &initializer)) {
return call(AS_CLOSURE(initializer), argCount);
} else if (argCount != 0) {
runtimeError("Expected 0 arguments but got %d.", argCount);
}
return true;
}
case OBJ_CLOSURE:
Expand Down
1 change: 1 addition & 0 deletions vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ typedef struct {
Obj** grayStack;

Table strings;
ObjString* initString;
Table globals;

ObjUpvalue* openUpvalues;
Expand Down

0 comments on commit 008e82c

Please sign in to comment.