remove `as` casts for basic types
parent
f782388148
commit
d66bc24e7f
|
@ -6,7 +6,7 @@ module builtin
|
||||||
#include <float.h>
|
#include <float.h>
|
||||||
pub fn (d f64) str() string {
|
pub fn (d f64) str() string {
|
||||||
buf := malloc(sizeof(double) * 5 + 1) // TODO
|
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))
|
return tos(buf, vstrlen(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,7 +28,7 @@ pub fn (nn int) str() string {
|
||||||
// Fill the string from the end
|
// Fill the string from the end
|
||||||
for n > 0 {
|
for n > 0 {
|
||||||
d := n % 10
|
d := n % 10
|
||||||
buf[max - len - 1] = d + (`0` as int)
|
buf[max - len - 1] = d + int(`0`)
|
||||||
len++
|
len++
|
||||||
n = n / 10
|
n = n / 10
|
||||||
}
|
}
|
||||||
|
@ -42,31 +42,31 @@ pub fn (nn int) str() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (n i8) str() string {
|
pub fn (n i8) str() string {
|
||||||
return (n as int).str()
|
return int(n).str()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (n i16) str() string {
|
pub fn (n i16) str() string {
|
||||||
return (n as int).str()
|
return int(n).str()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (n u16) str() string {
|
pub fn (n u16) str() string {
|
||||||
return (n as int).str()
|
return int(n).str()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (nn u32) str() string {
|
pub fn (nn u32) str() string {
|
||||||
mut n := nn
|
mut n := nn
|
||||||
if n == (0 as u32) {
|
if n == 0 {
|
||||||
return '0'
|
return '0'
|
||||||
}
|
}
|
||||||
max := 16
|
max := 16
|
||||||
mut buf := malloc(max)
|
mut buf := malloc(max)
|
||||||
mut len := 0
|
mut len := 0
|
||||||
// Fill the string from the end
|
// Fill the string from the end
|
||||||
for n > (0 as u32) {
|
for n > 0 {
|
||||||
d := n % (10 as u32)
|
d := n % 10
|
||||||
buf[max - len - 1] = d + (`0` as u32)
|
buf[max - len - 1] = d + u32(`0`)
|
||||||
len++
|
len++
|
||||||
n = n / (10 as u32)
|
n = n / 10
|
||||||
}
|
}
|
||||||
return tos(buf + max - len, len)
|
return tos(buf + max - len, len)
|
||||||
}
|
}
|
||||||
|
@ -94,14 +94,14 @@ pub fn (nn byte) str() string {
|
||||||
|
|
||||||
pub fn (nn i64) str() string {
|
pub fn (nn i64) str() string {
|
||||||
mut n := nn
|
mut n := nn
|
||||||
if n == (0 as i64) {
|
if n == 0 {
|
||||||
return '0'
|
return '0'
|
||||||
}
|
}
|
||||||
max := 32
|
max := 32
|
||||||
mut buf := malloc(max)
|
mut buf := malloc(max)
|
||||||
mut len := 0
|
mut len := 0
|
||||||
mut is_neg := false
|
mut is_neg := false
|
||||||
if n < 0 { //(0 as i64) {
|
if n < 0 {
|
||||||
n = -n
|
n = -n
|
||||||
is_neg = true
|
is_neg = true
|
||||||
}
|
}
|
||||||
|
@ -109,7 +109,7 @@ pub fn (nn i64) str() string {
|
||||||
for n > 0 {
|
for n > 0 {
|
||||||
//d := int(n % (10 as i64))
|
//d := int(n % (10 as i64))
|
||||||
d := n % 10
|
d := n % 10
|
||||||
buf[max - len - 1] = d + `0` as int
|
buf[max - len - 1] = d + int(`0`)
|
||||||
len++
|
len++
|
||||||
n /= 10
|
n /= 10
|
||||||
}
|
}
|
||||||
|
@ -132,7 +132,7 @@ pub fn (nn u64) str() string {
|
||||||
// Fill the string from the end
|
// Fill the string from the end
|
||||||
for n > 0 {
|
for n > 0 {
|
||||||
d := n % 10
|
d := n % 10
|
||||||
buf[max - len - 1] = d + (`0` as u64)
|
buf[max - len - 1] = d + u64(`0`)
|
||||||
len++
|
len++
|
||||||
n = n / (10)
|
n = n / (10)
|
||||||
}
|
}
|
||||||
|
@ -180,14 +180,14 @@ pub fn (a []byte) contains(val byte) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (c rune) str() string {
|
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)
|
len := utf8_char_len(fst_byte)
|
||||||
mut str := string{
|
mut str := string{
|
||||||
len: len
|
len: len
|
||||||
str: malloc(len + 1)
|
str: malloc(len + 1)
|
||||||
}
|
}
|
||||||
for i := 0; i < len; i++ {
|
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`
|
str[len] = `\0`
|
||||||
return str
|
return str
|
||||||
|
@ -208,7 +208,7 @@ pub fn (c byte) is_capital() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (b []byte) clone() []byte {
|
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++ {
|
for i := 0; i < b.len; i++ {
|
||||||
res[i] = b[i]
|
res[i] = b[i]
|
||||||
}
|
}
|
||||||
|
|
|
@ -88,10 +88,8 @@ fn (p mut Parser) key_as(typ string, start_ph int) string {
|
||||||
p.fspace()
|
p.fspace()
|
||||||
cast_typ := p.get_type()
|
cast_typ := p.get_type()
|
||||||
if typ == cast_typ {
|
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 {
|
if typ in p.table.sum_types {
|
||||||
T := p.table.find_type(cast_typ)
|
T := p.table.find_type(cast_typ)
|
||||||
if T.parent != typ {
|
if T.parent != typ {
|
||||||
|
@ -111,34 +109,11 @@ exit(1);
|
||||||
}
|
}
|
||||||
')
|
')
|
||||||
*/
|
*/
|
||||||
|
} else {
|
||||||
} else if cast_typ == 'string' {
|
p.error('`as` casts have been removed, use the old syntax: `Type(val)`')
|
||||||
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
|
return cast_typ
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (p mut Parser) bterm() string {
|
fn (p mut Parser) bterm() string {
|
||||||
ph := p.cgen.add_placeholder()
|
ph := p.cgen.add_placeholder()
|
||||||
|
|
|
@ -114,6 +114,7 @@ const(
|
||||||
)
|
)
|
||||||
|
|
||||||
fn filter_num_sep(txt byteptr, start int, end int) string {
|
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 b := malloc(end-start + 1) // add a byte for the endstring 0
|
||||||
mut i := start
|
mut i := start
|
||||||
mut i1 := 0
|
mut i1 := 0
|
||||||
|
@ -126,6 +127,7 @@ fn filter_num_sep(txt byteptr, start int, end int) string {
|
||||||
}
|
}
|
||||||
b[i1]=0 // C string compatibility
|
b[i1]=0 // C string compatibility
|
||||||
return string{b,i1}
|
return string{b,i1}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (s mut Scanner) ident_bin_number() string {
|
fn (s mut Scanner) ident_bin_number() string {
|
||||||
|
|
|
@ -94,7 +94,7 @@ const (
|
||||||
// f64 constants
|
// f64 constants
|
||||||
//
|
//
|
||||||
DIGITS = 18
|
DIGITS = 18
|
||||||
DOUBLE_PLUS_ZERO = u64(0x0000000000000000)// as u64
|
DOUBLE_PLUS_ZERO = u64(0x0000000000000000)
|
||||||
DOUBLE_MINUS_ZERO = 0x8000000000000000
|
DOUBLE_MINUS_ZERO = 0x8000000000000000
|
||||||
DOUBLE_PLUS_INFINITY = 0x7FF0000000000000
|
DOUBLE_PLUS_INFINITY = 0x7FF0000000000000
|
||||||
DOUBLE_MINUS_INFINITY = 0xFFF0000000000000
|
DOUBLE_MINUS_INFINITY = 0xFFF0000000000000
|
||||||
|
@ -128,7 +128,7 @@ const (
|
||||||
MINUS = `-`
|
MINUS = `-`
|
||||||
ZERO = `0`
|
ZERO = `0`
|
||||||
NINE = `9`
|
NINE = `9`
|
||||||
TEN = 10// as u32
|
TEN = u32(10)
|
||||||
)
|
)
|
||||||
/**********************************************************************
|
/**********************************************************************
|
||||||
*
|
*
|
||||||
|
@ -179,7 +179,7 @@ pub struct PrepNumber {
|
||||||
pub mut:
|
pub mut:
|
||||||
negative bool=false // 0 if positive number, 1 if negative
|
negative bool=false // 0 if positive number, 1 if negative
|
||||||
exponent int=0 // power of 10 exponent
|
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 C.timegm(&tm) time_t
|
||||||
|
|
||||||
fn make_unix_time(t tm) int {
|
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{
|
mut p := Parser{
|
||||||
scanner: s
|
scanner: s
|
||||||
table: table
|
table: table
|
||||||
|
pref: &pref.Preferences{}
|
||||||
}
|
}
|
||||||
p.init_parse_fns()
|
p.init_parse_fns()
|
||||||
p.read_first_token()
|
p.read_first_token()
|
||||||
|
|
|
@ -97,23 +97,26 @@ fn (s mut Scanner) ident_name() string {
|
||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
const(
|
const (
|
||||||
num_sep = `_` // char used as number separator
|
num_sep = `_` // char used as number separator
|
||||||
)
|
)
|
||||||
|
|
||||||
fn filter_num_sep(txt byteptr, start int, end int) string {
|
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 i := start
|
||||||
mut i1 := 0
|
mut i1 := 0
|
||||||
for i < end {
|
for i < end {
|
||||||
if txt[i] != num_sep {
|
if txt[i] != num_sep {
|
||||||
b[i1]=txt[i]
|
b[i1] = txt[i]
|
||||||
i1++
|
i1++
|
||||||
}
|
}
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
b[i1]=0 // C string compatibility
|
b[i1] = 0 // C string compatibility
|
||||||
return string{b,i1}
|
return string{
|
||||||
|
b,i1}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (s mut Scanner) ident_bin_number() string {
|
fn (s mut Scanner) ident_bin_number() string {
|
||||||
|
|
Loading…
Reference in New Issue