add readline support

This commit is contained in:
Yuichi Nishiwaki 2013-10-17 17:08:33 +09:00
parent 0ccff2f626
commit 17f0c928da
3 changed files with 23 additions and 2 deletions

View File

@ -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

View File

@ -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

View File

@ -2,6 +2,11 @@
#include "picrin.h"
#if PIC_ENABLE_READLINE
# include <readline/readline.h>
# include <readline/history.h>
#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);