diff --git a/src/pebblisp.c b/src/pebblisp.c index 48122d7..d02d759 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -1,9 +1,6 @@ #ifdef STANDALONE #define _GNU_SOURCE -#include -#include - #endif #include "pebblisp.h" @@ -19,6 +16,9 @@ #include #include +#include +#include + #include "web.h" #endif @@ -596,21 +596,39 @@ int _readFile(FILE* input, struct Environment* env) return 0; } +char* getPrompt(struct Environment* env) +{ + Object prompt = fetchFromEnvironment("prompt", env); + prompt = cloneObject(prompt); + if (prompt.type == TYPE_STRING) { + char* ret = readline(prompt.string); + cleanObject(&prompt); + return ret; + } + Object param = stringFromSlice("", 1); + Object e = listEvalLambda(&prompt, ¶m, 2, env); + cleanObject(&prompt); + cleanObject(¶m); + char* ret = readline(e.string); + cleanObject(&e); + return ret; +} + void repl(struct Environment* env) { char* buf; - Object prompt; using_history(); - while ((buf = readline((prompt = fetchFromEnvironment("prompt", env)).string)) != NULL) { - cleanObject(&prompt); + while ((buf = getPrompt(env)) != NULL) { if (strcmp("q", buf) == 0) { free(buf); break; } - if (buf[0] != '\0') { - add_history(buf); + if (buf[0] == '\0') { + free(buf); + continue; } + add_history(buf); if (buf[0] == '?' && (buf[1] == ' ' || buf[1] == '\0')) { char* oldBuf = buf; buf = malloc(sizeof(char) * strlen(buf + 3)); @@ -633,7 +651,6 @@ void repl(struct Environment* env) free(output); printf("\n"); } - cleanObject(&prompt); } void loadArgsIntoEnv(int argc, const char* argv[], struct Environment* env)