Compare commits
32
Commits
00afc5158f
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9bb5280c44 | ||
|
|
cfea07531d | ||
|
|
9fdfe76804 | ||
|
|
635747af1d | ||
|
|
d6af70056a | ||
|
|
9addce5f30 | ||
|
|
07402a6b42 | ||
|
|
9b605597b2 | ||
|
|
8eff72ee53 | ||
|
|
b98579699d | ||
|
|
74d20d9f37 | ||
|
|
13bb7399aa | ||
|
|
5edcf05d7a | ||
|
|
74bf888a84 | ||
|
|
099bb1d600 | ||
|
|
d30a715d06 | ||
|
|
c8189f5ec1 | ||
|
|
c543c00a9c | ||
|
|
b762ad154e | ||
|
|
a8e4bfd264 | ||
|
|
834ec9d3d4 | ||
|
|
e6027bbaf2 | ||
|
|
3aa280ba38 | ||
|
|
898fa9b195 | ||
|
|
71ee935f7b | ||
|
|
1c9de73b91 | ||
|
|
5cc866637e | ||
|
|
ab7f648e56 | ||
|
|
57af99e53a | ||
|
|
d5ccf1c0be | ||
|
|
a1d14e8cfa | ||
|
|
8bf0af96ee |
+375
-47
@@ -13,6 +13,36 @@ void string_append(char** string, char* append) {
|
||||
|
||||
}
|
||||
|
||||
void string_append_char(char** string, char append) {
|
||||
|
||||
int len = strlen(*string);
|
||||
|
||||
*string = realloc(*string, len + 2 * sizeof(char));
|
||||
string[0][len] = append;
|
||||
string[0][len + 1] = '\0';
|
||||
}
|
||||
|
||||
char* string_unescape(char* string) {
|
||||
char* tmp = calloc(1,sizeof(char));
|
||||
|
||||
size_t len = strlen(string);
|
||||
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
|
||||
if (string[i] == '\\')
|
||||
i++;
|
||||
string_append_char(&tmp, string[i]);
|
||||
|
||||
}
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
void string_append_free(char** string, char* append) {
|
||||
string_append(string, append);
|
||||
free(append);
|
||||
}
|
||||
|
||||
void compile_enter_tag(char** string) {
|
||||
|
||||
int len = strlen(*string);
|
||||
@@ -58,6 +88,7 @@ bool compile_isop(char* string) {
|
||||
else if (strcmp(string, "and") == 0) return true;
|
||||
else if (strcmp(string, "or") == 0) return true;
|
||||
else if (strcmp(string, "|") == 0) return true;
|
||||
else if (strcmp(string, ":?") == 0) return true;
|
||||
|
||||
else return false;
|
||||
|
||||
@@ -91,7 +122,7 @@ void compile_genexec(char** result, tSyntaxElement* se){
|
||||
|
||||
if (token != NULL) {
|
||||
|
||||
string_append(result,compile_expression(token));
|
||||
string_append_free(result,compile_expression(token));
|
||||
|
||||
if (token->next != NULL)
|
||||
string_append(result,",");
|
||||
@@ -105,6 +136,37 @@ void compile_genexec(char** result, tSyntaxElement* se){
|
||||
string_append(result,")");
|
||||
}
|
||||
|
||||
char* compile_gensplit(tSyntaxElement* se, char* operator, bool dosplit) {
|
||||
tSyntaxElement* token = se;
|
||||
char* result = malloc(sizeof(char));
|
||||
result[0] = '\0';
|
||||
while (1) {
|
||||
|
||||
if (token->next != NULL) {
|
||||
|
||||
if (token->next->type == TOKEN)
|
||||
string_append(&result,token->next->content.string);
|
||||
else
|
||||
string_append_free(&result,compile_expression_wrapped(token->next));
|
||||
|
||||
if (token->next->next != NULL) {
|
||||
|
||||
if (dosplit)
|
||||
string_append(&result," ");
|
||||
string_append(&result,operator);
|
||||
if (dosplit)
|
||||
string_append(&result," ");
|
||||
|
||||
}
|
||||
|
||||
token = token->next;
|
||||
|
||||
} else break;
|
||||
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
char* compile_expression(tSyntaxElement* syntaxelement) {
|
||||
|
||||
char* result = calloc(1,sizeof(char));
|
||||
@@ -138,38 +200,113 @@ char* compile_expression(tSyntaxElement* syntaxelement) {
|
||||
|
||||
char* operator = token->content.string;
|
||||
|
||||
while (1) {
|
||||
|
||||
if (token->next != NULL) {
|
||||
|
||||
if (token->next->type == TOKEN)
|
||||
string_append(&result,token->next->content.string);
|
||||
else
|
||||
string_append(&result,compile_expression_wrapped(token->next));
|
||||
|
||||
if (token->next->next != NULL)
|
||||
string_append(&result,operator);
|
||||
|
||||
token = token->next;
|
||||
|
||||
} else break;
|
||||
|
||||
}
|
||||
string_append_free(&result, compile_gensplit(token, operator, !!strcmp(".", token->content.string)));
|
||||
|
||||
} else {
|
||||
|
||||
if (strcmp(token->content.string, "if") == 0) {
|
||||
|
||||
string_append(&result,compile_expression_wrapped(token->next));
|
||||
string_append_free(&result,compile_expression_wrapped(token->next));
|
||||
string_append(&result,"?");
|
||||
|
||||
string_append(&result,compile_expression_wrapped(token->next->next));
|
||||
string_append_free(&result,compile_expression_wrapped(token->next->next));
|
||||
string_append(&result,":");
|
||||
|
||||
string_append(&result,compile_expression_wrapped(token->next->next->next));
|
||||
string_append_free(&result,compile_expression_wrapped(token->next->next->next));
|
||||
|
||||
} else if (strcmp(token->content.string, "get") == 0) {
|
||||
|
||||
string_append_free(&result,compile_expression(token->next));
|
||||
string_append(&result,"[");
|
||||
string_append_free(&result,compile_expression(token->next->next));
|
||||
string_append(&result,"]");
|
||||
|
||||
} else if (strcmp(token->content.string, "neg") == 0) {
|
||||
|
||||
string_append(&result,"-");
|
||||
string_append_free(&result,compile_expression_wrapped(token->next));
|
||||
|
||||
} else if (strcmp(token->content.string, "not") == 0) {
|
||||
|
||||
string_append(&result,"not");
|
||||
string_append_free(&result,compile_expression_wrapped(token->next));
|
||||
|
||||
} else if (strcmp(token->content.string, "list") == 0) {
|
||||
|
||||
string_append(&result,"[");
|
||||
string_append_free(&result,compile_gensplit(token, ",", true));
|
||||
string_append(&result,"]");
|
||||
|
||||
} else if (strcmp(token->content.string, "range") == 0) {
|
||||
|
||||
string_append(&result,"[");
|
||||
string_append_free(&result,compile_gensplit(token, "..", true));
|
||||
string_append(&result,"]");
|
||||
|
||||
} else if (strcmp(token->content.string, "map") == 0) {
|
||||
|
||||
string_append(&result,"{");
|
||||
|
||||
for (tSyntaxElement* i = token->next; i != NULL; i = i->next) {
|
||||
|
||||
string_append_free(&result, compile_expression(i->content.syntax));
|
||||
string_append(&result, ":");
|
||||
string_append_free(&result, compile_expression(i->content.syntax->next));
|
||||
|
||||
if (i->next != NULL)
|
||||
string_append(&result,",");
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
string_append(&result,"}");
|
||||
|
||||
} else if (strcmp(token->content.string, "isnull") == 0) {
|
||||
|
||||
string_append_free(&result, compile_expression_wrapped(token->next));
|
||||
string_append(&result, " is null");
|
||||
|
||||
} else if (strcmp(token->content.string, "notnull") == 0) {
|
||||
|
||||
string_append_free(&result, compile_expression_wrapped(token->next));
|
||||
string_append(&result, " is not null");
|
||||
|
||||
} else if (strcmp(token->content.string, "same") == 0) {
|
||||
|
||||
string_append_free(&result, compile_expression_wrapped(token->next));
|
||||
string_append(&result, " is same as ");
|
||||
string_append_free(&result, compile_expression_wrapped(token->next->next));
|
||||
|
||||
} else if (strcmp(token->content.string, "diff") == 0) {
|
||||
|
||||
string_append_free(&result, compile_expression_wrapped(token->next));
|
||||
string_append(&result, " is not same as ");
|
||||
string_append_free(&result, compile_expression_wrapped(token->next->next));
|
||||
|
||||
} else if (strcmp(token->content.string, "isdiv") == 0) {
|
||||
|
||||
string_append_free(&result, compile_expression_wrapped(token->next));
|
||||
string_append(&result, " is divisible by ");
|
||||
string_append_free(&result, compile_expression_wrapped(token->next->next));
|
||||
|
||||
} else if (strcmp(token->content.string, "notdiv") == 0) {
|
||||
|
||||
string_append_free(&result, compile_expression_wrapped(token->next));
|
||||
string_append(&result, " is divisible by ");
|
||||
string_append_free(&result, compile_expression_wrapped(token->next->next));
|
||||
|
||||
} else if (strcmp(token->content.string, "is") == 0) {
|
||||
|
||||
string_append_free(&result, compile_expression_wrapped(token->next));
|
||||
string_append(&result, " is ");
|
||||
string_append_free(&result, compile_expression(token->next->next));
|
||||
|
||||
} else if (strcmp(token->content.string, "not") == 0) {
|
||||
|
||||
string_append_free(&result, compile_expression_wrapped(token->next));
|
||||
string_append(&result, " is not ");
|
||||
string_append_free(&result, compile_expression(token->next->next));
|
||||
|
||||
} else {
|
||||
|
||||
string_append(&result,token->content.string);
|
||||
compile_genexec(&result, token);
|
||||
@@ -182,25 +319,29 @@ char* compile_expression(tSyntaxElement* syntaxelement) {
|
||||
|
||||
case TREE: {
|
||||
tSyntaxElement* token = syntaxelement->content.syntax;
|
||||
string_append(&result, compile_expression(syntaxelement->content.syntax));
|
||||
string_append_free(&result, compile_expression(syntaxelement->content.syntax));
|
||||
compile_genexec(&result, token);
|
||||
break;
|
||||
}
|
||||
|
||||
case STRING:
|
||||
return NULL;
|
||||
case NONE:
|
||||
case NEWTREE:
|
||||
return NULL;
|
||||
string_append(&result, "()");
|
||||
break;
|
||||
}
|
||||
|
||||
} else {
|
||||
string_append(&result, "()");
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case NEWTREE:
|
||||
case NONE:
|
||||
free(result);
|
||||
return NULL;
|
||||
string_append(&result, "()");
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
@@ -220,56 +361,80 @@ char* compile(tSyntaxElement* syntaxtree) {
|
||||
|
||||
tSyntaxElement* token = syntaxtree->content.syntax;
|
||||
|
||||
if (strcmp(token->content.string, "def") == 0) {
|
||||
} else
|
||||
|
||||
if (strcmp(token->content.string, "set") == 0) {
|
||||
if (strcmp(token->content.string, "#def") == 0) {
|
||||
} else if (strcmp(token->content.string, "set") == 0) {
|
||||
|
||||
compile_enter_tag(&result);
|
||||
|
||||
string_append(&result, "set ");
|
||||
string_append(&result,token->next->content.string);
|
||||
string_append(&result,"=");
|
||||
string_append(&result,compile_expression_wrapped(token->next->next));
|
||||
string_append_free(&result,compile_expression_wrapped(token->next->next));
|
||||
|
||||
compile_exit_tag(&result);
|
||||
|
||||
} else
|
||||
|
||||
if (strcmp(token->content.string, "global") == 0) {
|
||||
} else if (strcmp(token->content.string, "global") == 0) {
|
||||
|
||||
compile_enter_tag(&result);
|
||||
|
||||
string_append(&result, "global ");
|
||||
string_append(&result,token->next->content.string);
|
||||
string_append(&result,"=");
|
||||
string_append(&result,compile_expression_wrapped(token->next->next));
|
||||
string_append_free(&result,compile_expression_wrapped(token->next->next));
|
||||
|
||||
compile_exit_tag(&result);
|
||||
|
||||
} else
|
||||
|
||||
if (strcmp(token->content.string, "do") == 0) {
|
||||
} else if (strcmp(token->content.string, "do") == 0) {
|
||||
|
||||
compile_enter_tag(&result);
|
||||
|
||||
string_append(&result, "do ");
|
||||
string_append(&result,compile_expression(token->next));
|
||||
string_append_free(&result,compile_expression(token->next));
|
||||
|
||||
compile_exit_tag(&result);
|
||||
|
||||
} else
|
||||
} else if (strcmp(token->content.string, "use") == 0) {
|
||||
|
||||
if (strcmp(token->content.string, "if") == 0) {
|
||||
compile_enter_tag(&result);
|
||||
|
||||
string_append(&result, "use ");
|
||||
string_append_free(&result,compile_expression(token->next));
|
||||
|
||||
compile_exit_tag(&result);
|
||||
|
||||
} else if (strcmp(token->content.string, "return") == 0) {
|
||||
|
||||
compile_enter_tag(&result);
|
||||
|
||||
string_append(&result, "return ");
|
||||
string_append_free(&result,compile_expression(token->next));
|
||||
|
||||
compile_exit_tag(&result);
|
||||
|
||||
} else if (strcmp(token->content.string, "run") == 0) {
|
||||
|
||||
compile_enter_tag(&result);
|
||||
|
||||
string_append(&result, "run ");
|
||||
string_append_free(&result,compile_expression(token->next));
|
||||
|
||||
if (token->next->next != NULL) {
|
||||
string_append(&result, " with ");
|
||||
string_append_free(&result,compile_expression(token->next->next));
|
||||
}
|
||||
|
||||
compile_exit_tag(&result);
|
||||
|
||||
} else if (strcmp(token->content.string, "if") == 0) {
|
||||
|
||||
compile_enter_tag(&result);
|
||||
|
||||
string_append(&result, "if ");
|
||||
string_append(&result,compile_expression(token->next));
|
||||
string_append_free(&result,compile_expression(token->next));
|
||||
|
||||
compile_exit_tag(&result);
|
||||
|
||||
string_append(&result,compile(token->next->next));
|
||||
string_append_free(&result,compile(token->next->next->content.syntax));
|
||||
|
||||
if (token->next->next->next != NULL) {
|
||||
|
||||
@@ -277,7 +442,7 @@ char* compile(tSyntaxElement* syntaxtree) {
|
||||
string_append(&result, "else");
|
||||
compile_exit_tag(&result);
|
||||
|
||||
string_append(&result,compile(token->next->next->next));
|
||||
string_append_free(&result,compile(token->next->next->next->content.syntax));
|
||||
|
||||
}
|
||||
|
||||
@@ -287,16 +452,179 @@ char* compile(tSyntaxElement* syntaxtree) {
|
||||
|
||||
compile_exit_tag(&result);
|
||||
|
||||
} else
|
||||
} else if (strcmp(token->content.string, "filter") == 0) {
|
||||
|
||||
if (strcmp(token->content.string, "print") == 0) {
|
||||
compile_enter_tag(&result);
|
||||
|
||||
string_append(&result, "filter ");
|
||||
string_append_free(&result,compile_expression(token->next));
|
||||
|
||||
compile_exit_tag(&result);
|
||||
|
||||
string_append_free(&result,compile(token->next->next));
|
||||
|
||||
compile_enter_tag(&result);
|
||||
|
||||
string_append(&result, "endfilter");
|
||||
|
||||
compile_exit_tag(&result);
|
||||
|
||||
} else if (strcmp(token->content.string, "print") == 0) {
|
||||
|
||||
for(tSyntaxElement* i = token->next; i != NULL; i = i->next) {
|
||||
|
||||
string_append(&result, "{{");
|
||||
string_append(&result,compile_expression(token->next));
|
||||
string_append_free(&result,compile_expression(i));
|
||||
string_append(&result, "}}");
|
||||
|
||||
}
|
||||
|
||||
} else if (strcmp(token->content.string, "fn") == 0) {
|
||||
|
||||
compile_enter_tag(&result);
|
||||
|
||||
string_append(&result, "macro ");
|
||||
string_append_free(&result, compile_expression(token->next));
|
||||
|
||||
compile_exit_tag(&result);
|
||||
|
||||
|
||||
string_append_free(&result, compile(token->next->next));
|
||||
|
||||
|
||||
compile_enter_tag(&result);
|
||||
|
||||
string_append(&result, "endmacro");
|
||||
|
||||
compile_exit_tag(&result);
|
||||
|
||||
} else if (strcmp(token->content.string, "transform") == 0) {
|
||||
|
||||
compile_enter_tag(&result);
|
||||
|
||||
string_append(&result, "transform ");
|
||||
string_append_free(&result, compile_expression(token->next->content.syntax));
|
||||
string_append(&result, " in ");
|
||||
string_append_free(&result, compile_expression(token->next->content.syntax->next));
|
||||
string_append(&result, " as ");
|
||||
string_append_free(&result, compile_expression(token->next->content.syntax->next->next));
|
||||
|
||||
compile_exit_tag(&result);
|
||||
|
||||
|
||||
string_append_free(&result, compile(token->next->next));
|
||||
|
||||
|
||||
compile_enter_tag(&result);
|
||||
|
||||
string_append(&result, "endtransform");
|
||||
|
||||
compile_exit_tag(&result);
|
||||
|
||||
} else if (strcmp(token->content.string, "for") == 0) {
|
||||
|
||||
compile_enter_tag(&result);
|
||||
|
||||
string_append(&result, "for ");
|
||||
string_append_free(&result, compile_expression(token->next->content.syntax));
|
||||
string_append(&result, " in ");
|
||||
string_append_free(&result, compile_expression(token->next->content.syntax->next));
|
||||
|
||||
compile_exit_tag(&result);
|
||||
|
||||
|
||||
string_append_free(&result, compile(token->next->next));
|
||||
|
||||
|
||||
compile_enter_tag(&result);
|
||||
|
||||
string_append(&result, "endfor");
|
||||
|
||||
compile_exit_tag(&result);
|
||||
|
||||
} else if (strcmp(token->content.string, "break") == 0) {
|
||||
|
||||
compile_enter_tag(&result);
|
||||
|
||||
string_append(&result, "break");
|
||||
|
||||
compile_exit_tag(&result);
|
||||
|
||||
} else if (strcmp(token->content.string, "continue") == 0) {
|
||||
|
||||
compile_enter_tag(&result);
|
||||
|
||||
string_append(&result, "continue");
|
||||
|
||||
compile_exit_tag(&result);
|
||||
|
||||
} else if (strcmp(token->content.string, "formap") == 0) {
|
||||
|
||||
compile_enter_tag(&result);
|
||||
|
||||
string_append(&result, "for ");
|
||||
string_append_free(&result, compile_expression(token->next->content.syntax));
|
||||
string_append(&result, ", ");
|
||||
string_append_free(&result, compile_expression(token->next->content.syntax->next));
|
||||
string_append(&result, " in ");
|
||||
string_append_free(&result, compile_expression(token->next->content.syntax->next->next));
|
||||
|
||||
compile_exit_tag(&result);
|
||||
|
||||
|
||||
string_append_free(&result, compile(token->next->next));
|
||||
|
||||
|
||||
compile_enter_tag(&result);
|
||||
|
||||
string_append(&result, "endfor");
|
||||
|
||||
compile_exit_tag(&result);
|
||||
|
||||
} else if (strcmp(token->content.string, "verbatim") == 0) {
|
||||
|
||||
compile_enter_tag(&result);
|
||||
string_append(&result, "verbatim");
|
||||
compile_exit_tag(&result);
|
||||
|
||||
for(tSyntaxElement* i = token->next; i != NULL; i = i->next) {
|
||||
|
||||
string_append_free(&result,string_unescape(i->content.string));
|
||||
|
||||
}
|
||||
|
||||
compile_enter_tag(&result);
|
||||
string_append(&result, " endverbatim");
|
||||
compile_exit_tag(&result);
|
||||
|
||||
} else if (strcmp(token->content.string, "assert") == 0) {
|
||||
|
||||
compile_enter_tag(&result);
|
||||
string_append(&result, "require ");
|
||||
|
||||
string_append_free(&result, compile_expression(token->next));
|
||||
|
||||
string_append(&result, " returning ");
|
||||
|
||||
string_append_free(&result, compile_expression(token->next->next));
|
||||
compile_exit_tag(&result);
|
||||
|
||||
} else if (strcmp(token->content.string, "quote") == 0) {
|
||||
|
||||
if (token->next->type == STRING)
|
||||
string_append(&result,token->next->content.string);
|
||||
else
|
||||
string_append_free(&result,compile_expression(token->next));
|
||||
|
||||
} else {
|
||||
|
||||
string_append(&result, "{#");
|
||||
string_append(&result, "Unknown keyword: ");
|
||||
string_append(&result, token->content.string);
|
||||
string_append(&result, "#}");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+18
-1
@@ -31,7 +31,24 @@ int main(int argc, char *argv[]) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("%s\n", compile(code));
|
||||
#ifdef PROCESS_DEBUG
|
||||
|
||||
printf("Parsed: \n");
|
||||
printtree(code, 0);
|
||||
process(code, NULL);
|
||||
printf("\nProcessed: \n");
|
||||
printtree(code, 0);
|
||||
|
||||
#else
|
||||
process(code, NULL);
|
||||
|
||||
char* compiled = compile(code);
|
||||
printf("%s\n", compiled);
|
||||
|
||||
free(compiled);
|
||||
compiled = NULL;
|
||||
|
||||
#endif
|
||||
|
||||
se_free(code);
|
||||
code = NULL;
|
||||
|
||||
+29
-12
@@ -10,9 +10,14 @@ bool replace(tSyntaxElement* tree, tProcessingData* pd) {
|
||||
|
||||
free(tree->content.none);
|
||||
|
||||
tSyntaxElement* clone = se_clone_no_next(pd->data.replace.replacewiththis, tree);
|
||||
tSyntaxElement* clone = se_clone(pd->data.replace.replacewiththis, tree);
|
||||
tree->content = clone->content;
|
||||
tree->type = clone->type;
|
||||
if (clone->next != NULL) {
|
||||
tSyntaxElement* tmp = se_find_last(clone);
|
||||
tmp->next = tree->next;
|
||||
tree->next = clone->next;
|
||||
}
|
||||
|
||||
free(clone);
|
||||
|
||||
@@ -21,9 +26,10 @@ bool replace(tSyntaxElement* tree, tProcessingData* pd) {
|
||||
return false;
|
||||
}
|
||||
|
||||
void process_mod(tSyntaxElement* tree, tProcessingData* pdata) {
|
||||
bool process_mod(tSyntaxElement* tree, tProcessingData* pdata) {
|
||||
|
||||
tProcessingData* pd = pdata;
|
||||
bool b = false;
|
||||
|
||||
while (1) {
|
||||
|
||||
@@ -33,7 +39,8 @@ void process_mod(tSyntaxElement* tree, tProcessingData* pdata) {
|
||||
if (tree->type == TOKEN) {
|
||||
switch (pd->type) {
|
||||
case REPLACE:
|
||||
replace(tree,pd);
|
||||
if (replace(tree,pd))
|
||||
b = true;
|
||||
break;
|
||||
case FUNCTION:
|
||||
break;
|
||||
@@ -47,6 +54,8 @@ void process_mod(tSyntaxElement* tree, tProcessingData* pdata) {
|
||||
|
||||
}
|
||||
|
||||
return b;
|
||||
|
||||
}
|
||||
|
||||
bool process_find(tSyntaxElement* tree, tProcessingData** p_pdata) {
|
||||
@@ -61,7 +70,7 @@ bool process_find(tSyntaxElement* tree, tProcessingData** p_pdata) {
|
||||
|
||||
tSyntaxElement* token = tree->content.syntax;
|
||||
|
||||
if (strcmp(token->content.string,"def") == 0 &&
|
||||
if (strcmp(token->content.string,"#def") == 0 &&
|
||||
token->next != NULL &&
|
||||
token->next->next != NULL) {
|
||||
|
||||
@@ -69,7 +78,7 @@ bool process_find(tSyntaxElement* tree, tProcessingData** p_pdata) {
|
||||
|
||||
tSyntaxElement* replacewiththis = token->next->next;
|
||||
|
||||
tProcessingData* npd = malloc(sizeof(tSyntaxElement));
|
||||
tProcessingData* npd = malloc(sizeof(tProcessingData));
|
||||
|
||||
npd->type = REPLACE;
|
||||
npd->data.replace.replacethis = replacethis;
|
||||
@@ -80,12 +89,12 @@ bool process_find(tSyntaxElement* tree, tProcessingData** p_pdata) {
|
||||
|
||||
deffound = true;
|
||||
|
||||
} else if (strcmp(token->content.string,"fn") == 0 &&
|
||||
} else if (strcmp(token->content.string,"#fn") == 0 &&
|
||||
token->next != NULL && token->next->type == TOKEN &&
|
||||
token->next->next != NULL && se_istraversable(token->next->next) &&
|
||||
token->next->next->next != NULL) {
|
||||
|
||||
tProcessingData* npd = malloc(sizeof(tSyntaxElement));
|
||||
tProcessingData* npd = malloc(sizeof(tProcessingData));
|
||||
|
||||
npd->type = FUNCTION;
|
||||
|
||||
@@ -124,11 +133,12 @@ bool process_find(tSyntaxElement* tree, tProcessingData** p_pdata) {
|
||||
}
|
||||
|
||||
|
||||
void process(tSyntaxElement* tree, tProcessingData* pdata) {
|
||||
bool process(tSyntaxElement* tree, tProcessingData* pdata) {
|
||||
|
||||
tProcessingData* pd = pdata;
|
||||
|
||||
bool deffound = false;
|
||||
bool didreplace = false;
|
||||
|
||||
tProcessingData* firstpd = NULL;
|
||||
|
||||
@@ -142,22 +152,29 @@ void process(tSyntaxElement* tree, tProcessingData* pdata) {
|
||||
deffound = process_find(tree, &pd);
|
||||
|
||||
if (tree->next != NULL) {
|
||||
process(tree->next, pd);
|
||||
if (process(tree->next, pd))
|
||||
didreplace = true;
|
||||
}
|
||||
|
||||
if (!deffound) {
|
||||
|
||||
if (se_istraversable(tree))
|
||||
process(tree->content.syntax, pd);
|
||||
if (process(tree->content.syntax, pd))
|
||||
didreplace = true;
|
||||
|
||||
process_mod(tree, pd);
|
||||
if (process_mod(tree, pd))
|
||||
didreplace = true;
|
||||
|
||||
}
|
||||
|
||||
if (pd != pdata)
|
||||
free(pd);
|
||||
|
||||
if (firstpd != pd && firstpd != NULL)
|
||||
if (firstpd != pd && firstpd != NULL) {
|
||||
free(firstpd);
|
||||
if (didreplace)
|
||||
process(tree, NULL);
|
||||
}
|
||||
|
||||
return didreplace;
|
||||
}
|
||||
|
||||
+1
-1
@@ -45,4 +45,4 @@ typedef struct ProcessingData {
|
||||
|
||||
|
||||
|
||||
void process(tSyntaxElement*, tProcessingData*);
|
||||
bool process(tSyntaxElement*, tProcessingData*);
|
||||
|
||||
+11
-1
@@ -102,7 +102,7 @@ tSyntaxElement* se_clone(tSyntaxElement* syntaxelement, tSyntaxElement* newtop)
|
||||
clone = se_create();
|
||||
clone->type = syntaxelement->type;
|
||||
|
||||
clone->content.string = malloc(strlen(syntaxelement->content.string));
|
||||
clone->content.string = malloc(strlen(syntaxelement->content.string) + sizeof(char));
|
||||
strcpy(clone->content.string, syntaxelement->content.string);
|
||||
|
||||
if (syntaxelement->next != NULL)
|
||||
@@ -142,3 +142,13 @@ tSyntaxElement* se_clone_no_next(tSyntaxElement* syntaxelement, tSyntaxElement*
|
||||
return clone;
|
||||
|
||||
}
|
||||
|
||||
tSyntaxElement* se_find_last(tSyntaxElement* syntaxelement) {
|
||||
tSyntaxElement* i = syntaxelement;
|
||||
while (1)
|
||||
if (i->next == NULL)
|
||||
return i;
|
||||
else
|
||||
i = i->next;
|
||||
}
|
||||
|
||||
|
||||
@@ -47,4 +47,6 @@ tSyntaxElement* se_clone(tSyntaxElement*, tSyntaxElement*);
|
||||
|
||||
tSyntaxElement* se_clone_no_next(tSyntaxElement*, tSyntaxElement*);
|
||||
|
||||
tSyntaxElement* se_find_last(tSyntaxElement*);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user