From 7906e5522041f32fb6afdc652f8f651e33fb3353 Mon Sep 17 00:00:00 2001 From: = <=> Date: Wed, 6 May 2020 08:09:25 +0100 Subject: [PATCH] Slight clean-up and error-catching --- src/pebblisp.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/pebblisp.c b/src/pebblisp.c index 679aaa2..ed619c7 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -165,13 +165,9 @@ Object evalBuiltIns(const Object *first, const Object *rest, { if(strcmp(first->name, "def") == 0) { return evalDefArgs(rest, env); - }else if(strcmp(first->name, "if") == 0) { + } else if(strcmp(first->name, "if") == 0) { return evalIfArgs(rest, env); - }else if(strcmp(first->name, "fn") == 0) { - // printd("EVALBUILTINS first:\n"); - // debugObj(first); - // printd("\nEVALBUILTINS rest:\n"); - // debugObj(rest); + } else if(strcmp(first->name, "fn") == 0) { return evalLambdaArgs(rest); } @@ -193,16 +189,15 @@ Object eval(const Object *obj, struct Environment *env) switch(obj->type) { case TYPE_NUMBER: case TYPE_BOOL: - return *obj; + return *obj; // Return as is case TYPE_SYMBOL: return fetchFromEnvironment(obj->name, env); case TYPE_LIST: { - if(listLength(obj) == 0) { - printf("empty list\n"); return *obj; - } + if(listLength(obj) == 0) + return *obj; if(listLength(obj) == 1) return eval(obj->list, env); @@ -213,6 +208,7 @@ Object eval(const Object *obj, struct Environment *env) Object built_in = evalBuiltIns(first_form, first_form->forward, env); + // deleteList(obj); // Decreases indirectly lost memory, but fails on Pebble if(built_in.type != TYPE_ERROR) return built_in; } @@ -227,10 +223,9 @@ Object eval(const Object *obj, struct Environment *env) for(int i = 1; i < length; i++) { func_eval = first_eval.func(func_eval, rest[i]); } - // deleteList(obj); + // deleteList(obj); // Decreases indirectly lost memory, but fails on Pebble return func_eval; - } else if (first_eval.type == TYPE_LAMBDA) { struct Environment newEnv = envForLambda(&first_eval.lambda->params, first_form->forward, env); @@ -269,6 +264,7 @@ void addToEnv(struct Environment *env, const char *name, const Object obj) break; } if(strcmp(env->strings[i], name) == 0) { + cleanObject(&env->objects[i]); env->objects[i] = obj; break; } @@ -277,6 +273,10 @@ void addToEnv(struct Environment *env, const char *name, const Object obj) void printEnv(struct Environment *env) { + if(!env) { + printd("NULL env\n"); + return; + } for(int i = 0; i < MAX_ENV_ELM; i++) { if(env->strings[i] == NULL) return;