From 17f0c928da37729c928851039c93ace47eb73a74 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 17 Oct 2013 17:08:33 +0900 Subject: [PATCH] add readline support --- Makefile | 2 +- include/picconf.h | 3 +++ src/main.c | 20 +++++++++++++++++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 24ecd644..7f13f8db 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ build: cd src; \ yacc -d parse.y; \ lex scan.l - gcc -Wall -o bin/picrin -I./include src/main.c src/state.c src/gc.c src/pair.c src/port.c src/symbol.c src/value.c src/y.tab.c src/lex.yy.c src/bool.c src/vm.c src/init.c src/number.c + gcc -Wall -o bin/picrin -I./include -lreadline src/main.c src/state.c src/gc.c src/pair.c src/port.c src/symbol.c src/value.c src/y.tab.c src/lex.yy.c src/bool.c src/vm.c src/init.c src/number.c clean: rm -f src/y.tab.c src/y.tab.h src/lex.yy.c diff --git a/include/picconf.h b/include/picconf.h index c35c6261..35677f9f 100644 --- a/include/picconf.h +++ b/include/picconf.h @@ -4,6 +4,9 @@ /* switch normal VM and direct threaded VM */ #define PIC_DIRECT_THREADED_VM 1 +/* enable readline module */ +#define PIC_ENABLE_READLINE 1 + /* initial memory size (to be dynamically extended if necessary) */ #define PIC_ARENA_SIZE 100 #define PIC_HEAP_SIZE 8192 diff --git a/src/main.c b/src/main.c index 4f1f9871..36e14f4e 100644 --- a/src/main.c +++ b/src/main.c @@ -2,6 +2,11 @@ #include "picrin.h" +#if PIC_ENABLE_READLINE +# include +# include +#endif + void test_object_creation(pic_state *pic) { @@ -30,7 +35,7 @@ int main() { pic_state *pic; - char line[LINE_MAX_LENGTH], last_char; + char line[LINE_MAX_LENGTH], last_char, *read_line; int char_index; pic_value v; struct pic_proc *proc; @@ -45,6 +50,18 @@ main() ai = pic_gc_arena_preserve(pic); while (1) { + +#if PIC_ENABLE_READLINE + read_line = readline("> "); + if (read_line == NULL) { + line[0] = '\0'; + } + else { + strncpy(line, read_line, LINE_MAX_LENGTH - 1); + add_history(read_line); + free(read_line); + } +#else printf("> "); char_index = 0; @@ -56,6 +73,7 @@ main() line[char_index++] = last_char; } line[char_index] = '\0'; +#endif /* read */ v = pic_parse(pic, line);