diff --git a/src/process.c b/src/process.c index f48b372..eb29c37 100644 --- a/src/process.c +++ b/src/process.c @@ -5,12 +5,31 @@ #include "syntax.h" #include "process.h" -void process(tSyntaxElement* tree, tProcessingData* pdata) { +bool replace(tSyntaxElement* tree, tProcessingData* pdata) { tProcessingData* pd = pdata; - bool deffound = false; - if (pd == NULL) { - pd = calloc(1,sizeof(tProcessingData)); + bool didreplace = false; + while (1) { + if (pd->replacethis == NULL) + break; + if (tree->type == TOKEN) + if (strcmp(tree->content, pd->replacethis) == 0) { + didreplace = true; + free(tree->content); + tSyntaxElement* clone = se_clone_no_next(pd->replacewiththis, tree); + tree->content = clone->content; + tree->type = clone->type; + } + if (pd->prev != NULL) + pd = pd->prev; + else + break; } + return didreplace; +} + +bool process_find(tSyntaxElement* tree, tProcessingData** p_pdata) { + tProcessingData* pd = *p_pdata; + bool deffound = false; if (se_istraversable(tree)) if (((tSyntaxElement*)tree->content)->type == TOKEN && ((tSyntaxElement*)tree->content)->next != NULL) { tSyntaxElement* token = (tSyntaxElement*)tree->content; @@ -21,10 +40,20 @@ void process(tSyntaxElement* tree, tProcessingData* pdata) { npd->replacethis = replacethis; npd->replacewiththis = replacewiththis; npd->prev = pd; - pd = npd; + *p_pdata = npd; deffound = true; } } + return deffound; +} + +void process(tSyntaxElement* tree, tProcessingData* pdata) { + tProcessingData* pd = pdata; + bool deffound = false; + if (pd == NULL) { + pd = calloc(1,sizeof(tProcessingData)); + } + deffound = process_find(tree, &pd); if (tree->next != NULL) { process(tree->next, pd); } @@ -32,20 +61,6 @@ void process(tSyntaxElement* tree, tProcessingData* pdata) { if (se_istraversable(tree)) { process((tSyntaxElement*)tree->content, pd); } - while (1) { - if (pd->replacethis == NULL) - break; - if (tree->type == TOKEN) - if (strcmp(tree->content, pd->replacethis) == 0) { - free(tree->content); - tSyntaxElement* clone = se_clone_no_next(pd->replacewiththis, tree); - tree->content = clone->content; - tree->type = clone->type; - } - if (pd->prev != NULL) - pd = pd->prev; - else - break; - } + replace(tree, pd); } }