all: int => i64 (part 5)

This commit is contained in:
Alexander Medvednikov 2023-10-08 01:32:37 +03:00
parent 8c5ac3a529
commit e265e99a64
9 changed files with 45 additions and 21 deletions

View file

@ -125,6 +125,10 @@ pub fn (n int) str() string {
return n.str_l(12) return n.str_l(12)
} }
pub fn (n i32) str() string {
return int(n).str_l(12)
}
// str returns the value of the `int` as a `string`. // str returns the value of the `int` as a `string`.
// Example: assert int(-2020).str() == '-2020' // Example: assert int(-2020).str() == '-2020'
/* /*

View file

@ -80,7 +80,7 @@ pub fn decode_i32(value []u8) (i32, int) {
break break
} }
} }
return int(result), shift / 7 return i32(result), shift / 7
} }
// decode_i64 decodes an i64 and returns the number of bytes used from the given leb128 encoded array `value` // decode_i64 decodes an i64 and returns the number of bytes used from the given leb128 encoded array `value`

View file

@ -46,8 +46,8 @@ pub const (
max_i8 = i8(127) max_i8 = i8(127)
min_i16 = i16(-32768) min_i16 = i16(-32768)
max_i16 = i16(32767) max_i16 = i16(32767)
min_i32 = int(-2147483648) min_i32 = i32(-2147483648)
max_i32 = int(2147483647) max_i32 = i32(2147483647)
// -9223372036854775808 is wrong, because C compilers parse literal values // -9223372036854775808 is wrong, because C compilers parse literal values
// without sign first, and 9223372036854775808 overflows i64, hence the // without sign first, and 9223372036854775808 overflows i64, hence the
// consecutive subtraction by 1 // consecutive subtraction by 1

View file

@ -10,7 +10,7 @@ pub fn maxof[T]() T {
return max_i8 return max_i8
} $else $if T is i16 { } $else $if T is i16 {
return max_i16 return max_i16
} $else $if T is int { } $else $if T is i32 {
return max_i32 return max_i32
} $else $if T is i32 { } $else $if T is i32 {
return max_i32 return max_i32
@ -30,6 +30,11 @@ pub fn maxof[T]() T {
return max_f32 return max_f32
} $else $if T is f64 { } $else $if T is f64 {
return max_f64 return max_f64
} $else $if T is int {
$if new_int ? {
return int(max_i64)
}
return int(max_i32)
} $else { } $else {
panic('A maximum value of the type `${typeof[T]().name}` is not defined.') panic('A maximum value of the type `${typeof[T]().name}` is not defined.')
} }
@ -42,7 +47,7 @@ pub fn minof[T]() T {
return min_i8 return min_i8
} $else $if T is i16 { } $else $if T is i16 {
return min_i16 return min_i16
} $else $if T is int { } $else $if T is i32 {
return min_i32 return min_i32
} $else $if T is i32 { } $else $if T is i32 {
return min_i32 return min_i32
@ -62,6 +67,11 @@ pub fn minof[T]() T {
return -max_f32 return -max_f32
} $else $if T is f64 { } $else $if T is f64 {
return -max_f64 return -max_f64
} $else $if T is int {
$if new_int ? {
return int(min_i64)
}
return int(min_i32)
} $else { } $else {
panic('A minimum value of the type `${typeof[T]().name}` is not defined.') panic('A minimum value of the type `${typeof[T]().name}` is not defined.')
} }

View file

@ -59,13 +59,13 @@ pub fn log_b(x f64) f64 {
// ilog_b(nan) = max_i32 // ilog_b(nan) = max_i32
pub fn ilog_b(x f64) int { pub fn ilog_b(x f64) int {
if x == 0 { if x == 0 {
return min_i32 return int(min_i32)
} }
if is_nan(x) { if is_nan(x) {
return max_i32 return int(max_i32)
} }
if is_inf(x, 0) { if is_inf(x, 0) {
return max_i32 return int(max_i32)
} }
return ilog_b_(x) return ilog_b_(x)
} }

View file

@ -9,9 +9,10 @@ import v.pref
pub type TypeDecl = AliasTypeDecl | FnTypeDecl | SumTypeDecl pub type TypeDecl = AliasTypeDecl | FnTypeDecl | SumTypeDecl
pub const int_type_name = $if amd64 || arm64 { // pub const int_type_name = $if amd64 || arm64 {
'int' pub const int_type_name = $if new_int ? {
//'i64' //'int'
'i64'
} $else { } $else {
'int' 'int'
} }

View file

@ -6,7 +6,7 @@ pub type ComptTimeConstValue = EmptyExpr
| i16 | i16
| i64 | i64
| i8 | i8
| int | i32
| rune | rune
| string | string
| u16 | u16
@ -14,6 +14,7 @@ pub type ComptTimeConstValue = EmptyExpr
| u64 | u64
| u8 | u8
| voidptr | voidptr
//| int
pub fn empty_comptime_const_expr() ComptTimeConstValue { pub fn empty_comptime_const_expr() ComptTimeConstValue {
return EmptyExpr(0) return EmptyExpr(0)
@ -43,9 +44,17 @@ pub fn (val ComptTimeConstValue) int() ?int {
return none return none
} }
pub fn (val ComptTimeConstValue) i32() ?i32 {
x := val.i64()?
if x > -2147483649 && x < 2147483648 {
return i32(x)
}
return none
}
pub fn (val ComptTimeConstValue) voidptr() ?voidptr { pub fn (val ComptTimeConstValue) voidptr() ?voidptr {
match val { match val {
i8, i16, int, i64 { return voidptr(i64(val)) } i8, i16, i32, i64 { return voidptr(i64(val)) }
u8, u16, u32, u64 { return voidptr(u64(val)) } u8, u16, u32, u64 { return voidptr(u64(val)) }
rune { return voidptr(u64(val)) } rune { return voidptr(u64(val)) }
voidptr { return val } voidptr { return val }
@ -62,7 +71,7 @@ pub fn (val ComptTimeConstValue) i64() ?i64 {
i16 { i16 {
return i64(val) return i64(val)
} }
int { i32 {
return i64(val) return i64(val)
} }
i64 { i64 {
@ -144,7 +153,7 @@ pub fn (val ComptTimeConstValue) u64() ?u64 {
return u64(val) return u64(val)
} }
} }
int { i32 {
if val >= 0 { if val >= 0 {
return u64(val) return u64(val)
} }
@ -201,7 +210,7 @@ pub fn (val ComptTimeConstValue) f64() ?f64 {
i16 { i16 {
return f64(val) return f64(val)
} }
int { i32 {
return f64(val) return f64(val)
} }
i64 { i64 {
@ -243,7 +252,7 @@ pub fn (val ComptTimeConstValue) string() ?string {
i16 { i16 {
return val.str() return val.str()
} }
int { i32 {
return val.str() return val.str()
} }
i64 { i64 {

View file

@ -324,7 +324,7 @@ fn (mut c Checker) eval_comptime_const_expr(expr ast.Expr, nlevel int) ?ast.Comp
} }
ast.SizeOf { ast.SizeOf {
s, _ := c.table.type_size(expr.typ) s, _ := c.table.type_size(expr.typ)
return s return i64(s)
} }
ast.FloatLiteral { ast.FloatLiteral {
x := expr.val.f64() x := expr.val.f64()
@ -361,8 +361,8 @@ fn (mut c Checker) eval_comptime_const_expr(expr ast.Expr, nlevel int) ?ast.Comp
if expr.typ == ast.i16_type { if expr.typ == ast.i16_type {
return cast_expr_value.i16() or { return none } return cast_expr_value.i16() or { return none }
} }
if expr.typ == ast.int_type { if expr.typ == ast.i32_type {
return cast_expr_value.int() or { return none } return cast_expr_value.i32() or { return none }
} }
if expr.typ == ast.i64_type { if expr.typ == ast.i64_type {
return cast_expr_value.i64() or { return none } return cast_expr_value.i64() or { return none }

View file

@ -5387,7 +5387,7 @@ fn (mut g Gen) const_decl_precomputed(mod string, name string, field_name string
i16 { i16 {
g.const_decl_write_precomputed(mod, styp, cname, field_name, ct_value.str()) g.const_decl_write_precomputed(mod, styp, cname, field_name, ct_value.str())
} }
int { i32 {
g.const_decl_write_precomputed(mod, styp, cname, field_name, ct_value.str()) g.const_decl_write_precomputed(mod, styp, cname, field_name, ct_value.str())
} }
i64 { i64 {