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