From 9c5abd575f7c2c0680c8a8549d331cae579b1fbe Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sun, 27 Oct 2019 13:03:40 +0300 Subject: [PATCH] parser: remove switch_statement() --- vlib/bitfield/bitfield_test.v | 12 +++--- vlib/compiler/parser.v | 74 +---------------------------------- 2 files changed, 8 insertions(+), 78 deletions(-) diff --git a/vlib/bitfield/bitfield_test.v b/vlib/bitfield/bitfield_test.v index 66d5724dbe..4c091eb9f3 100644 --- a/vlib/bitfield/bitfield_test.v +++ b/vlib/bitfield/bitfield_test.v @@ -104,17 +104,19 @@ fn test_hamming() { mut input1 := bitfield.new(len) mut input2 := bitfield.new(len) for i := 0; i < len; i++ { - switch rand.next(4) { - case 0: - case 1: + match rand.next(4) { + 0, 1 { input1.setbit(i) count++ - case 2: + } + 2 { input2.setbit(i) count++ - case 3: + } + 3 { input1.setbit(i) input2.setbit(i) + } } } assert count == bitfield.hamming(input1, input2) diff --git a/vlib/compiler/parser.v b/vlib/compiler/parser.v index e280044464..d693262b66 100644 --- a/vlib/compiler/parser.v +++ b/vlib/compiler/parser.v @@ -3361,80 +3361,8 @@ fn (p mut Parser) for_st() { } fn (p mut Parser) switch_statement() { - p.warn('`switch` statement has been deprecated, use `match` instead:\n' + + p.error('`switch` statement has been removed, use `match` instead:\n' + 'https://vlang.io/docs#match') - if p.tok == .key_switch { - p.check(.key_switch) - } else { - p.check(.key_match) - } - p.cgen.start_tmp() - typ := p.bool_expression() - is_str := typ == 'string' - expr := p.cgen.end_tmp() - p.check(.lcbr) - mut i := 0 - mut all_cases_return := true - for p.tok == .key_case || p.tok == .key_default || p.peek() == .arrow || p.tok == .key_else { - p.returns = false - if p.tok == .key_default || p.tok == .key_else { - p.genln('else { // default:') - if p.tok == .key_default { - p.check(.key_default) - p.check(.colon) - } else { - p.check(.key_else) - p.check(.arrow) - } - p.statements() - p.returns = all_cases_return && p.returns - return - } - if i > 0 { - p.gen('else ') - } - p.gen('if (') - // Multiple checks separated by comma - mut got_comma := false - for { - if got_comma { - if is_str { - p.gen(')') - } - p.gen(' || ') - } - if typ == 'string' { - p.gen('string_eq($expr, ') - } - else { - p.gen('$expr == ') - } - if p.tok == .key_case || p.tok == .key_default { - p.check(p.tok) - } - p.bool_expression() - if p.tok != .comma { - break - } - p.check(.comma) - got_comma = true - } - if p.tok == .colon { - p.check(.colon) - } - else { - p.check(.arrow) - } - if is_str { - p.gen(')') - } - p.gen(') {') - p.genln('/* case */') - p.statements() - all_cases_return = all_cases_return && p.returns - i++ - } - p.returns = false // only get here when no default, so return is not guaranteed } // Returns typ if used as expession