diff options
| author | genotrance <dev@genotrance.com> | 2020-05-01 12:45:15 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-05-01 12:45:15 -0500 |
| commit | 568cfc5da6e541101294b956670dddd509dadc2a (patch) | |
| tree | 163443f050814a67af7276586ebf9962ccc332ee | |
| parent | 08f8ca32f41d16ade0b364e048a0a99b1a01eebe (diff) | |
| parent | 34043bd5af52d20df1e85f28c4369d5810248017 (diff) | |
| download | nimterop-568cfc5da6e541101294b956670dddd509dadc2a.tar.gz nimterop-568cfc5da6e541101294b956670dddd509dadc2a.zip | |
Fix #198: integer out of range
| -rw-r--r-- | nimterop/exprparser.nim | 44 | ||||
| -rw-r--r-- | tests/include/tast2.h | 3 | ||||
| -rw-r--r-- | tests/tast2.nim | 2 |
3 files changed, 34 insertions, 15 deletions
diff --git a/nimterop/exprparser.nim b/nimterop/exprparser.nim index c74f0b6..e68c0b1 100644 --- a/nimterop/exprparser.nim +++ b/nimterop/exprparser.nim @@ -133,13 +133,38 @@ proc getFloatNode(number, suffix: string): PNode {.inline.} = proc getIntNode(number, suffix: string): PNode {.inline.} = ## 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 of "u", "U": result = newNode(nkUintLit) 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": - 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": result = newNode(nkInt64Lit) of "ull", "ULL": @@ -147,19 +172,8 @@ proc getIntNode(number, suffix: string): PNode {.inline.} = else: result = newNode(nkIntLit) - # I realize these regex are wasteful on performance, but - # couldn't come up with a better idea. - 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) + result.intVal = val + result.flags = flags proc getNumNode(number, suffix: string): PNode {.inline.} = ## Convert a C number to a Nim number PNode diff --git a/tests/include/tast2.h b/tests/include/tast2.h index b47a801..e1d4529 100644 --- a/tests/include/tast2.h +++ b/tests/include/tast2.h @@ -33,6 +33,9 @@ extern "C" { #define EQ5 AVAL != BVAL #define EQ6 AVAL == BVAL +// testing integer out of long int range +#define INT_FAST16_MIN (-9223372036854775807L-1) + #define SIZEOF sizeof(char) #define REG_STR "regular string" #define NOTSUPPORTEDSTR "not a " REG_STR diff --git a/tests/tast2.nim b/tests/tast2.nim index 273d3e3..54b89e5 100644 --- a/tests/tast2.nim +++ b/tests/tast2.nim @@ -131,6 +131,8 @@ assert SIZEOF == 1 assert COERCE == 645635670332'u64 assert COERCE2 == 645635670332'i64 +assert INT_FAST16_MIN == -9223372036854775807'i64 - 1 + assert BINEXPR == 5 assert BOOL == true assert MATHEXPR == -99 |
