From 619df7cb45a063d7a003317d519595d2394f1fdb Mon Sep 17 00:00:00 2001 From: c Date: Sat, 25 May 2024 11:33:47 -0400 Subject: h --- src/doer.c | 71 ++++++++++++++++++++++++++++++++++++++++---------------------- src/main.c | 42 +------------------------------------ src/tree.c | 2 +- 3 files changed, 48 insertions(+), 67 deletions(-) diff --git a/src/doer.c b/src/doer.c index 14d7d02..002b13c 100644 --- a/src/doer.c +++ b/src/doer.c @@ -100,9 +100,7 @@ tree_t* doer_find_target_from_call(doer_t* doer) { } } - for (target_t* t = doer->targets; t; printf("%s\n", t->tree->data.def.tag->data.tag.nxt->data.tag.val) && (t = t->nxt)); - - DIEF("Call to missing target: %s", call_name); + return NULL; } tree_t* doer_eval_prim(doer_t* doer) { @@ -153,7 +151,7 @@ int doer_eval_lint(doer_t* doer) { // Already done \o/. return doer->tree->data.lint.val; default: - DIE("Wrong type, FOOL!"); + DIEF("Wrong type, FOOL! Found a %d.", doer->tree->type); } } @@ -363,28 +361,31 @@ void doer_do_call(doer_t* doer) { tree_t* doer_do_call_target(doer_t* doer) { tree_t* the_call = doer->tree; + + LOG_DBGF("%s", the_call->data.call.target); + tree_t* target_def = doer_find_target_from_call(doer); // Is def. + if (!target_def) { + DIEF("Call to missing target: %s", the_call->data.call.target); + } + tree_t* resolution; + tree_cpy(&resolution, target_def); + resolution->parent = the_call->parent; + tree_t* darg = target_def->data.def.arg; tree_t* carg = the_call->data.call.arg; - LOG_DBG("DARG:\n"); - tree_print(darg, 0); - - LOG_DBG("CARG:\n"); - tree_print(carg, 0); - - target_t - * targs = NULL, // Pointer to first target. - * targ = targs; // Pointer to where new targets will be added. + target_t* targs = NULL; // Pointer to first target. + target_t** targp = &targs; // Pointer to where new targets will be added. + target_t* newt = NULL; // Evaluate arguments. while (darg) { doer->tree = carg->data.carg.val; - // HACK: Assume already a primitive. - tree_t* carg_resolved = doer->tree; //doer_eval_prim(doer); + tree_t* carg_resolved = doer_eval_prim(doer); char * darg_type = darg->data.darg.tag->data.tag.val, @@ -392,46 +393,66 @@ tree_t* doer_do_call_target(doer_t* doer) { if (strcmp(darg_type, carg_type)) DIE("Type mismatch."); + // Create def to put in target. tree_t* carg_tree_def = tree_init(TREE_TYPE_DEF, NULL); tree_t* carg_tree_def_arg1 = carg_tree_def->data.def.tag = tree_init(TREE_TYPE_TAG, carg_tree_def); // Type. carg_tree_def_arg1->data.tag.val = darg_type; // Get type of the definition arg. tree_t* carg_tree_def_arg2 = carg_tree_def_arg1->data.tag.nxt = tree_init(TREE_TYPE_TAG, carg_tree_def_arg1); // Name. carg_tree_def_arg2->data.tag.val = darg->data.darg.tag->data.tag.nxt->data.tag.val; // Get the name of the definition arg. - targ = target_init( + carg_tree_def->data.def.val = carg_resolved; + + carg_tree_def->data.def.arg = NULL; + + newt = target_init( "This field doesn't actually do anything but I can't be bothered to remove it :P", carg_tree_def ); - targ = targ->nxt; + *targp = newt; + targp = &newt->nxt; + darg = darg->data.darg.nxt; - printf("\n\n[---\n"); - for (target_t* t = doer->targets; t; printf("%s\n", t->tree->data.def.tag->data.tag.nxt->data.tag.val) && (t = t->nxt)); - printf("---]\n"); + /* printf("["); + for ( + target_t* t = targs; + t; + printf("%s, ", t->tree->data.def.tag->data.tag.nxt->data.tag.val) + && (t = t->nxt) + ); + printf("]\n"); + */ } target_t* old_ltarget = doer->ltarget; // Append the new targets from the args. doer->ltarget->nxt = targs; - doer->ltarget = targ; + doer->ltarget = newt; // Execute the call. doer->tree = target_def->data.def.val; - printf("[---\n"); - for (target_t* t = doer->targets; t; printf("%s\n", t->tree->data.def.tag->data.tag.nxt->data.tag.val) && (t = t->nxt)); - printf("---]\n"); + + /* + printf("["); + for (target_t* t = doer->targets; t; printf("%s, ", t->tree->data.def.tag->data.tag.nxt->data.tag.val) && (t = t->nxt)); + printf("]\n"); + */ + + doer->tree = resolution; + tree_t* r = doer_eval_prim(doer); // Clean up temporary targets. target_destroy(targs); old_ltarget->nxt = NULL; doer->ltarget = old_ltarget; - return doer_eval_prim(doer); + return r; } tree_t* doer_do_call_blin(doer_t* doer) { + LOG_DBGF("%s", doer->tree->data.call.target); for (int i = 0; i < sizeof(blinfs) / sizeof(blinf_t); i++) if (!strcmp(blinfs[i].name, doer->tree->data.call.target)) return (blinfs[i].fp)(doer); diff --git a/src/main.c b/src/main.c index 20eb42e..9b539f4 100644 --- a/src/main.c +++ b/src/main.c @@ -1,47 +1,7 @@ #include "include/main.h" #include "include/doer.h" -#include "../test/include/test.h" - -void test_tree_cpy() { - tree_t* tree_block = tree_init(TREE_TYPE_BLOCK, NULL); - tree_t* tree_lstr = tree_block->data.block.val = tree_init(TREE_TYPE_LSTR, tree_block); - char* char_lstr = tree_lstr->data.lstr.val = calloc(12, sizeof(char)); - strcpy(char_lstr, "hello world"); - tree_lstr->data.lstr.len = 11; - tree_t* tree_block2 = tree_block->data.block.nxt = tree_init(TREE_TYPE_BLOCK, tree_block); - tree_t* tree_def = tree_block2->data.block.val = tree_init(TREE_TYPE_DEF, tree_block2); - tree_t* tree_tag = tree_def->data.def.tag = tree_init(TREE_TYPE_TAG, tree_def); - char* char_tag = tree_tag->data.tag.val = calloc(4, sizeof(char)); - strcpy(char_tag, "int"); - tree_tag->data.tag.nxt = NULL; - tree_t* tree_darg = tree_def->data.def.arg = tree_init(TREE_TYPE_DARG, tree_def); - tree_t* tree_tag2 = tree_darg->data.darg.tag = tree_init(TREE_TYPE_TAG, tree_darg); - char* char_tag2 = tree_tag2->data.tag.val = calloc(4, sizeof(char)); - strcpy(char_tag2, "int"); - tree_tag2->data.tag.nxt = NULL; - tree_darg->data.darg.nxt = NULL; - tree_t* tree_lint = tree_def->data.def.val = tree_init(TREE_TYPE_LINT, tree_def); - tree_lint->data.lint.val = 42; - tree_block2->data.block.nxt = NULL; - - tree_t* t2 = malloc(sizeof(tree_t)); - - tree_cpy(&t2, tree_block); - - printf(tree_cmp(tree_block, t2)?"yes":"no"); - - tree_print(tree_block, 0); - tree_print(t2, 0); - - - printf(&t2 != &tree_block ?"yes":"no"); -} - -/* FIXME: Segfaults ~30% of the time. No idea why. Thx future self <3. */ int main(int argc, char* argv[]) { - test_tree_cpy(); - return 0; char* src; pp_t* pp; lexer_t* lexer; @@ -90,7 +50,7 @@ int main(int argc, char* argv[]) { doer_destroy(doer); token_destroy(lexer->tokenl); lexer_destroy(lexer); - tree_destroy(parser->tree); + // tree_destroy(parser->tree); parser_destroy(parser); free(src); diff --git a/src/tree.c b/src/tree.c index beef0b7..edfe9a4 100644 --- a/src/tree.c +++ b/src/tree.c @@ -116,7 +116,7 @@ void tree_destroy(tree_t* tree) { void tree_cpy(tree_t** dest, tree_t* src) { if (!src) { *dest = NULL; return; } - *dest = malloc(sizeof(tree_t)); + *dest = tree_init(src->type, src->parent); memcpy(&(*dest)->data, &src->data, sizeof(union TREE_DATA)); -- cgit v1.2.3