From d66bc24e7fa676ae44452682d6cc6c63578ce1f6 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Fri, 7 Feb 2020 22:10:48 +0100 Subject: [PATCH] remove `as` casts for basic types --- vlib/builtin/float.v | 2 +- vlib/builtin/int.v | 32 ++++++++--------- vlib/compiler/expression.v | 73 +++++++++++++------------------------- vlib/compiler/scanner.v | 2 ++ vlib/strconv/atof.v | 6 ++-- vlib/time/time_nix.v | 2 +- vlib/v/parser/parser.v | 1 + vlib/v/scanner/scanner.v | 27 +++++++------- 8 files changed, 63 insertions(+), 82 deletions(-) diff --git a/vlib/builtin/float.v b/vlib/builtin/float.v index ecf1ecc79f..abd3438aa8 100644 --- a/vlib/builtin/float.v +++ b/vlib/builtin/float.v @@ -6,7 +6,7 @@ module builtin #include pub fn (d f64) str() string { buf := malloc(sizeof(double) * 5 + 1) // TODO - C.sprintf(buf as charptr, '%f', d) + C.sprintf(charptr(buf), '%f', d) return tos(buf, vstrlen(buf)) } diff --git a/vlib/builtin/int.v b/vlib/builtin/int.v index 8dbcdd642e..144823fe09 100644 --- a/vlib/builtin/int.v +++ b/vlib/builtin/int.v @@ -28,7 +28,7 @@ pub fn (nn int) str() string { // Fill the string from the end for n > 0 { d := n % 10 - buf[max - len - 1] = d + (`0` as int) + buf[max - len - 1] = d + int(`0`) len++ n = n / 10 } @@ -42,31 +42,31 @@ pub fn (nn int) str() string { } pub fn (n i8) str() string { - return (n as int).str() + return int(n).str() } pub fn (n i16) str() string { - return (n as int).str() + return int(n).str() } pub fn (n u16) str() string { - return (n as int).str() + return int(n).str() } pub fn (nn u32) str() string { mut n := nn - if n == (0 as u32) { + if n == 0 { return '0' } max := 16 mut buf := malloc(max) mut len := 0 // Fill the string from the end - for n > (0 as u32) { - d := n % (10 as u32) - buf[max - len - 1] = d + (`0` as u32) + for n > 0 { + d := n % 10 + buf[max - len - 1] = d + u32(`0`) len++ - n = n / (10 as u32) + n = n / 10 } return tos(buf + max - len, len) } @@ -94,14 +94,14 @@ pub fn (nn byte) str() string { pub fn (nn i64) str() string { mut n := nn - if n == (0 as i64) { + if n == 0 { return '0' } max := 32 mut buf := malloc(max) mut len := 0 mut is_neg := false - if n < 0 { //(0 as i64) { + if n < 0 { n = -n is_neg = true } @@ -109,7 +109,7 @@ pub fn (nn i64) str() string { for n > 0 { //d := int(n % (10 as i64)) d := n % 10 - buf[max - len - 1] = d + `0` as int + buf[max - len - 1] = d + int(`0`) len++ n /= 10 } @@ -132,7 +132,7 @@ pub fn (nn u64) str() string { // Fill the string from the end for n > 0 { d := n % 10 - buf[max - len - 1] = d + (`0` as u64) + buf[max - len - 1] = d + u64(`0`) len++ n = n / (10) } @@ -180,14 +180,14 @@ pub fn (a []byte) contains(val byte) bool { } pub fn (c rune) str() string { - fst_byte := (c as int)>>8 * 3 & 0xff + fst_byte := int(c)>>8 * 3 & 0xff len := utf8_char_len(fst_byte) mut str := string{ len: len str: malloc(len + 1) } for i := 0; i < len; i++ { - str.str[i] = (c as int)>>8 * (3 - i) & 0xff + str.str[i] = int(c)>>8 * (3 - i) & 0xff } str[len] = `\0` return str @@ -208,7 +208,7 @@ pub fn (c byte) is_capital() bool { } pub fn (b []byte) clone() []byte { - mut res := [(0 as byte)].repeat(b.len) + mut res := [byte(0)].repeat(b.len) for i := 0; i < b.len; i++ { res[i] = b[i] } diff --git a/vlib/compiler/expression.v b/vlib/compiler/expression.v index e3af20c76c..353a10be1e 100644 --- a/vlib/compiler/expression.v +++ b/vlib/compiler/expression.v @@ -83,62 +83,37 @@ fn (p mut Parser) bool_expression() string { } fn (p mut Parser) key_as(typ string, start_ph int) string { - p.fspace() - p.next() - p.fspace() - cast_typ := p.get_type() - if typ == cast_typ { - p.warn('casting `$typ` to `$cast_typ` is not needed') + p.fspace() + p.next() + p.fspace() + cast_typ := p.get_type() + if typ == cast_typ { + p.error('casting `$typ` to `$cast_typ` is not needed') + } + if typ in p.table.sum_types { + T := p.table.find_type(cast_typ) + if T.parent != typ { + p.error('cannot cast `$typ` to `$cast_typ`. `$cast_typ` is not a variant of `$typ`' + + 'parent=$T.parent') } - is_byteptr := typ == 'byte*' || typ == 'byteptr' - is_bytearr := typ == 'array_byte' - if typ in p.table.sum_types { - T := p.table.find_type(cast_typ) - if T.parent != typ { - p.error('cannot cast `$typ` to `$cast_typ`. `$cast_typ` is not a variant of `$typ`' + - 'parent=$T.parent') - } - p.cgen.set_placeholder(start_ph, '*($cast_typ*)') - p.gen('.obj') - // Make sure the sum type can be cast, otherwise throw a runtime error - /* - sum_type:= p.cgen.cur_line.all_after('*) (').replace('.obj', '.typ') + p.cgen.set_placeholder(start_ph, '*($cast_typ*)') + p.gen('.obj') + // Make sure the sum type can be cast, otherwise throw a runtime error + /* + sum_type:= p.cgen.cur_line.all_after('*) (').replace('.obj', '.typ') - n := cast_typ.all_after('__') - p.cgen.insert_before('if (($sum_type != SumType_$n) { + n := cast_typ.all_after('__') + p.cgen.insert_before('if (($sum_type != SumType_$n) { puts("runtime error: $p.file_name:$p.scanner.line_nr cannot cast sum type `$typ` to `$n`"); exit(1); } ') */ - - } else if cast_typ == 'string' { - if is_byteptr || is_bytearr { - if p.tok == .comma { - p.check(.comma) - p.cgen.set_placeholder(start_ph, 'tos((byte *)') - if is_bytearr { - p.gen('.data') - } - p.gen(', ') - p.check_types(p.expression(), 'int') - } - else { - if is_bytearr { - p.gen('.data') - } - p.cgen.set_placeholder(start_ph, '/*!!!*/tos2((byte *)') - p.gen(')') - } - } - } - else { - - p.cgen.set_placeholder(start_ph, '($cast_typ)(') - p.gen(')') - } - return cast_typ - } + } else { + p.error('`as` casts have been removed, use the old syntax: `Type(val)`') + } + return cast_typ +} fn (p mut Parser) bterm() string { ph := p.cgen.add_placeholder() diff --git a/vlib/compiler/scanner.v b/vlib/compiler/scanner.v index 150cd7ed23..855c1c1e3c 100644 --- a/vlib/compiler/scanner.v +++ b/vlib/compiler/scanner.v @@ -114,6 +114,7 @@ const( ) fn filter_num_sep(txt byteptr, start int, end int) string { + unsafe { mut b := malloc(end-start + 1) // add a byte for the endstring 0 mut i := start mut i1 := 0 @@ -126,6 +127,7 @@ fn filter_num_sep(txt byteptr, start int, end int) string { } b[i1]=0 // C string compatibility return string{b,i1} + } } fn (s mut Scanner) ident_bin_number() string { diff --git a/vlib/strconv/atof.v b/vlib/strconv/atof.v index 9058aa75c4..e681741a51 100644 --- a/vlib/strconv/atof.v +++ b/vlib/strconv/atof.v @@ -94,7 +94,7 @@ const ( // f64 constants // DIGITS = 18 - DOUBLE_PLUS_ZERO = u64(0x0000000000000000)// as u64 + DOUBLE_PLUS_ZERO = u64(0x0000000000000000) DOUBLE_MINUS_ZERO = 0x8000000000000000 DOUBLE_PLUS_INFINITY = 0x7FF0000000000000 DOUBLE_MINUS_INFINITY = 0xFFF0000000000000 @@ -128,7 +128,7 @@ const ( MINUS = `-` ZERO = `0` NINE = `9` - TEN = 10// as u32 + TEN = u32(10) ) /********************************************************************** * @@ -179,7 +179,7 @@ pub struct PrepNumber { pub mut: negative bool=false // 0 if positive number, 1 if negative exponent int=0 // power of 10 exponent - mantissa u64=0 as u64 // integer mantissa + mantissa u64=u64(0) // integer mantissa } /********************************************************************** * diff --git a/vlib/time/time_nix.v b/vlib/time/time_nix.v index 9a4fdbd085..9779a61055 100644 --- a/vlib/time/time_nix.v +++ b/vlib/time/time_nix.v @@ -16,5 +16,5 @@ struct C.tm { fn C.timegm(&tm) time_t fn make_unix_time(t tm) int { - return C.timegm(&t) as int + return int(C.timegm(&t)) } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 33d6e30051..409b2eb08e 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -53,6 +53,7 @@ pub fn parse_stmt(text string, table &table.Table) ast.Stmt { mut p := Parser{ scanner: s table: table + pref: &pref.Preferences{} } p.init_parse_fns() p.read_first_token() diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index 0642d94d9a..f81f72dcbd 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -97,23 +97,26 @@ fn (s mut Scanner) ident_name() string { return name } -const( - num_sep = `_` // char used as number separator +const ( + num_sep = `_` // char used as number separator ) fn filter_num_sep(txt byteptr, start int, end int) string { - mut b := malloc(end-start + 1) // add a byte for the endstring 0 - mut i := start - mut i1 := 0 - for i < end { - if txt[i] != num_sep { - b[i1]=txt[i] - i1++ + unsafe{ + mut b := malloc(end - start + 1) // add a byte for the endstring 0 + mut i := start + mut i1 := 0 + for i < end { + if txt[i] != num_sep { + b[i1] = txt[i] + i1++ + } + i++ } - i++ + b[i1] = 0 // C string compatibility + return string{ + b,i1} } - b[i1]=0 // C string compatibility - return string{b,i1} } fn (s mut Scanner) ident_bin_number() string {