From f7c00b818074689b5d87c1c571f0de5832820a7e Mon Sep 17 00:00:00 2001 From: joe-conigliaro Date: Sun, 20 Oct 2019 20:24:12 +1100 Subject: [PATCH] compiler: blank ident - consolidate duplicated code into single function --- vlib/compiler/comptime.v | 2 +- vlib/compiler/gen_c.v | 14 ++------------ vlib/compiler/gen_js.v | 14 ++------------ vlib/compiler/parser.v | 15 +++++++++++++++ 4 files changed, 20 insertions(+), 25 deletions(-) diff --git a/vlib/compiler/comptime.v b/vlib/compiler/comptime.v index 28b1062bf2..f073dbefa0 100644 --- a/vlib/compiler/comptime.v +++ b/vlib/compiler/comptime.v @@ -185,7 +185,7 @@ fn (p mut Parser) chash() { flag = flag.replace('@VROOT', p.vroot) flag = flag.replace('@VMOD', v_modules_path) //p.log('adding flag "$flag"') - _p := p.table.parse_cflag(flag, p.mod) or { + _ = p.table.parse_cflag(flag, p.mod) or { p.error_with_token_index(err, p.cur_tok_index()-1) return } diff --git a/vlib/compiler/gen_c.v b/vlib/compiler/gen_c.v index d6527ff40b..8e4fc5490b 100644 --- a/vlib/compiler/gen_c.v +++ b/vlib/compiler/gen_c.v @@ -93,21 +93,11 @@ fn (p mut Parser) gen_blank_identifier_assign() { p.check_name() p.check_space(.assign) is_indexer := p.peek() == .lsbr - mut expr := p.lit - mut is_fn_call := p.peek() == .lpar - if !is_fn_call { - mut i := p.token_idx+1 - for (p.tokens[i].tok == .dot || p.tokens[i].tok == .name) && - p.tokens[i].lit != '_' { - expr += if p.tokens[i].tok == .dot { '.' } else { p.tokens[i].lit } - i++ - } - is_fn_call = p.tokens[i].tok == .lpar - } + is_fn_call, next_expr := p.is_next_expr_fn_call() pos := p.cgen.add_placeholder() mut typ := p.bool_expression() if !is_indexer && !is_fn_call { - p.error_with_token_index('assigning `$expr` to `_` is redundant', assign_error_tok_idx) + p.error_with_token_index('assigning `$next_expr` to `_` is redundant', assign_error_tok_idx) } tmp := p.get_tmp() // handle or diff --git a/vlib/compiler/gen_js.v b/vlib/compiler/gen_js.v index 299f3d783e..461fa59815 100644 --- a/vlib/compiler/gen_js.v +++ b/vlib/compiler/gen_js.v @@ -39,20 +39,10 @@ fn (p mut Parser) gen_blank_identifier_assign() { p.check_name() p.check_space(.assign) is_indexer := p.peek() == .lsbr - mut expr := p.lit - mut is_fn_call := p.peek() == .lpar - if !is_fn_call { - mut i := p.token_idx+1 - for (p.tokens[i].tok == .dot || p.tokens[i].tok == .name) && - p.tokens[i].lit != '_' { - expr += if p.tokens[i].tok == .dot { '.' } else { p.tokens[i].lit } - i++ - } - is_fn_call = p.tokens[i].tok == .lpar - } + is_fn_call, next_expr := p.is_next_expr_fn_call() p.bool_expression() if !is_indexer && !is_fn_call { - p.error_with_token_index('assigning `$expr` to `_` is redundant', assign_error_tok_idx) + p.error_with_token_index('assigning `$next_expr` to `_` is redundant', assign_error_tok_idx) } or_else := p.tok == .key_orelse //tmp := p.get_tmp() diff --git a/vlib/compiler/parser.v b/vlib/compiler/parser.v index fb97e43eab..7792face67 100644 --- a/vlib/compiler/parser.v +++ b/vlib/compiler/parser.v @@ -4209,3 +4209,18 @@ fn (p mut Parser) check_unused_imports() { // the imports are usually at the start of the file p.production_error_with_token_index( 'the following imports were never used: $output', 0 ) } + +fn (p mut Parser) is_next_expr_fn_call() (bool, string) { + mut next_expr := p.lit + mut is_fn_call := p.peek() == .lpar + if !is_fn_call { + mut i := p.token_idx+1 + for (p.tokens[i].tok == .dot || p.tokens[i].tok == .name) && + p.tokens[i].lit != '_' && i < p.tokens.len { + next_expr += if p.tokens[i].tok == .dot { '.' } else { p.tokens[i].lit } + i++ + } + is_fn_call = p.tokens[i].tok == .lpar + } + return is_fn_call, next_expr +}