Fixed static allocation of builting functions.

Co-authored-by: bgiobbe-tcs <bgiobbe.tcs@gmail.com>
This commit is contained in:
Jacob Signorovitch 2025-03-30 16:09:09 -04:00
parent f8ee7cc66e
commit 5b163b26dd
4 changed files with 21 additions and 30 deletions

View File

@ -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);

View File

@ -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

View File

@ -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

@ -1 +1 @@
Subproject commit cdf1d0297effc2736ee847e557b4275b4f02310b
Subproject commit 73237c5d224169c7b4d2ec8321f9ac92e8071708