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:
parent
f782388148
commit
d66bc24e7f
@ -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))
|
||||
}
|
||||
|
||||
|
@ -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]
|
||||
}
|
||||
|
@ -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()
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
/**********************************************************************
|
||||
*
|
||||
|
@ -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))
|
||||
}
|
||||
|
@ -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()
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user