diff --git a/ast.cpp b/ast.cpp index 3352196..1a9e1bd 100644 --- a/ast.cpp +++ b/ast.cpp @@ -86,6 +86,9 @@ struct Ast_Call_Item: Ast_Expr{ Ast_Atom *name; // for calls only name, for compounds name | index Ast_Expr *item; Ast_Expr *index; + + Intern_String resolved_name; + S64 resolved_index; }; struct Ast_Call: Ast_Expr{ diff --git a/ccodegen.cpp b/ccodegen.cpp index fe8a1fb..8665694 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -341,7 +341,12 @@ gen_expr(Ast_Expr *ast, Ast_Type *type_of_var){ gen_simple_decl(node->resolved_type); gen(")"); gen("{"); + For(node->exprs){ + if(is_struct(node->resolved_type)) + gen("[%s] = ", it->resolved_name.str); + else if(is_array(node->resolved_type)) + gen("[%d] = ", (int)it->resolved_index); gen_expr(it->item); if(!node->exprs.is_last(&it)) gen(", "); } diff --git a/main.cpp b/main.cpp index 6bd50f5..5ac75b3 100644 --- a/main.cpp +++ b/main.cpp @@ -46,8 +46,7 @@ want to export all the symbols, we can namespace them optionally. [ ] - Should compound resolution use an algorithm to reorder compounds to initialize all fields in order [ ] - Switch [ ] - Some way to take slice of data -[ ] - elif -[ ] - Optional function renaming +[ ] - Optional function renaming in codegen [ ] - #assert that handles constants at compile time and vars at runtime [ ] - Comma notation when declaring variables thing1, thing2: S32 @@ -78,6 +77,7 @@ want to export all the symbols, we can namespace them optionally. [x] - Scope [x] - Hex 0x42 [x] - Rewrite where # happen, +[x] - elif [x] - cast -> [x] - Remodel compound from call to {} [x] - Fix codegen renames diff --git a/typechecking.cpp b/typechecking.cpp index bf82a22..b1ad869 100644 --- a/typechecking.cpp +++ b/typechecking.cpp @@ -605,7 +605,8 @@ resolve_compound_struct(Ast_Call *node, Ast_Type *type){ if(it->name){ For_Named(type->agg.members, m){ if(it->name->intern_val == m.name){ - + it->resolved_name = m.name; + it->resolved_index = type->agg.members.get_index(&m); // @copy_paste if(m.type == type_type) item_type = m.type_val; @@ -626,10 +627,15 @@ resolve_compound_struct(Ast_Call *node, Ast_Type *type){ if(default_counter >= type->agg.members.len) compiler_error(it->pos, "Too many struct initializers"); + Ast_Resolved_Member *m = &type->agg.members[default_counter]; + it->resolved_name = m->name; + it->resolved_index = default_counter; + m->visited = true; + // @copy_paste - item_type = type->agg.members[default_counter].type; - if(item_type == type_type) - item_type = type->agg.members[default_counter].type_val; + if(m->type == type_type) + item_type = m->type_val; + else item_type = m->type; default_counter+=1; } @@ -637,6 +643,8 @@ resolve_compound_struct(Ast_Call *node, Ast_Type *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); + + it->resolved_type = item_type; } For(type->agg.members) it.visited = false;