Dedicated getPrompt() function.

Can now handle strings or lambdas.
This commit is contained in:
Sage Vaillancourt 2022-03-30 11:39:28 -04:00 committed by Sage Vaillancourt
parent 92daee1d0d
commit 75ba0ac5ee
1 changed files with 26 additions and 9 deletions

View File

@ -1,9 +1,6 @@
#ifdef STANDALONE #ifdef STANDALONE
#define _GNU_SOURCE #define _GNU_SOURCE
#include <signal.h>
#include <ucontext.h>
#endif #endif
#include "pebblisp.h" #include "pebblisp.h"
@ -19,6 +16,9 @@
#include <readline/readline.h> #include <readline/readline.h>
#include <readline/history.h> #include <readline/history.h>
#include <signal.h>
#include <ucontext.h>
#include "web.h" #include "web.h"
#endif #endif
@ -596,21 +596,39 @@ int _readFile(FILE* input, struct Environment* env)
return 0; 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, &param, 2, env);
cleanObject(&prompt);
cleanObject(&param);
char* ret = readline(e.string);
cleanObject(&e);
return ret;
}
void repl(struct Environment* env) void repl(struct Environment* env)
{ {
char* buf; char* buf;
Object prompt;
using_history(); using_history();
while ((buf = readline((prompt = fetchFromEnvironment("prompt", env)).string)) != NULL) { while ((buf = getPrompt(env)) != NULL) {
cleanObject(&prompt);
if (strcmp("q", buf) == 0) { if (strcmp("q", buf) == 0) {
free(buf); free(buf);
break; break;
} }
if (buf[0] != '\0') { if (buf[0] == '\0') {
add_history(buf); free(buf);
continue;
} }
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));
@ -633,7 +651,6 @@ void repl(struct Environment* env)
free(output); free(output);
printf("\n"); printf("\n");
} }
cleanObject(&prompt);
} }
void loadArgsIntoEnv(int argc, const char* argv[], struct Environment* env) void loadArgsIntoEnv(int argc, const char* argv[], struct Environment* env)