]> git.lizzy.rs Git - nothing.git/blob - src/ebisp/repl.c
(#893) Remove source_code from Script
[nothing.git] / src / ebisp / repl.c
1 #include "system/stacktrace.h"
2 #include <stdbool.h>
3
4 #include "gc.h"
5 #include "interpreter.h"
6 #include "parser.h"
7 #include "repl_runtime.h"
8 #include "scope.h"
9 #include "std.h"
10
11 #define REPL_BUFFER_MAX 1024
12
13 static void eval_line(Gc *gc, Scope *scope, const char *line)
14 {
15     /* TODO(#465): eval_line could be implemented with read_all_exprs_from_string */
16     while (*line != 0) {
17         gc_collect(gc, scope->expr);
18
19         struct ParseResult parse_result = read_expr_from_string(gc, line);
20         if (parse_result.is_error) {
21             print_parse_error(stderr, line, parse_result);
22             return;
23         }
24
25         struct EvalResult eval_result = eval(gc, scope, parse_result.expr);
26         if (eval_result.is_error) {
27             fprintf(stderr, "Error:\t");
28             print_expr_as_sexpr(stderr, eval_result.expr);
29             fprintf(stderr, "\n");
30             return;
31         }
32
33         print_expr_as_sexpr(stderr, eval_result.expr);
34         fprintf(stdout, "\n");
35
36         line = next_token(parse_result.end).begin;
37     }
38 }
39
40 int main(int argc, char *argv[])
41 {
42     (void) argc;
43     (void) argv;
44
45     char buffer[REPL_BUFFER_MAX + 1];
46
47     Gc *gc = create_gc();
48     struct Scope scope = create_scope(gc);
49
50     load_std_library(gc, &scope);
51     load_repl_runtime(gc, &scope);
52
53     while (true) {
54         printf("> ");
55
56         if (fgets(buffer, REPL_BUFFER_MAX, stdin) == NULL) {
57             return -1;
58         }
59
60         eval_line(gc, &scope, buffer);
61     }
62
63     destroy_gc(gc);
64
65     return 0;
66 }