diff options
| author | Daniel Cerqueira <dan.git@lispclub.com> | 2026-08-18 11:59:36 +0100 |
|---|---|---|
| committer | Daniel Cerqueira <dan.git@lispclub.com> | 2026-08-18 16:11:31 +0100 |
| commit | 738e53849d39db2cba7c7fd97dd74a64bcafe363 (patch) | |
| tree | bbbae86081b88956c2941796f00ab2699da8c60f | |
| parent | b2db65a84bd8b9d802cd8323fa3fa82aab4d5782 (diff) | |
use readlinev.0.3.0
| -rw-r--r-- | Makefile | 5 | ||||
| -rw-r--r-- | lali.c | 62 |
2 files changed, 43 insertions, 24 deletions
@@ -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 @@ -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[]) { |
