Skip to content

Commit c0bdb4a

Browse files
authored
v: implement expr.remove_para (#24669)
1 parent a5f400e commit c0bdb4a

7 files changed

Lines changed: 19 additions & 30 deletions

File tree

‎vlib/v/ast/ast.v‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1571,7 +1571,7 @@ pub:
15711571
has_len bool
15721572
has_cap bool
15731573
has_init bool
1574-
has_index bool // true if temp variable index is used
1574+
has_index bool // true if temp variable index is used
15751575
pub mut:
15761576
exprs []Expr // `[expr, expr]` or `[expr]Type{}` for fixed array
15771577
len_expr Expr // len: expr
@@ -2728,6 +2728,15 @@ pub fn (expr Expr) is_reference() bool {
27282728
}
27292729
}
27302730

2731+
// remove_par removes all parenthesis and gets the innermost Expr
2732+
pub fn (mut expr Expr) remove_par() Expr {
2733+
mut e := expr
2734+
for mut e is ParExpr {
2735+
e = e.expr
2736+
}
2737+
return e
2738+
}
2739+
27312740
// is `expr` a literal, i.e. it does not depend on any other declarations (C compile time constant)
27322741
pub fn (expr Expr) is_literal() bool {
27332742
return match expr {

‎vlib/v/checker/assign.v‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,7 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
339339
if left is ast.ParExpr && is_decl {
340340
c.error('parentheses are not supported on the left side of `:=`', left.pos())
341341
}
342-
343-
for left is ast.ParExpr {
344-
left = (left as ast.ParExpr).expr
345-
}
342+
left = left.remove_par()
346343
is_assign := node.op in [.assign, .decl_assign]
347344
match mut left {
348345
ast.Ident {
@@ -943,9 +940,7 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
943940
c.inside_ref_lit = old_inside_ref_lit
944941
if right_node.op == .amp {
945942
mut expr := right_node.right
946-
for mut expr is ast.ParExpr {
947-
expr = expr.expr
948-
}
943+
expr = expr.remove_par()
949944
if mut expr is ast.Ident {
950945
if mut expr.obj is ast.Var {
951946
v := expr.obj

‎vlib/v/checker/checker.v‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4767,10 +4767,7 @@ fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) ast.Type {
47674767
c.inside_ref_lit = old_inside_ref_lit
47684768
node.right_type = right_type
47694769
mut expr := node.right
4770-
// if ParExpr get the innermost expr
4771-
for mut expr is ast.ParExpr {
4772-
expr = expr.expr
4773-
}
4770+
expr = expr.remove_par()
47744771
if node.op == .amp {
47754772
if expr is ast.Nil {
47764773
c.error('invalid operation: cannot take address of nil', expr.pos())

‎vlib/v/checker/fn.v‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,9 +1983,7 @@ fn (mut c Checker) check_type_and_visibility(name string, type_idx int, expected
19831983
fn (mut c Checker) method_call(mut node ast.CallExpr, mut continue_check &bool) ast.Type {
19841984
// `(if true { 'foo.bar' } else { 'foo.bar.baz' }).all_after('foo.')`
19851985
mut left_expr := node.left
1986-
for mut left_expr is ast.ParExpr {
1987-
left_expr = left_expr.expr
1988-
}
1986+
left_expr = left_expr.remove_par()
19891987
if mut left_expr is ast.IfExpr {
19901988
if left_expr.branches.len > 0 && left_expr.has_else {
19911989
mut last_stmt := left_expr.branches[0].stmts.last()

‎vlib/v/fmt/fmt.v‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -856,9 +856,7 @@ fn expr_is_single_line(expr ast.Expr) bool {
856856
pub fn (mut f Fmt) assert_stmt(node ast.AssertStmt) {
857857
f.write('assert ')
858858
mut expr := node.expr
859-
for mut expr is ast.ParExpr {
860-
expr = expr.expr
861-
}
859+
expr = expr.remove_par()
862860
f.expr(expr)
863861
if node.extra !is ast.EmptyExpr {
864862
f.write(', ')
@@ -2935,9 +2933,7 @@ pub fn (mut f Fmt) or_expr(node ast.OrExpr) {
29352933

29362934
pub fn (mut f Fmt) par_expr(node ast.ParExpr) {
29372935
mut expr := node.expr
2938-
for mut expr is ast.ParExpr {
2939-
expr = expr.expr
2940-
}
2936+
expr = expr.remove_par()
29412937
requires_paren := expr !is ast.Ident || node.comments.len > 0
29422938
if requires_paren {
29432939
f.par_level++

‎vlib/v/gen/golang/golang.v‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -729,9 +729,7 @@ fn expr_is_single_line(expr ast.Expr) bool {
729729
pub fn (mut f Gen) assert_stmt(node ast.AssertStmt) {
730730
f.write('assert ')
731731
mut expr := node.expr
732-
for mut expr is ast.ParExpr {
733-
expr = expr.expr
734-
}
732+
expr = expr.remove_par()
735733
f.expr(expr)
736734
f.writeln('')
737735
}
@@ -2058,9 +2056,7 @@ pub fn (mut f Gen) par_expr(node ast.ParExpr) {
20582056
f.write('(')
20592057
}
20602058
mut expr := node.expr
2061-
for mut expr is ast.ParExpr {
2062-
expr = expr.expr
2063-
}
2059+
expr = expr.remove_par()
20642060
f.expr(expr)
20652061
if requires_paren {
20662062
f.par_level--

‎vlib/v/parser/expr.v‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -790,9 +790,7 @@ fn (mut p Parser) infix_expr(left ast.Expr) ast.Expr {
790790
p.inside_assign_rhs = old_assign_rhs
791791
if op in [.plus, .minus, .mul, .div, .mod, .lt, .eq] && mut right is ast.PrefixExpr {
792792
mut right_expr := right.right
793-
for mut right_expr is ast.ParExpr {
794-
right_expr = right_expr.expr
795-
}
793+
right_expr = right_expr.remove_par()
796794
if right.op in [.plus, .minus, .mul, .div, .mod, .lt, .eq] && right_expr.is_pure_literal() {
797795
p.error_with_pos('invalid expression: unexpected token `${op}`', right_op_pos)
798796
}

0 commit comments

Comments
 (0)