diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index f1378280e6..c512f01323 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -591,6 +591,7 @@ pub: pub struct Assoc { pub: + typ table.Type var_name string fields []string exprs []Expr diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 6f551c2338..b31f197bc5 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -1349,7 +1349,29 @@ fn (g mut Gen) const_decl(node ast.ConstDecl) { } // { user | name: 'new name' } -fn (g mut Gen) assoc(node ast.Assoc) {} +fn (g mut Gen) assoc(node ast.Assoc) { + if node.typ == 0 { + return + } + g.writeln('// assoc') + styp := g.typ(node.typ) + g.writeln('($styp){') + for i, field in node.fields { + g.write('\t.$field = ') + g.expr(node.exprs[i]) + g.writeln(', ') + } + // Copy the rest of the fields. + sym := g.table.get_type_symbol(node.typ) + info := sym.info as table.Struct + for field in info.fields { + g.writeln('\t.$field.name = ${node.var_name}.$field.name,') + } + g.write('}') + if g.is_amp { + g.write(', sizeof($styp))') + } +} fn (g mut Gen) call_args(args []ast.CallArg) { for i, arg in args { diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index c4555a48c7..60bb585dc8 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -790,6 +790,10 @@ pub fn (p mut Parser) expr(precedence int) ast.Expr { } else { name := p.check_name() + var := p.scope.find_var(name) or { + p.error('unknown variable `$name`') + return node + } mut fields := []string mut vals := []ast.Expr p.check(.pipe) @@ -810,6 +814,7 @@ pub fn (p mut Parser) expr(precedence int) ast.Expr { fields: fields exprs: vals pos: p.tok.position() + typ: var.typ } } p.check(.rcbr)