2024-06-10 18:27:08 +00:00
|
|
|
#include <stdlib.h>
|
2024-06-10 21:40:36 +00:00
|
|
|
#include <stdbool.h>
|
2024-06-10 18:27:08 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "syntax.h"
|
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
void string_append(char** string, char* append) {
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
int len = strlen(*string);
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
*string = realloc(*string, len + strlen(append) + sizeof(char));
|
|
|
|
strcpy(*string + len, append);
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
}
|
|
|
|
|
2024-06-10 18:27:08 +00:00
|
|
|
void compile_enter_tag(char** string) {
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 18:27:08 +00:00
|
|
|
int len = strlen(*string);
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 18:27:08 +00:00
|
|
|
*string = realloc(*string, len + 3 * sizeof(char));
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 18:27:08 +00:00
|
|
|
string[0][len + 0] = '{';
|
|
|
|
string[0][len + 1] = '%';
|
|
|
|
string[0][len + 2] = '\0';
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void compile_exit_tag(char** string) {
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 18:27:08 +00:00
|
|
|
int len = strlen(*string);
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 18:27:08 +00:00
|
|
|
*string = realloc(*string, len + 3 * sizeof(char));
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 18:27:08 +00:00
|
|
|
string[0][len + 0] = '%';
|
|
|
|
string[0][len + 1] = '}';
|
|
|
|
string[0][len + 2] = '\0';
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
bool compile_isop(char* string) {
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
if (strcmp(string, "+") == 0) return true;
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
else if (strcmp(string, "-") == 0) return true;
|
|
|
|
else if (strcmp(string, "*") == 0) return true;
|
|
|
|
else if (strcmp(string, "/") == 0) return true;
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
else return false;
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char* compile_expression(tSyntaxElement* syntaxelement) {
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
char* result = calloc(1,sizeof(char));
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
string_append(&result, "(");
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
switch (syntaxelement->type) {
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
case TOKEN:
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
string_append(&result, syntaxelement->content.string);
|
|
|
|
break;
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
case STRING:
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
string_append(&result, "\"");
|
|
|
|
string_append(&result, syntaxelement->content.string);
|
|
|
|
string_append(&result, "\"");
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
break;
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
case TREE:
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
if (se_istraversable(syntaxelement)) {
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
if (syntaxelement->content.syntax->type == TOKEN) {
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
tSyntaxElement* token = syntaxelement->content.syntax;
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
if (compile_isop(token->content.string)) {
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
char* operator = token->content.string;
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
while (1) {
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
if (token->next != NULL) {
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
string_append(&result,token->next->content.string);
|
2024-06-10 21:43:06 +00:00
|
|
|
|
|
|
|
if (token->next->next != NULL)
|
2024-06-10 21:40:36 +00:00
|
|
|
string_append(&result,operator);
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
token = token->next;
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
} else break;
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
}
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
}
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
}
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
}
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
break;
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
case NEWTREE:
|
|
|
|
case NONE:
|
|
|
|
free(result);
|
|
|
|
return NULL;
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
}
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 21:40:36 +00:00
|
|
|
string_append(&result, ")");
|
|
|
|
return result;
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char* compile(tSyntaxElement* syntaxtree) {
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 18:27:08 +00:00
|
|
|
char* result = calloc(1,sizeof(char));
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 18:27:08 +00:00
|
|
|
if (se_istraversable(syntaxtree)) {
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 18:27:08 +00:00
|
|
|
if (syntaxtree->content.syntax->type == TOKEN) {
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 18:27:08 +00:00
|
|
|
tSyntaxElement* token = syntaxtree->content.syntax;
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 18:27:08 +00:00
|
|
|
if (strcmp(token->content.string, "def") == 0) {
|
|
|
|
} else
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 18:27:08 +00:00
|
|
|
if (strcmp(token->content.string, "set") == 0) {
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 18:27:08 +00:00
|
|
|
compile_enter_tag(&result);
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 18:27:08 +00:00
|
|
|
string_append(&result, "set ");
|
|
|
|
string_append(&result,token->next->content.string);
|
|
|
|
string_append(&result,"=");
|
2024-06-10 21:40:36 +00:00
|
|
|
string_append(&result,compile_expression(token->next->next));
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 18:27:08 +00:00
|
|
|
compile_exit_tag(&result);
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 18:27:08 +00:00
|
|
|
}
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 18:27:08 +00:00
|
|
|
}
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 18:27:08 +00:00
|
|
|
}
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 18:27:08 +00:00
|
|
|
return result;
|
2024-06-10 21:43:06 +00:00
|
|
|
|
2024-06-10 18:27:08 +00:00
|
|
|
}
|