Skip to content

Commit

Permalink
all: int => i64 (part 5)
Browse files Browse the repository at this point in the history
  • Loading branch information
medvednikov committed Oct 7, 2023
1 parent 8c5ac3a commit e265e99
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 21 deletions.
4 changes: 4 additions & 0 deletions vlib/builtin/int.v
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ pub fn (n int) str() string {
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`.
// Example: assert int(-2020).str() == '-2020'
/*
Expand Down
2 changes: 1 addition & 1 deletion vlib/encoding/leb128/leb128.v
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub fn decode_i32(value []u8) (i32, int) {
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`
Expand Down
4 changes: 2 additions & 2 deletions vlib/math/const.v
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ pub const (
max_i8 = i8(127)
min_i16 = i16(-32768)
max_i16 = i16(32767)
min_i32 = int(-2147483648)
max_i32 = int(2147483647)
min_i32 = i32(-2147483648)
max_i32 = i32(2147483647)
// -9223372036854775808 is wrong, because C compilers parse literal values
// without sign first, and 9223372036854775808 overflows i64, hence the
// consecutive subtraction by 1
Expand Down
14 changes: 12 additions & 2 deletions vlib/math/limit.v
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn maxof[T]() T {
return max_i8
} $else $if T is i16 {
return max_i16
} $else $if T is int {
} $else $if T is i32 {
return max_i32
} $else $if T is i32 {
return max_i32
Expand All @@ -30,6 +30,11 @@ pub fn maxof[T]() T {
return max_f32
} $else $if T is f64 {
return max_f64
} $else $if T is int {
$if new_int ? {
return int(max_i64)
}
return int(max_i32)
} $else {
panic('A maximum value of the type `${typeof[T]().name}` is not defined.')
}
Expand All @@ -42,7 +47,7 @@ pub fn minof[T]() T {
return min_i8
} $else $if T is i16 {
return min_i16
} $else $if T is int {
} $else $if T is i32 {
return min_i32
} $else $if T is i32 {
return min_i32
Expand All @@ -62,6 +67,11 @@ pub fn minof[T]() T {
return -max_f32
} $else $if T is f64 {
return -max_f64
} $else $if T is int {
$if new_int ? {
return int(min_i64)
}
return int(min_i32)
} $else {
panic('A minimum value of the type `${typeof[T]().name}` is not defined.')
}
Expand Down
6 changes: 3 additions & 3 deletions vlib/math/log.v
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ pub fn log_b(x f64) f64 {
// ilog_b(nan) = max_i32
pub fn ilog_b(x f64) int {
if x == 0 {
return min_i32
return int(min_i32)
}
if is_nan(x) {
return max_i32
return int(max_i32)
}
if is_inf(x, 0) {
return max_i32
return int(max_i32)
}
return ilog_b_(x)
}
Expand Down
7 changes: 4 additions & 3 deletions vlib/v/ast/ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import v.pref

pub type TypeDecl = AliasTypeDecl | FnTypeDecl | SumTypeDecl

pub const int_type_name = $if amd64 || arm64 {
'int'
//'i64'
// pub const int_type_name = $if amd64 || arm64 {
pub const int_type_name = $if new_int ? {
//'int'
'i64'
} $else {
'int'
}
Expand Down
21 changes: 15 additions & 6 deletions vlib/v/ast/comptime_const_values.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ pub type ComptTimeConstValue = EmptyExpr
| i16
| i64
| i8
| int
| i32
| rune
| string
| u16
| u32
| u64
| u8
| voidptr
//| int

pub fn empty_comptime_const_expr() ComptTimeConstValue {
return EmptyExpr(0)
Expand Down Expand Up @@ -43,9 +44,17 @@ pub fn (val ComptTimeConstValue) int() ?int {
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 {
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)) }
rune { return voidptr(u64(val)) }
voidptr { return val }
Expand All @@ -62,7 +71,7 @@ pub fn (val ComptTimeConstValue) i64() ?i64 {
i16 {
return i64(val)
}
int {
i32 {
return i64(val)
}
i64 {
Expand Down Expand Up @@ -144,7 +153,7 @@ pub fn (val ComptTimeConstValue) u64() ?u64 {
return u64(val)
}
}
int {
i32 {
if val >= 0 {
return u64(val)
}
Expand Down Expand Up @@ -201,7 +210,7 @@ pub fn (val ComptTimeConstValue) f64() ?f64 {
i16 {
return f64(val)
}
int {
i32 {
return f64(val)
}
i64 {
Expand Down Expand Up @@ -243,7 +252,7 @@ pub fn (val ComptTimeConstValue) string() ?string {
i16 {
return val.str()
}
int {
i32 {
return val.str()
}
i64 {
Expand Down
6 changes: 3 additions & 3 deletions vlib/v/checker/comptime.v
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ fn (mut c Checker) eval_comptime_const_expr(expr ast.Expr, nlevel int) ?ast.Comp
}
ast.SizeOf {
s, _ := c.table.type_size(expr.typ)
return s
return i64(s)
}
ast.FloatLiteral {
x := expr.val.f64()
Expand Down Expand Up @@ -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 {
return cast_expr_value.i16() or { return none }
}
if expr.typ == ast.int_type {
return cast_expr_value.int() or { return none }
if expr.typ == ast.i32_type {
return cast_expr_value.i32() or { return none }
}
if expr.typ == ast.i64_type {
return cast_expr_value.i64() or { return none }
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -5387,7 +5387,7 @@ fn (mut g Gen) const_decl_precomputed(mod string, name string, field_name string
i16 {
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())
}
i64 {
Expand Down

0 comments on commit e265e99

Please sign in to comment.