I am going insane

This commit is contained in:
bʰedoh₂ swé 2024-06-11 03:55:11 +05:00
parent 9e80de017f
commit 07f9a6abd8

View File

@ -63,12 +63,52 @@ bool compile_isop(char* string) {
}
char* compile_bracket_wrap(char* string) {
char* new = malloc(sizeof(char));
new[0] = '\0';
string_append(&new,"(");
string_append(&new,string);
string_append(&new,")");
free(string);
return new;
}
char* compile_expression(tSyntaxElement* syntaxelement);
char* compile_expression_wrapped(tSyntaxElement* syntaxelement) {
return compile_bracket_wrap(compile_expression(syntaxelement));
}
void compile_genexec(char** result, tSyntaxElement* se){
tSyntaxElement* token = se;
string_append(result,"(");
token = token->next;
while (1) {
if (token != NULL) {
string_append(result,token->content.string);
if (token->next != NULL)
string_append(result,",");
token = token->next;
} else break;
}
string_append(result,")");
}
char* compile_expression(tSyntaxElement* syntaxelement) {
char* result = calloc(1,sizeof(char));
string_append(&result, "(");
switch (syntaxelement->type) {
case TOKEN:
@ -88,31 +128,54 @@ char* compile_expression(tSyntaxElement* syntaxelement) {
if (se_istraversable(syntaxelement)) {
if (syntaxelement->content.syntax->type == TOKEN) {
switch (syntaxelement->content.syntax->type) {
tSyntaxElement* token = syntaxelement->content.syntax;
case TOKEN: {
tSyntaxElement* token = syntaxelement->content.syntax;
if (compile_isop(token->content.string)) {
if (compile_isop(token->content.string)) {
char* operator = token->content.string;
char* operator = token->content.string;
while (1) {
while (1) {
if (token->next != NULL) {
if (token->next != NULL) {
string_append(&result,token->next->content.string);
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);
if (token->next->next != NULL)
string_append(&result,operator);
token = token->next;
token = token->next;
} else break;
} else break;
}
} else {
string_append(&result,token->content.string);
compile_genexec(&result, token);
}
break;
}
case TREE: {
tSyntaxElement* token = syntaxelement->content.syntax;
string_append(&result, compile_expression(syntaxelement->content.syntax));
compile_genexec(&result, token);
break;
}
case STRING:
case NONE:
case NEWTREE:
return NULL;
}
}
@ -126,7 +189,6 @@ char* compile_expression(tSyntaxElement* syntaxelement) {
}
string_append(&result, ")");
return result;
}
@ -153,7 +215,31 @@ char* compile(tSyntaxElement* syntaxtree) {
string_append(&result, "set ");
string_append(&result,token->next->content.string);
string_append(&result,"=");
string_append(&result,compile_expression(token->next->next));
string_append(&result,compile_expression_wrapped(token->next->next));
compile_exit_tag(&result);
} 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));
compile_exit_tag(&result);
} else
if (strcmp(token->content.string, "do") == 0) {
compile_enter_tag(&result);
string_append(&result, "do ");
string_append(&result,compile_expression(token->next));
compile_exit_tag(&result);