Remove FuncEnv and FuncDef types as implementation details

This commit is contained in:
Calvin Rose 2017-02-13 16:48:11 -05:00
parent 42ecaf301a
commit 9cbe36cb01
6 changed files with 16 additions and 58 deletions

View File

@ -806,8 +806,8 @@ static Slot CompileFunction(Compiler * c, FormOptions opts, Array * form) {
uint16_t literalIndex; uint16_t literalIndex;
FuncDef * def = CompilerGenFuncDef(c, buffer->count - sizeBefore, params->count); FuncDef * def = CompilerGenFuncDef(c, buffer->count - sizeBefore, params->count);
/* Add this FuncDef as a literal in the outer scope */ /* Add this FuncDef as a literal in the outer scope */
newVal.type = TYPE_FUNCDEF; newVal.type = TYPE_NIL;
newVal.data.funcdef = def; newVal.data.pointer = def;
literalIndex = CompilerAddLiteral(c, scope, newVal); literalIndex = CompilerAddLiteral(c, scope, newVal);
BufferPushUInt16(c->vm, buffer, VM_OP_CLN); BufferPushUInt16(c->vm, buffer, VM_OP_CLN);
BufferPushUInt16(c->vm, buffer, ret.index); BufferPushUInt16(c->vm, buffer, ret.index);

View File

@ -20,9 +20,7 @@ typedef enum Type {
TYPE_BYTEBUFFER, TYPE_BYTEBUFFER,
TYPE_FUNCTION, TYPE_FUNCTION,
TYPE_CFUNCTION, TYPE_CFUNCTION,
TYPE_DICTIONARY, TYPE_DICTIONARY
TYPE_FUNCDEF,
TYPE_FUNCENV
} Type; } Type;
typedef double Number; typedef double Number;
@ -54,8 +52,6 @@ union ValueData {
Dictionary * dict; Dictionary * dict;
Func * func; Func * func;
void * pointer; void * pointer;
FuncDef * funcdef;
FuncEnv * funcenv;
CFunction cfunction; CFunction cfunction;
uint16_t u16[4]; uint16_t u16[4];
uint8_t u8[8]; uint8_t u8[8];

3
main.c
View File

@ -41,9 +41,6 @@ void debugRepl() {
for (;;) { for (;;) {
/* Run garbage collection */
VMMaybeCollect(&vm);
/* Reset state */ /* Reset state */
ParserInit(&p, &vm); ParserInit(&p, &vm);

1
temp.script Normal file
View File

@ -0,0 +1 @@
(do (:= a 0) (while (< a 10) (:= a (+ a 1)) (print a)) a)

35
value.c
View File

@ -94,10 +94,6 @@ uint8_t * ValueToString(VM * vm, Value x) {
return StringDescription(vm, "function", 8, x.data.pointer); return StringDescription(vm, "function", 8, x.data.pointer);
case TYPE_DICTIONARY: case TYPE_DICTIONARY:
return StringDescription(vm, "dictionary", 10, x.data.pointer); 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: case TYPE_THREAD:
return StringDescription(vm, "thread", 6, x.data.pointer); return StringDescription(vm, "thread", 6, x.data.pointer);
} }
@ -155,8 +151,6 @@ int ValueEqual(Value x, Value y) {
case TYPE_CFUNCTION: case TYPE_CFUNCTION:
case TYPE_DICTIONARY: case TYPE_DICTIONARY:
case TYPE_FUNCTION: case TYPE_FUNCTION:
case TYPE_FUNCDEF:
case TYPE_FUNCENV:
case TYPE_THREAD: case TYPE_THREAD:
/* compare pointers */ /* compare pointers */
result = (x.data.array == y.data.array); result = (x.data.array == y.data.array);
@ -201,8 +195,6 @@ uint32_t ValueHash(Value x) {
case TYPE_CFUNCTION: case TYPE_CFUNCTION:
case TYPE_DICTIONARY: case TYPE_DICTIONARY:
case TYPE_FUNCTION: case TYPE_FUNCTION:
case TYPE_FUNCDEF:
case TYPE_FUNCENV:
case TYPE_THREAD: case TYPE_THREAD:
/* Cast the pointer */ /* Cast the pointer */
{ {
@ -269,8 +261,6 @@ int ValueCompare(Value x, Value y) {
case TYPE_CFUNCTION: case TYPE_CFUNCTION:
case TYPE_FUNCTION: case TYPE_FUNCTION:
case TYPE_DICTIONARY: case TYPE_DICTIONARY:
case TYPE_FUNCDEF:
case TYPE_FUNCENV:
case TYPE_THREAD: case TYPE_THREAD:
if (x.data.string == y.data.string) { if (x.data.string == y.data.string) {
return 0; return 0;
@ -340,18 +330,6 @@ Value ValueGet(VM * vm, Value ds, Value key) {
break; break;
case TYPE_DICTIONARY: case TYPE_DICTIONARY:
return DictGet(ds.data.dict, key); 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: default:
VMError(vm, "Cannot get."); VMError(vm, "Cannot get.");
} }
@ -379,19 +357,6 @@ void ValueSet(VM * vm, Value ds, Value key, Value value) {
case TYPE_DICTIONARY: case TYPE_DICTIONARY:
DictPut(vm, ds.data.dict, key, value); DictPut(vm, ds.data.dict, key, value);
break; 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: default:
VMError(vm, "Cannot set."); VMError(vm, "Cannot set.");
} }

25
vm.c
View File

@ -61,8 +61,15 @@ static void VMMarkFuncDef(VM * vm, FuncDef * def) {
if (def->literals) { if (def->literals) {
count = def->literalsLen; count = def->literalsLen;
GCHeader(def->literals)->color = vm->black; GCHeader(def->literals)->color = vm->black;
for (i = 0; i < count; ++i) for (i = 0; i < count; ++i) {
VMMark(vm, def->literals + 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; 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; current = vm->frame->callee.data.func;
constant = LoadConstant(vm, current, literal); constant = LoadConstant(vm, current, literal);
if (constant.type != TYPE_FUNCDEF) { if (constant.type != TYPE_NIL) {
VMError(vm, EXPECTED_FUNCTION); VMError(vm, "Error trying to create closure");
} }
fn = VMAlloc(vm, sizeof(Func)); fn = VMAlloc(vm, sizeof(Func));
fn->def = constant.data.funcdef; fn->def = (FuncDef *) constant.data.pointer;
fn->parent = current; fn->parent = current;
fn->env = env; fn->env = env;
ret.type = TYPE_FUNCTION; ret.type = TYPE_FUNCTION;