Skip to content

Commit

Permalink
Merge branch 'devel' of github.com:nim-lang/Nim into devel
Browse files Browse the repository at this point in the history
  • Loading branch information
Araq committed Jun 15, 2017
2 parents c3b0eb5 + 17b55f9 commit d947753
Show file tree
Hide file tree
Showing 15 changed files with 172 additions and 43 deletions.
7 changes: 6 additions & 1 deletion compiler/ccgtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,12 @@ proc genRecordFieldsAux(m: BModule, n: PNode,
let a = genRecordFieldsAux(m, k, "$1.$2" % [ae, sname], rectype,
check)
if a != nil:
add(unionBody, "struct {")
if tfPacked notin rectype.flags:
add(unionBody, "struct {")
else:
addf(unionBody, CC[cCompiler].structStmtFmt,
[rope"struct", nil, rope(CC[cCompiler].packedPragma)])
add(unionBody, "{")
add(unionBody, a)
addf(unionBody, "} $1;$n", [sname])
else:
Expand Down
10 changes: 10 additions & 0 deletions compiler/cgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,18 @@ proc generateHeaders(m: BModule) =
addf(m.s[cfsHeaders], "#include \"$1\"$N", [rope(it)])
else:
addf(m.s[cfsHeaders], "#include $1$N", [rope(it)])
add(m.s[cfsHeaders], "#undef LANGUAGE_C" & tnl)
add(m.s[cfsHeaders], "#undef MIPSEB" & tnl)
add(m.s[cfsHeaders], "#undef MIPSEL" & tnl)
add(m.s[cfsHeaders], "#undef PPC" & tnl)
add(m.s[cfsHeaders], "#undef R3000" & tnl)
add(m.s[cfsHeaders], "#undef R4000" & tnl)
add(m.s[cfsHeaders], "#undef i386" & tnl)
add(m.s[cfsHeaders], "#undef linux" & tnl)
add(m.s[cfsHeaders], "#undef mips" & tnl)
add(m.s[cfsHeaders], "#undef near" & tnl)
add(m.s[cfsHeaders], "#undef powerpc" & tnl)
add(m.s[cfsHeaders], "#undef unix" & tnl)

proc initFrame(p: BProc, procname, filename: Rope): Rope =
discard cgsym(p.module, "nimFrame")
Expand Down
2 changes: 1 addition & 1 deletion compiler/msgs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ var

proc toCChar*(c: char): string =
case c
of '\0'..'\x1F', '\x80'..'\xFF': result = '\\' & toOctal(c)
of '\0'..'\x1F', '\x7F'..'\xFF': result = '\\' & toOctal(c)
of '\'', '\"', '\\', '?': result = '\\' & c
else: result = $(c)

Expand Down
2 changes: 1 addition & 1 deletion lib/pure/asynchttpserver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ proc parseProtocol(protocol: string): tuple[orig: string, major, minor: int] =
i.inc protocol.parseInt(result.minor, i)

proc sendStatus(client: AsyncSocket, status: string): Future[void] =
client.send("HTTP/1.1 " & status & "\c\L")
client.send("HTTP/1.1 " & status & "\c\L\c\L")

proc processClient(client: AsyncSocket, address: string,
callback: proc (request: Request):
Expand Down
3 changes: 2 additions & 1 deletion lib/pure/httpclient.nim
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
## let body = %*{
## "data": "some text"
## }
## echo client.request("http://some.api", httpMethod = HttpPost, body = $body)
## let response = client.request("http://some.api", httpMethod = HttpPost, body = $body)
## echo response.status
##
## Progress reporting
## ==================
Expand Down
2 changes: 1 addition & 1 deletion lib/pure/json.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@ proc escapeJson*(s: string; result: var string) =
result.add("\"")
for x in runes(s):
var r = int(x)
if r >= 32 and r <= 127:
if r >= 32 and r <= 126:
var c = chr(r)
case c
of '"': result.add("\\\"")
Expand Down
25 changes: 25 additions & 0 deletions lib/pure/math.nim
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,12 @@ proc radToDeg*[T: float32|float64](d: T): T {.inline.} =
## Convert from radians to degrees
result = T(d) / RadPerDeg

proc sgn*[T: SomeNumber](x: T): int {.inline.} =
## Sign function. Returns -1 for negative numbers and `NegInf`, 1 for
## positive numbers and `Inf`, and 0 for positive zero, negative zero and
## `NaN`.
ord(T(0) < x) - ord(x < T(0))

proc `mod`*[T: float32|float64](x, y: T): T =
## Computes the modulo operation for float operators. Equivalent
## to ``x - y * floor(x/y)``. Note that the remainder will always
Expand Down Expand Up @@ -447,6 +453,7 @@ when isMainModule and not defined(JS):
assert(lgamma(1.0) == 0.0) # ln(1.0) == 0.0
assert(erf(6.0) > erf(5.0))
assert(erfc(6.0) < erfc(5.0))

when isMainModule:
# Function for approximate comparison of floats
proc `==~`(x, y: float): bool = (abs(x-y) < 1e-9)
Expand Down Expand Up @@ -509,3 +516,21 @@ when isMainModule:
doAssert(classify(trunc(-1e1000000'f32)) == fcNegInf)
doAssert(classify(trunc(f_nan.float32)) == fcNan)
doAssert(classify(trunc(0.0'f32)) == fcZero)

block: # sgn() tests
assert sgn(1'i8) == 1
assert sgn(1'i16) == 1
assert sgn(1'i32) == 1
assert sgn(1'i64) == 1
assert sgn(1'u8) == 1
assert sgn(1'u16) == 1
assert sgn(1'u32) == 1
assert sgn(1'u64) == 1
assert sgn(-12342.8844'f32) == -1
assert sgn(123.9834'f64) == 1
assert sgn(0'i32) == 0
assert sgn(0'f32) == 0
assert sgn(NegInf) == -1
assert sgn(Inf) == 1
assert sgn(NaN) == 0

45 changes: 30 additions & 15 deletions lib/pure/parseopt.nim
Original file line number Diff line number Diff line change
Expand Up @@ -149,26 +149,41 @@ proc cmdLineRest*(p: OptParser): TaintedString {.rtl, extern: "npo$1".} =
## retrieves the rest of the command line that has not been parsed yet.
result = strip(substr(p.cmd, p.pos, len(p.cmd) - 1)).TaintedString

iterator getopt*(p: var OptParser): tuple[kind: CmdLineKind, key, val: TaintedString] =
## This is an convenience iterator for iterating over the given OptParser object.
## Example:
##
## .. code-block:: nim
## var p = initOptParser("--left --debug:3 -l=4 -r:2")
## for kind, key, val in p.getopt():
## case kind
## of cmdArgument:
## filename = key
## of cmdLongOption, cmdShortOption:
## case key
## of "help", "h": writeHelp()
## of "version", "v": writeVersion()
## of cmdEnd: assert(false) # cannot happen
## if filename == "":
## # no filename has been given, so we show the help:
## writeHelp()
p.pos = 0
while true:
next(p)
if p.kind == cmdEnd: break
yield (p.kind, p.key, p.val)

when declared(initOptParser):
iterator getopt*(): tuple[kind: CmdLineKind, key, val: TaintedString] =
## This is an convenience iterator for iterating over the command line.
## This uses the OptParser object. Example:
## This is an convenience iterator for iterating over the command line arguments.
## This create a new OptParser object.
## See above for a more detailed example
##
## .. code-block:: nim
## var
## filename = ""
## for kind, key, val in getopt():
## case kind
## of cmdArgument:
## filename = key
## of cmdLongOption, cmdShortOption:
## case key
## of "help", "h": writeHelp()
## of "version", "v": writeVersion()
## of cmdEnd: assert(false) # cannot happen
## if filename == "":
## # no filename has been given, so we show the help:
## writeHelp()
## # this will iterate over all arguments passed to the cmdline.
## continue
##
var p = initOptParser()
while true:
next(p)
Expand Down
45 changes: 30 additions & 15 deletions lib/pure/parseopt2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -123,26 +123,41 @@ type

{.deprecated: [TGetoptResult: GetoptResult].}

iterator getopt*(p: var OptParser): GetoptResult =
## This is an convenience iterator for iterating over the given OptParser object.
## Example:
##
## .. code-block:: nim
## var p = initOptParser("--left --debug:3 -l=4 -r:2")
## for kind, key, val in p.getopt():
## case kind
## of cmdArgument:
## filename = key
## of cmdLongOption, cmdShortOption:
## case key
## of "help", "h": writeHelp()
## of "version", "v": writeVersion()
## of cmdEnd: assert(false) # cannot happen
## if filename == "":
## # no filename has been given, so we show the help:
## writeHelp()
p.pos = 0
while true:
next(p)
if p.kind == cmdEnd: break
yield (p.kind, p.key, p.val)

when declared(paramCount):
iterator getopt*(): GetoptResult =
## This is an convenience iterator for iterating over the command line.
## This uses the OptParser object. Example:
## This is an convenience iterator for iterating over the command line arguments.
## This create a new OptParser object.
## See above for a more detailed example
##
## .. code-block:: nim
## var
## filename = ""
## for kind, key, val in getopt():
## case kind
## of cmdArgument:
## filename = key
## of cmdLongOption, cmdShortOption:
## case key
## of "help", "h": writeHelp()
## of "version", "v": writeVersion()
## of cmdEnd: assert(false) # cannot happen
## if filename == "":
## # no filename has been given, so we show the help:
## writeHelp()
## # this will iterate over all arguments passed to the cmdline.
## continue
##
var p = initOptParser()
while true:
next(p)
Expand Down
2 changes: 1 addition & 1 deletion lib/pure/pegs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ proc esc(c: char, reserved = {'\0'..'\255'}): string =
of '\a': result = "\\a"
of '\\': result = "\\\\"
of 'a'..'z', 'A'..'Z', '0'..'9', '_': result = $c
elif c < ' ' or c >= '\128': result = '\\' & $ord(c)
elif c < ' ' or c >= '\127': result = '\\' & $ord(c)
elif c in reserved: result = '\\' & c
else: result = $c

Expand Down
4 changes: 2 additions & 2 deletions lib/pure/strutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1643,7 +1643,7 @@ proc escape*(s: string, prefix = "\"", suffix = "\""): string {.noSideEffect,
## * replaces any ``\`` by ``\\``
## * replaces any ``'`` by ``\'``
## * replaces any ``"`` by ``\"``
## * replaces any other character in the set ``{'\0'..'\31', '\128'..'\255'}``
## * replaces any other character in the set ``{'\0'..'\31', '\127'..'\255'}``
## by ``\xHH`` where ``HH`` is its hexadecimal value.
## The procedure has been designed so that its output is usable for many
## different common syntaxes. The resulting string is prefixed with
Expand All @@ -1653,7 +1653,7 @@ proc escape*(s: string, prefix = "\"", suffix = "\""): string {.noSideEffect,
result.add(prefix)
for c in items(s):
case c
of '\0'..'\31', '\128'..'\255':
of '\0'..'\31', '\127'..'\255':
add(result, "\\x")
add(result, toHex(ord(c), 2))
of '\\': add(result, "\\\\")
Expand Down
3 changes: 2 additions & 1 deletion lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3774,7 +3774,8 @@ proc locals*(): RootObj {.magic: "Plugin", noSideEffect.} =

when hasAlloc and not defined(nimscript) and not defined(JS):
proc deepCopy*[T](x: var T, y: T) {.noSideEffect, magic: "DeepCopy".} =
## performs a deep copy of `x`. This is also used by the code generator
## performs a deep copy of `y` and copies it into `x`.
## This is also used by the code generator
## for the implementation of ``spawn``.
discard

Expand Down
4 changes: 2 additions & 2 deletions lib/system/repr.nim
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ proc reprStrAux(result: var string, s: cstring; len: int) =
of '"': add result, "\\\""
of '\\': add result, "\\\\" # BUGFIX: forgotten
of '\10': add result, "\\10\"\n\"" # " \n " # better readability
of '\128' .. '\255', '\0'..'\9', '\11'..'\31':
of '\127' .. '\255', '\0'..'\9', '\11'..'\31':
add result, "\\" & reprInt(ord(c))
else:
result.add(c)
Expand All @@ -68,7 +68,7 @@ proc reprChar(x: char): string {.compilerRtl.} =
case x
of '"': add result, "\\\""
of '\\': add result, "\\\\"
of '\128' .. '\255', '\0'..'\31': add result, "\\" & reprInt(ord(x))
of '\127' .. '\255', '\0'..'\31': add result, "\\" & reprInt(ord(x))
else: add result, x
add result, "\'"

Expand Down
4 changes: 2 additions & 2 deletions lib/system/reprjs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ proc reprChar(x: char): string {.compilerRtl.} =
case x
of '"': add(result, "\\\"")
of '\\': add(result, "\\\\")
of '\128'..'\255', '\0'..'\31': add( result, "\\" & reprInt(ord(x)) )
of '\127'..'\255', '\0'..'\31': add( result, "\\" & reprInt(ord(x)) )
else: add(result, x)
add(result, "\'")

Expand All @@ -56,7 +56,7 @@ proc reprStrAux(result: var string, s: cstring, len: int) =
of '"': add(result, "\\\"")
of '\\': add(result, "\\\\")
of '\10': add(result, "\\10\"\n\"")
of '\128'..'\255', '\0'..'\9', '\11'..'\31':
of '\127'..'\255', '\0'..'\9', '\11'..'\31':
add( result, "\\" & reprInt(ord(c)) )
else:
add( result, reprInt(ord(c)) ) # Not sure about this.
Expand Down
57 changes: 57 additions & 0 deletions tests/misc/tparseopt.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
discard """
file: "tparseopt.nim"
output: '''
parseopt
first round
kind: cmdLongOption key:val -- left:
second round
kind: cmdLongOption key:val -- left:
kind: cmdLongOption key:val -- debug:3
kind: cmdShortOption key:val -- l:4
kind: cmdShortOption key:val -- r:2
parseopt2
first round
kind: cmdLongOption key:val -- left:
second round
kind: cmdLongOption key:val -- left:
kind: cmdLongOption key:val -- debug:3
kind: cmdShortOption key:val -- l:4
kind: cmdShortOption key:val -- r:2'''
"""
from parseopt import nil
from parseopt2 import nil


block:
echo "parseopt"
for kind, key, val in parseopt.getopt():
echo "kind: ", kind, "\tkey:val -- ", key, ":", val

# pass custom cmdline arguments
echo "first round"
var argv = "--left --debug:3 -l=4 -r:2"
var p = parseopt.initOptParser(argv)
for kind, key, val in parseopt.getopt(p):
echo "kind: ", kind, "\tkey:val -- ", key, ":", val
break
# reset getopt iterator and check arguments are returned correctly.
echo "second round"
for kind, key, val in parseopt.getopt(p):
echo "kind: ", kind, "\tkey:val -- ", key, ":", val

block:
echo "parseopt2"
for kind, key, val in parseopt2.getopt():
echo "kind: ", kind, "\tkey:val -- ", key, ":", val

# pass custom cmdline arguments
echo "first round"
var argv: seq[string] = @["--left", "--debug:3", "-l=4", "-r:2"]
var p = parseopt2.initOptParser(argv)
for kind, key, val in parseopt2.getopt(p):
echo "kind: ", kind, "\tkey:val -- ", key, ":", val
break
# reset getopt iterator and check arguments are returned correctly.
echo "second round"
for kind, key, val in parseopt2.getopt(p):
echo "kind: ", kind, "\tkey:val -- ", key, ":", val

0 comments on commit d947753

Please sign in to comment.