From 8848b46c2873faa019f96b0194237745a80f78f2 Mon Sep 17 00:00:00 2001 From: c Date: Sat, 16 Mar 2024 10:01:31 -0400 Subject: Can now print first defined string variable. --- src/doer.c | 43 ++++++++++++++++++++++++++++++++++++++----- src/include/doer.h | 7 +++++-- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/doer.c b/src/doer.c index 995fcc5..c2db0cc 100644 --- a/src/doer.c +++ b/src/doer.c @@ -18,7 +18,7 @@ void target_destroy(target_t* target) { void target_print(target_t* target) { if (!target) return; - LOG_DBGF("target: %s; points to tree: %p", target->name, target->tree); + LOG_DBGF("target points to tree: %p", target->tree); target_print(target->nxt); } @@ -40,6 +40,7 @@ doer_t* doer_init(tree_t* tree) { - Does not destroy the `tree`. */ void doer_destroy(doer_t* doer) { + target_destroy(doer->targets); free(doer); } @@ -49,25 +50,54 @@ void doer_add_target(doer_t* doer, target_t* target) { else doer->ltarget->nxt = target; } +/* + + :str:var = "Hello" + printl.var + +*/ + +char* doer_eval_str(doer_t* doer) { + // Assume tree type is a simple call to variable. + // Search through target list for matching variable. + // Return its value as a string (assume it's a string). + // Assume there is only 1 target in the target list. + + switch (doer->tree->type) { + case TREE_TYPE_CALL: + // Assume there is only 1 target in the target list. + doer->tree = doer->ltarget->tree->data.def.val; + return doer_eval_str(doer); + case TREE_TYPE_LSTR: + // Already done \o/ + return doer->tree->data.lstr.val; + default: + DIE("Wrong type, FOOL!"); + } +} + void blin_die(doer_t* doer) { DIE(":(\nYour PC ran into a\n"); } void blin_print(doer_t* doer) { + doer->tree = doer->tree->data.call.arg->data.carg.val; + printf( "%s", - doer->tree->data.call.arg->data.carg.val->data.lstr.val + doer_eval_str(doer) ); } void blin_printl(doer_t* doer) { + doer->tree = doer->tree->data.call.arg->data.carg.val; + printf( "%s\n", - doer->tree->data.call.arg->data.carg.val->data.lstr.val + doer_eval_str(doer) ); } - void doer_do_block(doer_t* doer) { if (!doer->tree) return; @@ -106,11 +136,14 @@ void doer_do_def(doer_t* doer) { } void doer_do_call(doer_t* doer) { - /* Search through built-in functions first. */ + // 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); return; } } + + // Search through targets next. + LOG_DBGF("got %s", doer->ltarget->tree->data.def.tag->data.tag.nxt->data.tag.val); } diff --git a/src/include/doer.h b/src/include/doer.h index 429aa02..00c3c19 100644 --- a/src/include/doer.h +++ b/src/include/doer.h @@ -6,7 +6,6 @@ // Points to a part of the AST the doer wishes to remember. typedef struct TARGET { -// 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; @@ -49,10 +48,14 @@ typedef struct BLINF { void doer_add_target(doer_t* doer, target_t* target); + +// Given a tree, evaluate it to a string (or type error). +char* doer_eval_str(doer_t* doer); + // 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?) +// sound at all?) TODO: Make this actually clean up afterwards. void blin_die(doer_t* tree); // `print`: print a string. void blin_print(doer_t* tree); -- cgit v1.2.3