diff --git a/src/compile.c b/src/compile.c new file mode 100644 index 0000000..357d617 --- /dev/null +++ b/src/compile.c @@ -0,0 +1,46 @@ +#include +#include + +#include "syntax.h" + +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'; +} + +void string_append(char** string, char* append) { + int len = strlen(*string); + *string = realloc(*string, len + strlen(append) + sizeof(char)); + strcpy(*string + len, append); +} + +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,token->next->next->content.string); + compile_exit_tag(&result); + } + } + } + return result; +} diff --git a/src/compile.h b/src/compile.h new file mode 100644 index 0000000..56c6790 --- /dev/null +++ b/src/compile.h @@ -0,0 +1,3 @@ +#include "syntax.h" + +char* compile(tSyntaxElement*); diff --git a/src/main.c b/src/main.c index 82ca480..df4674b 100644 --- a/src/main.c +++ b/src/main.c @@ -5,6 +5,7 @@ #include "syntax.h" #include "printtree.h" #include "process.h" +#include "compile.h" int main(int argc, char *argv[]) { @@ -37,6 +38,8 @@ int main(int argc, char *argv[]) { process(code, NULL); printtree(code,1); + printf("\nCompiled: \n%s\n", compile(code)); + se_free(code); code = NULL;