remove `as` casts for basic types

pull/3699/head
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> #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))
} }

View File

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

View File

@ -83,62 +83,37 @@ fn (p mut Parser) bool_expression() string {
} }
fn (p mut Parser) key_as(typ string, start_ph int) string { fn (p mut Parser) key_as(typ string, start_ph int) string {
p.fspace() p.fspace()
p.next() p.next()
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')
}
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' p.cgen.set_placeholder(start_ph, '*($cast_typ*)')
is_bytearr := typ == 'array_byte' p.gen('.obj')
if typ in p.table.sum_types { // Make sure the sum type can be cast, otherwise throw a runtime error
T := p.table.find_type(cast_typ) /*
if T.parent != typ { sum_type:= p.cgen.cur_line.all_after('*) (').replace('.obj', '.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')
n := cast_typ.all_after('__') n := cast_typ.all_after('__')
p.cgen.insert_before('if (($sum_type != SumType_$n) { 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`"); puts("runtime error: $p.file_name:$p.scanner.line_nr cannot cast sum type `$typ` to `$n`");
exit(1); 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 { return cast_typ
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
}
fn (p mut Parser) bterm() string { fn (p mut Parser) bterm() string {
ph := p.cgen.add_placeholder() ph := p.cgen.add_placeholder()

View File

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

View File

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

View File

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

View File

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

View File

@ -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 i := start mut b := malloc(end - start + 1) // add a byte for the endstring 0
mut i1 := 0 mut i := start
for i < end { mut i1 := 0
if txt[i] != num_sep { for i < end {
b[i1]=txt[i] if txt[i] != num_sep {
i1++ 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 { fn (s mut Scanner) ident_bin_number() string {