]> git.lizzy.rs Git - nothing.git/commitdiff
(#382) Make REPL support several expressions in a row
authorrexim <reximkut@gmail.com>
Sat, 13 Oct 2018 17:52:46 +0000 (00:52 +0700)
committerrexim <reximkut@gmail.com>
Sat, 13 Oct 2018 17:52:46 +0000 (00:52 +0700)
src/script/repl.c

index 2e794bbc745efe7e5d2521604cc5df7b428c6aa8..823340d2d06b2b4cfe7e6a9eff4662a8abd23234 100644 (file)
@@ -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);