Remove readFromEnvironment().

Something about it was buggy. Might be worth coming back to.
This commit is contained in:
Sage Vaillancourt 2022-03-29 16:31:17 -04:00 committed by Sage Vaillancourt
parent f12d8bea7f
commit 857219a4e8
3 changed files with 20 additions and 27 deletions

View File

@ -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);

View File

@ -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,

View File

@ -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);
}
}
if (ret != -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;
}
}
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);
}