]> git.lizzy.rs Git - nothing.git/blob - src/script/repl.c
Merge pull request #313 from tsoding/311
[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     while (true) {
17         printf("> ");
18
19         if (fgets(buffer, REPL_BUFFER_MAX, stdin) == NULL) {
20             return -1;
21         }
22
23         struct ParseResult parse_result = read_expr_from_string(buffer);
24         if (parse_result.is_error) {
25             print_parse_error(stderr, buffer, parse_result);
26             continue;
27         }
28
29         struct EvalResult eval_result = eval(empty_scope(), parse_result.expr);
30         if (eval_result.is_error) {
31             print_eval_error(stderr, eval_result);
32             destroy_expr(parse_result.expr);
33             destroy_expr(eval_result.expr);
34             continue;
35         }
36
37         print_expr_as_sexpr(eval_result.expr);
38         destroy_expr(parse_result.expr);
39         destroy_expr(eval_result.expr);
40     }
41
42     return 0;
43 }