Compounds for structs

This commit is contained in:
Krzosa Karol
2022-06-11 23:48:17 +02:00
parent e085bce77a
commit 6a612cf1b4
4 changed files with 45 additions and 6 deletions

View File

@@ -62,3 +62,10 @@ thing0 :: 10
thing1 :: thing0 + 11
thing2 :: thing1 + 20
combin :: thing0 + thing1 + thing2
Some :: struct
len: S64
cap: S64
some := Some{3, 2}
other := Some{len = 1, cap = 3}

View File

@@ -203,12 +203,12 @@ int main(int argument_count, char **arguments){
#endif
Scratch scratch;
Array<String> files = {scratch};
files.add("lambdas.kl"_s);
files.add("order1.kl"_s);
files.add("order2.kl"_s);
files.add("new_types.kl"_s);
files.add("enums.kl"_s);
// files.add("G.globals.kl"_s);
// files.add("lambdas.kl"_s);
// files.add("order1.kl"_s);
// files.add("order2.kl"_s);
// files.add("new_types.kl"_s);
// files.add("enums.kl"_s);
files.add("G.globals.kl"_s);
// files.add("euler.kl"_s);
String result = compile_files(files);
printf("%s", result.str);

View File

@@ -614,7 +614,38 @@ resolve_compound_array(Ast_Call *node, Ast_Type *type){
function void
resolve_compound_struct(Ast_Call *node, Ast_Type *type){
// type->agg.members
S64 default_counter = 0;
For(node->exprs){
if(it->index)
compiler_error(it->pos, "Struct cant be indexed");
Ast_Type *item_type = 0;
if(it->name){
For_It(type->agg.members, m){
if(it->name->intern_val == m.name){
item_type = m.type;
if(m.visited){
compiler_error(it->pos, "Field already initialized");
} else m.visited = true;
break;
}
}
if(!item_type)
compiler_error(it->pos, "No member with that name");
}
else{
if(default_counter == -1)
compiler_error(it->pos, "Cant mix named fields and normal fields");
if(default_counter >= type->agg.members.len)
compiler_error(it->pos, "Too many struct initializers");
item_type = type->agg.members[default_counter++].type;
}
assert(item_type);
Operand item = resolve_expr(it->item, AST_CANT_BE_NULL);
make_sure_value_is_compatible_with_type(it->pos, &item, item_type, TYPE_AND_EXPR_REQUIRED);
}
}
function Operand

View File

@@ -86,6 +86,7 @@ struct Ast_Resolved_Member{
Ast_Type *type;
Intern_String name;
U64 offset;
B32 visited;
};
#define ARRAY_SIZE_INFERRED (-1)