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

remove as casts for basic types

This commit is contained in:
Alexander Medvednikov 2020-02-07 22:10:48 +01:00
parent f782388148
commit d66bc24e7f
8 changed files with 63 additions and 82 deletions

View File

@ -6,7 +6,7 @@ module builtin
#include <float.h>
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))
}

View File

@ -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]
}

View File

@ -88,10 +88,8 @@ fn (p mut Parser) key_as(typ string, start_ph int) string {
p.fspace()
cast_typ := p.get_type()
if typ == cast_typ {
p.warn('casting `$typ` to `$cast_typ` is not needed')
p.error('casting `$typ` to `$cast_typ` is not needed')
}
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 {
@ -111,34 +109,11 @@ 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(')')
} 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()

View File

@ -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 {

View File

@ -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
}
/**********************************************************************
*

View File

@ -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))
}

View File

@ -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()

View File

@ -97,23 +97,26 @@ fn (s mut Scanner) ident_name() string {
return name
}
const(
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
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]
b[i1] = txt[i]
i1++
}
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 {