summaryrefslogtreecommitdiff
path: root/lali.c
diff options
context:
space:
mode:
authorDaniel Cerqueira <dan.git@lispclub.com>2026-08-18 11:59:36 +0100
committerDaniel Cerqueira <dan.git@lispclub.com>2026-08-18 16:11:31 +0100
commit738e53849d39db2cba7c7fd97dd74a64bcafe363 (patch)
treebbbae86081b88956c2941796f00ab2699da8c60f /lali.c
parentb2db65a84bd8b9d802cd8323fa3fa82aab4d5782 (diff)
use readlinev.0.3.0
Diffstat (limited to 'lali.c')
-rw-r--r--lali.c62
1 files changed, 39 insertions, 23 deletions
diff --git a/lali.c b/lali.c
index 4ffa35b..48d4731 100644
--- a/lali.c
+++ b/lali.c
@@ -33,8 +33,13 @@
#include "liblali.h"
+
#include <getopt.h>
+#include <readline/readline.h>
+#include <readline/history.h>
+
+
#define YEARS "2025, 2026"
// MAIN ///////////////////////////////////////////////////////////////////////
@@ -58,31 +63,42 @@ void runFile(int infd, Object **env, GC_PARAM) {
}
void runREPL(int infd, Object **env, GC_PARAM) {
- Stream stream = { STREAM_TYPE_FILE, .fd = infd };
+ /// STRING
+ char *replLine = (char *)NULL;
+ Stream stream = { STREAM_TYPE_STRING, .buffer = "" };
GC_TRACE(gcObject, nil);
- for (;;) {
- if (setjmp(exceptionEnv))
- continue;
-
- for (;;) {
- *gcObject = nil;
-
- fputs("lali> ", stdout);
- fflush(stdout);
-
- if (peekNext(&stream) == EOF) {
- fputc('\n', stdout);
- return;
- }
-
- *gcObject = readExpr(&stream, GC_ROOTS);
- *gcObject = evalExpr(gcObject, env, GC_ROOTS);
-
- writeObject(*gcObject, true, stdout);
- fputc('\n', stdout);
- }
- }
+ do {
+ /* If the buffer has already been allocated,
+ return the memory to the free pool. */
+ if (replLine) {
+ free(replLine);
+ replLine = (char *)NULL;
+ }
+
+ replLine = readline("lali> ");
+
+ stream.length = 0;
+ stream.offset = 0;
+ stream.buffer = replLine;
+
+ while (peekNext(&stream) != EOF) {
+ *gcObject = nil;
+ *gcObject = readExpr(&stream, GC_ROOTS);
+ *gcObject = evalExpr(gcObject, env, GC_ROOTS);
+ writeObject(*gcObject, true, stdout);
+ fputc('\n', stdout);
+ }
+
+ // If the line has any text in it, save it on the history.
+ if (replLine && *replLine)
+ add_history (replLine);
+
+ } while (replLine != NULL);
+
+ fputc('\n', stdout);
+ stream.buffer = (char *)NULL;
+ free(replLine);
}
int main(int argc, char *argv[]) {