]> git.lizzy.rs Git - nothing.git/blob - src/script/repl.c
(#311) Add lisp repl executable
[nothing.git] / src / script / repl.c
1 #include <stdbool.h>
2
3 #include "parser.h"
4 #include "interpreter.h"
5 #include "scope.h"
6
7 #define REPL_BUFFER_MAX 1024
8
9 char buffer[REPL_BUFFER_MAX + 1];
10
11 int main(int argc, char *argv[])
12 {
13     (void) argc;
14     (void) argv;
15
16     /* TODO: memory leak */
17     while (true) {
18         printf("> ");
19
20         if (fgets(buffer, REPL_BUFFER_MAX, stdin) == NULL) {
21             return -1;
22         }
23
24         struct ParseResult parse_result = read_expr_from_string(buffer);
25         if (parse_result.is_error) {
26             print_parse_error(stderr, buffer, parse_result);
27             continue;
28         }
29
30         struct EvalResult eval_result = eval(empty_scope(), parse_result.expr);
31         if (eval_result.is_error) {
32             print_eval_error(stderr, eval_result);
33             continue;
34         }
35
36         print_expr_as_sexpr(eval_result.expr);
37     }
38
39     return 0;
40 }