Slight clean-up and error-catching

This commit is contained in:
= 2020-05-06 08:09:25 +01:00
parent 80ca6e1960
commit 7906e55220
1 changed files with 12 additions and 12 deletions

View File

@ -165,13 +165,9 @@ Object evalBuiltIns(const Object *first, const Object *rest,
{ {
if(strcmp(first->name, "def") == 0) { if(strcmp(first->name, "def") == 0) {
return evalDefArgs(rest, env); return evalDefArgs(rest, env);
}else if(strcmp(first->name, "if") == 0) { } else if(strcmp(first->name, "if") == 0) {
return evalIfArgs(rest, env); return evalIfArgs(rest, env);
}else if(strcmp(first->name, "fn") == 0) { } else if(strcmp(first->name, "fn") == 0) {
// printd("EVALBUILTINS first:\n");
// debugObj(first);
// printd("\nEVALBUILTINS rest:\n");
// debugObj(rest);
return evalLambdaArgs(rest); return evalLambdaArgs(rest);
} }
@ -193,16 +189,15 @@ Object eval(const Object *obj, struct Environment *env)
switch(obj->type) { switch(obj->type) {
case TYPE_NUMBER: case TYPE_NUMBER:
case TYPE_BOOL: case TYPE_BOOL:
return *obj; return *obj; // Return as is
case TYPE_SYMBOL: case TYPE_SYMBOL:
return fetchFromEnvironment(obj->name, env); return fetchFromEnvironment(obj->name, env);
case TYPE_LIST: case TYPE_LIST:
{ {
if(listLength(obj) == 0) { if(listLength(obj) == 0)
printf("empty list\n"); return *obj; return *obj;
}
if(listLength(obj) == 1) if(listLength(obj) == 1)
return eval(obj->list, env); return eval(obj->list, env);
@ -213,6 +208,7 @@ Object eval(const Object *obj, struct Environment *env)
Object built_in = Object built_in =
evalBuiltIns(first_form, first_form->forward, env); evalBuiltIns(first_form, first_form->forward, env);
// deleteList(obj); // Decreases indirectly lost memory, but fails on Pebble
if(built_in.type != TYPE_ERROR) if(built_in.type != TYPE_ERROR)
return built_in; return built_in;
} }
@ -227,10 +223,9 @@ Object eval(const Object *obj, struct Environment *env)
for(int i = 1; i < length; i++) { for(int i = 1; i < length; i++) {
func_eval = first_eval.func(func_eval, rest[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; return func_eval;
} else if (first_eval.type == TYPE_LAMBDA) { } else if (first_eval.type == TYPE_LAMBDA) {
struct Environment newEnv = struct Environment newEnv =
envForLambda(&first_eval.lambda->params, first_form->forward, env); 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; break;
} }
if(strcmp(env->strings[i], name) == 0) { if(strcmp(env->strings[i], name) == 0) {
cleanObject(&env->objects[i]);
env->objects[i] = obj; env->objects[i] = obj;
break; break;
} }
@ -277,6 +273,10 @@ void addToEnv(struct Environment *env, const char *name, const Object obj)
void printEnv(struct Environment *env) void printEnv(struct Environment *env)
{ {
if(!env) {
printd("NULL env\n");
return;
}
for(int i = 0; i < MAX_ENV_ELM; i++) { for(int i = 0; i < MAX_ENV_ELM; i++) {
if(env->strings[i] == NULL) if(env->strings[i] == NULL)
return; return;