Fix #198: integer out of range
This commit is contained in:
parent
4784616d98
commit
34043bd5af
3 changed files with 34 additions and 15 deletions
|
|
@ -133,13 +133,38 @@ proc getFloatNode(number, suffix: string): PNode {.inline.} =
|
||||||
|
|
||||||
proc getIntNode(number, suffix: string): PNode {.inline.} =
|
proc getIntNode(number, suffix: string): PNode {.inline.} =
|
||||||
## Get a Nim int node from a C integer expression + suffix
|
## Get a Nim int node from a C integer expression + suffix
|
||||||
|
var
|
||||||
|
val: BiggestInt
|
||||||
|
flags: TNodeFlags
|
||||||
|
# I realize these regex are wasteful on performance, but
|
||||||
|
# couldn't come up with a better idea.
|
||||||
|
if number.contains(re"0[xX]"):
|
||||||
|
val = parseHexInt(number)
|
||||||
|
flags = {nfBase16}
|
||||||
|
elif number.contains(re"0[bB]"):
|
||||||
|
val = parseBinInt(number)
|
||||||
|
flags = {nfBase2}
|
||||||
|
elif number.contains(re"0[oO]"):
|
||||||
|
val = parseOctInt(number)
|
||||||
|
flags = {nfBase8}
|
||||||
|
else:
|
||||||
|
val = parseInt(number)
|
||||||
|
|
||||||
case suffix
|
case suffix
|
||||||
of "u", "U":
|
of "u", "U":
|
||||||
result = newNode(nkUintLit)
|
result = newNode(nkUintLit)
|
||||||
of "l", "L":
|
of "l", "L":
|
||||||
result = newNode(nkInt32Lit)
|
# If the value doesn't fit, adjust
|
||||||
|
if val > int32.high or val < int32.low:
|
||||||
|
result = newNode(nkInt64Lit)
|
||||||
|
else:
|
||||||
|
result = newNode(nkInt32Lit)
|
||||||
of "ul", "UL":
|
of "ul", "UL":
|
||||||
result = newNode(nkUint32Lit)
|
# If the value doesn't fit, adjust
|
||||||
|
if val > uint32.high.BiggestInt:
|
||||||
|
result = newNode(nkUInt64Lit)
|
||||||
|
else:
|
||||||
|
result = newNode(nkUInt32Lit)
|
||||||
of "ll", "LL":
|
of "ll", "LL":
|
||||||
result = newNode(nkInt64Lit)
|
result = newNode(nkInt64Lit)
|
||||||
of "ull", "ULL":
|
of "ull", "ULL":
|
||||||
|
|
@ -147,19 +172,8 @@ proc getIntNode(number, suffix: string): PNode {.inline.} =
|
||||||
else:
|
else:
|
||||||
result = newNode(nkIntLit)
|
result = newNode(nkIntLit)
|
||||||
|
|
||||||
# I realize these regex are wasteful on performance, but
|
result.intVal = val
|
||||||
# couldn't come up with a better idea.
|
result.flags = flags
|
||||||
if number.contains(re"0[xX]"):
|
|
||||||
result.intVal = parseHexInt(number)
|
|
||||||
result.flags = {nfBase16}
|
|
||||||
elif number.contains(re"0[bB]"):
|
|
||||||
result.intVal = parseBinInt(number)
|
|
||||||
result.flags = {nfBase2}
|
|
||||||
elif number.contains(re"0[oO]"):
|
|
||||||
result.intVal = parseOctInt(number)
|
|
||||||
result.flags = {nfBase8}
|
|
||||||
else:
|
|
||||||
result.intVal = parseInt(number)
|
|
||||||
|
|
||||||
proc getNumNode(number, suffix: string): PNode {.inline.} =
|
proc getNumNode(number, suffix: string): PNode {.inline.} =
|
||||||
## Convert a C number to a Nim number PNode
|
## Convert a C number to a Nim number PNode
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,9 @@ extern "C" {
|
||||||
#define EQ5 AVAL != BVAL
|
#define EQ5 AVAL != BVAL
|
||||||
#define EQ6 AVAL == BVAL
|
#define EQ6 AVAL == BVAL
|
||||||
|
|
||||||
|
// testing integer out of long int range
|
||||||
|
#define INT_FAST16_MIN (-9223372036854775807L-1)
|
||||||
|
|
||||||
#define SIZEOF sizeof(char)
|
#define SIZEOF sizeof(char)
|
||||||
#define REG_STR "regular string"
|
#define REG_STR "regular string"
|
||||||
#define NOTSUPPORTEDSTR "not a " REG_STR
|
#define NOTSUPPORTEDSTR "not a " REG_STR
|
||||||
|
|
|
||||||
|
|
@ -131,6 +131,8 @@ assert SIZEOF == 1
|
||||||
assert COERCE == 645635670332'u64
|
assert COERCE == 645635670332'u64
|
||||||
assert COERCE2 == 645635670332'i64
|
assert COERCE2 == 645635670332'i64
|
||||||
|
|
||||||
|
assert INT_FAST16_MIN == -9223372036854775807'i64 - 1
|
||||||
|
|
||||||
assert BINEXPR == 5
|
assert BINEXPR == 5
|
||||||
assert BOOL == true
|
assert BOOL == true
|
||||||
assert MATHEXPR == -99
|
assert MATHEXPR == -99
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue