diff --git a/src/main.c b/src/main.c index f7d233b2..788c8b5b 100644 --- a/src/main.c +++ b/src/main.c @@ -43,27 +43,3 @@ int main(int argc, const char * argv[]) return exitCode; } - -/* -static void dumpTokens(Buffer* buffer, Token* token) -{ - while (token) - { - switch (token->type) - { - case TOKEN_LINE: printf("(line)"); break; - case TOKEN_ERROR: printf("(error)"); break; - case TOKEN_EOF: printf("(eof)"); break; - default: - printf("⊏"); - for (int i = token->start; i < token->end; i++) - { - putchar(buffer->bytes[i]); - } - printf("⊐"); - } - token = token->next; - } - printf("\n"); -} -*/ diff --git a/src/vm.c b/src/vm.c index 7d2d2faa..a9d6b3e3 100644 --- a/src/vm.c +++ b/src/vm.c @@ -62,6 +62,14 @@ void freeVM(VM* vm) free(vm); } +ObjBlock* makeBlock() +{ + ObjBlock* block = malloc(sizeof(ObjBlock)); + block->obj.type = OBJ_BLOCK; + block->obj.flags = 0; + return block; +} + ObjClass* makeSingleClass() { ObjClass* obj = malloc(sizeof(ObjClass)); @@ -87,12 +95,14 @@ ObjClass* makeClass() return classObj; } -ObjBlock* makeBlock() +ObjInstance* makeInstance(ObjClass* classObj) { - ObjBlock* block = malloc(sizeof(ObjBlock)); - block->obj.type = OBJ_BLOCK; - block->obj.flags = 0; - return block; + ObjInstance* instance = malloc(sizeof(ObjInstance)); + instance->obj.type = OBJ_INSTANCE; + instance->obj.flags = 0; + instance->classObj = classObj; + + return instance; } ObjNum* makeNum(double number) @@ -113,16 +123,6 @@ ObjString* makeString(const char* text) return string; } -ObjInstance* makeInstance(ObjClass* classObj) -{ - ObjInstance* instance = malloc(sizeof(ObjInstance)); - instance->obj.type = OBJ_INSTANCE; - instance->obj.flags = 0; - instance->classObj = classObj; - - return instance; -} - void initSymbolTable(SymbolTable* symbols) { symbols->count = 0; diff --git a/src/vm.h b/src/vm.h index 36c3c7da..9c30c43c 100644 --- a/src/vm.h +++ b/src/vm.h @@ -7,11 +7,11 @@ #define MAX_SYMBOLS 256 typedef enum { - OBJ_NUM, - OBJ_STRING, OBJ_BLOCK, OBJ_CLASS, - OBJ_INSTANCE + OBJ_INSTANCE, + OBJ_NUM, + OBJ_STRING } ObjType; typedef enum @@ -28,17 +28,7 @@ typedef struct typedef Obj* Value; -typedef struct -{ - Obj obj; - double value; -} ObjNum; - -typedef struct -{ - Obj obj; - const char* value; -} ObjString; +typedef Value (*Primitive)(Value* args, int numArgs); typedef struct { @@ -49,8 +39,6 @@ typedef struct int numLocals; } ObjBlock; -typedef Value (*Primitive)(Value* args, int numArgs); - typedef enum { METHOD_NONE, @@ -84,6 +72,18 @@ typedef struct // TODO(bob): Fields. } ObjInstance; +typedef struct +{ + Obj obj; + double value; +} ObjNum; + +typedef struct +{ + Obj obj; + const char* value; +} ObjString; + typedef enum { // Load the constant at index [arg]. @@ -156,13 +156,21 @@ typedef struct VM* newVM(); void freeVM(VM* vm); -ObjClass* makeClass(); +// Creates a new block object. Assumes the compiler will fill it in with +// bytecode, constants, etc. ObjBlock* makeBlock(); + +// Creates a new class object. +ObjClass* makeClass(); + +// Creates a new instance of the given [classObj]. +ObjInstance* makeInstance(ObjClass* classObj); + +// Creates a new number object. ObjNum* makeNum(double number); // Creates a new string object. Does not copy text. ObjString* makeString(const char* text); -ObjInstance* makeInstance(ObjClass* classObj); // Initializes the symbol table. void initSymbolTable(SymbolTable* symbols);