From 07f9a6abd8be99b7ad89eb5f7a70452cdf354efe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?b=CA=B0edoh=E2=82=82=20sw=C3=A9?= Date: Tue, 11 Jun 2024 03:55:11 +0500 Subject: [PATCH] I am going insane --- src/compile.c | 118 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 102 insertions(+), 16 deletions(-) diff --git a/src/compile.c b/src/compile.c index bfd8828..186b244 100644 --- a/src/compile.c +++ b/src/compile.c @@ -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);