summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile5
-rw-r--r--lali.c62
2 files changed, 43 insertions, 24 deletions
diff --git a/Makefile b/Makefile
index f9542d8..68d5d71 100644
--- a/Makefile
+++ b/Makefile
@@ -47,22 +47,25 @@ PREFIX ?= /usr/local
.PHONY: native
native: CPPFLAGS += -DNDEBUG
native: CFLAGS += -O2
+native: LDFLAGS += -lreadline
native: $(PROGRAM)
.PHONY: generic
generic: CPPFLAGS += -DNDEBUG
generic: CFLAGS += -mtune=generic -O2
+generic: LDFLAGS += -lreadline
generic: $(PROGRAM)
.PHONY: all
all: CPPFLAGS += -DNDEBUG
all: CFLAGS += -O2
+all: LDFLAGS += -lreadline
all: $(PROGRAM) $(LIB) $(HTML)
.PHONY: debug
debug: CPPFLAGS += -DDEBUG
debug: CFLAGS += -O0 -g
-debug: LDFLAGS += -g
+debug: LDFLAGS += -lreadline
debug: $(PROGRAM)
.PHONY: clean
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[]) {