From 5b163b26dd2cec9f31b69fcb82f4b6519d097261 Mon Sep 17 00:00:00 2001 From: Jacob Date: Sun, 30 Mar 2025 16:09:09 -0400 Subject: [PATCH] Fixed static allocation of builting functions. Co-authored-by: bgiobbe-tcs --- src/exec.c | 25 ++++--------------------- src/include/builtin.h | 15 ++++++++++----- src/include/util.h | 9 ++++++--- test/Unity | 2 +- 4 files changed, 21 insertions(+), 30 deletions(-) diff --git a/src/exec.c b/src/exec.c index 8fd887b..1dfc890 100644 --- a/src/exec.c +++ b/src/exec.c @@ -14,33 +14,17 @@ extern AST* root; AST* exec_find(char* name); AST* exec_start(AST* ast) { + log_dbg("Started execution."); scope = stack_init(); HTab* global = htab_init(); - for (int i = 0; i < NBUILTIN_FNS; i++) + for (int i = 0; i < BUILTIN_FNS_LN; i++) htab_ins( - global, builtin_fns_names[i], - ast_init(AST_TYPE_BIF, ast_bif_data_init(builtin_fns[i])) + global, BUILTIN_FNS[i].name, + ast_init(AST_TYPE_BIF, ast_bif_data_init(BUILTIN_FNS[i].fn)) ); - /* - htab_ins( - global, "sum", ast_init(AST_TYPE_BIF, ast_bif_data_init(builtin_sum)) - ); - - htab_ins( - global, "sub", ast_init(AST_TYPE_BIF, ast_bif_data_init(builtin_sub)) - ); - - htab_ins( - global, "mul", ast_init(AST_TYPE_BIF, ast_bif_data_init(builtin_mul)) - ); - - htab_ins( - global, "div", ast_init(AST_TYPE_BIF, ast_bif_data_init(builtin_div)) - );*/ - // Push global namespace to `scope`. stack_push(scope, global); @@ -48,7 +32,6 @@ AST* exec_start(AST* ast) { } AST* exec_exp(AST* ast) { - log_dbg("Started execution."); switch (ast->type) { case AST_TYPE_BLOCK: return exec_block(ast); case AST_TYPE_CALL: return exec_call(ast); diff --git a/src/include/builtin.h b/src/include/builtin.h index 18158a6..6b2c90a 100644 --- a/src/include/builtin.h +++ b/src/include/builtin.h @@ -15,12 +15,17 @@ AST* builtin_mul(size_t argc, AST** argv); // Divide nums. AST* builtin_div(size_t argc, AST** argv); -#define NBUILTIN_FNS 4 -// The list of built-in functions. -static AST* (*builtin_fns[NBUILTIN_FNS])(size_t argc, AST** argv) = { - builtin_sum, builtin_sub, builtin_mul, builtin_div +struct builtin_data { + char* name; + AST* (*fn)(size_t argc, AST** argv); }; -static char* builtin_fns_names[NBUILTIN_FNS] = {"sum", "sub", "mul", "div"}; +static struct builtin_data BUILTIN_FNS[] = { + { "sum", builtin_sum }, + { "sub", builtin_sub }, + { "mul", builtin_mul }, + { "div", builtin_div }, +}; +#define BUILTIN_FNS_LN (arrln(BUILTIN_FNS)) #endif diff --git a/src/include/util.h b/src/include/util.h index 0a0549c..7265cf1 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -4,6 +4,12 @@ // Most of this file is cursed printing macros for `ast_print()`. Do not attempt // to comprehend. +// Allocate a pointer with a type. +#define talloc(T, X) T* X = malloc(sizeof(T)); + +// Get the length of an array. +#define arrln(A) (sizeof(A)/sizeof(*A)) + #ifdef DBG // Debug macros // Log a message. @@ -101,7 +107,4 @@ #define INDENT_FIELD_LIST_CLOSE \ printf(COL_BWHI "%s ]\n" COL_RESET, INDENT_spacing->buf); -// Allocate a pointer with a type. -#define talloc(T, X) T* X = malloc(sizeof(T)); - #endif diff --git a/test/Unity b/test/Unity index cdf1d02..73237c5 160000 --- a/test/Unity +++ b/test/Unity @@ -1 +1 @@ -Subproject commit cdf1d0297effc2736ee847e557b4275b4f02310b +Subproject commit 73237c5d224169c7b4d2ec8321f9ac92e8071708