Mercurial > lbo > hg > ylisp
changeset 167:b28c56a172f7
repl: Move repl
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Tue, 03 Sep 2019 20:06:37 +0200 |
parents | b5f1940e8651 |
children | 5697df53fe81 |
files | src/CMakeLists.txt src/bin/CMakeLists.txt src/bin/repl.c src/repl.c |
diffstat | 4 files changed, 70 insertions(+), 69 deletions(-) [+] |
line wrap: on
line diff
--- a/src/CMakeLists.txt Tue Sep 03 20:05:18 2019 +0200 +++ b/src/CMakeLists.txt Tue Sep 03 20:06:37 2019 +0200 @@ -28,6 +28,8 @@ TARGET_LINK_LIBRARIES(frontend core base y_parser) +ADD_SUBDIRECTORY(bin) + # Atom test. ADD_EXECUTABLE(atom_test atom_test.c) TARGET_LINK_LIBRARIES(atom_test core) @@ -68,8 +70,3 @@ TARGET_LINK_LIBRARIES(value_test core) YADD_TEST(value_test) -## - -# REPL -ADD_EXECUTABLE(repl repl.c) -TARGET_LINK_LIBRARIES(repl core frontend)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/bin/CMakeLists.txt Tue Sep 03 20:06:37 2019 +0200 @@ -0,0 +1,4 @@ + +# REPL +ADD_EXECUTABLE(repl repl.c) +TARGET_LINK_LIBRARIES(repl core frontend)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/bin/repl.c Tue Sep 03 20:06:37 2019 +0200 @@ -0,0 +1,64 @@ +#include <stdio.h> + +#include <src/eval.h> +#include <src/parse.h> +#include <src/preprocess.h> + +int repl(void) { +#define bufsize 512 + char buf[bufsize]; + + yeval_state_t state; + state.call_stack = YVEC_NEW(NULL, 16, yexpr_t); + yvec_t* scope_stack = NULL; + + while (1) { + ystr_t input = ystr_new(NULL), output = ystr_new(NULL), + error = ystr_new(NULL); + yexpr_t stmt = yexpr_new(), result; + + fprintf(stdout, "() >> "); + + if (NULL == fgets(buf, bufsize, stdin)) break; + input = ystr_new(buf); + if (0 == ystr_cmp_str(&input, "quit\n")) { + return 0; + } + if (!yparse_str(&input, &stmt, &error)) { + fprintf(stderr, "parse error: %s\n", ystr_str(&error)); + goto cleanup_continue; + } + if (YEXPR_LIST != stmt.typ) { + fprintf(stderr, "bug: expression should be list!\n"); + goto cleanup_continue; + } + ypreprocess_resolve_builtins(&stmt); + ypreprocess_refs_repl(&stmt, &scope_stack); + + result = yeval_list_return_last(&state, &stmt.value.list, 0); + + if (result.typ != YEXPR_UNDEF) { + output = yexpr_debug_str(&result); + fprintf(stdout, "%s\n", ystr_str(&output)); + } else { + fputc('\n', stdout); + } + + cleanup_continue: + ystr_destroy(&input); + ystr_destroy(&error); + ystr_destroy(&output); + yexpr_destroy(&result); + // stmt is not destroyed, values in it may be used later + } + + yvec_destroy(&state.call_stack); + + return 0; +#undef bufsize +} + +int main(void) { + repl(); + return 0; +}
--- a/src/repl.c Tue Sep 03 20:05:18 2019 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,64 +0,0 @@ -#include <stdio.h> - -#include "eval.h" -#include "parse.h" -#include "preprocess.h" - -int repl(void) { -#define bufsize 512 - char buf[bufsize]; - - yeval_state_t state; - state.call_stack = YVEC_NEW(NULL, 16, yexpr_t); - yvec_t* scope_stack = NULL; - - while (1) { - ystr_t input = ystr_new(NULL), output = ystr_new(NULL), - error = ystr_new(NULL); - yexpr_t stmt = yexpr_new(), result; - - fprintf(stdout, "() >> "); - - if (NULL == fgets(buf, bufsize, stdin)) break; - input = ystr_new(buf); - if (0 == ystr_cmp_str(&input, "quit\n")) { - return 0; - } - if (!yparse_str(&input, &stmt, &error)) { - fprintf(stderr, "parse error: %s\n", ystr_str(&error)); - goto cleanup_continue; - } - if (YEXPR_LIST != stmt.typ) { - fprintf(stderr, "bug: expression should be list!\n"); - goto cleanup_continue; - } - ypreprocess_resolve_builtins(&stmt); - ypreprocess_refs_repl(&stmt, &scope_stack); - - result = yeval_list_return_last(&state, &stmt.value.list, 0); - - if (result.typ != YEXPR_UNDEF) { - output = yexpr_debug_str(&result); - fprintf(stdout, "%s\n", ystr_str(&output)); - } else { - fputc('\n', stdout); - } - - cleanup_continue: - ystr_destroy(&input); - ystr_destroy(&error); - ystr_destroy(&output); - yexpr_destroy(&result); - // stmt is not destroyed, values in it may be used later - } - - yvec_destroy(&state.call_stack); - - return 0; -#undef bufsize -} - -int main(void) { - repl(); - return 0; -}