More order independent globals
This commit is contained in:
@@ -285,7 +285,7 @@ gen_ast(Ast *ast){
|
||||
else if(sym->type == type_type){
|
||||
if(sym->type_val->kind == TYPE_STRUCT){
|
||||
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);
|
||||
global_indent++;
|
||||
For(agg->members){
|
||||
|
||||
4
main.cpp
4
main.cpp
@@ -47,8 +47,8 @@ int main(){
|
||||
test_intern_table();
|
||||
lex_test();
|
||||
|
||||
String result = compile_file("globals.kl"_s);
|
||||
// String result = compile_file("structs.kl"_s);
|
||||
// String result = compile_file("globals.kl"_s);
|
||||
String result = compile_file("structs.kl"_s);
|
||||
// String result = compile_file("order_independent_globals.kl"_s);
|
||||
printf("%s", result.str);
|
||||
|
||||
|
||||
25
new_ast.cpp
25
new_ast.cpp
@@ -211,9 +211,11 @@ struct Ast_Array: Ast_Expr{
|
||||
Ast_Expr *expr;
|
||||
};
|
||||
|
||||
struct Ast_Resolved_Type;
|
||||
struct Ast_Struct: Ast_Expr{
|
||||
// Required to be Ast_Struct or Ast_Var or Ast_Const
|
||||
Array<Ast *> members;
|
||||
Ast_Resolved_Type *type;
|
||||
};
|
||||
|
||||
struct Ast_Named:Ast{
|
||||
@@ -469,11 +471,20 @@ ast_package(Token *pos, String name, Array<Ast_Named *> decls){
|
||||
// Utillities
|
||||
//-----------------------------------------------------------------------------
|
||||
function Ast_Struct *
|
||||
const_get_struct(Ast *ast){
|
||||
const_try_getting_struct(Ast *ast){
|
||||
assert(ast->kind == AST_CONST);
|
||||
Ast_Const *constant = (Ast_Const *)ast;
|
||||
assert(constant->value->kind == AST_STRUCT);
|
||||
if(constant->value->kind == AST_STRUCT){
|
||||
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
|
||||
@@ -482,3 +493,13 @@ ast_get_name(Ast *ast){
|
||||
auto constant = (Ast_Named *)ast;
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -269,6 +269,7 @@ resolve_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type, Sym *const_sym){
|
||||
result.is_const = true;
|
||||
}
|
||||
else if(sym->kind == SYM_CONST || sym->kind == SYM_VAR){
|
||||
type_complete(sym->type);
|
||||
result.type = sym->type;
|
||||
result.is_const = sym->kind == SYM_CONST ? true : false;
|
||||
result.value = sym->value;
|
||||
@@ -543,6 +544,9 @@ function void
|
||||
resolve_package(Ast_Package *package){
|
||||
For(package->decls){
|
||||
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);
|
||||
if(decl->kind == AST_CONST) {
|
||||
sym->kind = SYM_CONST;
|
||||
auto constant = (Ast_Const *)decl;
|
||||
if(constant->value->kind == AST_STRUCT){
|
||||
sym->type = type_incomplete(sym);
|
||||
Ast_Struct *s = const_try_getting_struct(decl);
|
||||
if(s){
|
||||
s->type = type_incomplete(sym);
|
||||
sym->type_val = s->type;
|
||||
sym->type = type_type;
|
||||
sym->state = SYM_RESOLVED;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -185,6 +185,7 @@ type_complete(Ast_Resolved_Type *type){
|
||||
// @note: complete struct
|
||||
type->kind = TYPE_STRUCT;
|
||||
// @todo: compute size, alignement, offset
|
||||
pctx->resolving_package->ordered.add((Ast_Named *)node->parent);
|
||||
}
|
||||
|
||||
function Ast_Resolved_Type *
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
|
||||
arena_pointer: *Arena = null
|
||||
Arena :: struct
|
||||
// next: *Arena
|
||||
next: *Arena
|
||||
data: *int
|
||||
len : int
|
||||
cap : int
|
||||
|
||||
Reference in New Issue
Block a user