Seem to have resolved bad nesting issue

copyList() now properly deep-copies.
Recursive functions still not working.
This commit is contained in:
= 2020-05-10 01:39:01 +01:00
parent 9fe35450df
commit cb945a1e41
2 changed files with 36 additions and 26 deletions

View File

@ -361,20 +361,36 @@ void deleteList(Object *dest)
dest->list = NULL;
}
void _copyList(Object *dest, const Object *src, int delete) {
if(!dest || !src) {
printd("NULL\n");
return;
}
if(dest->type != TYPE_LIST || src->type != TYPE_LIST) {
printd("NOT A LIST\n");
return;
}
if(delete)
deleteList(dest);
FOR_POINTER_IN_LIST(src) {
debugObj(POINTER);
nf_addToList(dest, *POINTER);
if(POINTER->type == TYPE_LIST) {
tail(dest)->list = NULL;
_copyList(tail(dest), POINTER, 0);
}
}
}
/**
* Copies all items from `src` to `dest`
* Does a deep copy of all items from `src` to `dest`
* Does nothing if either is NULL, or not a list type
* Deletes all items from `dest` before copying
*/
void copyList(Object *dest, const Object *src)
{
if(!dest || !src || dest->type != TYPE_LIST || src->type != TYPE_LIST)
return;
deleteList(dest);
FOR_POINTER_IN_LIST(src) {
nf_addToList(dest, *POINTER);
}
_copyList(dest, src, 1);
}
// Returns a basic object with NULL forward and the given `type`

View File

@ -37,10 +37,10 @@ Object fetchFromEnvironment(const char *name, struct Environment *env)
if(env->size == 0)
return errorObject(EMPTY_ENV);
printf("Fetching '%s' from env\n", name);
printd("Fetching '%s' from env\n", name);
// printEnv(env);
for(int i = 0; i < env->size; i++) {
printf("Try %d\n", i);
printd("Try %d\n", i);
if(env->strings[i] == NULL)
break;
if(strcmp(name, env->strings[i]) == 0) {
@ -48,9 +48,9 @@ Object fetchFromEnvironment(const char *name, struct Environment *env)
}
}
printf("Trying outer %p\n", env->outer);
printd("Trying outer %p\n", env->outer);
if(env->outer) {
printEnv(env->outer);
// printEnv(env->outer);
return fetchFromEnvironment(name, env->outer);
}
@ -200,12 +200,12 @@ struct Environment envForLambda(const Object *params, const Object *arg_forms,
env.objects = malloc(sizeof(Object) * paramCount + 1);
const Object *march = arg_forms;
printf("Param count: %d\n", paramCount);
printd("Param count: %d\n", paramCount);
for(int i = 0; i < paramCount; i++) {
const char *newObjName = itemAt(params, i)->name;
const Object newEnvObj = eval(march, outer);
printf("Adding new object '%s' to lambda env: ", newObjName);
printObj(&newEnvObj);
printd("Adding new object '%s' to lambda env: ", newObjName);
debugObj(&newEnvObj);
addToEnv(&env, newObjName, newEnvObj); // Could use eval_forms?
march = march->forward;
}
@ -238,8 +238,6 @@ void eval_forms(Object *destList, const Object *src, struct Environment *env)
}
}
static int depth = 0;
Object eval(const Object *obj, struct Environment *env)
{
printd("eval():\n");
@ -250,13 +248,7 @@ Object eval(const Object *obj, struct Environment *env)
return *obj; // Return as is
case TYPE_SYMBOL:
{
depth++;
printf("depth: %d\n", depth);
const Object oi = fetchFromEnvironment(obj->name, env);
depth--;
return oi;
}
return fetchFromEnvironment(obj->name, env);
case TYPE_LIST:
{
@ -295,7 +287,7 @@ Object eval(const Object *obj, struct Environment *env)
struct Environment newEnv =
envForLambda(&first_eval.lambda->params, first_form->forward, env);
Object ret = eval(&first_eval.lambda->body, &newEnv);
printEnv(&newEnv);
//printEnv(&newEnv);
deleteEnv(&newEnv);
return ret;
@ -452,6 +444,8 @@ struct Environment defaultEnv() {
addFunc(">", &gth, &e);
addFunc("<", &lth, &e);
addFunc("len", &len, &e);
parseEval("(def max (fn (a b) (if (> a b) a b)))", &e);
parseEval("(def min (fn (a b) (if (< a b) a b)))", &e);
return e;
}