merged main in branch

This commit is contained in:
Felix Müller 2024-06-06 23:21:06 +02:00
parent af07d82d94
commit b9226ad3f5
2 changed files with 41 additions and 36 deletions

View File

@ -221,7 +221,7 @@ void mem_purge_namespace(MemoryNamespaceName name) {
namespace_purge(cache); namespace_purge(cache);
} else { } else {
PANIC("purging invalid namespace: %s", name); WARN("purging invalid namespace: %s", name);
} }
} }

View File

@ -10,6 +10,7 @@
#include <glib.h> #include <glib.h>
#include <assert.h> #include <assert.h>
#include <set/set.h> #include <set/set.h>
#include <mem/cache.h>
extern ModuleFile * current_file; extern ModuleFile * current_file;
@ -218,6 +219,7 @@ int impl_composite_type(AST_NODE_PTR ast_type, CompositeType* composite) {
/** /**
* @brief Converts the given AST node to a gemstone type implementation. * @brief Converts the given AST node to a gemstone type implementation.
* @param currentNode AST node of type kind type * @param currentNode AST node of type kind type
* @param type pointer output for the type
* @return the gemstone type implementation * @return the gemstone type implementation
*/ */
int get_type_impl(AST_NODE_PTR currentNode, Type** type) { int get_type_impl(AST_NODE_PTR currentNode, Type** type) {
@ -241,7 +243,7 @@ int get_type_impl(AST_NODE_PTR currentNode, Type** type) {
} }
// type is not yet declared, make a new one // type is not yet declared, make a new one
Type* new_type = malloc(sizeof(Type)); Type* new_type = mem_alloc(MemoryNamespaceSet,sizeof(Type));
new_type->nodePtr = currentNode; new_type->nodePtr = currentNode;
// only one child means either composite or primitive // only one child means either composite or primitive
@ -294,6 +296,7 @@ int createDecl(AST_NODE_PTR currentNode, GArray** variables) {
VariableDeclaration decl; VariableDeclaration decl;
decl.nodePtr = currentNode; decl.nodePtr = currentNode;
decl.qualifier = Static;
int status = SEMANTIC_OK; int status = SEMANTIC_OK;
@ -318,7 +321,7 @@ int createDecl(AST_NODE_PTR currentNode, GArray** variables) {
} }
for(size_t i = 0; i < ident_list->child_count; i++) { for(size_t i = 0; i < ident_list->child_count; i++) {
Variable* variable = malloc(sizeof(Variable)); Variable* variable = mem_alloc(MemoryNamespaceSet,sizeof(Variable));
variable->kind = VariableKindDeclaration; variable->kind = VariableKindDeclaration;
variable->nodePtr = currentNode; variable->nodePtr = currentNode;
@ -354,6 +357,8 @@ int createDef(AST_NODE_PTR currentNode, GArray** variables) {
VariableDeclaration decl; VariableDeclaration decl;
VariableDefiniton def; VariableDefiniton def;
def.nodePtr = currentNode; def.nodePtr = currentNode;
decl.qualifier = Static;
decl.nodePtr = AST_get_node(currentNode, 0);
int status = SEMANTIC_OK; int status = SEMANTIC_OK;
@ -385,7 +390,7 @@ int createDef(AST_NODE_PTR currentNode, GArray** variables) {
for(size_t i = 0; i < ident_list->child_count; i++) { for(size_t i = 0; i < ident_list->child_count; i++) {
Variable* variable = malloc(sizeof(Variable)); Variable* variable = mem_alloc(MemoryNamespaceSet,sizeof(Variable));
variable->kind = VariableKindDefinition; variable->kind = VariableKindDefinition;
variable->nodePtr = currentNode; variable->nodePtr = currentNode;
@ -484,7 +489,7 @@ int addVarToScope(Variable * variable){
return SEMANTIC_OK; return SEMANTIC_OK;
} }
int fillTablesWithVars(GHashTable *variableTable, GArray* variables) { int fillTablesWithVars(GHashTable *variableTable, const GArray* variables) {
DEBUG("filling vars in scope and table"); DEBUG("filling vars in scope and table");
for(size_t i = 0; i < variables->len; i++) { for(size_t i = 0; i < variables->len; i++) {
@ -507,7 +512,7 @@ int fillTablesWithVars(GHashTable *variableTable, GArray* variables) {
TypeValue createTypeValue(AST_NODE_PTR currentNode){ TypeValue createTypeValue(AST_NODE_PTR currentNode){
DEBUG("create TypeValue"); DEBUG("create TypeValue");
TypeValue value; TypeValue value;
Type *type = malloc(sizeof(Type)); Type *type = mem_alloc(MemoryNamespaceSet,sizeof(Type));
value.type = type; value.type = type;
type->kind = TypeKindPrimitive; type->kind = TypeKindPrimitive;
type->nodePtr = currentNode; type->nodePtr = currentNode;
@ -532,7 +537,7 @@ TypeValue createTypeValue(AST_NODE_PTR currentNode){
} }
static inline void* clone(int size, void* ptr) { static inline void* clone(int size, void* ptr) {
char* data = malloc(size); char* data = mem_alloc(MemoryNamespaceSet,size);
memcpy(data, ptr, size); memcpy(data, ptr, size);
return data; return data;
} }
@ -551,7 +556,7 @@ TypeValue createString(AST_NODE_PTR currentNode) {
Type* createTypeFromOperands(Type* LeftOperandType, Type* RightOperandType, AST_NODE_PTR currentNode) { Type* createTypeFromOperands(Type* LeftOperandType, Type* RightOperandType, AST_NODE_PTR currentNode) {
DEBUG("create type from operands"); DEBUG("create type from operands");
Type *result = malloc(sizeof(Type)); Type *result = mem_alloc(MemoryNamespaceSet,sizeof(Type));
result->nodePtr = currentNode; result->nodePtr = currentNode;
DEBUG("LeftOperandType->kind: %i", LeftOperandType->kind); DEBUG("LeftOperandType->kind: %i", LeftOperandType->kind);
DEBUG("RightOperandType->kind: %i", RightOperandType->kind); DEBUG("RightOperandType->kind: %i", RightOperandType->kind);
@ -589,7 +594,7 @@ Type* createTypeFromOperands(Type* LeftOperandType, Type* RightOperandType, AST_
result->impl.composite.primitive = MAX(Int, LeftOperandType->impl.composite.primitive); result->impl.composite.primitive = MAX(Int, LeftOperandType->impl.composite.primitive);
result->impl.composite.nodePtr = currentNode; result->impl.composite.nodePtr = currentNode;
} else { } else {
free(result); mem_free(result);
return NULL; return NULL;
} }
DEBUG("Succsessfully created type"); DEBUG("Succsessfully created type");
@ -692,7 +697,7 @@ int createRelationalOperation(Expression* ParentExpression, AST_NODE_PTR current
break; break;
} }
Type* result = malloc(sizeof(Type)); Type* result = mem_alloc(MemoryNamespaceSet,sizeof(Type));
result->impl.primitive = Int; result->impl.primitive = Int;
result->kind = TypeKindPrimitive; result->kind = TypeKindPrimitive;
result->nodePtr = currentNode; result->nodePtr = currentNode;
@ -789,7 +794,7 @@ int createBoolNotOperation(Expression *ParentExpression, AST_NODE_PTR currentNod
Type* Operand = ((Expression**)ParentExpression->impl.operation.operands)[0]->result; Type* Operand = ((Expression**)ParentExpression->impl.operation.operands)[0]->result;
Type* result = malloc(sizeof(Type)); Type* result = mem_alloc(MemoryNamespaceSet,sizeof(Type));
result->nodePtr = currentNode; result->nodePtr = currentNode;
if (Operand->kind == TypeKindBox || Operand->kind == TypeKindReference) { if (Operand->kind == TypeKindBox || Operand->kind == TypeKindReference) {
@ -856,7 +861,7 @@ int createBitOperation(Expression* ParentExpression, AST_NODE_PTR currentNode) {
break; break;
} }
Type *result = malloc(sizeof(Type)); Type *result = mem_alloc(MemoryNamespaceSet,sizeof(Type));
result->nodePtr = currentNode; result->nodePtr = currentNode;
Expression* lhs = ((Expression**) ParentExpression->impl.operation.operands->data)[0]; Expression* lhs = ((Expression**) ParentExpression->impl.operation.operands->data)[0];
@ -963,7 +968,7 @@ int createBitNotOperation(Expression* ParentExpression, AST_NODE_PTR currentNode
Type* Operand = ((Expression**) ParentExpression->impl.operation.operands)[0]->result; Type* Operand = ((Expression**) ParentExpression->impl.operation.operands)[0]->result;
Type* result = malloc(sizeof(Type)); Type* result = mem_alloc(MemoryNamespaceSet,sizeof(Type));
result->nodePtr = currentNode; result->nodePtr = currentNode;
if (Operand->kind == TypeKindPrimitive) { if (Operand->kind == TypeKindPrimitive) {
@ -1056,7 +1061,7 @@ int createBoxAccess(Expression* ParentExpression,AST_NODE_PTR currentNode) {
ParentExpression->impl.variable->impl.member.variable = boxVariable; ParentExpression->impl.variable->impl.member.variable = boxVariable;
//first one is the box itself //first one is the box itself
GArray* names = malloc(sizeof(GArray)); GArray* names = mem_alloc(MemoryNamespaceSet,sizeof(GArray));
if(currentNode->kind == AST_IdentList){ if(currentNode->kind == AST_IdentList){
for (size_t i = 1; i < currentNode->child_count; i++){ for (size_t i = 1; i < currentNode->child_count; i++){
g_array_append_val(names, currentNode->children[i]->value); g_array_append_val(names, currentNode->children[i]->value);
@ -1085,7 +1090,7 @@ int createTypeCast(Expression* ParentExpression, AST_NODE_PTR currentNode){
return SEMANTIC_ERROR; return SEMANTIC_ERROR;
} }
Type* target = malloc(sizeof(Type)); Type* target = mem_alloc(MemoryNamespaceSet,sizeof(Type));
int status = get_type_impl(currentNode->children[1], &target); int status = get_type_impl(currentNode->children[1], &target);
if (status) { if (status) {
print_diagnostic(current_file, &currentNode->children[1]->location, Error, "Unknown type"); print_diagnostic(current_file, &currentNode->children[1]->location, Error, "Unknown type");
@ -1104,7 +1109,7 @@ int createTransmute(Expression* ParentExpression, AST_NODE_PTR currentNode){
return SEMANTIC_ERROR; return SEMANTIC_ERROR;
} }
Type* target = malloc(sizeof(Type)); Type* target = mem_alloc(MemoryNamespaceSet,sizeof(Type));
int status = get_type_impl(currentNode->children[1], &target); int status = get_type_impl(currentNode->children[1], &target);
if (status){ if (status){
print_diagnostic(current_file, &currentNode->children[1]->location, Error, "Unknown type"); print_diagnostic(current_file, &currentNode->children[1]->location, Error, "Unknown type");
@ -1122,7 +1127,7 @@ int createTransmute(Expression* ParentExpression, AST_NODE_PTR currentNode){
Expression *createExpression(AST_NODE_PTR currentNode){ Expression *createExpression(AST_NODE_PTR currentNode){
DEBUG("create Expression"); DEBUG("create Expression");
Expression *expression = malloc(sizeof(Expression)); Expression *expression = mem_alloc(MemoryNamespaceSet,sizeof(Expression));
expression->nodePtr = currentNode; expression->nodePtr = currentNode;
switch(currentNode->kind){ switch(currentNode->kind){
@ -1404,7 +1409,7 @@ int createStatement(Block * Parentblock , AST_NODE_PTR currentNode){
} }
for(size_t i = 0; i < variable->len ; i++){ for(size_t i = 0; i < variable->len ; i++){
Statement * statement = malloc(sizeof(Statement)); Statement * statement = mem_alloc(MemoryNamespaceSet,sizeof(Statement));
statement->nodePtr = currentNode; statement->nodePtr = currentNode;
statement->kind = StatementKindDeclaration; statement->kind = StatementKindDeclaration;
@ -1425,7 +1430,7 @@ int createStatement(Block * Parentblock , AST_NODE_PTR currentNode){
} }
for(size_t i = 0; i < variable->len ; i++){ for(size_t i = 0; i < variable->len ; i++){
Statement * statement = malloc(sizeof(Statement)); Statement * statement = mem_alloc(MemoryNamespaceSet,sizeof(Statement));
statement->nodePtr = currentNode; statement->nodePtr = currentNode;
statement->kind = StatementKindDefinition; statement->kind = StatementKindDefinition;
@ -1436,7 +1441,7 @@ int createStatement(Block * Parentblock , AST_NODE_PTR currentNode){
} }
break; break;
case AST_While:{ case AST_While:{
Statement * statement = malloc(sizeof(Statement)); Statement * statement = mem_alloc(MemoryNamespaceSet,sizeof(Statement));
statement->nodePtr = currentNode; statement->nodePtr = currentNode;
statement->kind = StatementKindWhile; statement->kind = StatementKindWhile;
if(createWhile(statement, currentNode)){ if(createWhile(statement, currentNode)){
@ -1446,7 +1451,7 @@ int createStatement(Block * Parentblock , AST_NODE_PTR currentNode){
} }
break; break;
case AST_Stmt:{ case AST_Stmt:{
Statement * statement = malloc(sizeof(Statement)); Statement * statement = mem_alloc(MemoryNamespaceSet,sizeof(Statement));
statement->nodePtr = currentNode; statement->nodePtr = currentNode;
statement->kind = StatementKindBranch; statement->kind = StatementKindBranch;
if(createBranch(statement, currentNode)){ if(createBranch(statement, currentNode)){
@ -1456,7 +1461,7 @@ int createStatement(Block * Parentblock , AST_NODE_PTR currentNode){
} }
break; break;
case AST_Assign:{ case AST_Assign:{
Statement * statement = malloc(sizeof(Statement)); Statement * statement = mem_alloc(MemoryNamespaceSet,sizeof(Statement));
statement->nodePtr = currentNode; statement->nodePtr = currentNode;
statement->kind = StatementKindAssignment; statement->kind = StatementKindAssignment;
if(createAssign(statement, currentNode)){ if(createAssign(statement, currentNode)){
@ -1516,7 +1521,7 @@ int createParam(GArray * Paramlist ,AST_NODE_PTR currentNode){
DEBUG("create var for param"); DEBUG("create var for param");
Variable * paramvar = malloc(sizeof(Variable)); Variable * paramvar = mem_alloc(MemoryNamespaceSet,sizeof(Variable));
paramvar->kind = VariableKindDeclaration; paramvar->kind = VariableKindDeclaration;
paramvar->name = param.name; paramvar->name = param.name;
paramvar->nodePtr = currentNode; paramvar->nodePtr = currentNode;
@ -1547,7 +1552,7 @@ int createFunDef(Function * Parentfunction ,AST_NODE_PTR currentNode){
fundef.nodePtr = currentNode; fundef.nodePtr = currentNode;
fundef.name = nameNode->value; fundef.name = nameNode->value;
fundef.body = malloc(sizeof(Block)); fundef.body = mem_alloc(MemoryNamespaceSet,sizeof(Block));
fundef.parameter = g_array_new(FALSE, FALSE, sizeof(Parameter)); fundef.parameter = g_array_new(FALSE, FALSE, sizeof(Parameter));
DEBUG("paramlistlist child count: %i", paramlistlist->child_count); DEBUG("paramlistlist child count: %i", paramlistlist->child_count);
@ -1593,7 +1598,7 @@ int createFunDecl(Function * Parentfunction ,AST_NODE_PTR currentNode){
fundecl.nodePtr = currentNode; fundecl.nodePtr = currentNode;
fundecl.name = nameNode->value; fundecl.name = nameNode->value;
fundecl.parameter = malloc(sizeof(GArray)); fundecl.parameter = mem_alloc(MemoryNamespaceSet,sizeof(GArray));
for(size_t i = 0; i < paramlistlist->child_count; i++){ for(size_t i = 0; i < paramlistlist->child_count; i++){
@ -1621,7 +1626,7 @@ int createFunDecl(Function * Parentfunction ,AST_NODE_PTR currentNode){
int createFunction(GHashTable* functions, AST_NODE_PTR currentNode){ int createFunction(GHashTable* functions, AST_NODE_PTR currentNode){
assert(currentNode->kind == AST_Fun); assert(currentNode->kind == AST_Fun);
Function * fun = malloc(sizeof(Function)); Function * fun = mem_alloc(MemoryNamespaceSet,sizeof(Function));
functionParameter = g_hash_table_new(g_str_hash,g_str_equal); functionParameter = g_hash_table_new(g_str_hash,g_str_equal);
if(currentNode->child_count == 2){ if(currentNode->child_count == 2){
@ -1650,7 +1655,7 @@ int createFunction(GHashTable* functions, AST_NODE_PTR currentNode){
int createDeclMember(BoxType * ParentBox, AST_NODE_PTR currentNode){ int createDeclMember(BoxType * ParentBox, AST_NODE_PTR currentNode){
Type * declType = malloc(sizeof(Type)); Type * declType = mem_alloc(MemoryNamespaceSet,sizeof(Type));
int status = get_type_impl(currentNode->children[0],&declType); int status = get_type_impl(currentNode->children[0],&declType);
if(status){ if(status){
return SEMANTIC_ERROR; return SEMANTIC_ERROR;
@ -1658,7 +1663,7 @@ int createDeclMember(BoxType * ParentBox, AST_NODE_PTR currentNode){
AST_NODE_PTR nameList = currentNode->children[1]; AST_NODE_PTR nameList = currentNode->children[1];
for(size_t i = 0; i < nameList->child_count; i++){ for(size_t i = 0; i < nameList->child_count; i++){
BoxMember * decl = malloc(sizeof(BoxMember)); BoxMember * decl = mem_alloc(MemoryNamespaceSet,sizeof(BoxMember));
decl->name = nameList->children[i]->value; decl->name = nameList->children[i]->value;
decl->nodePtr = currentNode; decl->nodePtr = currentNode;
decl->box = ParentBox; decl->box = ParentBox;
@ -1677,7 +1682,7 @@ int createDefMember(BoxType *ParentBox, AST_NODE_PTR currentNode){
AST_NODE_PTR expressionNode = currentNode->children[1]; AST_NODE_PTR expressionNode = currentNode->children[1];
AST_NODE_PTR nameList = declNode->children[1]; AST_NODE_PTR nameList = declNode->children[1];
Type * declType = malloc(sizeof(Type)); Type * declType = mem_alloc(MemoryNamespaceSet,sizeof(Type));
int status = get_type_impl(currentNode->children[0],&declType); int status = get_type_impl(currentNode->children[0],&declType);
if(status){ if(status){
return SEMANTIC_ERROR; return SEMANTIC_ERROR;
@ -1689,7 +1694,7 @@ int createDefMember(BoxType *ParentBox, AST_NODE_PTR currentNode){
} }
for (size_t i = 0; i < nameList->child_count; i++){ for (size_t i = 0; i < nameList->child_count; i++){
BoxMember *def = malloc(sizeof(BoxMember)); BoxMember *def = mem_alloc(MemoryNamespaceSet,sizeof(BoxMember));
def->box = ParentBox; def->box = ParentBox;
def->type = declType; def->type = declType;
def->initalizer = init; def->initalizer = init;
@ -1704,7 +1709,7 @@ int createDefMember(BoxType *ParentBox, AST_NODE_PTR currentNode){
} }
int createBox(GHashTable *boxes, AST_NODE_PTR currentNode){ int createBox(GHashTable *boxes, AST_NODE_PTR currentNode){
BoxType * box = malloc(sizeof(BoxType)); BoxType * box = mem_alloc(MemoryNamespaceSet,sizeof(BoxType));
box->nodePtr = currentNode; box->nodePtr = currentNode;
const char * boxName = currentNode->children[0]->value; const char * boxName = currentNode->children[0]->value;
@ -1763,13 +1768,13 @@ int createTypeDef(GHashTable *types, AST_NODE_PTR currentNode){
AST_NODE_PTR nameNode = currentNode->children[1]; AST_NODE_PTR nameNode = currentNode->children[1];
Type * type = malloc(sizeof(Type)); Type * type = mem_alloc(MemoryNamespaceSet,sizeof(Type));
int status = get_type_impl( typeNode, &type); int status = get_type_impl( typeNode, &type);
if(status){ if(status){
return SEMANTIC_ERROR; return SEMANTIC_ERROR;
} }
Typedefine *def = malloc(sizeof(Typedefine)); Typedefine *def = mem_alloc(MemoryNamespaceSet,sizeof(Typedefine));
def->name = nameNode->value; def->name = nameNode->value;
def->nodePtr = currentNode; def->nodePtr = currentNode;
def->type = type; def->type = type;
@ -1797,11 +1802,11 @@ Module *create_set(AST_NODE_PTR currentNode){
//building current scope for module //building current scope for module
GHashTable *globalscope = malloc(sizeof(GHashTable*)); GHashTable *globalscope = mem_alloc(MemoryNamespaceSet,sizeof(GHashTable*));
globalscope = g_hash_table_new(g_str_hash,g_str_equal); globalscope = g_hash_table_new(g_str_hash,g_str_equal);
g_array_append_val(Scope, globalscope); g_array_append_val(Scope, globalscope);
Module *rootModule = malloc(sizeof(Module)); Module *rootModule = mem_alloc(MemoryNamespaceSet,sizeof(Module));
GHashTable *boxes = g_hash_table_new(g_str_hash,g_str_equal); GHashTable *boxes = g_hash_table_new(g_str_hash,g_str_equal);
GHashTable *types = g_hash_table_new(g_str_hash,g_str_equal); GHashTable *types = g_hash_table_new(g_str_hash,g_str_equal);