fmt: simplify `IndexExpr` processing

pull/3883/head
Alexey 2020-02-29 19:45:08 +03:00 committed by GitHub
parent a678be7195
commit 5a5f4ce99a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 27 deletions

View File

@ -402,7 +402,10 @@ fn (f mut Fmt) expr(node ast.Expr) {
f.expr(it.right)
}
ast.IndexExpr {
f.index_expr(it)
f.expr(it.left)
f.write('[')
f.expr(it.index)
f.write(']')
}
ast.IntegerLiteral {
f.write(it.val.str())
@ -471,6 +474,11 @@ fn (f mut Fmt) expr(node ast.Expr) {
f.write(it.op.str())
f.expr(it.right)
}
ast.RangeExpr {
f.expr(it.low)
f.write('..')
f.expr(it.high)
}
ast.SelectorExpr {
f.expr(it.expr)
f.write('.')
@ -512,25 +520,3 @@ fn (f mut Fmt) wrap_long_line() {
f.line_len = 0
}
}
fn (f mut Fmt) index_expr(node ast.IndexExpr) {
mut is_range := false
match node.index {
ast.RangeExpr {
is_range = true
f.expr(node.left)
f.write('[')
f.expr(it.low)
f.write('..')
f.expr(it.high)
f.write(']')
}
else {}
}
if !is_range {
f.expr(node.left)
f.write('[')
f.expr(node.index)
f.write(']')
}
}

View File

@ -56,8 +56,14 @@ fn new_user() User {
}
[inline]
fn fn_contains_range_expr() {
a := 1 in arr[0..2]
fn fn_contains_index_expr() {
a := 1 in arr[0..]
b := 1 in arr[..2]
c := 1 in arr[1..3]
d := arr[2]
e := arr[2..]
f := arr[..2]
g := arr[1..3]
}
fn voidfn() {

View File

@ -59,8 +59,14 @@ User
[inline]
fn fn_contains_range_expr() {
a:=1 in arr[0..2]
fn fn_contains_index_expr() {
a := 1 in arr[0..]
b := 1 in arr[..2]
c := 1 in arr[1..3]
d := arr[2]
e := arr[2..]
f := arr[..2]
g := arr[1..3]
}
fn voidfn(){