From 6b9a7788e1950a8c56ca0819ddc01c56c71fb6d0 Mon Sep 17 00:00:00 2001 From: "Sunrim KIM (keen)" <3han5chou7@gmail.com> Date: Thu, 7 Aug 2014 16:35:33 +0900 Subject: [PATCH] add scratch --- contrib/10.readline/CMakeLists.txt | 19 +++++ .../10.readline/cmake}/FindLibedit.cmake | 0 contrib/10.readline/src/readline.c | 74 +++++++++++++++++++ contrib/10.readline/t/test.scm | 15 ++++ tools/CMakeLists.txt | 7 -- 5 files changed, 108 insertions(+), 7 deletions(-) create mode 100644 contrib/10.readline/CMakeLists.txt rename {cmake => contrib/10.readline/cmake}/FindLibedit.cmake (100%) create mode 100644 contrib/10.readline/src/readline.c create mode 100644 contrib/10.readline/t/test.scm diff --git a/contrib/10.readline/CMakeLists.txt b/contrib/10.readline/CMakeLists.txt new file mode 100644 index 00000000..67d78733 --- /dev/null +++ b/contrib/10.readline/CMakeLists.txt @@ -0,0 +1,19 @@ +# readline + +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/contrib/10.readline/cmake/") + +find_package(Libedit) + +find_package(Libedit) +if (Libedit_FOUND) + add_definitions(${Libedit_DEFINITIONS} -DPIC_READLINE_FOUND=1) + include_directories(${Libedit_INCLUDE_DIR}) + + file(GLOB PICRIN_READLINE_SOURCES ${PROJECT_SOURCE_DIR}/contrib/10.readline/src/*.c) + + list(APPEND PICRIN_CONTRIB_INITS readline) + list(APPEND PICRIN_CONTRIB_LIBRARIES ${Libedit_LIBRARIES}) + list(APPEND PICRIN_CONTRIB_SOURCES ${PICRIN_READLINE_SOURCES}) + # add_custom_target(test-readline for test in ${PROJECT_SOURCE_DIR}/contrib/10.regexp/t/*.scm \; do bin/picrin "$$test" \; done DEPENDS repl) + # set(CONTRIB_TESTS ${CONTRIB_TESTS} test-readline) +endif() diff --git a/cmake/FindLibedit.cmake b/contrib/10.readline/cmake/FindLibedit.cmake similarity index 100% rename from cmake/FindLibedit.cmake rename to contrib/10.readline/cmake/FindLibedit.cmake diff --git a/contrib/10.readline/src/readline.c b/contrib/10.readline/src/readline.c new file mode 100644 index 00000000..a12e5558 --- /dev/null +++ b/contrib/10.readline/src/readline.c @@ -0,0 +1,74 @@ +/* + cc -g test.c -o test -ledit -ltermcap +*/ + +/* This will include all our libedit functions. If you use C++ don't +forget to use the C++ extern "C" to get it to compile. +*/ +#include +#include +#include +#include "picrin.h" +#include "picrin/string.h" +#include "picrin/port.h" + + +static pic_value +pic_rl_readline(pic_state *pic) +{ + char *prompt, *result; + + pic_get_args(pic, "z", &prompt); + + result = readline(prompt); + + if(result) + return pic_obj_value(pic_str_new_cstr(pic, result)); + else + return pic_eof_object(); +} + +static pic_value +pic_rl_add_history(pic_state *pic) +{ + char *line; + + pic_get_args(pic, "z", &line); + + add_history(line); + + return pic_undef_value(); +} + +static pic_value +pic_rl_clear_history(pic_state *pic) +{ + pic_get_args(pic, ""); + + clear_history(); + + return pic_undef_value(); +} + +static pic_value +pic_rl_remove_history(pic_state *pic) +{ + int line; + + pic_get_args(pic, "i", &line); + + remove_history(line); + + return pic_undef_value(); +} + +void +pic_init_readline(pic_state *pic){ + using_history(); + pic_deflibrary (pic, "(picrin readline)") { + pic_defun(pic, "readline", pic_rl_readline); + pic_defun(pic, "add-history", pic_rl_add_history); + pic_defun(pic, "clear-history", pic_rl_clear_history); + pic_defun(pic, "remove-history", pic_rl_remove_history); + } +} diff --git a/contrib/10.readline/t/test.scm b/contrib/10.readline/t/test.scm new file mode 100644 index 00000000..68b39de4 --- /dev/null +++ b/contrib/10.readline/t/test.scm @@ -0,0 +1,15 @@ +(import (scheme base) + (scheme read) + (scheme eval) + (scheme write) + (picrin readline)) + +(let loop ((n 1)) + (let ((input (readline "> "))) + (if (eof-object? input) + (newline) + (begin + (add-history input) + (write (eval (read (open-input-string input)) '(picrin user))) + (newline) + (loop 1))))) diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index ad7824be..24794f53 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -1,12 +1,5 @@ list(APPEND REPL_LIBRARIES picrin) -find_package(Libedit) -if (Libedit_FOUND) - include_directories(${Libedit_INCLUDE_DIRS}) - add_definitions(${Libedit_DEFINITIONS} -DPIC_READLINE_FOUND=1) - list(APPEND REPL_LIBRARIES ${Libedit_LIBRARIES}) -endif() - # build add_executable(repl tools/main.c) set_target_properties(repl PROPERTIES OUTPUT_NAME picrin)