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