fmt: else, for in

pull/3811/head
Alexander Medvednikov 2020-02-22 16:59:50 +01:00
parent 5fef8390a1
commit d510cd1e0d
5 changed files with 44 additions and 22 deletions

View File

@ -2556,7 +2556,9 @@ fn (p mut Parser) assoc() string {
p.check_types(p.bool_expression(), f.typ)
p.gen(',')
if p.tok != .rcbr {
p.check(.comma)
if p.tok == .comma {
p.check(.comma)
}
}
p.fgen_nl()
}

View File

@ -319,6 +319,7 @@ pub:
pos token.Position
mut:
typ table.Type
has_else bool
}
pub struct MatchExpr {

View File

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

View File

@ -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,13 +393,19 @@ fn (f mut Fmt) expr(node ast.Expr) {
}
ast.StructInit {
type_sym := f.table.get_type_symbol(it.typ)
f.writeln('$type_sym.name{')
for i, field in it.fields {
f.write('\t$field: ')
f.expr(it.exprs[i])
f.writeln('')
// `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: ')
f.expr(it.exprs[i])
f.writeln('')
}
f.write('}')
}
f.write('}')
}
else {}
}

View File

@ -56,7 +56,7 @@ pub fn parse_stmt(text string, table &table.Table, scope &ast.Scope) ast.Stmt {
pref: &pref.Preferences{}
scope: scope
// scope: &ast.Scope{start_pos: 0, parent: 0}
}
p.init_parse_fns()
p.read_first_token()
@ -320,7 +320,7 @@ pub fn (p mut Parser) stmt() ast.Stmt {
return ast.ExprStmt{
expr: expr
// typ: typ
}
}
}
@ -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()
}
@ -1091,10 +1096,11 @@ fn (p mut Parser) if_expr() ast.Expr {
stmts: stmts
else_stmts: else_stmts
// typ: typ
pos: pos
has_else : has_else
// left: left
}
return node
}
@ -1493,10 +1499,10 @@ fn (p mut Parser) var_decl() ast.VarDecl {
node := ast.VarDecl{
name: name
expr: expr // p.expr(token.lowest_prec)
is_mut: is_mut
// typ: typ
pos: p.tok.position()
}
p.scope.register_var(node)
@ -1615,7 +1621,7 @@ fn (p mut Parser) match_expr() ast.Expr {
blocks: blocks
match_exprs: match_exprs
// typ: typ
cond: cond
}
return node