fmt: else, for in
parent
5fef8390a1
commit
d510cd1e0d
|
@ -2556,8 +2556,10 @@ fn (p mut Parser) assoc() string {
|
|||
p.check_types(p.bool_expression(), f.typ)
|
||||
p.gen(',')
|
||||
if p.tok != .rcbr {
|
||||
if p.tok == .comma {
|
||||
p.check(.comma)
|
||||
}
|
||||
}
|
||||
p.fgen_nl()
|
||||
}
|
||||
// Copy the rest of the fields
|
||||
|
|
|
@ -319,6 +319,7 @@ pub:
|
|||
pos token.Position
|
||||
mut:
|
||||
typ table.Type
|
||||
has_else bool
|
||||
}
|
||||
|
||||
pub struct MatchExpr {
|
||||
|
|
|
@ -21,7 +21,8 @@ pub fn (node &FnDecl) str(t &table.Table) string {
|
|||
m := if node.rec_mut { 'mut ' } else { '' }
|
||||
receiver = '($node.receiver.name ${m}$name) '
|
||||
}
|
||||
f.write('fn ${receiver}${node.name}(')
|
||||
name := node.name.after('.')
|
||||
f.write('fn ${receiver}${name}(')
|
||||
for i, arg in node.args {
|
||||
is_last_arg := i == node.args.len - 1
|
||||
should_add_type := is_last_arg || node.args[i + 1].typ != arg.typ
|
||||
|
|
|
@ -43,7 +43,7 @@ pub fn fmt(file ast.File, table &table.Table) string {
|
|||
pub fn (f mut Fmt) write(s string) {
|
||||
if f.indent > 0 && f.empty_line {
|
||||
f.out.write(tabs[f.indent])
|
||||
f.line_len += f.indent
|
||||
f.line_len += f.indent * 4
|
||||
}
|
||||
f.out.write(s)
|
||||
f.line_len += s.len
|
||||
|
@ -126,7 +126,8 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
|
|||
f.writeln('const (')
|
||||
f.indent++
|
||||
for i, field in it.fields {
|
||||
f.write('$field.name = ')
|
||||
name := field.name.after('.')
|
||||
f.write('$name = ')
|
||||
f.expr(it.exprs[i])
|
||||
f.writeln('')
|
||||
}
|
||||
|
@ -146,7 +147,9 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
|
|||
f.writeln('}\n')
|
||||
}
|
||||
ast.ForInStmt {
|
||||
f.writeln(' for in {')
|
||||
f.write('for $it.var in ')
|
||||
f.expr(it.cond)
|
||||
f.writeln(' {')
|
||||
f.stmts(it.stmts)
|
||||
f.writeln('}')
|
||||
}
|
||||
|
@ -231,7 +234,7 @@ fn (f mut Fmt) expr(node ast.Expr) {
|
|||
// type_sym := f.table.get_type_symbol(it.typ)
|
||||
f.write('[')
|
||||
for i, expr in it.exprs {
|
||||
if i > 0 {
|
||||
if i > 0 && it.exprs.len > 1 {
|
||||
f.wrap_long_line()
|
||||
}
|
||||
f.expr(expr)
|
||||
|
@ -294,7 +297,10 @@ fn (f mut Fmt) expr(node ast.Expr) {
|
|||
f.write(' ')
|
||||
}
|
||||
f.write('}')
|
||||
if it.else_stmts.len > 0 {
|
||||
if it.has_else {
|
||||
f.write(' else ')
|
||||
}
|
||||
else if it.else_stmts.len > 0 {
|
||||
f.write(' else {')
|
||||
if single_line {
|
||||
f.write(' ')
|
||||
|
@ -387,6 +393,11 @@ fn (f mut Fmt) expr(node ast.Expr) {
|
|||
}
|
||||
ast.StructInit {
|
||||
type_sym := f.table.get_type_symbol(it.typ)
|
||||
// `Foo{}` on one line if there are no fields
|
||||
if it.fields.len == 0 {
|
||||
f.write('$type_sym.name{}')
|
||||
}
|
||||
else {
|
||||
f.writeln('$type_sym.name{')
|
||||
for i, field in it.fields {
|
||||
f.write('\t$field: ')
|
||||
|
@ -395,6 +406,7 @@ fn (f mut Fmt) expr(node ast.Expr) {
|
|||
}
|
||||
f.write('}')
|
||||
}
|
||||
}
|
||||
else {}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -985,7 +985,7 @@ fn (p mut Parser) for_statement() ast.Stmt {
|
|||
p.check(.key_in)
|
||||
mut elem_type := table.void_type
|
||||
// arr_expr
|
||||
_,arr_typ := p.expr(0)
|
||||
cond,arr_typ := p.expr(0)
|
||||
// array / map
|
||||
if table.type_idx(arr_typ) == table.string_type_idx {
|
||||
elem_type = table.byte_type
|
||||
|
@ -1026,6 +1026,8 @@ fn (p mut Parser) for_statement() ast.Stmt {
|
|||
return ast.ForInStmt{
|
||||
stmts: stmts
|
||||
pos: p.tok.position()
|
||||
cond: cond
|
||||
var: var_name
|
||||
}
|
||||
}
|
||||
// `for cond {`
|
||||
|
@ -1057,13 +1059,16 @@ fn (p mut Parser) if_expr() ast.Expr {
|
|||
cond,_ = p.expr(0)
|
||||
}
|
||||
p.inside_if = false
|
||||
mut has_else := false
|
||||
stmts := p.parse_block()
|
||||
mut else_stmts := []ast.Stmt
|
||||
if p.tok.kind == .key_else {
|
||||
p.check(.key_else)
|
||||
if p.tok.kind == .key_if {
|
||||
p.if_expr()
|
||||
// The next if block is handled by next if_expr()
|
||||
has_else = true
|
||||
}
|
||||
// p.if_expr()
|
||||
else {
|
||||
else_stmts = p.parse_block()
|
||||
}
|
||||
|
@ -1093,6 +1098,7 @@ fn (p mut Parser) if_expr() ast.Expr {
|
|||
// typ: typ
|
||||
|
||||
pos: pos
|
||||
has_else : has_else
|
||||
// left: left
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue