new match statement

pull/1198/head
Alexander Medvednikov 2019-07-17 01:43:59 +02:00
parent 3f506714ec
commit a9463a180d
4 changed files with 35 additions and 8 deletions

View File

@ -946,7 +946,7 @@ fn (p mut Parser) statements_no_curly_end() string {
mut i := 0 mut i := 0
mut last_st_typ := '' mut last_st_typ := ''
for p.tok != .rcbr && p.tok != .eof && p.tok != .key_case && for p.tok != .rcbr && p.tok != .eof && p.tok != .key_case &&
p.tok != .key_default { p.tok != .key_default && p.peek() != .arrow {
// println(p.tok.str()) // println(p.tok.str())
// p.print_tok() // p.print_tok()
last_st_typ = p.statement(true) last_st_typ = p.statement(true)
@ -961,7 +961,7 @@ fn (p mut Parser) statements_no_curly_end() string {
p.error('more than 50 000 statements in function `$p.cur_fn.name`') p.error('more than 50 000 statements in function `$p.cur_fn.name`')
} }
} }
if p.tok != .key_case && p.tok != .key_default { if p.tok != .key_case && p.tok != .key_default && p.peek() != .arrow {
// p.next() // p.next()
p.check(.rcbr) p.check(.rcbr)
} }
@ -1044,7 +1044,7 @@ fn (p mut Parser) statement(add_semi bool) string {
p.if_st(false, 0) p.if_st(false, 0)
case Token.key_for: case Token.key_for:
p.for_st() p.for_st()
case Token.key_switch: case Token.key_switch, Token.key_match:
p.switch_statement() p.switch_statement()
case Token.key_mut, Token.key_static: case Token.key_mut, Token.key_static:
p.var_decl() p.var_decl()
@ -3031,14 +3031,18 @@ fn (p mut Parser) for_st() {
} }
fn (p mut Parser) switch_statement() { fn (p mut Parser) switch_statement() {
if p.tok == .key_switch {
p.check(.key_switch) p.check(.key_switch)
} else {
p.check(.key_match)
}
p.cgen.start_tmp() p.cgen.start_tmp()
typ := p.bool_expression() typ := p.bool_expression()
expr := p.cgen.end_tmp() expr := p.cgen.end_tmp()
p.check(.lcbr) p.check(.lcbr)
mut i := 0 mut i := 0
for p.tok == .key_case || p.tok == .key_default { for p.tok == .key_case || p.tok == .key_default || p.peek() == .arrow || p.tok == .key_else {
if p.tok == .key_default { if p.tok == .key_default || p.tok == .key_else {
p.genln('else { // default:') p.genln('else { // default:')
p.check(.key_default) p.check(.key_default)
p.check(.colon) p.check(.colon)
@ -3071,7 +3075,12 @@ fn (p mut Parser) switch_statement() {
p.check(.comma) p.check(.comma)
got_comma = true got_comma = true
} }
if p.tok == .colon {
p.check(.colon) p.check(.colon)
}
else {
p.check(.arrow)
}
p.gen(')) {') p.gen(')) {')
p.genln('/* case */') p.genln('/* case */')
p.statements() p.statements()

View File

@ -430,6 +430,10 @@ fn (s mut Scanner) scan() ScanRes {
s.pos++ s.pos++
return scan_res(.eq, '') return scan_res(.eq, '')
} }
else if nextc == `>` {
s.pos++
return scan_res(.arrow, '')
}
else { else {
return scan_res(.assign, '') return scan_res(.assign, '')
} }

View File

@ -0,0 +1,11 @@
fn test_switch() {
a := 3
mut b := 0
match a {
2 => println('two')
3 => println('three')
b = 3
4 => println('4')
}
assert b == 3
}

View File

@ -27,6 +27,7 @@ enum Token {
comma comma
semicolon semicolon
colon colon
arrow // =>
amp amp
hash hash
dollar dollar
@ -90,7 +91,7 @@ enum Token {
key_import_const key_import_const
key_in key_in
key_interface key_interface
MATCH key_match
key_module key_module
key_mut key_mut
key_return key_return
@ -148,6 +149,7 @@ fn build_token_str() []string {
s[Token.comma] = ',' s[Token.comma] = ','
s[Token.semicolon] = ';' s[Token.semicolon] = ';'
s[Token.colon] = ':' s[Token.colon] = ':'
s[Token.arrow] = '=>'
s[Token.assign] = '=' s[Token.assign] = '='
s[Token.decl_assign] = ':=' s[Token.decl_assign] = ':='
s[Token.plus_assign] = '+=' s[Token.plus_assign] = '+='
@ -215,6 +217,7 @@ fn build_token_str() []string {
s[Token.key_static] = 'static' s[Token.key_static] = 'static'
s[Token.key_as] = 'as' s[Token.key_as] = 'as'
s[Token.key_defer] = 'defer' s[Token.key_defer] = 'defer'
s[Token.key_match] = 'match'
return s return s
} }