Start implementing compiler

This commit is contained in:
bʰedoh₂ swé 2024-06-10 23:27:08 +05:00
parent 24042b3f88
commit 3321ca8e1b
3 changed files with 52 additions and 0 deletions

46
src/compile.c Normal file
View File

@ -0,0 +1,46 @@
#include <stdlib.h>
#include <string.h>
#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;
}

3
src/compile.h Normal file
View File

@ -0,0 +1,3 @@
#include "syntax.h"
char* compile(tSyntaxElement*);

View File

@ -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;