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
#define _GNU_SOURCE
#include <signal.h>
#include <ucontext.h>
#endif
#include "pebblisp.h"
@ -19,6 +16,9 @@
#include <readline/readline.h>
#include <readline/history.h>
#include <signal.h>
#include <ucontext.h>
#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, &param, 2, env);
cleanObject(&prompt);
cleanObject(&param);
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)