From 96c63e2fc4d70f20c8e2bac79e3547dfd6a5fcb8 Mon Sep 17 00:00:00 2001 From: c Date: Sat, 2 Mar 2024 11:00:37 -0500 Subject: Doer is. --- src/doer.c | 70 ++++++++++++++++++++++++++++-------------------------- src/include/doer.h | 35 +++++++++++++++++---------- src/include/main.h | 1 + src/main.c | 7 ++++-- src/parser.c | 2 -- 5 files changed, 65 insertions(+), 50 deletions(-) (limited to 'src') diff --git a/src/doer.c b/src/doer.c index 21a1045..f446011 100644 --- a/src/doer.c +++ b/src/doer.c @@ -1,5 +1,6 @@ #include "include/doer.h" - +#include "include/tree.h" +#include "include/util.h" /* Creates a new doer from a tree. */ doer_t* doer_init(tree_t* tree) { @@ -20,47 +21,48 @@ void doer_destroy(doer_t* doer) { free(doer); } - -void doer_do_blin_print(char* s) { - +void doer_do_blin_print(doer_t* doer) { + printf( + "%s", + doer->tree->data.call.arg->data.carg.val->data.lstr.val + ); } -void doer_do_block(tree_t* tree) { - tree->data.block.val +void doer_do_blin_printl(doer_t* doer) { + printf( + "%s\n", + doer->tree->data.call.arg->data.carg.val->data.lstr.val + ); } -void doer_do_expr(tree_t* tree) { - /* For now, assume the only expression that exists is a call to print(). */ - +void doer_do_block(doer_t* doer) { + if (!doer->tree) return; + tree_t* tree_root = doer->tree; + doer->tree = doer->tree->data.block.val; + doer_do_expr(doer); + doer->tree = tree_root->data.block.nxt; + doer_do_block(doer); } -void doer_do_lint(tree_t* tree) { - -} - -void doer_do_lstr(tree_t* tree) { - -} - -void doer_do_tag(tree_t* tree) { - -} - -void doer_do_darg(tree_t* tree) { - +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; + default: + LOG_WARF("unknown tree type: %d", doer->tree->type); + } } -void doer_do_carg(tree_t* tree) { - +void doer_do_call(doer_t* doer) { + /* Search through built-in functions first. */ + 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; + } + } } - -void doer_do_def(tree_t* tree) { - -} - -void doer_do_call(tree_t* tree) { - -} - diff --git a/src/include/doer.h b/src/include/doer.h index a672f88..8f51266 100644 --- a/src/include/doer.h +++ b/src/include/doer.h @@ -4,7 +4,6 @@ #include "util.h" #include "tree.h" -/* Doer? I 'ardly know 'er! */ typedef struct DOER { tree_t* tree; } doer_t; @@ -18,16 +17,28 @@ doer_t* doer_init(tree_t* tree); */ void doer_destroy(doer_t* doer); -void doer_do_blin_print(char* s); - -void doer_do_block(tree_t* tree); -void doer_do_expr(tree_t* tree); -void doer_do_lint(tree_t* tree); -void doer_do_lstr(tree_t* tree); -void doer_do_tag(tree_t* tree); -void doer_do_darg(tree_t* tree); -void doer_do_carg(tree_t* tree); -void doer_do_def(tree_t* tree); -void doer_do_call(tree_t* tree); +/* Built-in function. */ +typedef struct BLINF { + void (*fp)(doer_t*); + char name[24]; +} blinf_t; + +void doer_do_blin_print(doer_t* tree); // Built-in function `print`. +void doer_do_blin_printl(doer_t* tree); // Built-in function `print`. + +void doer_do_block(doer_t* tree); +void doer_do_expr(doer_t* tree); +void doer_do_lint(doer_t* tree); +void doer_do_lstr(doer_t* tree); +void doer_do_tag(doer_t* tree); +void doer_do_darg(doer_t* tree); +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[] = { + { doer_do_blin_print, "print" }, + { doer_do_blin_printl, "printl" } +}; #endif diff --git a/src/include/main.h b/src/include/main.h index 63dd15c..d4f98dd 100644 --- a/src/include/main.h +++ b/src/include/main.h @@ -10,5 +10,6 @@ #include "pp.h" #include "lexer.h" #include "parser.h" +#include "doer.h" #endif diff --git a/src/main.c b/src/main.c index 2537233..e7d345d 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,4 @@ #include "include/main.h" -#include "include/util.h" /* FIXME: Segfaults ~30% of the time. No idea why. Thx future self <3. */ int main(int argc, char* argv[]) { @@ -41,7 +40,11 @@ int main(int argc, char* argv[]) { parser_run(parser); tree_print(parser->tree, 0); - /* Clean up parser stuff. */ + doer_t* doer = doer_init(parser->tree); + doer_do_block(doer); + + /* Clean up. */ + doer_destroy(doer); token_destroy(lexer->tokenl); lexer_destroy(lexer); tree_destroy(parser->tree); diff --git a/src/parser.c b/src/parser.c index 3dc26a1..2dcf96b 100644 --- a/src/parser.c +++ b/src/parser.c @@ -16,7 +16,6 @@ void parser_destroy(parser_t* parser) { if (parser) { free(parser); } } -/* TODO: What if the program begins with a ";"? */ int parser_nxt_token(parser_t* parser) { /* Preserve original token list, to be cleaned up by lexer. */ if (!parser->token) { return 0; } @@ -100,7 +99,6 @@ tree_t* parser_parse_def(parser_t* parser) { def->data.def.arg = NULL; } - LOG_DBGF("%s <<<<<<<<<<<<,, HERE HER HERE", parser->token->val); if (parser->token->type == TOKEN_TYPE_SET) { parser_nxt_token(parser); /* Skip over set. */ def->data.def.val = parser_parse_expr(parser); -- cgit v1.2.3