From 9cbe36cb010e793845e531484851e541698ca2df Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Mon, 13 Feb 2017 16:48:11 -0500 Subject: [PATCH] Remove FuncEnv and FuncDef types as implementation details --- compile.c | 4 ++-- datatypes.h | 6 +----- main.c | 3 --- temp.script | 1 + value.c | 35 ----------------------------------- vm.c | 25 ++++++++++++------------- 6 files changed, 16 insertions(+), 58 deletions(-) create mode 100644 temp.script diff --git a/compile.c b/compile.c index f99d546b..0fa21eab 100644 --- a/compile.c +++ b/compile.c @@ -806,8 +806,8 @@ static Slot CompileFunction(Compiler * c, FormOptions opts, Array * form) { uint16_t literalIndex; FuncDef * def = CompilerGenFuncDef(c, buffer->count - sizeBefore, params->count); /* Add this FuncDef as a literal in the outer scope */ - newVal.type = TYPE_FUNCDEF; - newVal.data.funcdef = def; + newVal.type = TYPE_NIL; + newVal.data.pointer = def; literalIndex = CompilerAddLiteral(c, scope, newVal); BufferPushUInt16(c->vm, buffer, VM_OP_CLN); BufferPushUInt16(c->vm, buffer, ret.index); diff --git a/datatypes.h b/datatypes.h index f2bac657..0b8bb18f 100644 --- a/datatypes.h +++ b/datatypes.h @@ -20,9 +20,7 @@ typedef enum Type { TYPE_BYTEBUFFER, TYPE_FUNCTION, TYPE_CFUNCTION, - TYPE_DICTIONARY, - TYPE_FUNCDEF, - TYPE_FUNCENV + TYPE_DICTIONARY } Type; typedef double Number; @@ -54,8 +52,6 @@ union ValueData { Dictionary * dict; Func * func; void * pointer; - FuncDef * funcdef; - FuncEnv * funcenv; CFunction cfunction; uint16_t u16[4]; uint8_t u8[8]; diff --git a/main.c b/main.c index 0ffd52d5..6e8eda85 100644 --- a/main.c +++ b/main.c @@ -41,9 +41,6 @@ void debugRepl() { for (;;) { - /* Run garbage collection */ - VMMaybeCollect(&vm); - /* Reset state */ ParserInit(&p, &vm); diff --git a/temp.script b/temp.script new file mode 100644 index 00000000..d42dfc69 --- /dev/null +++ b/temp.script @@ -0,0 +1 @@ +(do (:= a 0) (while (< a 10) (:= a (+ a 1)) (print a)) a) diff --git a/value.c b/value.c index 72a4b519..eb94a849 100644 --- a/value.c +++ b/value.c @@ -94,10 +94,6 @@ uint8_t * ValueToString(VM * vm, Value x) { return StringDescription(vm, "function", 8, x.data.pointer); case TYPE_DICTIONARY: return StringDescription(vm, "dictionary", 10, x.data.pointer); - case TYPE_FUNCDEF: - return StringDescription(vm, "funcdef", 7, x.data.pointer); - case TYPE_FUNCENV: - return StringDescription(vm, "funcenv", 7, x.data.pointer); case TYPE_THREAD: return StringDescription(vm, "thread", 6, x.data.pointer); } @@ -155,8 +151,6 @@ int ValueEqual(Value x, Value y) { case TYPE_CFUNCTION: case TYPE_DICTIONARY: case TYPE_FUNCTION: - case TYPE_FUNCDEF: - case TYPE_FUNCENV: case TYPE_THREAD: /* compare pointers */ result = (x.data.array == y.data.array); @@ -201,8 +195,6 @@ uint32_t ValueHash(Value x) { case TYPE_CFUNCTION: case TYPE_DICTIONARY: case TYPE_FUNCTION: - case TYPE_FUNCDEF: - case TYPE_FUNCENV: case TYPE_THREAD: /* Cast the pointer */ { @@ -269,8 +261,6 @@ int ValueCompare(Value x, Value y) { case TYPE_CFUNCTION: case TYPE_FUNCTION: case TYPE_DICTIONARY: - case TYPE_FUNCDEF: - case TYPE_FUNCENV: case TYPE_THREAD: if (x.data.string == y.data.string) { return 0; @@ -340,18 +330,6 @@ Value ValueGet(VM * vm, Value ds, Value key) { break; case TYPE_DICTIONARY: return DictGet(ds.data.dict, key); - case TYPE_FUNCENV: - VMAssertType(vm, key, TYPE_NUMBER); - if (ds.data.funcenv->thread) { - Array * thread = ds.data.funcenv->thread; - index = ToIndex(key.data.number, vm->frame->size); - if (index == -1) VMError(vm, "Invalid funcenv access"); - return thread->data[thread->count + index]; - } else { - index = ToIndex(key.data.number, ds.data.funcenv->stackOffset); - if (index == -1) VMError(vm, "Invalid funcenv access"); - return ds.data.funcenv->values[index]; - } default: VMError(vm, "Cannot get."); } @@ -379,19 +357,6 @@ void ValueSet(VM * vm, Value ds, Value key, Value value) { case TYPE_DICTIONARY: DictPut(vm, ds.data.dict, key, value); break; - case TYPE_FUNCENV: - VMAssertType(vm, key, TYPE_NUMBER); - if (ds.data.funcenv->thread) { - Array * thread = ds.data.funcenv->thread; - index = ToIndex(key.data.number, vm->frame->size); - if (index == -1) VMError(vm, "Invalid funcenv access"); - thread->data[thread->count + index] = value; - } else { - index = ToIndex(key.data.number, ds.data.funcenv->stackOffset); - if (index == -1) VMError(vm, "Invalid funcenv access"); - ds.data.funcenv->values[index] = value; - } - break; default: VMError(vm, "Cannot set."); } diff --git a/vm.c b/vm.c index b89e9ca6..7625acee 100644 --- a/vm.c +++ b/vm.c @@ -61,8 +61,15 @@ static void VMMarkFuncDef(VM * vm, FuncDef * def) { if (def->literals) { count = def->literalsLen; GCHeader(def->literals)->color = vm->black; - for (i = 0; i < count; ++i) - VMMark(vm, def->literals + i); + for (i = 0; i < count; ++i) { + /* If the literal is a NIL type, it actually + * contains a FuncDef */ + if (def->literals[i].type == TYPE_NIL) { + VMMarkFuncDef(vm, (FuncDef *) def->literals[i].data.pointer); + } else { + VMMark(vm, def->literals + i); + } + } } } } @@ -154,14 +161,6 @@ static void VMMark(VM * vm, Value * x) { } break; - case TYPE_FUNCDEF: - VMMarkFuncDef(vm, x->data.funcdef); - break; - - case TYPE_FUNCENV: - VMMarkFuncEnv(vm, x->data.funcenv); - break; - } } @@ -435,11 +434,11 @@ static Value VMMakeClosure(VM * vm, uint16_t literal) { } current = vm->frame->callee.data.func; constant = LoadConstant(vm, current, literal); - if (constant.type != TYPE_FUNCDEF) { - VMError(vm, EXPECTED_FUNCTION); + if (constant.type != TYPE_NIL) { + VMError(vm, "Error trying to create closure"); } fn = VMAlloc(vm, sizeof(Func)); - fn->def = constant.data.funcdef; + fn->def = (FuncDef *) constant.data.pointer; fn->parent = current; fn->env = env; ret.type = TYPE_FUNCTION;