From d87322b6a5d341c40c7656c66db86f5248c92763 Mon Sep 17 00:00:00 2001 From: = <=> Date: Tue, 4 Aug 2020 20:26:47 +0100 Subject: [PATCH] Some env.c refactoring and clean-up --- src/env.c | 61 ++++++++++++++++++++++--------------------------------- 1 file changed, 24 insertions(+), 37 deletions(-) diff --git a/src/env.c b/src/env.c index 26a9573..59e941a 100644 --- a/src/env.c +++ b/src/env.c @@ -24,6 +24,7 @@ Object fetchFromEnvironment(const char *name, struct Environment *env) printd("Try %d (NULL)\n", i); break; } + printd("Try %d (%s)\n", i, env->strings[i]); debugObj(&env->objects[i]); if(strcmp(name, env->strings[i]) == 0) { @@ -34,7 +35,6 @@ Object fetchFromEnvironment(const char *name, struct Environment *env) printd("Trying outer %p\n", env->outer); if(env->outer) { - // printEnv(env->outer); return fetchFromEnvironment(name, env->outer); } @@ -71,12 +71,11 @@ struct Environment envForLambda(const Object *params, const Object *arg_forms, return env; } -// todo could include and return a starting index for faster multi-adds -// Maybe extend existing environment using a malloc'd outer, instead of realloc +// TODO Maybe extend environment using a new outer, instead of realloc void addToEnv(struct Environment *env, const char *name, const Object obj) { int i; - for(i = 0; i < env->size ; i++) { + for(i = 0; i < env->size; i++) { if(env->strings[i] == NULL) { env->strings[i] = calloc(sizeof(char), strlen(name) + 1); strncpy(env->strings[i], name, strlen(name)); @@ -163,53 +162,41 @@ const char *codes[] = { // Return the smaller of the two "(def min (fn (a b) (if (< a b) a b)))", - // Arbitrary recursion tester - "(def ad (fn (a) (if (> a 10) a (ad (* 10 a) ))))", - // A demo tip calculator "(def spent (fn (a) \ (cat \"Tip: $\" (/ a 5) \".\" \ (/ (* 100 (% a 5)) 5) \ ) \ ))", +}; - "(def r (fn (L) \ - (if (= L () \ - ", - - NULL +struct symFunc { + const char *sym; + Object (*func)(Object, Object, struct Environment*); }; struct Environment defaultEnv() { - struct Environment e; - e.outer = NULL; - e.strings = calloc(sizeof(char*), MAX_ENV_ELM); - e.objects = malloc(sizeof(Object) * MAX_ENV_ELM); - e.size = MAX_ENV_ELM; + struct Environment e = { + .outer = NULL, + .strings = calloc(sizeof(char*), MAX_ENV_ELM), + .objects = malloc(sizeof(Object) * MAX_ENV_ELM), + .size = MAX_ENV_ELM + }; - #define af(S, F) addFunc(S, F, &e) - af("+", &add); af("-", &sub); - af("*", &mul); af("/", &dvi); - af("%", &mod); + struct symFunc symFuncs[] = { + {"+", &add}, {"-", &sub}, {"*", &mul}, {"/", &dvi}, {"%", &mod}, + {"=", &equ}, {">", >h}, {"<", <h}, + {"cat", &catObjects}, {"fil", &filter}, {"len", &len}, {"ap", &append}, + {"at", &at}, {"rest", &rest}, {"rev", &reverse}, + }; - af("=", &equ); - af(">", >h); - af("<", <h); + for(int i = 0; i < sizeof(symFuncs)/sizeof(symFuncs[0]); i++) { + addFunc(symFuncs[i].sym, symFuncs[i].func, &e); + } - af("cat", &catObjects); - af("fil", &filter); - - af("len", &len); - af("ap", &append); - af("at", &at); - af("rest", &rest); - af("rev", &reverse); - #undef af - - int i = 0; - while(codes[i]) { - parseEval(codes[i++], &e); + for(int i = 0; i < sizeof(codes)/sizeof(codes[0]); i++) { + parseEval(codes[i], &e); } return e;