Skip to content

Commit

Permalink
cgen: simplify in infix_expr_and_or_op() (vlang#21745)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 authored Jun 27, 2024
1 parent d9a300c commit fccd7cd
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 127 deletions.
87 changes: 45 additions & 42 deletions vlib/v/gen/c/if.v
Original file line number Diff line number Diff line change
Expand Up @@ -39,34 +39,21 @@ fn (mut g Gen) need_tmp_var_in_expr(expr ast.Expr) bool {
return true
}
match expr {
ast.Ident {
return expr.or_expr.kind != .absent
}
ast.StringInterLiteral {
for e in expr.exprs {
if g.need_tmp_var_in_expr(e) {
return true
}
}
}
ast.IfExpr {
if g.need_tmp_var_in_if(expr) {
ast.ArrayInit {
if g.need_tmp_var_in_expr(expr.len_expr) {
return true
}
}
ast.IfGuardExpr {
return true
}
ast.InfixExpr {
if g.need_tmp_var_in_expr(expr.left) {
if g.need_tmp_var_in_expr(expr.cap_expr) {
return true
}
if g.need_tmp_var_in_expr(expr.right) {
if g.need_tmp_var_in_expr(expr.init_expr) {
return true
}
}
ast.MatchExpr {
return true
for elem_expr in expr.exprs {
if g.need_tmp_var_in_expr(elem_expr) {
return true
}
}
}
ast.CallExpr {
if expr.is_method {
Expand All @@ -90,9 +77,6 @@ fn (mut g Gen) need_tmp_var_in_expr(expr ast.Expr) bool {
ast.CastExpr {
return g.need_tmp_var_in_expr(expr.expr)
}
ast.ParExpr {
return g.need_tmp_var_in_expr(expr.expr)
}
ast.ConcatExpr {
for val in expr.vals {
if val is ast.CallExpr {
Expand All @@ -102,6 +86,17 @@ fn (mut g Gen) need_tmp_var_in_expr(expr ast.Expr) bool {
}
}
}
ast.Ident {
return expr.or_expr.kind != .absent
}
ast.IfExpr {
if g.need_tmp_var_in_if(expr) {
return true
}
}
ast.IfGuardExpr {
return true
}
ast.IndexExpr {
if expr.or_expr.kind != .absent {
return true
Expand All @@ -110,21 +105,13 @@ fn (mut g Gen) need_tmp_var_in_expr(expr ast.Expr) bool {
return true
}
}
ast.ArrayInit {
if g.need_tmp_var_in_expr(expr.len_expr) {
return true
}
if g.need_tmp_var_in_expr(expr.cap_expr) {
ast.InfixExpr {
if g.need_tmp_var_in_expr(expr.left) {
return true
}
if g.need_tmp_var_in_expr(expr.init_expr) {
if g.need_tmp_var_in_expr(expr.right) {
return true
}
for elem_expr in expr.exprs {
if g.need_tmp_var_in_expr(elem_expr) {
return true
}
}
}
ast.MapInit {
for key in expr.keys {
Expand All @@ -138,6 +125,28 @@ fn (mut g Gen) need_tmp_var_in_expr(expr ast.Expr) bool {
}
}
}
ast.MatchExpr {
return true
}
ast.ParExpr {
return g.need_tmp_var_in_expr(expr.expr)
}
ast.PrefixExpr {
return g.need_tmp_var_in_expr(expr.right)
}
ast.SelectorExpr {
if g.need_tmp_var_in_expr(expr.expr) {
return true
}
return expr.or_block.kind != .absent
}
ast.StringInterLiteral {
for e in expr.exprs {
if g.need_tmp_var_in_expr(e) {
return true
}
}
}
ast.StructInit {
if g.need_tmp_var_in_expr(expr.update_expr) {
return true
Expand All @@ -148,12 +157,6 @@ fn (mut g Gen) need_tmp_var_in_expr(expr ast.Expr) bool {
}
}
}
ast.SelectorExpr {
if g.need_tmp_var_in_expr(expr.expr) {
return true
}
return expr.or_block.kind != .absent
}
else {}
}
return false
Expand Down
89 changes: 4 additions & 85 deletions vlib/v/gen/c/infix.v
Original file line number Diff line number Diff line change
Expand Up @@ -959,47 +959,7 @@ fn (mut g Gen) need_tmp_var_in_array_call(node ast.Expr) bool {

// infix_expr_and_or_op generates code for `&&` and `||`
fn (mut g Gen) infix_expr_and_or_op(node ast.InfixExpr) {
if node.right is ast.IfExpr {
// `b := a && if true { a = false ...} else {...}`
prev_inside_ternary := g.inside_ternary
g.inside_ternary = 0
if g.need_tmp_var_in_if(node.right) {
tmp := g.new_tmp_var()
cur_line := g.go_before_last_stmt().trim_space()
g.empty_line = true
g.write('bool ${tmp} = (')
g.expr(node.left)
g.writeln(');')
g.set_current_pos_as_last_stmt_pos()
g.write('${cur_line} ${tmp} ${node.op.str()} ')
g.infix_left_var_name = if node.op == .and { tmp } else { '!${tmp}' }
g.expr(node.right)
g.infix_left_var_name = ''
g.inside_ternary = prev_inside_ternary
return
}
g.inside_ternary = prev_inside_ternary
} else if node.right is ast.MatchExpr {
// `b := a && match true { true { a = false ...} else {...}}`
prev_inside_ternary := g.inside_ternary
g.inside_ternary = 0
if g.need_tmp_var_in_match(node.right) {
tmp := g.new_tmp_var()
cur_line := g.go_before_last_stmt().trim_space()
g.empty_line = true
g.write('bool ${tmp} = (')
g.expr(node.left)
g.writeln(');')
g.set_current_pos_as_last_stmt_pos()
g.write('${cur_line} ${tmp} ${node.op.str()} ')
g.infix_left_var_name = if node.op == .and { tmp } else { '!${tmp}' }
g.expr(node.right)
g.infix_left_var_name = ''
g.inside_ternary = prev_inside_ternary
return
}
g.inside_ternary = prev_inside_ternary
} else if g.need_tmp_var_in_array_call(node.right) {
if g.need_tmp_var_in_array_call(node.right) {
// `if a == 0 || arr.any(it.is_letter()) {...}`
tmp := g.new_tmp_var()
cur_line := g.go_before_last_stmt().trim_space()
Expand All @@ -1016,48 +976,7 @@ fn (mut g Gen) infix_expr_and_or_op(node ast.InfixExpr) {
g.infix_left_var_name = if node.op == .and { tmp } else { '!${tmp}' }
g.expr(node.right)
g.infix_left_var_name = ''
return
} else if node.right is ast.CallExpr {
if node.right.or_block.kind != .absent {
prev_inside_ternary := g.inside_ternary
g.inside_ternary = 0
tmp := g.new_tmp_var()
cur_line := g.go_before_last_stmt().trim_space()
g.empty_line = true
g.write('bool ${tmp} = (')
g.expr(node.left)
g.writeln(');')
g.set_current_pos_as_last_stmt_pos()
g.write('${cur_line} ${tmp} ${node.op.str()} ')
g.infix_left_var_name = if node.op == .and { tmp } else { '!${tmp}' }
g.expr(node.right)
g.infix_left_var_name = ''
g.inside_ternary = prev_inside_ternary
return
}
} else if node.right is ast.PrefixExpr && g.inside_ternary == 0 {
prefix := node.right
if prefix.op == .not && prefix.right is ast.CallExpr {
call_expr := prefix.right as ast.CallExpr
if call_expr.or_block.kind != .absent {
prev_inside_ternary := g.inside_ternary
g.inside_ternary = 0
tmp := g.new_tmp_var()
cur_line := g.go_before_last_stmt().trim_space()
g.empty_line = true
g.write('bool ${tmp} = (')
g.expr(node.left)
g.writeln(');')
g.set_current_pos_as_last_stmt_pos()
g.write('${cur_line} ${tmp} ${node.op.str()} ')
g.infix_left_var_name = '!${tmp}'
g.expr(node.right)
g.infix_left_var_name = ''
g.inside_ternary = prev_inside_ternary
return
}
}
} else if node.right is ast.InfixExpr && g.need_tmp_var_in_expr(node.right) {
} else if g.need_tmp_var_in_expr(node.right) && g.inside_ternary == 0 {
prev_inside_ternary := g.inside_ternary
g.inside_ternary = 0
tmp := g.new_tmp_var()
Expand All @@ -1072,9 +991,9 @@ fn (mut g Gen) infix_expr_and_or_op(node ast.InfixExpr) {
g.expr(node.right)
g.infix_left_var_name = ''
g.inside_ternary = prev_inside_ternary
return
} else {
g.gen_plain_infix_expr(node)
}
g.gen_plain_infix_expr(node)
}

fn (mut g Gen) gen_is_none_check(node ast.InfixExpr) {
Expand Down

0 comments on commit fccd7cd

Please sign in to comment.