2020-12-26 03:32:21 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "memory.h"
|
|
|
|
#include "object.h"
|
|
|
|
#include "value.h"
|
|
|
|
#include "vm.h"
|
2020-12-26 08:33:34 +03:00
|
|
|
#include "table.h"
|
2020-12-26 03:32:21 +03:00
|
|
|
|
|
|
|
#define ALLOCATE_OBJECT(type, objectType) \
|
|
|
|
(type*)allocateObject(sizeof(type), objectType)
|
|
|
|
|
|
|
|
static KrkObj * allocateObject(size_t size, ObjType type) {
|
|
|
|
KrkObj * object = (KrkObj*)krk_reallocate(NULL, 0, size);
|
|
|
|
object->type = type;
|
|
|
|
object->next = vm.objects;
|
|
|
|
vm.objects = object;
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2020-12-26 08:33:34 +03:00
|
|
|
static KrkString * allocateString(char * chars, size_t length, uint32_t hash) {
|
2020-12-26 03:32:21 +03:00
|
|
|
KrkString * string = ALLOCATE_OBJECT(KrkString, OBJ_STRING);
|
|
|
|
string->length = length;
|
|
|
|
string->chars = chars;
|
2020-12-26 08:33:34 +03:00
|
|
|
string->hash = hash;
|
|
|
|
krk_tableSet(&vm.strings, OBJECT_VAL(string), NONE_VAL());
|
2020-12-26 03:32:21 +03:00
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2020-12-26 08:33:34 +03:00
|
|
|
static uint32_t hashString(const char * key, size_t length) {
|
|
|
|
uint32_t hash = 0;
|
|
|
|
/* This is the so-called "sdbm" hash. It comes from a piece of
|
|
|
|
* public domain code from a clone of ndbm. */
|
|
|
|
for (size_t i = 0; i < length; ++i) {
|
|
|
|
hash = (int)key[i] + (hash << 6) + (hash << 16) - hash;
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2020-12-26 03:32:21 +03:00
|
|
|
KrkString * takeString(char * chars, size_t length) {
|
2020-12-26 08:33:34 +03:00
|
|
|
uint32_t hash = hashString(chars, length);
|
|
|
|
KrkString * interned = tableFindString(&vm.strings, chars, length, hash);
|
|
|
|
if (interned != NULL) {
|
|
|
|
FREE_ARRAY(char, chars, length + 1);
|
|
|
|
return interned;
|
|
|
|
}
|
|
|
|
return allocateString(chars, length, hash);
|
2020-12-26 03:32:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
KrkString * copyString(const char * chars, size_t length) {
|
2020-12-26 08:33:34 +03:00
|
|
|
uint32_t hash = hashString(chars, length);
|
|
|
|
KrkString * interned = tableFindString(&vm.strings, chars, length, hash);
|
|
|
|
if (interned != NULL) return interned;
|
2020-12-26 03:32:21 +03:00
|
|
|
char * heapChars = ALLOCATE(char, length + 1);
|
|
|
|
memcpy(heapChars, chars, length);
|
|
|
|
heapChars[length] = '\0';
|
2020-12-26 08:33:34 +03:00
|
|
|
return allocateString(heapChars, length, hash);
|
2020-12-26 03:32:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void krk_printObject(FILE * f, KrkValue value) {
|
|
|
|
switch (OBJECT_TYPE(value)) {
|
|
|
|
case OBJ_STRING:
|
|
|
|
fprintf(stderr, "%s", AS_CSTRING(value));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|