fmt: assoc

pull/3810/head
Alexander Medvednikov 2020-02-22 14:39:25 +01:00
parent cd41967aa1
commit 5fef8390a1
2 changed files with 42 additions and 16 deletions

View File

@ -145,6 +145,11 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
f.stmts(it.stmts)
f.writeln('}\n')
}
ast.ForInStmt {
f.writeln(' for in {')
f.stmts(it.stmts)
f.writeln('}')
}
ast.ForStmt {
f.write('for ')
f.expr(it.cond)
@ -241,6 +246,19 @@ fn (f mut Fmt) expr(node ast.Expr) {
f.write(' $it.op.str() ')
f.expr(it.val)
}
ast.Assoc {
f.writeln('{')
// f.indent++
f.writeln('\t$it.name |')
// TODO StructInit copy pasta
for i, field in it.fields {
f.write('\t$field: ')
f.expr(it.exprs[i])
f.writeln('')
}
// f.indent--
f.write('}')
}
ast.BoolLiteral {
f.write(it.val.str())
}

View File

@ -466,7 +466,10 @@ pub fn (p mut Parser) parse_ident(is_c bool) ast.Ident {
else {
// handle consts/fns in checker
ident.kind = .unresolved
return {ident | name: p.prepend_mod(name)}
return {
ident |
name:p.prepend_mod(name)
}
}
}
@ -666,8 +669,8 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
mut keys := []ast.Expr
mut vals := []ast.Expr
for p.tok.kind != .rcbr && p.tok.kind != .eof {
//p.check(.str)
key, _ := p.expr(0)
// p.check(.str)
key,_ := p.expr(0)
keys << key
p.check(.colon)
val,_ := p.expr(0)
@ -676,19 +679,22 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
p.next()
}
}
node = ast.MapInit {
keys:keys
node = ast.MapInit{
keys: keys
vals: vals
pos: p.tok.position()
}
}
else {
name := p.check_name()
mut fields := []string
mut vals := []ast.Expr
p.check(.pipe)
for {
p.check_name()
fields << p.check_name()
p.check(.colon)
p.expr(0)
expr,_ := p.expr(0)
vals << expr
if p.tok.kind == .comma {
p.check(.comma)
}
@ -698,6 +704,8 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
}
node = ast.Assoc{
name: name
fields: fields
exprs: vals
}
}
p.check(.rcbr)