From 857219a4e870fbe61bca844d8fc1da74ddc2726b Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Tue, 29 Mar 2022 16:31:17 -0400 Subject: [PATCH] Remove readFromEnvironment(). Something about it was buggy. Might be worth coming back to. --- src/env.c | 11 +++-------- src/env.h | 3 --- src/pebblisp.c | 33 +++++++++++++++++---------------- 3 files changed, 20 insertions(+), 27 deletions(-) diff --git a/src/env.c b/src/env.c index 773d04b..c4df943 100644 --- a/src/env.c +++ b/src/env.c @@ -23,11 +23,6 @@ void setGlobal(struct Environment* env) struct Dictionary dictionary; Object fetchFromEnvironment(const char* name, struct Environment* env) -{ - return cloneObject(readFromEnvironment(name, env)); -} - -Object readFromEnvironment(const char* name, struct Environment* env) { if (!env) { return errorObject(NULL_ENV); @@ -35,7 +30,7 @@ Object readFromEnvironment(const char* name, struct Environment* env) if (env->capacity == 0) { if (env->outer) { - return readFromEnvironment(name, env->outer); + return fetchFromEnvironment(name, env->outer); } else { return errorObject(EMPTY_ENV); } @@ -48,13 +43,13 @@ Object readFromEnvironment(const char* name, struct Environment* env) printd("Try %d (%s)\n", i, env->strings[i]); if (strcmp(name, env->strings[i]) == 0) { printd("Returning!\n"); - return env->objects[i]; + return cloneObject(env->objects[i]); } } printd("Trying outer %p\n", env->outer); if (env->outer) { - return readFromEnvironment(name, env->outer); + return fetchFromEnvironment(name, env->outer); } return errorWithContext(DID_NOT_FIND_SYMBOL, name); diff --git a/src/env.h b/src/env.h index 311cb1d..93cb070 100644 --- a/src/env.h +++ b/src/env.h @@ -23,9 +23,6 @@ struct Environment* global(); void setGlobal(struct Environment* env); -/// Does NOT clone -Object readFromEnvironment(const char* name, struct Environment* env); - Object fetchFromEnvironment(const char* name, struct Environment* env); struct Environment envForLambda(const Object* params, const Object* arg_forms, int paramCount, diff --git a/src/pebblisp.c b/src/pebblisp.c index 890eda3..9016107 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -600,7 +600,9 @@ void repl(struct Environment* env) { char* buf; using_history(); - while ((buf = readline(readFromEnvironment("prompt", env).string)) != NULL) { + Object prompt; + while ((buf = readline((prompt = fetchFromEnvironment("prompt", env)).string)) != NULL) { + cleanObject(&prompt); if (strcmp("q", buf) == 0) { free(buf); break; @@ -618,11 +620,13 @@ void repl(struct Environment* env) size_t length; char *output = stringObj(&o, &length); printColored(output); - free(output); printf("\n"); + cleanObject(&o); + free(output); free(buf); } + cleanObject(&prompt); } void loadArgsIntoEnv(int argc, const char* argv[], struct Environment* env) @@ -660,19 +664,15 @@ int main(int argc, const char* argv[]) struct Environment env = defaultEnv(); setGlobal(&env); - int ret = -1; if (argc == 2) { - if (strcmp(argv[1], "--run-tests") == 0) { - ret = runTests(0); - } else if (strcmp(argv[1], "--run-tests=detailed") == 0) { - ret = runTests(1); + const char* runTestsArg = "--run-tests"; + if (strncmp(argv[1], runTestsArg, strlen(runTestsArg)) == 0) { + int ret = runTests(strcmp(argv[1] + strlen(runTestsArg), "=detailed") == 0); + shredDictionary(); + deleteEnv(global()); + return ret; } } - if (ret != -1) { - shredDictionary(); - deleteEnv(global()); - return ret; - } struct sigaction action; memset(&action, 0, sizeof(struct sigaction)); @@ -682,7 +682,8 @@ int main(int argc, const char* argv[]) readFile(SCRIPTDIR "/lib.pbl", &env); - parseEval("(def prompt \"pebblisp::> \")", &env); + Object o = parseEval("(def prompt \"pebblisp::> \")", &env); + cleanObject(&o); char config[strlen(getenv("HOME")) + 15]; sprintf(config, "%s/.pebblisp.pbl", getenv("HOME")); @@ -691,11 +692,11 @@ int main(int argc, const char* argv[]) if (argc >= 2) { FILE* file = fopen(argv[1], "r"); if (file) { - // Executing a file + // Execute a file loadArgsIntoEnv(argc, argv, &env); _readFile(file, &env); } else { - // Running arguments directly as pl code + // Run arguments directly as pl code Object r = numberObject(0); for (int i = 1; i < argc; i++) { r = parseEval(argv[i], &env); @@ -703,7 +704,7 @@ int main(int argc, const char* argv[]) } } } else { - // Running a repl + // Run a repl loadArgsIntoEnv(argc, argv, &env); repl(&env); }