Skip to content

Commit

Permalink
[other] preparations for --styleCheck:error for the Nim compiler (#11478
Browse files Browse the repository at this point in the history
)

(cherry picked from commit 572b7c3)
  • Loading branch information
Araq authored and narimiran committed Jun 16, 2019
1 parent 4c8a021 commit 05d6473
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 43 deletions.
6 changes: 2 additions & 4 deletions lib/system/ansi_c.nim
Expand Up @@ -88,8 +88,6 @@ when defined(macosx):
const SIGBUS* = cint(10)
elif defined(haiku):
const SIGBUS* = cint(30)
else:
template SIGBUS*: untyped = SIGSEGV

when defined(nimSigSetjmp) and not defined(nimStdSetjmp):
proc c_longjmp*(jmpb: C_JmpBuf, retval: cint) {.
Expand All @@ -109,8 +107,8 @@ else:
proc c_setjmp*(jmpb: C_JmpBuf): cint {.
header: "<setjmp.h>", importc: "setjmp".}

type c_sighandler_t = proc (a: cint) {.noconv.}
proc c_signal*(sign: cint, handler: proc (a: cint) {.noconv.}): c_sighandler_t {.
type CSighandlerT = proc (a: cint) {.noconv.}
proc c_signal*(sign: cint, handler: proc (a: cint) {.noconv.}): CSighandlerT {.
importc: "signal", header: "<signal.h>", discardable.}

type
Expand Down
6 changes: 3 additions & 3 deletions lib/system/dyncalls.nim
Expand Up @@ -118,11 +118,11 @@ elif defined(windows) or defined(dos):
proc nimGetProcAddr(lib: LibHandle, name: cstring): ProcAddr =
result = getProcAddress(cast[THINSTANCE](lib), name)
if result != nil: return
const decorated_length = 250
var decorated: array[decorated_length, char]
const decoratedLength = 250
var decorated: array[decoratedLength, char]
decorated[0] = '_'
var m = 1
while m < (decorated_length - 5):
while m < (decoratedLength - 5):
if name[m - 1] == '\x00': break
decorated[m] = name[m - 1]
inc(m)
Expand Down
13 changes: 7 additions & 6 deletions lib/system/excpt.nim
Expand Up @@ -474,10 +474,10 @@ when defined(endb):
when defined(cpp) and appType != "lib" and
not defined(js) and not defined(nimscript) and
hostOS != "standalone" and not defined(noCppExceptions):
type

type
StdException {.importcpp: "std::exception", header: "<exception>".} = object

proc what(ex: StdException): cstring {.importcpp: "((char *)#.what())".}

proc setTerminate(handler: proc() {.noconv.})
Expand All @@ -497,7 +497,7 @@ when defined(cpp) and appType != "lib" and
msg = "Error: unhandled cpp exception: " & $e.what()
except:
msg = "Error: unhandled unknown cpp exception"

when defined(genode):
# stderr not available by default, use the LOG session
echo msg
Expand All @@ -518,7 +518,7 @@ when not defined(noSignalHandler) and not defined(useNimRtl):
action("SIGABRT: Abnormal termination.\n")
elif s == SIGFPE: action("SIGFPE: Arithmetic error.\n")
elif s == SIGILL: action("SIGILL: Illegal operation.\n")
elif s == SIGBUS:
elif (when declared(SIGBUS): s == SIGBUS else: false):
action("SIGBUS: Illegal storage access. (Attempt to read from nil?)\n")
else:
block platformSpecificSignal:
Expand Down Expand Up @@ -553,7 +553,8 @@ when not defined(noSignalHandler) and not defined(useNimRtl):
c_signal(SIGABRT, signalHandler)
c_signal(SIGFPE, signalHandler)
c_signal(SIGILL, signalHandler)
c_signal(SIGBUS, signalHandler)
when declared(SIGBUS):
c_signal(SIGBUS, signalHandler)
when declared(SIGPIPE):
c_signal(SIGPIPE, signalHandler)

Expand Down
63 changes: 33 additions & 30 deletions lib/system/strmantle.nim
Expand Up @@ -149,14 +149,14 @@ proc nimParseBiggestFloat(s: string, number: var BiggestFloat,
kdigits, fdigits = 0
exponent: int
integer: uint64
frac_exponent = 0
exp_sign = 1
first_digit = -1
has_sign = false
fracExponent = 0
expSign = 1
firstDigit = -1
hasSign = false

# Sign?
if i < s.len and (s[i] == '+' or s[i] == '-'):
has_sign = true
hasSign = true
if s[i] == '-':
sign = -1.0
inc(i)
Expand All @@ -180,7 +180,7 @@ proc nimParseBiggestFloat(s: string, number: var BiggestFloat,
return 0

if i < s.len and s[i] in {'0'..'9'}:
first_digit = (s[i].ord - '0'.ord)
firstDigit = (s[i].ord - '0'.ord)
# Integer part?
while i < s.len and s[i] in {'0'..'9'}:
inc(kdigits)
Expand All @@ -194,31 +194,31 @@ proc nimParseBiggestFloat(s: string, number: var BiggestFloat,
# if no integer part, Skip leading zeros
if kdigits <= 0:
while i < s.len and s[i] == '0':
inc(frac_exponent)
inc(fracExponent)
inc(i)
while i < s.len and s[i] == '_': inc(i)

if first_digit == -1 and i < s.len and s[i] in {'0'..'9'}:
first_digit = (s[i].ord - '0'.ord)
if firstDigit == -1 and i < s.len and s[i] in {'0'..'9'}:
firstDigit = (s[i].ord - '0'.ord)
# get fractional part
while i < s.len and s[i] in {'0'..'9'}:
inc(fdigits)
inc(frac_exponent)
inc(fracExponent)
integer = integer * 10'u64 + (s[i].ord - '0'.ord).uint64
inc(i)
while i < s.len and s[i] == '_': inc(i)

# if has no digits: return error
if kdigits + fdigits <= 0 and
(i == start or # no char consumed (empty string).
(i == start + 1 and has_sign)): # or only '+' or '-
(i == start + 1 and hasSign)): # or only '+' or '-
return 0

if i+1 < s.len and s[i] in {'e', 'E'}:
inc(i)
if s[i] == '+' or s[i] == '-':
if s[i] == '-':
exp_sign = -1
expSign = -1

inc(i)
if s[i] notin {'0'..'9'}:
Expand All @@ -228,13 +228,13 @@ proc nimParseBiggestFloat(s: string, number: var BiggestFloat,
inc(i)
while i < s.len and s[i] == '_': inc(i) # underscores are allowed and ignored

var real_exponent = exp_sign*exponent - frac_exponent
let exp_negative = real_exponent < 0
var abs_exponent = abs(real_exponent)
var realExponent = expSign*exponent - fracExponent
let expNegative = realExponent < 0
var absExponent = abs(realExponent)

# if exponent greater than can be represented: +/- zero or infinity
if abs_exponent > 999:
if exp_negative:
if absExponent > 999:
if expNegative:
number = 0.0*sign
else:
number = Inf*sign
Expand All @@ -243,20 +243,20 @@ proc nimParseBiggestFloat(s: string, number: var BiggestFloat,
# if integer is representable in 53 bits: fast path
# max fast path integer is 1<<53 - 1 or 8999999999999999 (16 digits)
let digits = kdigits + fdigits
if digits <= 15 or (digits <= 16 and first_digit <= 8):
if digits <= 15 or (digits <= 16 and firstDigit <= 8):
# max float power of ten with set bits above the 53th bit is 10^22
if abs_exponent <= 22:
if exp_negative:
number = sign * integer.float / powtens[abs_exponent]
if absExponent <= 22:
if expNegative:
number = sign * integer.float / powtens[absExponent]
else:
number = sign * integer.float * powtens[abs_exponent]
number = sign * integer.float * powtens[absExponent]
return i - start

# if exponent is greater try to fit extra exponent above 22 by multiplying
# integer part is there is space left.
let slop = 15 - kdigits - fdigits
if abs_exponent <= 22 + slop and not exp_negative:
number = sign * integer.float * powtens[slop] * powtens[abs_exponent-slop]
if absExponent <= 22 + slop and not expNegative:
number = sign * integer.float * powtens[slop] * powtens[absExponent-slop]
return i - start

# if failed: slow path with strtod.
Expand All @@ -276,14 +276,17 @@ proc nimParseBiggestFloat(s: string, number: var BiggestFloat,
inc(i)

# insert exponent
t[ti] = 'E'; inc(ti)
t[ti] = (if exp_negative: '-' else: '+'); inc(ti)
inc(ti, 3)
t[ti] = 'E'
inc(ti)
t[ti] = if expNegative: '-' else: '+'
inc(ti, 4)

# insert adjusted exponent
t[ti-1] = ('0'.ord + abs_exponent mod 10).char; abs_exponent = abs_exponent div 10
t[ti-2] = ('0'.ord + abs_exponent mod 10).char; abs_exponent = abs_exponent div 10
t[ti-3] = ('0'.ord + abs_exponent mod 10).char
t[ti-1] = ('0'.ord + absExponent mod 10).char
absExponent = absExponent div 10
t[ti-2] = ('0'.ord + absExponent mod 10).char
absExponent = absExponent div 10
t[ti-3] = ('0'.ord + absExponent mod 10).char

when defined(nimNoArrayToCstringConversion):
number = c_strtod(addr t, nil)
Expand Down

0 comments on commit 05d6473

Please sign in to comment.