TCPU/src/compiler/AST.c
2025-02-03 22:30:56 +05:00

65 lines
1.4 KiB
C

#include "AST.h"
List_define(Argument);
List_define(Operation);
List_define(DataDefinition);
List_define(Section);
static str _ArgumentType_str[] = {
STR("Unset"),
STR("Register"),
STR("ConstValue"),
STR("VarDataName"),
STR("ConstDataPointer"),
STR("ConstDataSize"),
};
str ArgumentType_toString(ArgumentType t){
if(t >= ARRAY_SIZE(_ArgumentType_str))
return STR("!!ArgumentType INDEX_ERROR!!");
return _ArgumentType_str[t];
}
RegisterCode RegisterCode_parse(str r){
if(str_equals(r, STR("ax")))
return RegisterCode_ax;
if(str_equals(r, STR("bx")))
return RegisterCode_bx;
if(str_equals(r, STR("cx")))
return RegisterCode_cx;
if(str_equals(r, STR("dx")))
return RegisterCode_dx;
return RegisterCode_Unset;
}
void Section_init(Section* sec, str name){
sec->name = name;
sec->data = List_DataDefinition_alloc(256);
sec->code = List_Operation_alloc(1024);
}
void Section_free(Section* sec){
for(u32 i = 0; i < sec->data.len; i++){
free(sec->data.data[i].data.data);
}
free(sec->data.data);
for(u32 i = 0; i < sec->code.len; i++){
free(sec->code.data[i].args.data);
}
free(sec->code.data);
}
void AST_init(AST* ast){
ast->sections = List_Section_alloc(32);
}
void AST_free(AST* ast){
for(u32 i = 0; i != ast->sections.len; i++){
Section_free(&ast->sections.data[i]);
}
free(ast->sections.data);
}