More order independent globals

This commit is contained in:
Krzosa Karol
2022-05-29 22:07:08 +02:00
parent 2ad3131dba
commit 4434ad1fb5
6 changed files with 39 additions and 11 deletions

View File

@@ -285,7 +285,7 @@ gen_ast(Ast *ast){
else if(sym->type == type_type){ else if(sym->type == type_type){
if(sym->type_val->kind == TYPE_STRUCT){ if(sym->type_val->kind == TYPE_STRUCT){
Ast_Struct *agg = const_get_struct(sym->type_val->sym->ast); Ast_Struct *agg = const_get_struct(sym->type_val->sym->ast);
if(node->value->kind == AST_STRUCT){ if(agg){
gen("struct %s{", node->name.str); gen("struct %s{", node->name.str);
global_indent++; global_indent++;
For(agg->members){ For(agg->members){

View File

@@ -47,8 +47,8 @@ int main(){
test_intern_table(); test_intern_table();
lex_test(); lex_test();
String result = compile_file("globals.kl"_s); // String result = compile_file("globals.kl"_s);
// String result = compile_file("structs.kl"_s); String result = compile_file("structs.kl"_s);
// String result = compile_file("order_independent_globals.kl"_s); // String result = compile_file("order_independent_globals.kl"_s);
printf("%s", result.str); printf("%s", result.str);

View File

@@ -211,9 +211,11 @@ struct Ast_Array: Ast_Expr{
Ast_Expr *expr; Ast_Expr *expr;
}; };
struct Ast_Resolved_Type;
struct Ast_Struct: Ast_Expr{ struct Ast_Struct: Ast_Expr{
// Required to be Ast_Struct or Ast_Var or Ast_Const // Required to be Ast_Struct or Ast_Var or Ast_Const
Array<Ast *> members; Array<Ast *> members;
Ast_Resolved_Type *type;
}; };
struct Ast_Named:Ast{ struct Ast_Named:Ast{
@@ -469,11 +471,20 @@ ast_package(Token *pos, String name, Array<Ast_Named *> decls){
// Utillities // Utillities
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
function Ast_Struct * function Ast_Struct *
const_get_struct(Ast *ast){ const_try_getting_struct(Ast *ast){
assert(ast->kind == AST_CONST); assert(ast->kind == AST_CONST);
Ast_Const *constant = (Ast_Const *)ast; Ast_Const *constant = (Ast_Const *)ast;
assert(constant->value->kind == AST_STRUCT); if(constant->value->kind == AST_STRUCT){
return (Ast_Struct *)constant->value; return (Ast_Struct *)constant->value;
}
return 0;
}
function Ast_Struct *
const_get_struct(Ast *ast){
auto result = const_try_getting_struct(ast);
assert(result);
return result;
} }
function Intern_String function Intern_String
@@ -482,3 +493,13 @@ ast_get_name(Ast *ast){
auto constant = (Ast_Named *)ast; auto constant = (Ast_Named *)ast;
return constant->name; return constant->name;
} }
function B32
ast_is_struct(Ast *ast){
if(ast->kind == AST_CONST){
auto a = (Ast_Const *)ast;
B32 result = a->agg->kind == AST_STRUCT;
return result;
}
return false;
}

View File

@@ -269,6 +269,7 @@ resolve_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type, Sym *const_sym){
result.is_const = true; result.is_const = true;
} }
else if(sym->kind == SYM_CONST || sym->kind == SYM_VAR){ else if(sym->kind == SYM_CONST || sym->kind == SYM_VAR){
type_complete(sym->type);
result.type = sym->type; result.type = sym->type;
result.is_const = sym->kind == SYM_CONST ? true : false; result.is_const = sym->kind == SYM_CONST ? true : false;
result.value = sym->value; result.value = sym->value;
@@ -543,6 +544,9 @@ function void
resolve_package(Ast_Package *package){ resolve_package(Ast_Package *package){
For(package->decls){ For(package->decls){
resolve_name(it[0]->pos, it[0]->name); resolve_name(it[0]->pos, it[0]->name);
if(ast_is_struct(it[0])){
type_complete(const_get_struct(it[0])->type);
}
} }
} }
@@ -567,9 +571,11 @@ parse_file(){
Sym *sym = sym_new(SYM_VAR, decl->name, decl); Sym *sym = sym_new(SYM_VAR, decl->name, decl);
if(decl->kind == AST_CONST) { if(decl->kind == AST_CONST) {
sym->kind = SYM_CONST; sym->kind = SYM_CONST;
auto constant = (Ast_Const *)decl; Ast_Struct *s = const_try_getting_struct(decl);
if(constant->value->kind == AST_STRUCT){ if(s){
sym->type = type_incomplete(sym); s->type = type_incomplete(sym);
sym->type_val = s->type;
sym->type = type_type;
sym->state = SYM_RESOLVED; sym->state = SYM_RESOLVED;
} }
} }

View File

@@ -185,6 +185,7 @@ type_complete(Ast_Resolved_Type *type){
// @note: complete struct // @note: complete struct
type->kind = TYPE_STRUCT; type->kind = TYPE_STRUCT;
// @todo: compute size, alignement, offset // @todo: compute size, alignement, offset
pctx->resolving_package->ordered.add((Ast_Named *)node->parent);
} }
function Ast_Resolved_Type * function Ast_Resolved_Type *

View File

@@ -1,7 +1,7 @@
arena_pointer: *Arena = null arena_pointer: *Arena = null
Arena :: struct Arena :: struct
// next: *Arena next: *Arena
data: *int data: *int
len : int len : int
cap : int cap : int