1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: assoc

This commit is contained in:
Alexander Medvednikov 2020-03-19 08:14:09 +01:00
parent 3dc3b11435
commit 28309da1f1
3 changed files with 29 additions and 1 deletions

View File

@ -591,6 +591,7 @@ pub:
pub struct Assoc {
pub:
typ table.Type
var_name string
fields []string
exprs []Expr

View File

@ -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 {

View File

@ -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)