From fba0e505aaa067d3e19849536aa0142ab6fe187d Mon Sep 17 00:00:00 2001 From: Flavian Kaufmann Date: Thu, 22 May 2025 10:27:36 +0200 Subject: print proc table --- src/interpreter.c | 24 ++++++++++++++++++++++++ src/repl.c | 3 +++ 2 files changed, 27 insertions(+) diff --git a/src/interpreter.c b/src/interpreter.c index 2f13ec0..8bcc6f4 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -73,6 +73,30 @@ void context_print_var_table(Context *context) { hashmap_keys_iter_free(iter); } +void context_print_proc_table(Context *context) { + hashmap_keys_iter_t iter = hashmap_keys_iter_create(context->proc_table); + const char *key; + while ((key = hashmap_keys_iter_next(iter)) != NULL) { + ASTNode *procdecl = context_get_proc(context, key); + printf("%s(", key); + ASTNodeList *args = procdecl->u.d_procdecl.args; + while (args) { + printf("%s", args->node->u.d_var.name); + args = args->next; + if (args) printf(", "); + } + printf("; "); + ASTNodeList *vargs = procdecl->u.d_procdecl.vargs; + while (vargs) { + printf("%s", vargs->node->u.d_var.name); + vargs = vargs->next; + if (vargs) printf(", "); + } + } + printf(")\n"); + hashmap_keys_iter_free(iter); +} + static int eval_aexpr(Context *context, ASTNode *node) { switch (node->type) { case NT_INT: return node->u.d_int.val; diff --git a/src/repl.c b/src/repl.c index 7c285b5..d61ccd4 100644 --- a/src/repl.c +++ b/src/repl.c @@ -18,6 +18,7 @@ static void print_help(void) { " %%run interpret program\n" " %%set set variable\n" " %%print [] print variable, or all variables\n" + " %%proc print declared procedures\n" " %%help show this message\n"); } @@ -43,6 +44,8 @@ static void repl_exec_command(context_t context, char *command) { char *var = strtok(NULL, " \t"); if (var) printf("%s = %d\n", var, context_get_var(context, var)); else context_print_var_table(context); + } else if (strcmp(cmd, "%proc") == 0) { + context_print_proc_table(context); } else { fprintf(stderr, "Unknown command: %s\n", cmd); } -- cgit v1.2.3