From cb945a1e41bf0d2973fa245ad624370b5fa96578 Mon Sep 17 00:00:00 2001 From: = <=> Date: Sun, 10 May 2020 01:39:01 +0100 Subject: [PATCH] Seem to have resolved bad nesting issue copyList() now properly deep-copies. Recursive functions still not working. --- src/object.c | 32 ++++++++++++++++++++++++-------- src/pebblisp.c | 30 ++++++++++++------------------ 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/object.c b/src/object.c index 9bbbcbe..1924b10 100644 --- a/src/object.c +++ b/src/object.c @@ -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` diff --git a/src/pebblisp.c b/src/pebblisp.c index a35e172..c87852a 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -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(">", >h, &e); addFunc("<", <h, &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; } @@ -489,7 +483,7 @@ int repl(struct Environment *env) Object obj = parseEval(input, env); // printAndClean(&obj); printObj(&obj); - // printEnv(env); + //printEnv(env); } }