From 5a5f4ce99a6d126ef2282601daadd773a4522f69 Mon Sep 17 00:00:00 2001 From: Alexey Date: Sat, 29 Feb 2020 19:45:08 +0300 Subject: [PATCH] fmt: simplify `IndexExpr` processing --- vlib/v/fmt/fmt.v | 32 ++++++++--------------------- vlib/v/fmt/tests/simple_expected.vv | 10 +++++++-- vlib/v/fmt/tests/simple_input.vv | 10 +++++++-- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index f4d5724c3a..a44de865bf 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -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(']') - } -} diff --git a/vlib/v/fmt/tests/simple_expected.vv b/vlib/v/fmt/tests/simple_expected.vv index 3ec758809a..39d5b2478b 100644 --- a/vlib/v/fmt/tests/simple_expected.vv +++ b/vlib/v/fmt/tests/simple_expected.vv @@ -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() { diff --git a/vlib/v/fmt/tests/simple_input.vv b/vlib/v/fmt/tests/simple_input.vv index 92b8fdeec0..11e4e860a3 100644 --- a/vlib/v/fmt/tests/simple_input.vv +++ b/vlib/v/fmt/tests/simple_input.vv @@ -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(){