From 29aa001de858a85d6049a285ea1850a9c1cee6c8 Mon Sep 17 00:00:00 2001 From: = <=> Date: Fri, 8 May 2020 17:00:14 +0100 Subject: [PATCH] Clean-up. Added startList(). Fixed `T` and `F` --- src/object.c | 7 ++++++ src/object.h | 1 + src/pebblisp.c | 59 ++++++++++++++++++++++++++------------------------ 3 files changed, 39 insertions(+), 28 deletions(-) diff --git a/src/object.c b/src/object.c index cee606d..16a9af9 100644 --- a/src/object.c +++ b/src/object.c @@ -389,6 +389,13 @@ inline Object listObject() return list; } +inline Object startList(const Object start) +{ + Object list = listObject(); + nf_addToList(&list, start); + return list; +} + inline Object numberObject(int num) { Object o = newObject(TYPE_NUMBER); diff --git a/src/object.h b/src/object.h index f2956ae..a3630b6 100644 --- a/src/object.h +++ b/src/object.h @@ -96,6 +96,7 @@ void allocObject(Object **spot, const Object src); Object newObject(Type type); Object listObject(); +Object startList(const Object start); Object lambdaObject(); Object symbolObject(); Object boolObject(int b); diff --git a/src/pebblisp.c b/src/pebblisp.c index 3578ab5..003dc15 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -35,7 +35,7 @@ Object fetchFromEnvironment(const char *name, struct Environment *env) return errorObject(NULL_ENV); printd("Fetching '%s' from env\n", name); - printEnv(env); + // printEnv(env); for(int i = 0; i < env->size; i++) { if(strcmp(name, env->strings[i]) == 0) { return env->objects[i]; @@ -72,7 +72,7 @@ Result readSeq(struct Slice *tokens) { Object res = listObject(); for(;;) { - struct Slice *next = &tokens[0]; + struct Slice *next = tokens; struct Slice *rest = next->text? &next[1] : NULL; if(next->text[0] == ')') { return result(res, rest); @@ -93,10 +93,10 @@ Object parseAtom(struct Slice *s) } return numberObject(num); - } else if (s->text[0] == 'T' && s->text[1] == '\0') { + } else if (s->text[0] == 'T' && s->length == 1) { return boolObject(1); - } else if (s->text[0] == 'F' && s->text[1] == '\0') { + } else if (s->text[0] == 'F' && s->length == 1) { return boolObject(0); } else { @@ -108,7 +108,7 @@ Object parseAtom(struct Slice *s) Object evalDefArgs(const Object *arg_forms, struct Environment *env) { - const Object *first_form = &arg_forms[0]; + const Object *first_form = arg_forms; const char *name = first_form->name; // Immediately adding the function to the env might allow recursion? @@ -121,6 +121,15 @@ Object evalDefArgs(const Object *arg_forms, struct Environment *env) Object evalIfArgs(const Object *arg_forms, struct Environment *env) { + printf("eval(arg_forms):\n "); + Object o = eval(arg_forms, env); + printObj(&o); + printf("eval(arg_forms->forward):\n "); + o = eval(arg_forms->forward, env); + printObj(&o); + printf("eval(arg_forms->forward->forward):\n "); + o = eval(arg_forms->forward->forward, env); + printObj(&o); return eval(arg_forms, env).number? eval(arg_forms->forward, env) : eval(arg_forms->forward->forward, env); @@ -137,8 +146,8 @@ Object evalMapArgs(const Object *arg_forms, struct Environment *env) if(!arg_forms) return errorObject(NULL_MAP_ARGS); - const Object lambda = eval(&arg_forms[0], env); - Object *oldList = (&arg_forms[0])->forward; + const Object lambda = eval(arg_forms, env); + Object *oldList = arg_forms->forward; if(lambda.type != TYPE_LAMBDA || oldList->type != TYPE_LIST) return errorObject(BAD_TYPE); @@ -149,10 +158,7 @@ Object evalMapArgs(const Object *arg_forms, struct Environment *env) while(oldElement != NULL) { // Create a new list for each element in the list, // since lambda evaluation looks for a list - Object tempList = listObject(); - Object listBack[1] = {*oldElement}; - listBack[0].forward = NULL; - tempList.list = listBack; + const Object tempList = startList(*oldElement); struct Environment newEnv = envForLambda(&lambda.lambda->params, &tempList, env); @@ -173,11 +179,12 @@ struct Environment envForLambda(const Object *params, const Object *arg_forms, int length = listLength(params); - struct Environment env; - env.outer = outer; - env.strings = NULL; - env.objects = NULL; - env.size = length; + struct Environment env = { + .outer = outer, + .strings = NULL, + .objects = NULL, + .size = length + }; if(length == 0) return env; @@ -185,15 +192,12 @@ struct Environment envForLambda(const Object *params, const Object *arg_forms, env.strings = calloc(sizeof(char*), length + 1); env.objects = malloc(sizeof(Object) * length + 1); - Object vs[length]; - eval_forms(vs, arg_forms, outer); - + const Object *o = arg_forms; for(int i = 0; i < length; i++) { const char *n = itemAt(params, i)->name; - addToEnv(&env, n, eval(&arg_forms[i], outer)); // May not need eval? - // SHOULD BE `vs` not arg_forms??? - } // Something is segfaulting, anyway - // env.strings[length] = NULL; + addToEnv(&env, n, eval(o, outer)); // Could use eval_forms? + o = o->forward; + } return env; } @@ -271,6 +275,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); deleteEnv(&newEnv); return ret; @@ -338,8 +343,8 @@ void printEnv(struct Environment *env) for(int i = 0; i < env->size; i++) { if(env->strings[i] == NULL) return; - printd("env[%d]: '%s'\n ", i, env->strings[i]); - debugObj(&env->objects[i]); + printf("env[%d]: '%s'\n ", i, env->strings[i]); + printObj(&env->objects[i]); } } @@ -384,9 +389,7 @@ bopf(lth, '<'); void addFunc(const char *name, Object (*func)(Object, Object), struct Environment *env) { - Object o; - o.type = TYPE_FUNC; - o.forward = NULL; + Object o = newObject(TYPE_FUNC); o.func = func; addToEnv(env, name, o); }