From b8bad3bf5af3261f25780a8cd8b90a659fe29bab Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Wed, 13 Oct 2021 14:19:32 +0200 Subject: Add support for environments and recursive evaluation --- src/bootstrap/main.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'src/bootstrap/main.c') diff --git a/src/bootstrap/main.c b/src/bootstrap/main.c index e6155b4..2aa3038 100755 --- a/src/bootstrap/main.c +++ b/src/bootstrap/main.c @@ -12,6 +12,8 @@ #include "objects.c" #include "parser.c" +#define MAKE_SYM(STR) make_symbol((StringView){(STR), sizeof(STR) - 1}) + void init(void) { // Initialize singletons. @@ -19,6 +21,11 @@ init(void) { obj_true = alloc_object(OBJ_TYPE_BOOL); obj_false = alloc_object(OBJ_TYPE_BOOL); obj_err = alloc_object(OBJ_TYPE_ERR); + + // Global environment. + global_env = env_create(NULL); + env_add_symbol(global_env, MAKE_SYM("quote"), make_procedure(proc_quote)); + env_add_symbol(global_env, MAKE_SYM("+"), make_procedure(proc_sum)); } void @@ -41,8 +48,15 @@ process_source(const StringView *source) { free_objects(root); break; } + + // FIXME: Not freeing result or intermediate objects, can leak memory. + Object *result = eval(global_env, root); + printf("AST: "); display(root); printf("\n"); + printf("EVAL: "); + display(result); + printf("\n"); free_objects(root); } @@ -116,9 +130,9 @@ run_file(char *file_name) { Error err = errors[i]; fprintf(stderr, "%s", file_name); if (err.line != 0) { - fprintf(stderr, ":%ld:%ld: ", err.line, err.col); + fprintf(stderr, ":%ld:%ld", err.line, err.col); } - fprintf(stderr, "%s\n", error_msgs[err.value]); + fprintf(stderr, ": %s\n", error_msgs[err.value]); } errors_n = 0; } @@ -158,9 +172,9 @@ run_stdin(void) { Error err = errors[i]; fprintf(stderr, "stdin"); if (err.line != 0) { - fprintf(stderr, ":%ld:%ld: ", err.line, err.col); + fprintf(stderr, ":%ld:%ld", err.line, err.col); } - fprintf(stderr, "%s\n", error_msgs[err.value]); + fprintf(stderr, ": %s\n", error_msgs[err.value]); } errors_n = 0; } -- cgit v1.2.1