diff --git a/vlib/compiler/cgen.v b/vlib/compiler/cgen.v index 86aae48c58..90382415f4 100644 --- a/vlib/compiler/cgen.v +++ b/vlib/compiler/cgen.v @@ -271,18 +271,18 @@ fn build_thirdparty_obj_file(path string, moduleflags []CFlag) { } fn os_name_to_ifdef(name string) string { - switch name { - case 'windows': return '_WIN32' - case 'mac': return '__APPLE__' - case 'linux': return '__linux__' - case 'freebsd': return '__FreeBSD__' - case 'openbsd': return '__OpenBSD__' - case 'netbsd': return '__NetBSD__' - case 'dragonfly': return '__DragonFly__' - case 'msvc': return '_MSC_VER' - case 'android': return '__BIONIC__' - case 'js': return '_VJS' - case 'solaris': return '__sun' + match name { + 'windows' { return '_WIN32'} + 'mac' { return '__APPLE__'} + 'linux' { return '__linux__'} + 'freebsd' { return '__FreeBSD__'} + 'openbsd'{ return '__OpenBSD__'} + 'netbsd'{ return '__NetBSD__'} + 'dragonfly'{ return '__DragonFly__'} + 'msvc'{ return '_MSC_VER'} + 'android'{ return '__BIONIC__'} + 'js' {return '_VJS'} + 'solaris'{ return '__sun'} } verror('bad os ifdef name "$name"') return '' diff --git a/vlib/compiler/compile_errors.v b/vlib/compiler/compile_errors.v index 9c1ce53702..7c306d8c3f 100644 --- a/vlib/compiler/compile_errors.v +++ b/vlib/compiler/compile_errors.v @@ -270,6 +270,8 @@ fn (s mut Scanner) eat_single_newline(){ if s.text[ s.pos ] == `\r` { s.pos ++ return } } +/////////////////////////////// + const ( match_arrow_warning = '=> is no longer needed in match statements, use\n' + 'match foo { @@ -277,4 +279,6 @@ const ( 2 { baz } else { ... } }' + + //make_receiver_mutable = ) diff --git a/vlib/compiler/gen_c.v b/vlib/compiler/gen_c.v index 8e4fc5490b..87467c0e8e 100644 --- a/vlib/compiler/gen_c.v +++ b/vlib/compiler/gen_c.v @@ -512,22 +512,22 @@ fn type_default(typ string) string { return '{0}' } // Default values for other types are not needed because of mandatory initialization - switch typ { - case 'bool': return '0' - case 'string': return 'tos((byte *)"", 0)' - case 'i8': return '0' - case 'i16': return '0' - case 'i64': return '0' - case 'u16': return '0' - case 'u32': return '0' - case 'u64': return '0' - case 'byte': return '0' - case 'int': return '0' - case 'rune': return '0' - case 'f32': return '0.0' - case 'f64': return '0.0' - case 'byteptr': return '0' - case 'voidptr': return '0' + match typ { + 'bool'{ return '0'} + 'string'{ return 'tos3("")'} + 'i8'{ return '0'} + 'i16'{ return '0'} + 'i64'{ return '0'} + 'u16'{ return '0'} + 'u32'{ return '0'} + 'u64'{ return '0'} + 'byte'{ return '0'} + 'int'{ return '0'} + 'rune'{ return '0'} + 'f32'{ return '0.0'} + 'f64'{ return '0.0'} + 'byteptr'{ return '0'} + 'voidptr'{ return '0'} } return '{0}' } diff --git a/vlib/compiler/main.v b/vlib/compiler/main.v index f56c445b24..58a214a14b 100644 --- a/vlib/compiler/main.v +++ b/vlib/compiler/main.v @@ -1081,21 +1081,22 @@ pub fn cescaped_path(s string) string { } pub fn os_from_string(os string) OS { - switch os { - case 'linux': return .linux - case 'windows': return .windows - case 'mac': return .mac - case 'freebsd': return .freebsd - case 'openbsd': return .openbsd - case 'netbsd': return .netbsd - case 'dragonfly': return .dragonfly - case 'js': return .js - case 'solaris': return .solaris - case 'android': return .android - case 'msvc': + match os { + 'linux' { return .linux} + 'windows' { return .windows} + 'mac' { return .mac} + 'freebsd' { return .freebsd} + 'openbsd' { return .openbsd} + 'netbsd' { return .netbsd} + 'dragonfly' { return .dragonfly} + 'js' { return .js} + 'solaris' { return .solaris} + 'android' { return .android} + 'msvc' { // notice that `-os msvc` became `-cc msvc` verror('use the flag `-cc msvc` to build using msvc') } + } println('bad os $os') // todo panic? return .linux } diff --git a/vlib/compiler/parser.v b/vlib/compiler/parser.v index d7e3a1994d..41e5e52afd 100644 --- a/vlib/compiler/parser.v +++ b/vlib/compiler/parser.v @@ -658,10 +658,10 @@ fn (p mut Parser) interface_method(field_name, receiver string) &Fn { } fn key_to_type_cat(tok TokenKind) TypeCategory { - switch tok { - case TokenKind.key_interface: return TypeCategory.interface_ - case TokenKind.key_struct: return TypeCategory.struct_ - case TokenKind.key_union: return TypeCategory.union_ + match tok { + .key_interface { return TypeCategory.interface_ } + .key_struct { return TypeCategory.struct_ } + .key_union { return TypeCategory.union_ } //TokenKind.key_ => return .interface_ } verror('Unknown token: $tok') @@ -2447,8 +2447,8 @@ fn (p mut Parser) term() string { fn (p mut Parser) unary() string { mut typ := '' tok := p.tok - switch tok { - case TokenKind.not: + match tok { + .not { p.gen('!') p.check(.not) // typ should be bool type @@ -2456,29 +2456,32 @@ fn (p mut Parser) unary() string { if typ != 'bool' { p.error('operator ! requires bool type, not `$typ`') } - - case TokenKind.bit_not: + } + .bit_not { p.gen('~') p.check(.bit_not) typ = p.bool_expression() - default: + } + else { typ = p.factor() } + } return typ } fn (p mut Parser) factor() string { mut typ := '' tok := p.tok - switch tok { - case .key_none: + match tok { + .key_none { if !p.expected_type.starts_with('Option_') { p.error('need "$p.expected_type" got none') } p.gen('opt_none()') p.check(.key_none) return p.expected_type - case TokenKind.number: + } + .number { typ = 'int' // Check if float (`1.0`, `1e+3`) but not if is hexa if (p.lit.contains('.') || (p.lit.contains('e') || p.lit.contains('E'))) && @@ -2496,13 +2499,15 @@ fn (p mut Parser) factor() string { } p.gen(p.lit) p.fgen(p.lit) - case TokenKind.minus: + } + .minus { p.gen('-') p.fgen('-') p.next() return p.factor() // Variable - case TokenKind.key_sizeof: + } + .key_sizeof { p.gen('sizeof(') p.fgen('sizeof(') p.next() @@ -2512,10 +2517,12 @@ fn (p mut Parser) factor() string { p.gen('$sizeof_typ)') p.fgen('$sizeof_typ)') return 'int' - case TokenKind.amp, TokenKind.dot, TokenKind.mul: + } + .amp, .dot, .mul { // (dot is for enum vals: `.green`) return p.name_expr() - case TokenKind.name: + } + .name { // map[string]int if p.lit == 'map' && p.peek() == .lsbr { return p.map_init() @@ -2532,7 +2539,8 @@ fn (p mut Parser) factor() string { //} typ = p.name_expr() return typ - case TokenKind.key_default: + } + .key_default { p.next() p.next() name := p.check_name() @@ -2542,7 +2550,8 @@ fn (p mut Parser) factor() string { p.gen('default(T)') p.next() return 'T' - case TokenKind.lpar: + } + .lpar { //p.gen('(/*lpar*/') p.gen('(') p.check(.lpar) @@ -2556,41 +2565,50 @@ fn (p mut Parser) factor() string { p.ptr_cast = false p.gen(')') return typ - case TokenKind.chartoken: + } + .chartoken { p.char_expr() typ = 'byte' return typ - case TokenKind.str: + } + .str { p.string_expr() typ = 'string' return typ - case TokenKind.key_false: + } + .key_false { typ = 'bool' p.gen('0') p.fgen('false') - case TokenKind.key_true: + } + .key_true { typ = 'bool' p.gen('1') p.fgen('true') - case TokenKind.lsbr: + } + .lsbr { // `[1,2,3]` or `[]` or `[20]byte` // TODO have to return because arrayInit does next() // everything should do next() return p.array_init() - case TokenKind.lcbr: + } + .lcbr { // `m := { 'one': 1 }` if p.peek() == .str { return p.map_init() } // { user | name :'new name' } return p.assoc() - case TokenKind.key_if: + } + .key_if { typ = p.if_st(true, 0) return typ - case TokenKind.key_match: + } + .key_match { typ = p.match_statement(true) return typ - default: + } + else { if p.pref.is_verbose || p.pref.is_debug { next := p.peek() println('prev=${p.prev_tok.str()}') @@ -2598,6 +2616,7 @@ fn (p mut Parser) factor() string { } p.error('unexpected token: `${p.tok.str()}`') } + } p.next()// TODO everything should next() return typ } @@ -3316,6 +3335,8 @@ fn (p mut Parser) for_st() { } fn (p mut Parser) switch_statement() { + p.warn('`switch` statement has been deprecated, use `match` instead:\n' + + 'https://vlang.io/docs#match') if p.tok == .key_switch { p.check(.key_switch) } else { diff --git a/vlib/compiler/scanner.v b/vlib/compiler/scanner.v index b5e57de91b..ccb71f7234 100644 --- a/vlib/compiler/scanner.v +++ b/vlib/compiler/scanner.v @@ -306,8 +306,8 @@ fn (s mut Scanner) scan() ScanRes { return scan_res(.number, num) } // all other tokens - switch c { - case `+`: + match c { + `+` { if nextc == `+` { s.pos++ return scan_res(.inc, '') @@ -317,7 +317,8 @@ fn (s mut Scanner) scan() ScanRes { return scan_res(.plus_assign, '') } return scan_res(.plus, '') - case `-`: + } + `-` { if nextc == `-` { s.pos++ return scan_res(.dec, '') @@ -327,47 +328,62 @@ fn (s mut Scanner) scan() ScanRes { return scan_res(.minus_assign, '') } return scan_res(.minus, '') - case `*`: + } + `*` { if nextc == `=` { s.pos++ return scan_res(.mult_assign, '') } return scan_res(.mul, '') - case `^`: + } + `^` { if nextc == `=` { s.pos++ return scan_res(.xor_assign, '') } return scan_res(.xor, '') - case `%`: + } + `%` { if nextc == `=` { s.pos++ return scan_res(.mod_assign, '') } return scan_res(.mod, '') - case `?`: + } + `?` { return scan_res(.question, '') - case single_quote, double_quote: + } + single_quote, double_quote { return scan_res(.str, s.ident_string()) - case `\``: // ` // apostrophe balance comment. do not remove + } + `\`` { // ` // apostrophe balance comment. do not remove return scan_res(.chartoken, s.ident_char()) - case `(`: + } + `(` { + return scan_res(.lpar, '') - case `)`: + } + `)` { return scan_res(.rpar, '') - case `[`: + } + `[` { return scan_res(.lsbr, '') - case `]`: + } + `]` { return scan_res(.rsbr, '') - case `{`: + } + `{` { // Skip { in ${ in strings + // } if s.inside_string { return s.scan() } return scan_res(.lcbr, '') - case `$`: + } + `$` { return scan_res(.dollar, '') - case `}`: + } + `}` { // s = `hello $name !` // s = `hello ${name} !` if s.inside_string { @@ -382,7 +398,8 @@ fn (s mut Scanner) scan() ScanRes { else { return scan_res(.rcbr, '') } - case `&`: + } + `&` { if nextc == `=` { s.pos++ return scan_res(.and_assign, '') @@ -392,7 +409,8 @@ fn (s mut Scanner) scan() ScanRes { return scan_res(.and, '') } return scan_res(.amp, '') - case `|`: + } + `|` { if nextc == `|` { s.pos++ return scan_res(.logical_or, '') @@ -402,9 +420,11 @@ fn (s mut Scanner) scan() ScanRes { return scan_res(.or_assign, '') } return scan_res(.pipe, '') - case `,`: + } + `,` { return scan_res(.comma, '') - case `@`: + } + `@` { s.pos++ name := s.ident_name() // @FN => will be substituted with the name of the current V function @@ -424,6 +444,7 @@ fn (s mut Scanner) scan() ScanRes { s.error('@ must be used before keywords (e.g. `@type string`)') } return scan_res(.name, name) + } /* case `\r`: if nextc == `\n` { @@ -431,11 +452,13 @@ fn (s mut Scanner) scan() ScanRes { s.last_nl_pos = s.pos return scan_res(.nl, '') } + } case `\n`: s.last_nl_pos = s.pos return scan_res(.nl, '') + } */ - case `.`: + `.` { if nextc == `.` { s.pos++ if s.text[s.pos+1] == `.` { @@ -445,7 +468,8 @@ fn (s mut Scanner) scan() ScanRes { return scan_res(.dotdot, '') } return scan_res(.dot, '') - case `#`: + } + `#` { start := s.pos + 1 s.ignore_line() if nextc == `!` { @@ -456,7 +480,8 @@ fn (s mut Scanner) scan() ScanRes { } hash := s.text.substr(start, s.pos) return scan_res(.hash, hash.trim_space()) - case `>`: + } + `>` { if nextc == `=` { s.pos++ return scan_res(.ge, '') @@ -472,7 +497,8 @@ fn (s mut Scanner) scan() ScanRes { else { return scan_res(.gt, '') } - case 0xE2: + } + 0xE2 { //case `≠`: if nextc == 0x89 && s.text[s.pos + 2] == 0xA0 { s.pos += 2 @@ -488,7 +514,8 @@ fn (s mut Scanner) scan() ScanRes { s.pos += 2 return scan_res(.ge, '') } - case `<`: + } + `<` { if nextc == `=` { s.pos++ return scan_res(.le, '') @@ -504,7 +531,8 @@ fn (s mut Scanner) scan() ScanRes { else { return scan_res(.lt, '') } - case `=`: + } + `=` { if nextc == `=` { s.pos++ return scan_res(.eq, '') @@ -516,7 +544,8 @@ fn (s mut Scanner) scan() ScanRes { else { return scan_res(.assign, '') } - case `:`: + } + `:` { if nextc == `=` { s.pos++ return scan_res(.decl_assign, '') @@ -524,9 +553,11 @@ fn (s mut Scanner) scan() ScanRes { else { return scan_res(.colon, '') } - case `;`: + } + `;` { return scan_res(.semicolon, '') - case `!`: + } + `!` { if nextc == `=` { s.pos++ return scan_res(.ne, '') @@ -534,9 +565,11 @@ fn (s mut Scanner) scan() ScanRes { else { return scan_res(.not, '') } - case `~`: + } + `~` { return scan_res(.bit_not, '') - case `/`: + } + `/` { if nextc == `=` { s.pos++ return scan_res(.div_assign, '') @@ -581,6 +614,7 @@ fn (s mut Scanner) scan() ScanRes { return s.scan() } return scan_res(.div, '') + } } $if windows { if c == `\0` { diff --git a/vlib/compiler/table.v b/vlib/compiler/table.v index 0c29ef3748..4d1ef6f7ce 100644 --- a/vlib/compiler/table.v +++ b/vlib/compiler/table.v @@ -803,13 +803,13 @@ fn (table &Table) cgen_name_type_pair(name, typ string) string { fn is_valid_int_const(val, typ string) bool { x := val.int() - switch typ { - case 'byte': return 0 <= x && x <= 255 - case 'u16': return 0 <= x && x <= 65535 + match typ { + 'byte' { return 0 <= x && x <= 255 } + 'u16' { return 0 <= x && x <= 65535 } //case 'u32': return 0 <= x && x <= math.MaxU32 //case 'u64': return 0 <= x && x <= math.MaxU64 ////////////// - case 'i8': return -128 <= x && x <= 127 + 'i8' { return -128 <= x && x <= 127 } /* case 'i16': return math.min_i16 <= x && x <= math.max_i16 case 'int': return math.min_i32 <= x && x <= math.max_i32 @@ -826,22 +826,23 @@ fn (p mut Parser) typ_to_fmt(typ string, level int) string { if t.cat == .enum_ { return '%d' } - switch typ { - case 'string': return '%.*s' - //case 'bool': return '%.*s' - case 'ustring': return '%.*s' - case 'byte', 'bool', 'int', 'char', 'byte', 'i16', 'i8': return '%d' - case 'u16', 'u32': return '%u' - case 'f64', 'f32': return '%f' - case 'i64': return '%lld' - case 'u64': return '%llu' - case 'byte*', 'byteptr': return '%s' - // case 'array_string': return '%s' - // case 'array_int': return '%s' - case 'void': p.error('cannot interpolate this value') - default: - if typ.ends_with('*') { - return '%p' + match typ { + 'string' { return '%.*s'} + //case 'bool': return '%.*s' + 'ustring' { return '%.*s'} + 'byte', 'bool', 'int', 'char', 'byte', 'i16', 'i8' { return '%d'} + 'u16', 'u32' { return '%u'} + 'f64', 'f32' { return '%f'} + 'i64' { return '%lld'} + 'u64' { return '%llu'} + 'byte*', 'byteptr' { return '%s'} + // case 'array_string': return '%s' + // case 'array_int': return '%s' + 'void' { p.error('cannot interpolate this value')} + else { + if typ.ends_with('*') { + return '%p' + } } } if t.parent != '' && level == 0 { diff --git a/vlib/os/os.v b/vlib/os/os.v index 54ef5d5fcc..0425d2c62d 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -380,34 +380,33 @@ pub fn system(cmd string) int { pub fn sigint_to_signal_name(si int) string { // POSIX signals: - switch si { - case 1: return 'SIGHUP' - case 2: return 'SIGINT' - case 3: return 'SIGQUIT' - case 4: return 'SIGILL' - case 6: return 'SIGABRT' - case 8: return 'SIGFPE' - case 9: return 'SIGKILL' - case 11: return 'SIGSEGV' - case 13: return 'SIGPIPE' - case 14: return 'SIGALRM' - case 15: return 'SIGTERM' + match si { + 1 {return 'SIGHUP'} + 2 {return 'SIGINT'} + 3 {return 'SIGQUIT'} + 4 {return 'SIGILL'} + 6 {return 'SIGABRT'} + 8 {return 'SIGFPE'} + 9 {return 'SIGKILL'} + 11 {return 'SIGSEGV'} + 13 {return 'SIGPIPE'} + 14 {return 'SIGALRM'} + 15 {return 'SIGTERM'} } - /////////////////////////////////// $if linux { // From `man 7 signal` on linux: - switch si { - case 30,10,16: return 'SIGUSR1' - case 31,12,17: return 'SIGUSR2' - case 20,17,18: return 'SIGCHLD' - case 19,18,25: return 'SIGCONT' - case 17,19,23: return 'SIGSTOP' - case 18,20,24: return 'SIGTSTP' - case 21,21,26: return 'SIGTTIN' - case 22,22,27: return 'SIGTTOU' - /////////////////////////////// - case 5: return 'SIGTRAP' - case 7: return 'SIGBUS' + match si { + 30,10,16{ return 'SIGUSR1'} + 31,12,17{ return 'SIGUSR2'} + 20,17,18{ return 'SIGCHLD'} + 19,18,25{ return 'SIGCONT'} + 17,19,23{ return 'SIGSTOP'} + 18,20,24{ return 'SIGTSTP'} + 21,21,26{ return 'SIGTTIN'} + 22,22,27{ return 'SIGTTOU'} + /////////////////////////////// + 5{ return 'SIGTRAP'} + 7{ return 'SIGBUS' } } } return 'unknown'