1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: enable new if expression implementation everywhere

This commit is contained in:
Alexander Medvednikov 2020-10-18 20:14:58 +02:00
parent 9cf5c9ac43
commit 958577b98b

View File

@ -724,6 +724,7 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) {
}
for i, stmt in stmts {
if i == stmts.len - 1 && tmp_var != '' {
// Handle if expressions, set the value of the last expression to the temp var.
g.write('$tmp_var = ')
}
g.stmt(stmt)
@ -3145,13 +3146,13 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
g.comp_if(node)
return
}
// For simpe if expressions we can use C's `?:`
// For simple if expressions we can use C's `?:`
// `if x > 0 { 1 } else { 2 }` => `(x > 0) ? (1) : (2)`
// For if expressions with multiple statements or another if expression inside, it's much
// easier to use a temp var, than do C tricks with commas, introduce special vars etc
// (as it used to be done).
needs_tmp_var := node.is_expr && g.pref.experimental &&
(node.branches[0].stmts.len > 1 || node.branches[0].stmts[0] is ast.IfExpr)
needs_tmp_var := node.is_expr &&
((node.branches[0].stmts.len > 1 || node.branches[0].stmts[0] is ast.IfExpr) || g.pref.autofree)
tmp := if needs_tmp_var { g.new_tmp_var() } else { '' }
mut cur_line := ''
if needs_tmp_var {