Add readFromEnvironment() for reading without cloning.

Define the REPL prompt in the environment.
This commit is contained in:
Sage Vaillancourt 2022-03-29 15:49:46 -04:00 committed by Sage Vaillancourt
parent 79a2d09995
commit 2b09187a94
3 changed files with 16 additions and 5 deletions

View File

@ -23,6 +23,11 @@ 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);
@ -30,7 +35,7 @@ Object fetchFromEnvironment(const char* name, struct Environment* env)
if (env->capacity == 0) {
if (env->outer) {
return fetchFromEnvironment(name, env->outer);
return readFromEnvironment(name, env->outer);
} else {
return errorObject(EMPTY_ENV);
}
@ -43,13 +48,13 @@ Object fetchFromEnvironment(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 cloneObject(env->objects[i]);
return env->objects[i];
}
}
printd("Trying outer %p\n", env->outer);
if (env->outer) {
return fetchFromEnvironment(name, env->outer);
return readFromEnvironment(name, env->outer);
}
return errorWithContext(DID_NOT_FIND_SYMBOL, name);

View File

@ -23,6 +23,9 @@ 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,12 +600,15 @@ void repl(struct Environment* env)
{
char* buf;
using_history();
while ((buf = readline("pebblisp::> ")) != NULL) {
parseEval("(def prompt \"pebblisp::> \")", env);
while ((buf = readline(readFromEnvironment("prompt", env).string)) != NULL) {
if (strcmp("q", buf) == 0) {
free(buf);
break;
}
if (buf[0] != '\0') {
add_history(buf);
}
if (buf[0] == '?' && (buf[1] == ' ' || buf[1] == '\0')) {
char* oldBuf = buf;
buf = malloc(sizeof(char) * strlen(buf + 3));