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;
|
2020-12-27 09:58:32 +03:00
|
|
|
object->isMarked = 0;
|
2020-12-26 03:32:21 +03:00
|
|
|
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;
|
2020-12-27 09:58:32 +03:00
|
|
|
krk_push(OBJECT_VAL(string));
|
2020-12-26 08:33:34 +03:00
|
|
|
krk_tableSet(&vm.strings, OBJECT_VAL(string), NONE_VAL());
|
2020-12-27 09:58:32 +03:00
|
|
|
krk_pop();
|
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-28 05:11:50 +03:00
|
|
|
KrkString * krk_takeString(char * chars, size_t length) {
|
2020-12-26 08:33:34 +03:00
|
|
|
uint32_t hash = hashString(chars, length);
|
2020-12-28 05:11:50 +03:00
|
|
|
KrkString * interned = krk_tableFindString(&vm.strings, chars, length, hash);
|
2020-12-26 08:33:34 +03:00
|
|
|
if (interned != NULL) {
|
|
|
|
FREE_ARRAY(char, chars, length + 1);
|
|
|
|
return interned;
|
|
|
|
}
|
|
|
|
return allocateString(chars, length, hash);
|
2020-12-26 03:32:21 +03:00
|
|
|
}
|
|
|
|
|
2020-12-28 05:11:50 +03:00
|
|
|
KrkString * krk_copyString(const char * chars, size_t length) {
|
2020-12-26 08:33:34 +03:00
|
|
|
uint32_t hash = hashString(chars, length);
|
2020-12-28 05:11:50 +03:00
|
|
|
KrkString * interned = krk_tableFindString(&vm.strings, chars, length, hash);
|
2020-12-27 09:58:32 +03:00
|
|
|
if (interned) 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
|
|
|
}
|
|
|
|
|
2020-12-27 12:55:52 +03:00
|
|
|
#define NAME(obj) ((obj)->name ? obj->name->chars : "(unnamed)")
|
2020-12-26 03:32:21 +03:00
|
|
|
void krk_printObject(FILE * f, KrkValue value) {
|
|
|
|
switch (OBJECT_TYPE(value)) {
|
|
|
|
case OBJ_STRING:
|
2020-12-29 10:19:17 +03:00
|
|
|
fprintf(f,"\"");
|
|
|
|
for (char * c = AS_CSTRING(value); *c; ++c) {
|
|
|
|
switch (*c) {
|
|
|
|
/* XXX: Other non-printables should probably be escaped as well. */
|
|
|
|
case '\n': fprintf(f,"\\n"); break;
|
|
|
|
case '\r': fprintf(f,"\\r"); break;
|
|
|
|
case '\t': fprintf(f,"\\t"); break;
|
|
|
|
case '"': fprintf(f,"\\\""); break;
|
|
|
|
case '\033': fprintf(f,"\\["); break;
|
|
|
|
default: fprintf(f,"%c",*c); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(f,"\"");
|
2020-12-26 03:32:21 +03:00
|
|
|
break;
|
2020-12-27 03:33:28 +03:00
|
|
|
case OBJ_FUNCTION:
|
|
|
|
if (AS_FUNCTION(value)->name == NULL) fprintf(f, "<module>");
|
2020-12-27 12:55:52 +03:00
|
|
|
else fprintf(f, "<def %s>", NAME(AS_FUNCTION(value)));
|
2020-12-27 03:33:28 +03:00
|
|
|
break;
|
|
|
|
case OBJ_NATIVE:
|
|
|
|
fprintf(f, "<native bind>");
|
|
|
|
break;
|
2020-12-27 07:02:26 +03:00
|
|
|
case OBJ_CLOSURE:
|
2020-12-27 12:55:52 +03:00
|
|
|
fprintf(f, "<closure <def %s>>", NAME(AS_CLOSURE(value)->function));
|
2020-12-27 07:02:26 +03:00
|
|
|
break;
|
|
|
|
case OBJ_UPVALUE:
|
|
|
|
fprintf(f, "<upvalue>");
|
|
|
|
break;
|
2020-12-27 10:45:34 +03:00
|
|
|
case OBJ_CLASS:
|
2020-12-27 12:55:52 +03:00
|
|
|
fprintf(f, "<class %s>", NAME(AS_CLASS(value)));
|
2020-12-27 10:45:34 +03:00
|
|
|
break;
|
|
|
|
case OBJ_INSTANCE:
|
2020-12-27 12:55:52 +03:00
|
|
|
fprintf(f, "<instance of %s>", NAME(AS_INSTANCE(value)->_class));
|
2020-12-27 10:45:34 +03:00
|
|
|
break;
|
2020-12-27 11:53:46 +03:00
|
|
|
case OBJ_BOUND_METHOD:
|
2020-12-29 14:25:34 +03:00
|
|
|
fprintf(f, "<bound <def %s>>", (AS_BOUND_METHOD(value)->method->type == OBJ_CLOSURE) ?
|
|
|
|
NAME(((KrkClosure*)AS_BOUND_METHOD(value)->method)->function) : (
|
|
|
|
(AS_BOUND_METHOD(value)->method->type == OBJ_NATIVE) ? "<native>" : "<unknown>"
|
|
|
|
));
|
2020-12-27 11:53:46 +03:00
|
|
|
break;
|
2020-12-26 03:32:21 +03:00
|
|
|
}
|
|
|
|
}
|
2020-12-27 03:33:28 +03:00
|
|
|
|
2020-12-28 05:11:50 +03:00
|
|
|
KrkFunction * krk_newFunction() {
|
2020-12-27 03:33:28 +03:00
|
|
|
KrkFunction * function = ALLOCATE_OBJECT(KrkFunction, OBJ_FUNCTION);
|
2020-12-30 02:00:48 +03:00
|
|
|
function->requiredArgs = 0;
|
|
|
|
function->defaultArgs = 0;
|
2020-12-27 07:02:26 +03:00
|
|
|
function->upvalueCount = 0;
|
2020-12-27 03:33:28 +03:00
|
|
|
function->name = NULL;
|
2020-12-30 10:59:21 +03:00
|
|
|
function->docstring = NULL;
|
2020-12-27 03:33:28 +03:00
|
|
|
krk_initChunk(&function->chunk);
|
|
|
|
return function;
|
|
|
|
}
|
|
|
|
|
2020-12-28 05:11:50 +03:00
|
|
|
KrkNative * krk_newNative(NativeFn function) {
|
2020-12-27 03:33:28 +03:00
|
|
|
KrkNative * native = ALLOCATE_OBJECT(KrkNative, OBJ_NATIVE);
|
|
|
|
native->function = function;
|
2020-12-29 14:25:34 +03:00
|
|
|
native->isMethod = 0;
|
2020-12-27 03:33:28 +03:00
|
|
|
return native;
|
|
|
|
}
|
2020-12-27 07:02:26 +03:00
|
|
|
|
2020-12-28 05:11:50 +03:00
|
|
|
KrkClosure * krk_newClosure(KrkFunction * function) {
|
2020-12-27 07:02:26 +03:00
|
|
|
KrkUpvalue ** upvalues = ALLOCATE(KrkUpvalue*, function->upvalueCount);
|
|
|
|
for (size_t i = 0; i < function->upvalueCount; ++i) {
|
|
|
|
upvalues[i] = NULL;
|
|
|
|
}
|
|
|
|
KrkClosure * closure = ALLOCATE_OBJECT(KrkClosure, OBJ_CLOSURE);
|
|
|
|
closure->function = function;
|
|
|
|
closure->upvalues = upvalues;
|
|
|
|
closure->upvalueCount = function->upvalueCount;
|
|
|
|
return closure;
|
|
|
|
}
|
|
|
|
|
2020-12-28 16:02:39 +03:00
|
|
|
KrkUpvalue * krk_newUpvalue(int slot) {
|
2020-12-27 07:02:26 +03:00
|
|
|
KrkUpvalue * upvalue = ALLOCATE_OBJECT(KrkUpvalue, OBJ_UPVALUE);
|
|
|
|
upvalue->location = slot;
|
|
|
|
upvalue->next = NULL;
|
|
|
|
upvalue->closed = NONE_VAL();
|
|
|
|
return upvalue;
|
|
|
|
}
|
2020-12-27 10:45:34 +03:00
|
|
|
|
2020-12-28 05:11:50 +03:00
|
|
|
KrkClass * krk_newClass(KrkString * name) {
|
2020-12-27 10:45:34 +03:00
|
|
|
KrkClass * _class = ALLOCATE_OBJECT(KrkClass, OBJ_CLASS);
|
|
|
|
_class->name = name;
|
2020-12-28 06:16:44 +03:00
|
|
|
_class->filename = NULL;
|
2020-12-30 10:59:21 +03:00
|
|
|
_class->docstring = NULL;
|
2020-12-31 03:15:53 +03:00
|
|
|
_class->base = NULL;
|
2020-12-27 11:53:46 +03:00
|
|
|
krk_initTable(&_class->methods);
|
2020-12-27 10:45:34 +03:00
|
|
|
return _class;
|
|
|
|
}
|
|
|
|
|
2020-12-28 05:11:50 +03:00
|
|
|
KrkInstance * krk_newInstance(KrkClass * _class) {
|
2020-12-27 10:45:34 +03:00
|
|
|
KrkInstance * instance = ALLOCATE_OBJECT(KrkInstance, OBJ_INSTANCE);
|
|
|
|
instance->_class = _class;
|
|
|
|
krk_initTable(&instance->fields);
|
|
|
|
return instance;
|
|
|
|
}
|
2020-12-27 11:53:46 +03:00
|
|
|
|
2020-12-29 14:25:34 +03:00
|
|
|
KrkBoundMethod * krk_newBoundMethod(KrkValue receiver, KrkObj * method) {
|
2020-12-27 11:53:46 +03:00
|
|
|
KrkBoundMethod * bound = ALLOCATE_OBJECT(KrkBoundMethod, OBJ_BOUND_METHOD);
|
|
|
|
bound->receiver = receiver;
|
|
|
|
bound->method = method;
|
|
|
|
return bound;
|
|
|
|
}
|