Some formatting
This commit is contained in:
parent
3b8f14cab0
commit
d8bd79e8c9
@ -5,91 +5,148 @@
|
||||
#include "syntax.h"
|
||||
|
||||
void string_append(char** string, char* append) {
|
||||
|
||||
int len = strlen(*string);
|
||||
|
||||
*string = realloc(*string, len + strlen(append) + sizeof(char));
|
||||
strcpy(*string + len, append);
|
||||
|
||||
}
|
||||
|
||||
void compile_enter_tag(char** string) {
|
||||
|
||||
int len = strlen(*string);
|
||||
|
||||
*string = realloc(*string, len + 3 * sizeof(char));
|
||||
|
||||
string[0][len + 0] = '{';
|
||||
string[0][len + 1] = '%';
|
||||
string[0][len + 2] = '\0';
|
||||
|
||||
}
|
||||
|
||||
void compile_exit_tag(char** string) {
|
||||
|
||||
int len = strlen(*string);
|
||||
|
||||
*string = realloc(*string, len + 3 * sizeof(char));
|
||||
|
||||
string[0][len + 0] = '%';
|
||||
string[0][len + 1] = '}';
|
||||
string[0][len + 2] = '\0';
|
||||
|
||||
}
|
||||
|
||||
bool compile_isop(char* string) {
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
char* compile_expression(tSyntaxElement* syntaxelement) {
|
||||
|
||||
char* result = calloc(1,sizeof(char));
|
||||
|
||||
string_append(&result, "(");
|
||||
|
||||
switch (syntaxelement->type) {
|
||||
|
||||
case TOKEN:
|
||||
|
||||
string_append(&result, syntaxelement->content.string);
|
||||
break;
|
||||
|
||||
case STRING:
|
||||
|
||||
string_append(&result, "\"");
|
||||
string_append(&result, syntaxelement->content.string);
|
||||
string_append(&result, "\"");
|
||||
|
||||
break;
|
||||
|
||||
case TREE:
|
||||
|
||||
if (se_istraversable(syntaxelement)) {
|
||||
|
||||
if (syntaxelement->content.syntax->type == TOKEN) {
|
||||
|
||||
tSyntaxElement* token = syntaxelement->content.syntax;
|
||||
|
||||
if (compile_isop(token->content.string)) {
|
||||
|
||||
char* operator = token->content.string;
|
||||
|
||||
while (1) {
|
||||
|
||||
if (token->next != NULL) {
|
||||
|
||||
string_append(&result,token->next->content.string);
|
||||
if (token->next->next != NULL) {
|
||||
|
||||
if (token->next->next != NULL)
|
||||
string_append(&result,operator);
|
||||
}
|
||||
|
||||
token = token->next;
|
||||
|
||||
} else break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case NEWTREE:
|
||||
case NONE:
|
||||
free(result);
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
string_append(&result, ")");
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
char* compile(tSyntaxElement* syntaxtree) {
|
||||
|
||||
char* result = calloc(1,sizeof(char));
|
||||
|
||||
if (se_istraversable(syntaxtree)) {
|
||||
|
||||
if (syntaxtree->content.syntax->type == TOKEN) {
|
||||
|
||||
tSyntaxElement* token = syntaxtree->content.syntax;
|
||||
|
||||
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(token->next->next));
|
||||
|
||||
compile_exit_tag(&result);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user