cgen: format if_expr generated c codes (#9084)

pull/9085/head
yuyi 2021-03-03 20:33:00 +08:00 committed by GitHub
parent a5dd7faf3c
commit 6825b4cb30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 8 deletions

View File

@ -4117,14 +4117,12 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
if node.typ.has_flag(.optional) {
g.inside_if_optional = true
}
g.write('/*experimental if expr*/')
styp := g.typ(node.typ)
// g.insert_before_stmt('$styp $tmp;')
cur_line = g.go_before_stmt(0)
g.empty_line = true
g.writeln('$styp $tmp; /* if prepend */')
} else if node.is_expr || g.inside_ternary != 0 {
g.inside_ternary++
// g.inside_if_expr = true
g.write('(')
for i, branch in node.branches {
if i > 0 {
@ -4239,8 +4237,8 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
}
g.writeln('}')
if needs_tmp_var {
// g.writeln('$cur_line $tmp; /*Z*/')
g.write('$cur_line $tmp /*Z*/')
g.empty_line = false
g.write('$cur_line $tmp')
}
if node.typ.has_flag(.optional) {
g.inside_if_optional = false
@ -4455,9 +4453,6 @@ fn (mut g Gen) return_statement(node ast.Return) {
if expr is ast.Ident {
g.returned_var_name = expr.name
}
if tmp != '' {
g.writeln('; // free tmp exprs + all vars before return')
}
g.writeln(';')
// autofree before `return`
// set free_parent_scopes to true, since all variables defined in parent