crafting-interpreters/clox/src/object.c

107 lines
2.6 KiB
C
Raw Normal View History

2021-09-09 22:57:03 -07:00
#include <stdio.h>
#include <string.h>
#include "memory.h"
#include "object.h"
2021-09-18 11:10:11 -07:00
#include "table.h"
2021-09-09 22:57:03 -07:00
#include "value.h"
#include "vm.h"
#define ALLOCATE_OBJ(type, objectType) \
(type *)allocateObject(sizeof(type), objectType)
static Obj *allocateObject(size_t size, ObjType type) {
Obj *object = (Obj *)reallocate(NULL, 0, size);
object->type = type;
object->next = vm.objects;
vm.objects = object;
return object;
}
2022-03-23 17:00:22 -07:00
ObjClosure *newClosure(ObjFunction *function) {
ObjClosure *closure = ALLOCATE_OBJ(ObjClosure, OBJ_CLOSURE);
closure->function = function;
return closure;
}
2022-01-21 21:24:26 -08:00
ObjFunction *newFunction() {
ObjFunction *function = ALLOCATE_OBJ(ObjFunction, OBJ_FUNCTION);
function->arity = 0;
function->name = NULL;
initChunk(&function->chunk);
return function;
}
2022-03-22 17:15:17 -07:00
ObjNative *newNative(NativeFn function) {
ObjNative *native = ALLOCATE_OBJ(ObjNative, OBJ_NATIVE);
native->function = function;
return native;
}
2021-09-18 11:10:11 -07:00
static ObjString *allocateString(char *chars, int length, uint32_t hash) {
2021-09-09 22:57:03 -07:00
ObjString *string = ALLOCATE_OBJ(ObjString, OBJ_STRING);
string->length = length;
string->chars = chars;
2021-09-18 11:10:11 -07:00
string->hash = hash;
tableSet(&vm.strings, string, NIL_VAL);
2021-09-09 22:57:03 -07:00
return string;
}
2021-09-18 11:10:11 -07:00
static uint32_t hashString(const char *key, int length) {
uint32_t hash = 2166136261u;
for (int i = 0; i < length; i++) {
hash ^= (uint32_t)key[i];
hash *= 16777619;
}
return hash;
}
2021-09-09 22:57:03 -07:00
ObjString *takeString(char *chars, int length) {
2021-09-18 11:10:11 -07:00
uint32_t hash = hashString(chars, length);
ObjString *interned = tableFindString(&vm.strings, chars, length, hash);
if (interned != NULL) {
FREE_ARRAY(char, chars, length + 1);
return interned;
}
return allocateString(chars, length, hash);
2021-09-09 22:57:03 -07:00
}
ObjString *copyString(const char *chars, int length) {
2021-09-18 11:10:11 -07:00
uint32_t hash = hashString(chars, length);
ObjString *interned = tableFindString(&vm.strings, chars, length, hash);
if (interned != NULL)
return interned;
2021-09-09 22:57:03 -07:00
char *heapChars = ALLOCATE(char, length + 1);
memcpy(heapChars, chars, length);
heapChars[length] = '\0';
2021-09-18 11:10:11 -07:00
return allocateString(heapChars, length, hash);
2021-09-09 22:57:03 -07:00
}
2022-01-21 21:24:26 -08:00
static void printFunction(ObjFunction *function) {
2022-01-22 22:01:54 -08:00
if (function->name == NULL) {
printf("<script>");
return;
}
2022-01-21 21:24:26 -08:00
printf("<fn %s>", function->name->chars);
}
2021-09-09 22:57:03 -07:00
void printObject(Value value) {
switch (OBJ_TYPE(value)) {
2022-03-23 17:00:22 -07:00
case OBJ_CLOSURE:
printFunction(AS_CLOSURE(value)->function);
break;
2022-01-21 21:24:26 -08:00
case OBJ_FUNCTION:
printFunction(AS_FUNCTION(value));
break;
2022-03-22 17:15:17 -07:00
case OBJ_NATIVE:
printf("<native fn>");
break;
2021-09-09 22:57:03 -07:00
case OBJ_STRING:
printf("%s", AS_CSTRING(value));
break;
}
}