Wanky constant in structs sort of working but long way to go
This commit is contained in:
@@ -314,6 +314,11 @@ gen_ast(Ast *ast){
|
|||||||
genln("");
|
genln("");
|
||||||
gen_ast(it);
|
gen_ast(it);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
For(agg->const_members){
|
||||||
|
genln("");
|
||||||
|
gen_ast(it);
|
||||||
|
}
|
||||||
global_indent--;
|
global_indent--;
|
||||||
genln("};");
|
genln("};");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -554,19 +554,15 @@ is_atom(Ast *ast){
|
|||||||
|
|
||||||
function Ast *
|
function Ast *
|
||||||
query_struct(Ast_Struct *agg, Intern_String string){
|
query_struct(Ast_Struct *agg, Intern_String string){
|
||||||
|
|
||||||
For(agg->members){
|
For(agg->members){
|
||||||
if(it->name == string){
|
if(it->name == string){
|
||||||
return it;
|
return it;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
For(agg->const_members){
|
For(agg->const_members){
|
||||||
if(it->name == string){
|
if(it->name == string){
|
||||||
return it;
|
return it;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
21
new_type.cpp
21
new_type.cpp
@@ -181,6 +181,7 @@ type_complete(Ast_Resolved_Type *type){
|
|||||||
}
|
}
|
||||||
Ast_Struct *node = (Ast_Struct *)type->ast;
|
Ast_Struct *node = (Ast_Struct *)type->ast;
|
||||||
|
|
||||||
|
// @todo: compute size, alignement, offset !!!
|
||||||
// @note: resolve all the struct members
|
// @note: resolve all the struct members
|
||||||
type->kind = TYPE_COMPLETING;
|
type->kind = TYPE_COMPLETING;
|
||||||
{
|
{
|
||||||
@@ -195,18 +196,20 @@ type_complete(Ast_Resolved_Type *type){
|
|||||||
type->agg.members = members.tight_copy(pctx->perm);
|
type->agg.members = members.tight_copy(pctx->perm);
|
||||||
}
|
}
|
||||||
|
|
||||||
// @note: complete struct
|
|
||||||
type->kind = TYPE_STRUCT;
|
type->kind = TYPE_STRUCT;
|
||||||
// @todo: compute size, alignement, offset
|
|
||||||
pctx->resolving_package->ordered.add((Ast_Named *)node->parent);
|
// @note: resolve constant members after the struct got resolved
|
||||||
|
// this way we avoid a problem where we start resolving the function
|
||||||
|
// and this function has parameter of type parent struct
|
||||||
|
// which is being resolved right now, cyclic dependency happens.
|
||||||
|
// constants arent required to make struct work
|
||||||
|
For(node->const_members){
|
||||||
|
Operand op = resolve_binding(it);
|
||||||
|
Intern_String name = ast_get_name(it);
|
||||||
|
sym_new_resolved(SYM_CONST, name, op.type, op.value, it);
|
||||||
}
|
}
|
||||||
|
|
||||||
function Ast_Resolved_Type *
|
pctx->resolving_package->ordered.add((Ast_Named *)node->parent);
|
||||||
type_struct(Ast *ast, Array<Ast_Resolved_Member> members){
|
|
||||||
Ast_Resolved_Type *result = type_new(pctx->perm, TYPE_STRUCT, 0, 0); // @todo: align,size
|
|
||||||
result->agg.members = members.tight_copy(pctx->perm);
|
|
||||||
result->ast = ast;
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function void
|
function void
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ Arena :: struct
|
|||||||
constant_outside :: 10000
|
constant_outside :: 10000
|
||||||
|
|
||||||
get_len :: (s: *Arena): int
|
get_len :: (s: *Arena): int
|
||||||
return s.next.len
|
return s.next.constant_inside
|
||||||
|
|
||||||
|
|
||||||
string16: Str16
|
string16: Str16
|
||||||
|
|||||||
@@ -246,6 +246,15 @@ resolve_stmt(Ast *ast, Ast_Resolved_Type *ret){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function Operand
|
||||||
|
operand(Sym *sym){
|
||||||
|
Operand result = {};
|
||||||
|
result.type = sym->type;
|
||||||
|
result.is_const = sym->kind == SYM_CONST ? true : false;
|
||||||
|
result.value = sym->value;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
function Operand
|
function Operand
|
||||||
resolve_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type, Sym *const_sym){
|
resolve_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type, Sym *const_sym){
|
||||||
if(!ast) return {}; // @todo: add option for better error prevention
|
if(!ast) return {}; // @todo: add option for better error prevention
|
||||||
@@ -277,9 +286,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){
|
||||||
result.type = sym->type;
|
result = operand(sym);
|
||||||
result.is_const = sym->kind == SYM_CONST ? true : false;
|
|
||||||
result.value = sym->value;
|
|
||||||
sym_new_resolved(SYM_CONST, sym->name, sym->type, sym->value, node);
|
sym_new_resolved(SYM_CONST, sym->name, sym->type, sym->value, node);
|
||||||
}
|
}
|
||||||
else invalid_codepath;
|
else invalid_codepath;
|
||||||
@@ -598,7 +605,7 @@ resolve_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type, Sym *const_sym){
|
|||||||
Ast *query = query_struct(agg, ident->intern_val);
|
Ast *query = query_struct(agg, ident->intern_val);
|
||||||
if(query){
|
if(query){
|
||||||
Sym *sym = resolved_get(query);
|
Sym *sym = resolved_get(query);
|
||||||
result.type = sym->type;
|
result = operand(sym);
|
||||||
sym_new_resolved(SYM_VAR, {}, sym->type, {}, ident);
|
sym_new_resolved(SYM_VAR, {}, sym->type, {}, ident);
|
||||||
} else parsing_error(ident->pos, "No such member in struct");
|
} else parsing_error(ident->pos, "No such member in struct");
|
||||||
|
|
||||||
@@ -627,30 +634,6 @@ resolve_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type, Sym *const_sym){
|
|||||||
BREAK();
|
BREAK();
|
||||||
}
|
}
|
||||||
|
|
||||||
// @todo: add const first level function? expecting only structs, exprs, lambdas
|
|
||||||
CASE(STRUCT, Struct){
|
|
||||||
assert(const_sym);
|
|
||||||
|
|
||||||
Scratch scratch;
|
|
||||||
Array<Ast_Resolved_Member> members = {scratch};
|
|
||||||
For(node->members){
|
|
||||||
Operand op = resolve_binding(it);
|
|
||||||
|
|
||||||
Intern_String name = {};
|
|
||||||
if(is_flag_set(it->flags, AST_BINDING)){
|
|
||||||
Ast_Named *named = (Ast_Named *)it;
|
|
||||||
name = named->name;
|
|
||||||
}
|
|
||||||
|
|
||||||
sym_new_resolved(SYM_VAR, name, op.type, {}, it);
|
|
||||||
members.add({op.type, name});
|
|
||||||
}
|
|
||||||
Ast_Resolved_Type *resolved = type_struct(node, members);
|
|
||||||
Operand result = {type_type, true}; result.type_val = resolved;
|
|
||||||
return result;
|
|
||||||
BREAK();
|
|
||||||
}
|
|
||||||
|
|
||||||
invalid_default_case;
|
invalid_default_case;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user