From 099fd363f7972ad7e1c48f5fd9ab83aa38a40ea2 Mon Sep 17 00:00:00 2001 From: c Date: Sat, 9 Mar 2024 11:01:23 -0500 Subject: Introduced mortality. --- src/doer.c | 9 +++++---- src/include/doer.h | 20 +++++++++++++++----- src/include/util.h | 9 +++++++++ src/main.c | 8 ++++++-- 4 files changed, 35 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/doer.c b/src/doer.c index 11aaeac..995fcc5 100644 --- a/src/doer.c +++ b/src/doer.c @@ -4,7 +4,6 @@ target_t* target_init(char* name, tree_t* tree) { target_t* target = emalloc(sizeof(target_t)); - target->name = name; target->tree = tree; target->nxt = NULL; return target; @@ -13,7 +12,6 @@ target_t* target_init(char* name, tree_t* tree) { void target_destroy(target_t* target) { if (target) { target_destroy(target->nxt); - free(target->name); free(target); } } @@ -51,6 +49,10 @@ void doer_add_target(doer_t* doer, target_t* target) { else doer->ltarget->nxt = target; } +void blin_die(doer_t* doer) { + DIE(":(\nYour PC ran into a\n"); +} + void blin_print(doer_t* doer) { printf( "%s", @@ -80,7 +82,6 @@ void doer_do_block(doer_t* doer) { void doer_do_expr(doer_t* doer) { switch (doer->tree->type) { case TREE_TYPE_CALL: - /* Assume only call is `print` for now. */ doer_do_call(doer); break; case TREE_TYPE_DEF: @@ -109,7 +110,7 @@ void doer_do_call(doer_t* doer) { for (int i = 0; i < sizeof(blinfs) / sizeof(blinf_t); i++) { if (!strcmp(blinfs[i].name, doer->tree->data.call.target)) { (blinfs[i].fp)(doer); - break; + return; } } } diff --git a/src/include/doer.h b/src/include/doer.h index afa80dd..429aa02 100644 --- a/src/include/doer.h +++ b/src/include/doer.h @@ -4,8 +4,9 @@ #include "util.h" #include "tree.h" +// Points to a part of the AST the doer wishes to remember. typedef struct TARGET { - char* name; // The name of the target (unique). +// char* name; // The name of the target (unique). tree_t* tree; // The tree to which the target refers. struct TARGET* nxt; // The next target in the list. } target_t; @@ -40,17 +41,25 @@ void doer_destroy(doer_t* doer); /* Built-in function. */ typedef struct BLINF { - void (*fp)(doer_t*); + void (*fp)(doer_t*); // The callback function in C. + tree_type_t returnt; // The return type of the function. + tree_type_t* argts; // Array of the arguments' types. char name[24]; } blinf_t; void doer_add_target(doer_t* doer, target_t* target); // Built-in functions. +// `die`: dies. Does not accept any arguments, returns int (if a tree falls in +// the forest, but it burns down before anyone can hear it, did it ever make a +// sound at all?) +void blin_die(doer_t* tree); // `print`: print a string. void blin_print(doer_t* tree); +static tree_type_t blin_print_args[] = { TREE_TYPE_LSTR }; // `printl`: print a string, and add a newline. void blin_printl(doer_t* tree); +static tree_type_t blin_printl_args[] = { TREE_TYPE_LSTR }; void doer_do_block(doer_t* tree); void doer_do_expr(doer_t* tree); @@ -62,9 +71,10 @@ void doer_do_carg(doer_t* tree); void doer_do_def(doer_t* tree); void doer_do_call(doer_t* tree); -const static blinf_t blinfs[] = { - { blin_print, "print" }, - { blin_printl, "printl" } +static blinf_t blinfs[] = { + { blin_die, TREE_TYPE_LINT, NULL, "die" }, + { blin_print, TREE_TYPE_LSTR, blin_print_args, "print" }, + { blin_printl, TREE_TYPE_LSTR, blin_printl_args, "printl" }, }; #endif diff --git a/src/include/util.h b/src/include/util.h index 08654fd..60db492 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -21,6 +21,8 @@ /* Call `f` on `x` if `x` exists. */ #define EDO(f, x) HIDE(if (x) {f(x);}) +#ifdef DBG + /* Log some debug information. */ #define LOG_DBGF(fmt, ...) HIDE( \ fprintf(stderr, "\x1b[37m[\x1b[95;1m==\x1b[0m\x1b[37m]\x1b[0m\x1b[35m "); \ @@ -36,6 +38,13 @@ fprintf(stderr, "\x1b[0m\n"); \ ) +#else // ifdef DBG + +#define LOG_DBGF(fmt, ...); +#define LOG_DBG(body); + +#endif // ifdef DBG + /* c: */ #define LOG_YAYF(fmt, ...) HIDE( \ fprintf(stderr, "\x1b[37m[\x1b[92;1m==\x1b[0m\x1b[37m]\x1b[32m "); \ diff --git a/src/main.c b/src/main.c index 2a1507e..38a4269 100644 --- a/src/main.c +++ b/src/main.c @@ -10,7 +10,7 @@ int main(int argc, char* argv[]) { /* Get source. */ src = source_get(argv[1]); - LOG_INFF("Source: %s", src); + LOG_DBGF("Source: %s", src); /* Create pre-processor. */ pp = pp_init(src); @@ -20,7 +20,7 @@ int main(int argc, char* argv[]) { pp_run(pp); free(src); src = pp->psrc; - LOG_INFF("pre-processed source: %s", pp->psrc); + LOG_DBGF("pre-processed source: %s", pp->psrc); /* destroy pre-processor */ pp_destroy(pp); LOG_DBG("preprocessor ran"); @@ -39,12 +39,16 @@ int main(int argc, char* argv[]) { /* Create the parser from the lexer's tokens. */ parser = parser_init(lexer->tokenl); parser_run(parser); +#ifdef DBG tree_print(parser->tree, 0); +#endif doer_t* doer = doer_init(parser->tree); doer_do_block(doer); target_print(doer->targets); + sudden_death: + /* Clean up. */ doer_destroy(doer); token_destroy(lexer->tokenl); -- cgit v1.2.3