Merge pull request #1841 from skyfex/devel
Add support for big 'u64 literals and remove SomeUInt from unsigned.nim
This commit is contained in:
commit
e2faa40a4e
5 changed files with 233 additions and 30 deletions
|
|
@ -264,6 +264,19 @@ proc isFloatLiteral(s: string): bool =
|
||||||
return true
|
return true
|
||||||
result = false
|
result = false
|
||||||
|
|
||||||
|
{.push overflowChecks: off.}
|
||||||
|
# We need to parse the largest uint literal without overflow checks
|
||||||
|
proc unsafeParseUInt(s: string, b: var BiggestInt, start = 0): int =
|
||||||
|
var i = start
|
||||||
|
if s[i] in {'0'..'9'}:
|
||||||
|
b = 0
|
||||||
|
while s[i] in {'0'..'9'}:
|
||||||
|
b = b * 10 + (ord(s[i]) - ord('0'))
|
||||||
|
inc(i)
|
||||||
|
while s[i] == '_': inc(i) # underscores are allowed and ignored
|
||||||
|
result = i - start
|
||||||
|
{.pop.} # overflowChecks
|
||||||
|
|
||||||
proc getNumber(L: var TLexer): TToken =
|
proc getNumber(L: var TLexer): TToken =
|
||||||
var
|
var
|
||||||
pos, endpos: int
|
pos, endpos: int
|
||||||
|
|
@ -425,6 +438,12 @@ proc getNumber(L: var TLexer): TToken =
|
||||||
(result.tokType == tkFloat64Lit):
|
(result.tokType == tkFloat64Lit):
|
||||||
result.fNumber = parseFloat(result.literal)
|
result.fNumber = parseFloat(result.literal)
|
||||||
if result.tokType == tkIntLit: result.tokType = tkFloatLit
|
if result.tokType == tkIntLit: result.tokType = tkFloatLit
|
||||||
|
elif result.tokType == tkUint64Lit:
|
||||||
|
xi = 0
|
||||||
|
let len = unsafeParseUInt(result.literal, xi)
|
||||||
|
if len != result.literal.len or len == 0:
|
||||||
|
raise newException(ValueError, "invalid integer: " & $xi)
|
||||||
|
result.iNumber = xi
|
||||||
else:
|
else:
|
||||||
result.iNumber = parseBiggestInt(result.literal)
|
result.iNumber = parseBiggestInt(result.literal)
|
||||||
if (result.iNumber < low(int32)) or (result.iNumber > high(int32)):
|
if (result.iNumber < low(int32)) or (result.iNumber > high(int32)):
|
||||||
|
|
|
||||||
|
|
@ -267,7 +267,7 @@ proc concreteType(c: TCandidate, t: PType): PType =
|
||||||
else:
|
else:
|
||||||
result = t # Note: empty is valid here
|
result = t # Note: empty is valid here
|
||||||
|
|
||||||
proc handleRange(f, a: PType, min, max: TTypeKind): TTypeRelation =
|
proc handleRange(f, a: PType, validconv: set[TTypeKind]): TTypeRelation =
|
||||||
if a.kind == f.kind:
|
if a.kind == f.kind:
|
||||||
result = isEqual
|
result = isEqual
|
||||||
else:
|
else:
|
||||||
|
|
@ -281,9 +281,9 @@ proc handleRange(f, a: PType, min, max: TTypeKind): TTypeRelation =
|
||||||
# integer literal in the proper range; we want ``i16 + 4`` to stay an
|
# integer literal in the proper range; we want ``i16 + 4`` to stay an
|
||||||
# ``int16`` operation so we declare the ``4`` pseudo-equal to int16
|
# ``int16`` operation so we declare the ``4`` pseudo-equal to int16
|
||||||
result = isFromIntLit
|
result = isFromIntLit
|
||||||
elif f.kind == tyInt and k in {tyInt8..tyInt32}:
|
elif f.kind == tyInt and k in {tyInt8..tyInt32, tyUint8..tyUInt16}:
|
||||||
result = isIntConv
|
result = isIntConv
|
||||||
elif k >= min and k <= max:
|
elif k in validconv:
|
||||||
result = isConvertible
|
result = isConvertible
|
||||||
elif a.kind == tyRange and a.sons[0].kind in {tyInt..tyInt64,
|
elif a.kind == tyRange and a.sons[0].kind in {tyInt..tyInt64,
|
||||||
tyUInt8..tyUInt32} and
|
tyUInt8..tyUInt32} and
|
||||||
|
|
@ -663,16 +663,16 @@ proc typeRel(c: var TCandidate, f, aOrig: PType, doBind = true): TTypeRelation =
|
||||||
result = isIntConv
|
result = isIntConv
|
||||||
elif isConvertibleToRange(skipTypes(f, {tyRange}), a):
|
elif isConvertibleToRange(skipTypes(f, {tyRange}), a):
|
||||||
result = isConvertible # a convertible to f
|
result = isConvertible # a convertible to f
|
||||||
of tyInt: result = handleRange(f, a, tyInt8, tyInt32)
|
of tyInt: result = handleRange(f, a, {tyInt8..tyInt32,tyUInt8..tyUInt16})
|
||||||
of tyInt8: result = handleRange(f, a, tyInt8, tyInt8)
|
of tyInt8: result = handleRange(f, a, {tyInt8})
|
||||||
of tyInt16: result = handleRange(f, a, tyInt8, tyInt16)
|
of tyInt16: result = handleRange(f, a, {tyInt8..tyInt16,tyUInt8})
|
||||||
of tyInt32: result = handleRange(f, a, tyInt8, tyInt32)
|
of tyInt32: result = handleRange(f, a, {tyInt8..tyInt32,tyUInt8..tyUInt16})
|
||||||
of tyInt64: result = handleRange(f, a, tyInt, tyInt64)
|
of tyInt64: result = handleRange(f, a, {tyInt..tyInt64,tyUInt8..tyUInt32})
|
||||||
of tyUInt: result = handleRange(f, a, tyUInt8, tyUInt32)
|
of tyUInt: result = handleRange(f, a, {tyUInt8..tyUInt32})
|
||||||
of tyUInt8: result = handleRange(f, a, tyUInt8, tyUInt8)
|
of tyUInt8: result = handleRange(f, a, {tyUInt8})
|
||||||
of tyUInt16: result = handleRange(f, a, tyUInt8, tyUInt16)
|
of tyUInt16: result = handleRange(f, a, {tyUInt8..tyUInt16})
|
||||||
of tyUInt32: result = handleRange(f, a, tyUInt8, tyUInt32)
|
of tyUInt32: result = handleRange(f, a, {tyUInt8..tyUInt32})
|
||||||
of tyUInt64: result = handleRange(f, a, tyUInt, tyUInt64)
|
of tyUInt64: result = handleRange(f, a, {tyUInt..tyUInt64})
|
||||||
of tyFloat: result = handleFloatRange(f, a)
|
of tyFloat: result = handleFloatRange(f, a)
|
||||||
of tyFloat32: result = handleFloatRange(f, a)
|
of tyFloat32: result = handleFloatRange(f, a)
|
||||||
of tyFloat64: result = handleFloatRange(f, a)
|
of tyFloat64: result = handleFloatRange(f, a)
|
||||||
|
|
|
||||||
|
|
@ -11,49 +11,47 @@
|
||||||
## To discourage users from using ``unsigned``, it's not part of ``system``,
|
## To discourage users from using ``unsigned``, it's not part of ``system``,
|
||||||
## but an extra import.
|
## but an extra import.
|
||||||
|
|
||||||
type
|
proc `not`*[T: SomeUnsignedInt](x: T): T {.magic: "BitnotI", noSideEffect.}
|
||||||
SomeUInt = uint|uint8|uint16|uint32|uint64
|
|
||||||
|
|
||||||
proc `not`*[T: SomeUInt](x: T): T {.magic: "BitnotI", noSideEffect.}
|
|
||||||
## computes the `bitwise complement` of the integer `x`.
|
## computes the `bitwise complement` of the integer `x`.
|
||||||
|
|
||||||
proc `shr`*[T: SomeUInt](x, y: T): T {.magic: "ShrI", noSideEffect.}
|
proc `shr`*[T: SomeUnsignedInt](x, y: T): T {.magic: "ShrI", noSideEffect.}
|
||||||
## computes the `shift right` operation of `x` and `y`.
|
## computes the `shift right` operation of `x` and `y`.
|
||||||
|
|
||||||
proc `shl`*[T: SomeUInt](x, y: T): T {.magic: "ShlI", noSideEffect.}
|
proc `shl`*[T: SomeUnsignedInt](x, y: T): T {.magic: "ShlI", noSideEffect.}
|
||||||
## computes the `shift left` operation of `x` and `y`.
|
## computes the `shift left` operation of `x` and `y`.
|
||||||
|
|
||||||
proc `and`*[T: SomeUInt](x, y: T): T {.magic: "BitandI", noSideEffect.}
|
proc `and`*[T: SomeUnsignedInt](x, y: T): T {.magic: "BitandI", noSideEffect.}
|
||||||
## computes the `bitwise and` of numbers `x` and `y`.
|
## computes the `bitwise and` of numbers `x` and `y`.
|
||||||
|
|
||||||
proc `or`*[T: SomeUInt](x, y: T): T {.magic: "BitorI", noSideEffect.}
|
proc `or`*[T: SomeUnsignedInt](x, y: T): T {.magic: "BitorI", noSideEffect.}
|
||||||
## computes the `bitwise or` of numbers `x` and `y`.
|
## computes the `bitwise or` of numbers `x` and `y`.
|
||||||
|
|
||||||
proc `xor`*[T: SomeUInt](x, y: T): T {.magic: "BitxorI", noSideEffect.}
|
proc `xor`*[T: SomeUnsignedInt](x, y: T): T {.magic: "BitxorI", noSideEffect.}
|
||||||
## computes the `bitwise xor` of numbers `x` and `y`.
|
## computes the `bitwise xor` of numbers `x` and `y`.
|
||||||
|
|
||||||
proc `==`*[T: SomeUInt](x, y: T): bool {.magic: "EqI", noSideEffect.}
|
proc `==`*[T: SomeUnsignedInt](x, y: T): bool {.magic: "EqI", noSideEffect.}
|
||||||
## Compares two unsigned integers for equality.
|
## Compares two unsigned integers for equality.
|
||||||
|
|
||||||
proc `+`*[T: SomeUInt](x, y: T): T {.magic: "AddU", noSideEffect.}
|
proc `+`*[T: SomeUnsignedInt](x, y: T): T {.magic: "AddU", noSideEffect.}
|
||||||
## Binary `+` operator for unsigned integers.
|
## Binary `+` operator for unsigned integers.
|
||||||
|
|
||||||
proc `-`*[T: SomeUInt](x, y: T): T {.magic: "SubU", noSideEffect.}
|
proc `-`*[T: SomeUnsignedInt](x, y: T): T {.magic: "SubU", noSideEffect.}
|
||||||
## Binary `-` operator for unsigned integers.
|
## Binary `-` operator for unsigned integers.
|
||||||
|
|
||||||
proc `*`*[T: SomeUInt](x, y: T): T {.magic: "MulU", noSideEffect.}
|
proc `*`*[T: SomeUnsignedInt](x, y: T): T {.magic: "MulU", noSideEffect.}
|
||||||
## Binary `*` operator for unsigned integers.
|
## Binary `*` operator for unsigned integers.
|
||||||
|
|
||||||
proc `div`*[T: SomeUInt](x, y: T): T {.magic: "DivU", noSideEffect.}
|
proc `div`*[T: SomeUnsignedInt](x, y: T): T {.magic: "DivU", noSideEffect.}
|
||||||
## computes the integer division. This is roughly the same as
|
## computes the integer division. This is roughly the same as
|
||||||
## ``floor(x/y)``.
|
## ``floor(x/y)``.
|
||||||
|
|
||||||
proc `mod`*[T: SomeUInt](x, y: T): T {.magic: "ModU", noSideEffect.}
|
proc `mod`*[T: SomeUnsignedInt](x, y: T): T {.magic: "ModU", noSideEffect.}
|
||||||
## computes the integer modulo operation. This is the same as
|
## computes the integer modulo operation. This is the same as
|
||||||
## ``x - (x div y) * y``.
|
## ``x - (x div y) * y``.
|
||||||
|
|
||||||
proc `<=`*[T: SomeUInt](x, y: T): bool {.magic: "LeU", noSideEffect.}
|
proc `<=`*[T: SomeUnsignedInt](x, y: T): bool {.magic: "LeU", noSideEffect.}
|
||||||
## Returns true iff ``x <= y``.
|
## Returns true iff ``x <= y``.
|
||||||
|
|
||||||
proc `<`*[T: SomeUInt](x, y: T): bool {.magic: "LtU", noSideEffect.}
|
proc `<`*[T: SomeUnsignedInt](x, y: T): bool {.magic: "LtU", noSideEffect.}
|
||||||
## Returns true iff ``unsigned(x) < unsigned(y)``.
|
## Returns true iff ``unsigned(x) < unsigned(y)``.
|
||||||
|
|
||||||
|
|
|
||||||
134
tests/misc/tunsignedcomp.nim
Normal file
134
tests/misc/tunsignedcomp.nim
Normal file
|
|
@ -0,0 +1,134 @@
|
||||||
|
discard """
|
||||||
|
output: ''''''
|
||||||
|
"""
|
||||||
|
|
||||||
|
# All operations involving uint64 are commented out
|
||||||
|
# as they're not yet supported.
|
||||||
|
# All other operations are handled by implicit conversions from uints to ints
|
||||||
|
# uint64 could be supported but would need special implementation of the operators
|
||||||
|
|
||||||
|
# unsigned < signed
|
||||||
|
assert 10'u8 < 20'i8
|
||||||
|
assert 10'u8 < 20'i16
|
||||||
|
assert 10'u8 < 20'i32
|
||||||
|
assert 10'u8 < 20'i64
|
||||||
|
|
||||||
|
assert 10'u16 < 20'i8
|
||||||
|
assert 10'u16 < 20'i16
|
||||||
|
assert 10'u16 < 20'i32
|
||||||
|
assert 10'u16 < 20'i64
|
||||||
|
|
||||||
|
assert 10'u32 < 20'i8
|
||||||
|
assert 10'u32 < 20'i16
|
||||||
|
assert 10'u32 < 20'i32
|
||||||
|
assert 10'u32 < 20'i64
|
||||||
|
|
||||||
|
# assert 10'u64 < 20'i8
|
||||||
|
# assert 10'u64 < 20'i16
|
||||||
|
# assert 10'u64 < 20'i32
|
||||||
|
# assert 10'u64 < 20'i64
|
||||||
|
|
||||||
|
# signed < unsigned
|
||||||
|
assert 10'i8 < 20'u8
|
||||||
|
assert 10'i8 < 20'u16
|
||||||
|
assert 10'i8 < 20'u32
|
||||||
|
# assert 10'i8 < 20'u64
|
||||||
|
|
||||||
|
assert 10'i16 < 20'u8
|
||||||
|
assert 10'i16 < 20'u16
|
||||||
|
assert 10'i16 < 20'u32
|
||||||
|
# assert 10'i16 < 20'u64
|
||||||
|
|
||||||
|
assert 10'i32 < 20'u8
|
||||||
|
assert 10'i32 < 20'u16
|
||||||
|
assert 10'i32 < 20'u32
|
||||||
|
# assert 10'i32 < 20'u64
|
||||||
|
|
||||||
|
assert 10'i64 < 20'u8
|
||||||
|
assert 10'i64 < 20'u16
|
||||||
|
assert 10'i64 < 20'u32
|
||||||
|
# assert 10'i64 < 20'u64
|
||||||
|
|
||||||
|
# unsigned <= signed
|
||||||
|
assert 10'u8 <= 20'i8
|
||||||
|
assert 10'u8 <= 20'i16
|
||||||
|
assert 10'u8 <= 20'i32
|
||||||
|
assert 10'u8 <= 20'i64
|
||||||
|
|
||||||
|
assert 10'u16 <= 20'i8
|
||||||
|
assert 10'u16 <= 20'i16
|
||||||
|
assert 10'u16 <= 20'i32
|
||||||
|
assert 10'u16 <= 20'i64
|
||||||
|
|
||||||
|
assert 10'u32 <= 20'i8
|
||||||
|
assert 10'u32 <= 20'i16
|
||||||
|
assert 10'u32 <= 20'i32
|
||||||
|
assert 10'u32 <= 20'i64
|
||||||
|
|
||||||
|
# assert 10'u64 <= 20'i8
|
||||||
|
# assert 10'u64 <= 20'i16
|
||||||
|
# assert 10'u64 <= 20'i32
|
||||||
|
# assert 10'u64 <= 20'i64
|
||||||
|
|
||||||
|
# signed <= unsigned
|
||||||
|
assert 10'i8 <= 20'u8
|
||||||
|
assert 10'i8 <= 20'u16
|
||||||
|
assert 10'i8 <= 20'u32
|
||||||
|
# assert 10'i8 <= 20'u64
|
||||||
|
|
||||||
|
assert 10'i16 <= 20'u8
|
||||||
|
assert 10'i16 <= 20'u16
|
||||||
|
assert 10'i16 <= 20'u32
|
||||||
|
# assert 10'i16 <= 20'u64
|
||||||
|
|
||||||
|
assert 10'i32 <= 20'u8
|
||||||
|
assert 10'i32 <= 20'u16
|
||||||
|
assert 10'i32 <= 20'u32
|
||||||
|
# assert 10'i32 <= 20'u64
|
||||||
|
|
||||||
|
assert 10'i64 <= 20'u8
|
||||||
|
assert 10'i64 <= 20'u16
|
||||||
|
assert 10'i64 <= 20'u32
|
||||||
|
# assert 10'i64 <= 20'u64
|
||||||
|
|
||||||
|
# signed == unsigned
|
||||||
|
assert 10'i8 == 10'u8
|
||||||
|
assert 10'i8 == 10'u16
|
||||||
|
assert 10'i8 == 10'u32
|
||||||
|
# assert 10'i8 == 10'u64
|
||||||
|
|
||||||
|
assert 10'i16 == 10'u8
|
||||||
|
assert 10'i16 == 10'u16
|
||||||
|
assert 10'i16 == 10'u32
|
||||||
|
# assert 10'i16 == 10'u64
|
||||||
|
|
||||||
|
assert 10'i32 == 10'u8
|
||||||
|
assert 10'i32 == 10'u16
|
||||||
|
assert 10'i32 == 10'u32
|
||||||
|
# assert 10'i32 == 10'u64
|
||||||
|
|
||||||
|
assert 10'i64 == 10'u8
|
||||||
|
assert 10'i64 == 10'u16
|
||||||
|
assert 10'i64 == 10'u32
|
||||||
|
# assert 10'i64 == 10'u64
|
||||||
|
|
||||||
|
# unsigned == signed
|
||||||
|
assert 10'u8 == 10'i8
|
||||||
|
assert 10'u8 == 10'i16
|
||||||
|
assert 10'u8 == 10'i32
|
||||||
|
# assert 10'u8 == 10'i64
|
||||||
|
|
||||||
|
assert 10'u16 == 10'i8
|
||||||
|
assert 10'u16 == 10'i16
|
||||||
|
assert 10'u16 == 10'i32
|
||||||
|
# assert 10'u16 == 10'i64
|
||||||
|
|
||||||
|
assert 10'u32 == 10'i8
|
||||||
|
assert 10'u32 == 10'i16
|
||||||
|
assert 10'u32 == 10'i32
|
||||||
|
# assert 10'u32 == 10'i64
|
||||||
|
|
||||||
|
# assert 10'u64 == 10'i8
|
||||||
|
# assert 10'u64 == 10'i16
|
||||||
|
# assert 10'u64 == 10'i32
|
||||||
|
# assert 10'u64 == 10'i64
|
||||||
52
tests/misc/tunsignedconv.nim
Normal file
52
tests/misc/tunsignedconv.nim
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
discard """
|
||||||
|
output: ''''''
|
||||||
|
"""
|
||||||
|
|
||||||
|
import unsigned
|
||||||
|
|
||||||
|
# Tests unsigned literals and implicit conversion between uints and ints
|
||||||
|
# Passes if it compiles
|
||||||
|
|
||||||
|
var h8:uint8 = 128
|
||||||
|
var h16:uint16 = 32768
|
||||||
|
var h32:uint32 = 2147483648'u32
|
||||||
|
var h64:uint64 = 9223372036854775808'u64
|
||||||
|
var foobar:uint64 = 9223372036854775813'u64 # Issue 728
|
||||||
|
|
||||||
|
var v8:uint8 = 10
|
||||||
|
var v16:uint16 = 10
|
||||||
|
var v32:uint32 = 10
|
||||||
|
var v64:uint64 = 10
|
||||||
|
|
||||||
|
var a8:int = v8 + 10
|
||||||
|
var a16:int = v16 + 10
|
||||||
|
# var a32:int = v32 + 10
|
||||||
|
# var a64:int = v64 + 10
|
||||||
|
|
||||||
|
var d8 = v8 + 10'i8
|
||||||
|
var d16 = v8 + 10'i16
|
||||||
|
var d32 = v8 + 10'i32
|
||||||
|
# var d64 = v8 + 10'i64
|
||||||
|
|
||||||
|
var f8 = v16 + 10'i8
|
||||||
|
var f16 = v16 + 10'i16
|
||||||
|
var f32 = v16 + 10'i32
|
||||||
|
# var f64 = v16 + 10'i64
|
||||||
|
|
||||||
|
var g8 = v32 + 10'i8
|
||||||
|
var g16 = v32 + 10'i16
|
||||||
|
var g32 = v32 + 10'i32
|
||||||
|
# var g64 = v32 + 10'i64
|
||||||
|
|
||||||
|
# var n8 = v64 + 10'i8
|
||||||
|
# var n16 = v64 + 10'i16
|
||||||
|
# var n32 = v64 + 10'i32
|
||||||
|
# var n64 = v64 + 10'i64
|
||||||
|
|
||||||
|
var ar: array[0..20, int]
|
||||||
|
var n8 = ar[v8]
|
||||||
|
var n16 = ar[v16]
|
||||||
|
var n32 = ar[v32]
|
||||||
|
var n64 = ar[v64]
|
||||||
|
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue