Add readFromEnvironment() for reading without cloning.
Define the REPL prompt in the environment.
This commit is contained in:
parent
79a2d09995
commit
2b09187a94
11
src/env.c
11
src/env.c
|
@ -23,6 +23,11 @@ void setGlobal(struct Environment* env)
|
||||||
struct Dictionary dictionary;
|
struct Dictionary dictionary;
|
||||||
|
|
||||||
Object fetchFromEnvironment(const char* name, struct Environment* env)
|
Object fetchFromEnvironment(const char* name, struct Environment* env)
|
||||||
|
{
|
||||||
|
return cloneObject(readFromEnvironment(name, env));
|
||||||
|
}
|
||||||
|
|
||||||
|
Object readFromEnvironment(const char* name, struct Environment* env)
|
||||||
{
|
{
|
||||||
if (!env) {
|
if (!env) {
|
||||||
return errorObject(NULL_ENV);
|
return errorObject(NULL_ENV);
|
||||||
|
@ -30,7 +35,7 @@ Object fetchFromEnvironment(const char* name, struct Environment* env)
|
||||||
|
|
||||||
if (env->capacity == 0) {
|
if (env->capacity == 0) {
|
||||||
if (env->outer) {
|
if (env->outer) {
|
||||||
return fetchFromEnvironment(name, env->outer);
|
return readFromEnvironment(name, env->outer);
|
||||||
} else {
|
} else {
|
||||||
return errorObject(EMPTY_ENV);
|
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]);
|
printd("Try %d (%s)\n", i, env->strings[i]);
|
||||||
if (strcmp(name, env->strings[i]) == 0) { printd("Returning!\n");
|
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);
|
printd("Trying outer %p\n", env->outer);
|
||||||
if (env->outer) {
|
if (env->outer) {
|
||||||
return fetchFromEnvironment(name, env->outer);
|
return readFromEnvironment(name, env->outer);
|
||||||
}
|
}
|
||||||
|
|
||||||
return errorWithContext(DID_NOT_FIND_SYMBOL, name);
|
return errorWithContext(DID_NOT_FIND_SYMBOL, name);
|
||||||
|
|
|
@ -23,6 +23,9 @@ struct Environment* global();
|
||||||
|
|
||||||
void setGlobal(struct Environment* env);
|
void setGlobal(struct Environment* env);
|
||||||
|
|
||||||
|
/// Does NOT clone
|
||||||
|
Object readFromEnvironment(const char* name, struct Environment* env);
|
||||||
|
|
||||||
Object fetchFromEnvironment(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,
|
struct Environment envForLambda(const Object* params, const Object* arg_forms, int paramCount,
|
||||||
|
|
|
@ -600,12 +600,15 @@ void repl(struct Environment* env)
|
||||||
{
|
{
|
||||||
char* buf;
|
char* buf;
|
||||||
using_history();
|
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) {
|
if (strcmp("q", buf) == 0) {
|
||||||
free(buf);
|
free(buf);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
add_history(buf);
|
if (buf[0] != '\0') {
|
||||||
|
add_history(buf);
|
||||||
|
}
|
||||||
if (buf[0] == '?' && (buf[1] == ' ' || buf[1] == '\0')) {
|
if (buf[0] == '?' && (buf[1] == ' ' || buf[1] == '\0')) {
|
||||||
char* oldBuf = buf;
|
char* oldBuf = buf;
|
||||||
buf = malloc(sizeof(char) * strlen(buf + 3));
|
buf = malloc(sizeof(char) * strlen(buf + 3));
|
||||||
|
|
Loading…
Reference in New Issue