A bit of refactoring
This commit is contained in:
parent
743bda0437
commit
eedecc7b51
@ -5,38 +5,15 @@
|
||||
#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));
|
||||
}
|
||||
if (se_istraversable(tree))
|
||||
if (((tSyntaxElement*)tree->content)->type == TOKEN && ((tSyntaxElement*)tree->content)->next != NULL) {
|
||||
tSyntaxElement* token = (tSyntaxElement*)tree->content;
|
||||
if (strcmp((char*)token->content,"def") == 0) {
|
||||
char* replacethis = token->next->content;
|
||||
tSyntaxElement* replacewiththis = token->next->next;
|
||||
tProcessingData* npd = malloc(sizeof(tSyntaxElement));
|
||||
npd->replacethis = replacethis;
|
||||
npd->replacewiththis = replacewiththis;
|
||||
npd->prev = pd;
|
||||
pd = npd;
|
||||
deffound = true;
|
||||
}
|
||||
}
|
||||
if (tree->next != NULL) {
|
||||
process(tree->next, pd);
|
||||
}
|
||||
if (!deffound) {
|
||||
if (se_istraversable(tree)) {
|
||||
process((tSyntaxElement*)tree->content, pd);
|
||||
}
|
||||
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;
|
||||
@ -47,5 +24,43 @@ void process(tSyntaxElement* tree, tProcessingData* pdata) {
|
||||
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;
|
||||
if (strcmp((char*)token->content,"def") == 0) {
|
||||
char* replacethis = token->next->content;
|
||||
tSyntaxElement* replacewiththis = token->next->next;
|
||||
tProcessingData* npd = malloc(sizeof(tSyntaxElement));
|
||||
npd->replacethis = replacethis;
|
||||
npd->replacewiththis = replacewiththis;
|
||||
npd->prev = pd;
|
||||
*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);
|
||||
}
|
||||
if (!deffound) {
|
||||
if (se_istraversable(tree)) {
|
||||
process((tSyntaxElement*)tree->content, pd);
|
||||
}
|
||||
replace(tree, pd);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user