1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: remove more switch statements

This commit is contained in:
Alexander Medvednikov 2019-10-24 16:02:24 +03:00
parent 695245d307
commit 753fe32793

View File

@ -1064,8 +1064,8 @@ fn (p mut Parser) statement(add_semi bool) string {
// } // }
tok := p.tok tok := p.tok
mut q := '' mut q := ''
switch tok { match tok {
case .name: .name {
next := p.peek() next := p.peek()
//if p.pref.is_verbose { //if p.pref.is_verbose {
//println(next.str()) //println(next.str())
@ -1096,19 +1096,23 @@ fn (p mut Parser) statement(add_semi bool) string {
// `a + 3`, `a(7)`, or just `a` // `a + 3`, `a(7)`, or just `a`
q = p.bool_expression() q = p.bool_expression()
} }
case TokenKind.key_goto: }
.key_goto {
p.check(.key_goto) p.check(.key_goto)
p.fgen(' ') p.fgen(' ')
label := p.check_name() label := p.check_name()
p.genln('goto $label;') p.genln('goto $label;')
return '' return ''
case TokenKind.key_defer: }
.key_defer {
p.defer_st() p.defer_st()
return '' return ''
case TokenKind.hash: }
.hash {
p.chash() p.chash()
return '' return ''
case TokenKind.key_unsafe: }
.key_unsafe {
p.next() p.next()
p.inside_unsafe = true p.inside_unsafe = true
p.check(.lcbr) p.check(.lcbr)
@ -1116,42 +1120,55 @@ fn (p mut Parser) statement(add_semi bool) string {
p.statements() p.statements()
p.inside_unsafe = false p.inside_unsafe = false
//p.check(.rcbr) //p.check(.rcbr)
case TokenKind.dollar: }
.dollar {
p.comp_time() p.comp_time()
case TokenKind.key_if: }
.key_if {
p.if_st(false, 0) p.if_st(false, 0)
case TokenKind.key_for: }
.key_for {
p.for_st() p.for_st()
case TokenKind.key_switch: }
.key_switch {
p.switch_statement() p.switch_statement()
case TokenKind.key_match: }
.key_match {
p.match_statement(false) p.match_statement(false)
case TokenKind.key_mut, TokenKind.key_static: }
.key_mut, .key_static {
p.var_decl() p.var_decl()
case TokenKind.key_return: }
.key_return {
p.return_st() p.return_st()
case TokenKind.lcbr:// {} block }
.lcbr {// {} block
p.check(.lcbr) p.check(.lcbr)
p.genln('{') p.genln('{')
p.statements() p.statements()
return '' return ''
case TokenKind.key_continue: }
.key_continue {
if p.for_expr_cnt == 0 { if p.for_expr_cnt == 0 {
p.error('`continue` statement outside `for`') p.error('`continue` statement outside `for`')
} }
p.genln('continue') p.genln('continue')
p.check(.key_continue) p.check(.key_continue)
case TokenKind.key_break: }
.key_break {
if p.for_expr_cnt == 0 { if p.for_expr_cnt == 0 {
p.error('`break` statement outside `for`') p.error('`break` statement outside `for`')
} }
p.genln('break') p.genln('break')
p.check(.key_break) p.check(.key_break)
case TokenKind.key_go: }
.key_go {
p.go_statement() p.go_statement()
case TokenKind.key_assert: }
.key_assert {
p.assert_statement() p.assert_statement()
default: }
else {
// An expression as a statement // An expression as a statement
typ := p.expression() typ := p.expression()
if p.inside_if_expr { if p.inside_if_expr {
@ -1161,6 +1178,7 @@ fn (p mut Parser) statement(add_semi bool) string {
} }
return typ return typ
} }
}
// ? : uses , as statement separators // ? : uses , as statement separators
if p.inside_if_expr && p.tok != .rcbr { if p.inside_if_expr && p.tok != .rcbr {
p.gen(', ') p.gen(', ')
@ -1195,12 +1213,13 @@ fn ($v.name mut $v.typ) $p.cur_fn.name (...) {
} }
is_str := v.typ == 'string' is_str := v.typ == 'string'
is_ustr := v.typ == 'ustring' is_ustr := v.typ == 'ustring'
switch tok { match tok {
case TokenKind.assign: .assign {
if !is_map && !p.is_empty_c_struct_init { if !is_map && !p.is_empty_c_struct_init {
p.gen(' = ') p.gen(' = ')
} }
case TokenKind.plus_assign: }
.plus_assign {
if is_str && !p.is_js { if is_str && !p.is_js {
p.gen('= string_add($v.name, ')// TODO can't do `foo.bar += '!'` p.gen('= string_add($v.name, ')// TODO can't do `foo.bar += '!'`
} }
@ -1210,7 +1229,10 @@ fn ($v.name mut $v.typ) $p.cur_fn.name (...) {
else { else {
p.gen(' += ') p.gen(' += ')
} }
default: p.gen(' ' + p.tok.str() + ' ') }
else {
p.gen(' ' + p.tok.str() + ' ')
}
} }
p.fspace() p.fspace()
p.fgen(tok.str()) p.fgen(tok.str())
@ -1469,43 +1491,35 @@ fn (p mut Parser) bterm() string {
typ = 'bool' typ = 'bool'
if is_str && !p.is_js { //&& !p.is_sql { if is_str && !p.is_js { //&& !p.is_sql {
p.gen(')') p.gen(')')
switch tok { match tok {
case TokenKind.eq: p.cgen.set_placeholder(ph, 'string_eq(') .eq { p.cgen.set_placeholder(ph, 'string_eq(') }
case TokenKind.ne: p.cgen.set_placeholder(ph, 'string_ne(') .ne { p.cgen.set_placeholder(ph, 'string_ne(') }
case TokenKind.le: p.cgen.set_placeholder(ph, 'string_le(') .le { p.cgen.set_placeholder(ph, 'string_le(') }
case TokenKind.ge: p.cgen.set_placeholder(ph, 'string_ge(') .ge { p.cgen.set_placeholder(ph, 'string_ge(') }
case TokenKind.gt: p.cgen.set_placeholder(ph, 'string_gt(') .gt { p.cgen.set_placeholder(ph, 'string_gt(') }
case TokenKind.lt: p.cgen.set_placeholder(ph, 'string_lt(') .lt { p.cgen.set_placeholder(ph, 'string_lt(') }
} }
/*
TokenKind.eq => p.cgen.set_placeholder(ph, 'string_eq(')
TokenKind.ne => p.cgen.set_placeholder(ph, 'string_ne(')
TokenKind.le => p.cgen.set_placeholder(ph, 'string_le(')
TokenKind.ge => p.cgen.set_placeholder(ph, 'string_ge(')
TokenKind.gt => p.cgen.set_placeholder(ph, 'string_gt(')
TokenKind.lt => p.cgen.set_placeholder(ph, 'string_lt(')
*/
} }
if is_ustr { if is_ustr {
p.gen(')') p.gen(')')
switch tok { match tok {
case TokenKind.eq: p.cgen.set_placeholder(ph, 'ustring_eq(') .eq { p.cgen.set_placeholder(ph, 'ustring_eq(') }
case TokenKind.ne: p.cgen.set_placeholder(ph, 'ustring_ne(') .ne { p.cgen.set_placeholder(ph, 'ustring_ne(') }
case TokenKind.le: p.cgen.set_placeholder(ph, 'ustring_le(') .le { p.cgen.set_placeholder(ph, 'ustring_le(') }
case TokenKind.ge: p.cgen.set_placeholder(ph, 'ustring_ge(') .ge { p.cgen.set_placeholder(ph, 'ustring_ge(') }
case TokenKind.gt: p.cgen.set_placeholder(ph, 'ustring_gt(') .gt { p.cgen.set_placeholder(ph, 'ustring_gt(') }
case TokenKind.lt: p.cgen.set_placeholder(ph, 'ustring_lt(') .lt { p.cgen.set_placeholder(ph, 'ustring_lt(') }
} }
} }
if is_float && p.cur_fn.name != 'f32_abs' && p.cur_fn.name != 'f64_abs' { if is_float && p.cur_fn.name != 'f32_abs' && p.cur_fn.name != 'f64_abs' {
p.gen(')') p.gen(')')
switch tok { match tok {
case TokenKind.eq: p.cgen.set_placeholder(ph, '${expr_type}_eq(') .eq { p.cgen.set_placeholder(ph, '${expr_type}_eq(') }
case TokenKind.ne: p.cgen.set_placeholder(ph, '${expr_type}_ne(') .ne { p.cgen.set_placeholder(ph, '${expr_type}_ne(') }
case TokenKind.le: p.cgen.set_placeholder(ph, '${expr_type}_le(') .le { p.cgen.set_placeholder(ph, '${expr_type}_le(') }
case TokenKind.ge: p.cgen.set_placeholder(ph, '${expr_type}_ge(') .ge { p.cgen.set_placeholder(ph, '${expr_type}_ge(') }
case TokenKind.gt: p.cgen.set_placeholder(ph, '${expr_type}_gt(') .gt { p.cgen.set_placeholder(ph, '${expr_type}_gt(') }
case TokenKind.lt: p.cgen.set_placeholder(ph, '${expr_type}_lt(') .lt { p.cgen.set_placeholder(ph, '${expr_type}_lt(') }
} }
} }
} }