checker/fmt/cgen: fun vfmt on changes from previous commit

pull/4079/head
Joe Conigliaro 2020-03-21 00:46:00 +11:00
parent a331abf675
commit 15d4b5fcdb
3 changed files with 8 additions and 14 deletions

View File

@ -912,7 +912,7 @@ pub fn (c mut Checker) if_expr(node mut ast.IfExpr) table.Type {
node.typ = table.void_type
for i, branch in node.branches {
typ := c.expr(branch.cond)
if i < node.branches.len-1 || !node.has_else {
if i < node.branches.len - 1 || !node.has_else {
typ_sym := c.table.get_type_symbol(typ)
// if typ_sym.kind != .bool {
if table.type_idx(typ) != table.bool_type_idx {
@ -922,7 +922,7 @@ pub fn (c mut Checker) if_expr(node mut ast.IfExpr) table.Type {
c.stmts(branch.stmts)
}
if node.has_else && node.is_expr {
last_branch := node.branches[node.branches.len-1]
last_branch := node.branches[node.branches.len - 1]
if last_branch.stmts.len > 0 {
match last_branch.stmts[last_branch.stmts.len - 1] {
ast.ExprStmt {
@ -933,7 +933,7 @@ pub fn (c mut Checker) if_expr(node mut ast.IfExpr) table.Type {
return t
}
else {}
}
}
}
}
return table.bool_type

View File

@ -406,9 +406,6 @@ fn (f mut Fmt) expr(node ast.Expr) {
f.write(' ')
}
}
// if !single_line {
// f.writeln('')
// }
f.write('}')
f.single_line_if = false
}

View File

@ -1176,14 +1176,14 @@ fn (g mut Gen) if_expr(node ast.IfExpr) {
}
// one line ?:
// TODO clean this up once `is` is supported
// TODO: make sure only one stmt in eac branch
// TODO: make sure only one stmt in each branch
if node.is_expr && node.branches.len >= 2 && node.has_else && type_sym.kind != .void {
g.inside_ternary = true
for i, branch in node.branches {
if i > 0 {
g.write(' : ')
}
if i < node.branches.len-1 || !node.has_else {
if i < node.branches.len - 1 || !node.has_else {
g.expr(branch.cond)
g.write(' ? ')
}
@ -1210,15 +1210,14 @@ fn (g mut Gen) if_expr(node ast.IfExpr) {
g.expr(branch.cond)
g.writeln(') {')
}
}
}
}
else if i < node.branches.len-1 || !node.has_else {
else if i < node.branches.len - 1 || !node.has_else {
g.writeln('} else if (')
g.expr(branch.cond)
g.write(') {')
}
else if i == node.branches.len-1 && node.has_else {
else if i == node.branches.len - 1 && node.has_else {
if is_guard {
g.writeln('} if (!$guard_ok) { /* else */')
}
@ -1229,9 +1228,7 @@ fn (g mut Gen) if_expr(node ast.IfExpr) {
// Assign ret value
// if i == node.stmts.len - 1 && type_sym.kind != .void {}
// g.writeln('$tmp =')
//
g.stmts(branch.stmts)
// g.writeln('')
}
if is_guard {
g.write('}')