]> git.lizzy.rs Git - nothing.git/blob - src/script/repl.c
068fca839eedd40e5c18777e76b6ba886e92bfe3
[nothing.git] / src / script / repl.c
1 #include <stdbool.h>
2
3 #include "parser.h"
4 #include "interpreter.h"
5 #include "scope.h"
6 #include "gc.h"
7
8 #define REPL_BUFFER_MAX 1024
9
10 char buffer[REPL_BUFFER_MAX + 1];
11
12 int main(int argc, char *argv[])
13 {
14     (void) argc;
15     (void) argv;
16
17     Gc *gc = create_gc();
18
19     struct Expr scope = NIL(gc);
20
21     while (true) {
22         printf("> ");
23
24         if (fgets(buffer, REPL_BUFFER_MAX, stdin) == NULL) {
25             return -1;
26         }
27
28         printf("Before parse:\t");
29         gc_inspect(gc);
30
31         struct ParseResult parse_result = read_expr_from_string(gc, buffer);
32         if (parse_result.is_error) {
33             print_parse_error(stderr, buffer, parse_result);
34             continue;
35         }
36         printf("After parse:\t");
37         gc_inspect(gc);
38
39         struct EvalResult eval_result = eval(gc, scope, parse_result.expr);
40         scope = eval_result.scope;
41         if (eval_result.is_error) {
42             print_eval_error(stderr, eval_result);
43             continue;
44         }
45         printf("After eval:\t");
46         gc_inspect(gc);
47
48         gc_collect(gc, CONS(gc, scope, eval_result.expr));
49         printf("After collect:\t");
50         gc_inspect(gc);
51
52         printf("Scope:\t");
53         print_expr_as_sexpr(eval_result.scope);
54         printf("\n");
55
56         print_expr_as_sexpr(eval_result.expr);
57         printf("\n");
58     }
59
60     destroy_gc(gc);
61
62     return 0;
63 }