From 2b09187a94502442778e00f126c760f6739f9ab8 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Tue, 29 Mar 2022 15:49:46 -0400 Subject: [PATCH] Add readFromEnvironment() for reading without cloning. Define the REPL prompt in the environment. --- src/env.c | 11 ++++++++--- src/env.h | 3 +++ src/pebblisp.c | 7 +++++-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/env.c b/src/env.c index c4df943..773d04b 100644 --- a/src/env.c +++ b/src/env.c @@ -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); diff --git a/src/env.h b/src/env.h index 93cb070..311cb1d 100644 --- a/src/env.h +++ b/src/env.h @@ -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, diff --git a/src/pebblisp.c b/src/pebblisp.c index a01464a..8eae15f 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -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; } - add_history(buf); + 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));