Some formatting

This commit is contained in:
bʰedoh₂ swé 2024-06-11 02:43:06 +05:00
parent 3b8f14cab0
commit d8bd79e8c9

View File

@ -5,91 +5,148 @@
#include "syntax.h" #include "syntax.h"
void string_append(char** string, char* append) { void string_append(char** string, char* append) {
int len = strlen(*string); int len = strlen(*string);
*string = realloc(*string, len + strlen(append) + sizeof(char)); *string = realloc(*string, len + strlen(append) + sizeof(char));
strcpy(*string + len, append); strcpy(*string + len, append);
} }
void compile_enter_tag(char** string) { void compile_enter_tag(char** string) {
int len = strlen(*string); int len = strlen(*string);
*string = realloc(*string, len + 3 * sizeof(char)); *string = realloc(*string, len + 3 * sizeof(char));
string[0][len + 0] = '{'; string[0][len + 0] = '{';
string[0][len + 1] = '%'; string[0][len + 1] = '%';
string[0][len + 2] = '\0'; string[0][len + 2] = '\0';
} }
void compile_exit_tag(char** string) { void compile_exit_tag(char** string) {
int len = strlen(*string); int len = strlen(*string);
*string = realloc(*string, len + 3 * sizeof(char)); *string = realloc(*string, len + 3 * sizeof(char));
string[0][len + 0] = '%'; string[0][len + 0] = '%';
string[0][len + 1] = '}'; string[0][len + 1] = '}';
string[0][len + 2] = '\0'; string[0][len + 2] = '\0';
} }
bool compile_isop(char* string) { bool compile_isop(char* string) {
if (strcmp(string, "+") == 0) return true; if (strcmp(string, "+") == 0) return true;
else if (strcmp(string, "-") == 0) return true; else if (strcmp(string, "-") == 0) return true;
else if (strcmp(string, "*") == 0) return true; else if (strcmp(string, "*") == 0) return true;
else if (strcmp(string, "/") == 0) return true; else if (strcmp(string, "/") == 0) return true;
else return false; else return false;
} }
char* compile_expression(tSyntaxElement* syntaxelement) { char* compile_expression(tSyntaxElement* syntaxelement) {
char* result = calloc(1,sizeof(char)); char* result = calloc(1,sizeof(char));
string_append(&result, "("); string_append(&result, "(");
switch (syntaxelement->type) { switch (syntaxelement->type) {
case TOKEN: case TOKEN:
string_append(&result, syntaxelement->content.string); string_append(&result, syntaxelement->content.string);
break; break;
case STRING: case STRING:
string_append(&result, "\""); string_append(&result, "\"");
string_append(&result, syntaxelement->content.string); string_append(&result, syntaxelement->content.string);
string_append(&result, "\""); string_append(&result, "\"");
break; break;
case TREE: case TREE:
if (se_istraversable(syntaxelement)) { if (se_istraversable(syntaxelement)) {
if (syntaxelement->content.syntax->type == TOKEN) { if (syntaxelement->content.syntax->type == TOKEN) {
tSyntaxElement* token = syntaxelement->content.syntax; 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); string_append(&result,token->next->content.string);
if (token->next->next != NULL) {
if (token->next->next != NULL)
string_append(&result,operator); string_append(&result,operator);
}
token = token->next; token = token->next;
} else break; } else break;
} }
} }
} }
} }
break; break;
case NEWTREE: case NEWTREE:
case NONE: case NONE:
free(result); free(result);
return NULL; return NULL;
} }
string_append(&result, ")"); string_append(&result, ")");
return result; return result;
} }
char* compile(tSyntaxElement* syntaxtree) { char* compile(tSyntaxElement* syntaxtree) {
char* result = calloc(1,sizeof(char)); char* result = calloc(1,sizeof(char));
if (se_istraversable(syntaxtree)) { if (se_istraversable(syntaxtree)) {
if (syntaxtree->content.syntax->type == TOKEN) { if (syntaxtree->content.syntax->type == TOKEN) {
tSyntaxElement* token = syntaxtree->content.syntax; tSyntaxElement* token = syntaxtree->content.syntax;
if (strcmp(token->content.string, "def") == 0) { if (strcmp(token->content.string, "def") == 0) {
} else } else
if (strcmp(token->content.string, "set") == 0) { if (strcmp(token->content.string, "set") == 0) {
compile_enter_tag(&result); compile_enter_tag(&result);
string_append(&result, "set "); string_append(&result, "set ");
string_append(&result,token->next->content.string); string_append(&result,token->next->content.string);
string_append(&result,"="); string_append(&result,"=");
string_append(&result,compile_expression(token->next->next)); string_append(&result,compile_expression(token->next->next));
compile_exit_tag(&result); compile_exit_tag(&result);
} }
} }
} }
return result; return result;
} }