5 #include "interpreter.h"
9 #define REPL_BUFFER_MAX 1024
11 static struct EvalResult quit(void *param, Gc *gc, struct Scope *scope, struct Expr args)
19 return eval_success(NIL(gc));
22 static void eval_line(Gc *gc, Scope *scope, const char *line)
24 /* TODO: there is no way to disable REPL debug output */
25 const char *read_iter = line;
26 while (*read_iter != 0) {
27 printf("Before parse:\t");
30 struct ParseResult parse_result = read_expr_from_string(gc, read_iter);
31 if (parse_result.is_error) {
32 print_parse_error(stderr, line, parse_result);
35 printf("After parse:\t");
38 struct EvalResult eval_result = eval(gc, scope, parse_result.expr);
39 printf("After eval:\t");
42 gc_collect(gc, CONS(gc, scope->expr, eval_result.expr));
43 printf("After collect:\t");
47 print_expr_as_sexpr(stdout, scope->expr);
50 if (eval_result.is_error) {
51 fprintf(stderr, "Error:\t");
52 print_expr_as_sexpr(stderr, eval_result.expr);
53 fprintf(stderr, "\n");
57 print_expr_as_sexpr(stderr, eval_result.expr);
58 fprintf(stdout, "\n");
60 read_iter = next_token(parse_result.end).begin;
64 int main(int argc, char *argv[])
69 char buffer[REPL_BUFFER_MAX + 1];
72 struct Scope scope = {
73 .expr = CONS(gc, NIL(gc), NIL(gc))
76 set_scope_value(gc, &scope, SYMBOL(gc, "quit"), NATIVE(gc, quit, NULL));
81 if (fgets(buffer, REPL_BUFFER_MAX, stdin) == NULL) {
85 eval_line(gc, &scope, buffer);