From bac690bbc86b25cefd202f321fa9067e0f7ad2c1 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 24 Oct 2019 15:44:46 +0300 Subject: [PATCH] parser: replace the switch statement in `parse()` --- vlib/compiler/parser.v | 44 +++++++++++++++++++++------------ vlib/compiler/tests/enum_test.v | 11 +++++++++ 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/vlib/compiler/parser.v b/vlib/compiler/parser.v index 4db745b856..95f1c2c81f 100644 --- a/vlib/compiler/parser.v +++ b/vlib/compiler/parser.v @@ -300,8 +300,8 @@ fn (p mut Parser) parse(pass Pass) { } // Go through every top level token or throw a compilation error if a non-top level token is met for { - switch p.tok { - case .key_import: + match p.tok { + .key_import { if p.peek() == .key_const { p.const_decl() } @@ -312,7 +312,8 @@ fn (p mut Parser) parse(pass Pass) { p.fgenln('') } } - case TokenKind.key_enum: + } + .key_enum { p.next() if p.tok == .name { p.fgen('enum ') @@ -331,7 +332,8 @@ fn (p mut Parser) parse(pass Pass) { else { p.check(.name) } - case TokenKind.key_pub: + } + .key_pub { next := p.peek() match next { .key_fn { p.fn_decl() } @@ -342,27 +344,34 @@ fn (p mut Parser) parse(pass Pass) { p.error('wrong pub keyword usage') } } - case TokenKind.key_fn: + } + .key_fn { p.fn_decl() - case TokenKind.key_type: + } + .key_type { p.type_decl() - case TokenKind.lsbr: + } + .lsbr { // `[` can only mean an [attribute] before a function // or a struct definition p.attribute() - case TokenKind.key_struct, TokenKind.key_interface, TokenKind.key_union, TokenKind.lsbr: + } + .key_struct, .key_interface, .key_union, .lsbr { p.struct_decl() - case TokenKind.key_const: + } + .key_const { p.const_decl() - case TokenKind.hash: - // insert C code, TODO this is going to be removed ASAP - // some libraries (like UI) still have lots of C code + } + .hash { + // insert C code (only for ui module) // # puts("hello"); p.chash() - case TokenKind.dollar: + } + .dollar { // $if, $else p.comp_time() - case TokenKind.key_global: + } + .key_global { if !p.pref.translated && !p.pref.is_live && !p.builtin_mod && !p.pref.building_v && !os.getwd().contains('/volt') { p.error('__global is only allowed in translated code') @@ -384,7 +393,8 @@ fn (p mut Parser) parse(pass Pass) { if !p.cgen.nogen { p.cgen.consts << g } - case TokenKind.eof: + } + .eof { //p.log('end of parse()') // TODO: check why this was added? everything seems to work // without it, and it's already happening in fn_decl @@ -404,7 +414,8 @@ fn (p mut Parser) parse(pass Pass) { out.close() } return - default: + } + else { // no `fn main`, add this "global" statement to cgen.fn_main if p.pref.is_script && !p.pref.is_test { // cur_fn is empty since there was no fn main declared @@ -437,6 +448,7 @@ fn (p mut Parser) parse(pass Pass) { else { p.error('unexpected token `${p.strtok()}`') } + } } } } diff --git a/vlib/compiler/tests/enum_test.v b/vlib/compiler/tests/enum_test.v index a413bc8c56..29703e6a84 100644 --- a/vlib/compiler/tests/enum_test.v +++ b/vlib/compiler/tests/enum_test.v @@ -29,3 +29,14 @@ fn test_in() { assert color in [.red, .green] assert num == 3 } + +fn test_match() { + color := Color.red + num := 3 + match color { + .red { assert true } + .green { assert false } + else { assert false } + } + assert num == 3 +}