Added support for big 'u64 literals

Removed duplicate "SomeUInt' typedef from unsigned.nim
This commit is contained in:
Audun Wilhelmsen 2015-01-02 22:10:49 +01:00
commit e5bfb7d550
2 changed files with 34 additions and 17 deletions

View file

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

View file

@ -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)``.