From 8b771b5540327bf47fcdaf67a6d3d40a69ea6640 Mon Sep 17 00:00:00 2001 From: rexim Date: Sun, 14 Oct 2018 00:52:46 +0700 Subject: [PATCH] (#382) Make REPL support several expressions in a row --- src/script/repl.c | 65 ++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 26 deletions(-) diff --git a/src/script/repl.c b/src/script/repl.c index 2e794bbc..823340d2 100644 --- a/src/script/repl.c +++ b/src/script/repl.c @@ -19,48 +19,31 @@ static struct EvalResult quit(void *param, Gc *gc, struct Scope *scope, struct E return eval_success(NIL(gc)); } -char buffer[REPL_BUFFER_MAX + 1]; - -int main(int argc, char *argv[]) +static void eval_line(Gc *gc, Scope *scope, const char *line) { - (void) argc; - (void) argv; - - Gc *gc = create_gc(); - struct Scope scope = { - .expr = CONS(gc, NIL(gc), NIL(gc)) - }; - - set_scope_value(gc, &scope, SYMBOL(gc, "quit"), NATIVE(gc, quit, NULL)); - - while (true) { - printf("> "); - - if (fgets(buffer, REPL_BUFFER_MAX, stdin) == NULL) { - return -1; - } - + const char *read_iter = line; + while (read_iter != NULL) { printf("Before parse:\t"); gc_inspect(gc); - struct ParseResult parse_result = read_expr_from_string(gc, buffer); + struct ParseResult parse_result = read_expr_from_string(gc, read_iter); if (parse_result.is_error) { - print_parse_error(stderr, buffer, parse_result); - continue; + print_parse_error(stderr, line, parse_result); + return; } printf("After parse:\t"); gc_inspect(gc); - struct EvalResult eval_result = eval(gc, &scope, parse_result.expr); + struct EvalResult eval_result = eval(gc, scope, parse_result.expr); printf("After eval:\t"); gc_inspect(gc); - gc_collect(gc, CONS(gc, scope.expr, eval_result.expr)); + gc_collect(gc, CONS(gc, scope->expr, eval_result.expr)); printf("After collect:\t"); gc_inspect(gc); printf("Scope:\t"); - print_expr_as_sexpr(scope.expr); + print_expr_as_sexpr(scope->expr); printf("\n"); if (eval_result.is_error) { @@ -69,6 +52,36 @@ int main(int argc, char *argv[]) print_expr_as_sexpr(eval_result.expr); printf("\n"); + + if (*next_token(parse_result.end).begin == 0) { + return; + } + read_iter = parse_result.end; + } +} + +int main(int argc, char *argv[]) +{ + (void) argc; + (void) argv; + + char buffer[REPL_BUFFER_MAX + 1]; + + Gc *gc = create_gc(); + struct Scope scope = { + .expr = CONS(gc, NIL(gc), NIL(gc)) + }; + + set_scope_value(gc, &scope, SYMBOL(gc, "quit"), NATIVE(gc, quit, NULL)); + + while (true) { + printf("> "); + + if (fgets(buffer, REPL_BUFFER_MAX, stdin) == NULL) { + return -1; + } + + eval_line(gc, &scope, buffer); } destroy_gc(gc); -- 2.44.0