From 3321ca8e1b36c6d7f1820ac93e2e8e7feac57bc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?b=CA=B0edoh=E2=82=82=20sw=C3=A9?= Date: Mon, 10 Jun 2024 23:27:08 +0500 Subject: [PATCH] Start implementing compiler --- src/compile.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/compile.h | 3 +++ src/main.c | 3 +++ 3 files changed, 52 insertions(+) create mode 100644 src/compile.c create mode 100644 src/compile.h 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;