Merge branch 'devel' into coroutines
This commit is contained in:
commit
24ad2cb392
89 changed files with 2829 additions and 1405 deletions
|
|
@ -719,7 +719,13 @@ proc `$`*(node: NimNode): string {.compileTime.} =
|
|||
proc ident*(name: string): NimNode {.compileTime,inline.} = newIdentNode(name)
|
||||
## Create a new ident node from a string
|
||||
|
||||
iterator children*(n: NimNode): NimNode {.inline.}=
|
||||
iterator items*(n: NimNode): NimNode {.inline.} =
|
||||
## Iterates over the children of the NimNode ``n``.
|
||||
for i in 0 ..< n.len:
|
||||
yield n[i]
|
||||
|
||||
iterator children*(n: NimNode): NimNode {.inline.} =
|
||||
## Iterates over the children of the NimNode ``n``.
|
||||
for i in 0 ..< n.len:
|
||||
yield n[i]
|
||||
|
||||
|
|
|
|||
|
|
@ -360,7 +360,7 @@ struct TFrame {
|
|||
FR.procname = proc; FR.filename = file; FR.line = 0; FR.len = 0; nimFrame(&FR);
|
||||
|
||||
#define nimfrs(proc, file, slots, length) \
|
||||
struct {TFrame* prev;NCSTRING procname;NI line;NCSTRING filename; NI len; TVarSlot s[slots];} FR; \
|
||||
struct {TFrame* prev;NCSTRING procname;NI line;NCSTRING filename; NI len; VarSlot s[slots];} FR; \
|
||||
FR.procname = proc; FR.filename = file; FR.line = 0; FR.len = length; nimFrame((TFrame*)&FR);
|
||||
|
||||
#define nimln(n, file) \
|
||||
|
|
|
|||
|
|
@ -755,7 +755,7 @@ proc renderTocEntries*(d: var RstGenerator, j: var int, lvl: int,
|
|||
|
||||
proc renderImage(d: PDoc, n: PRstNode, result: var string) =
|
||||
template valid(s): expr =
|
||||
s.len > 0 and allCharsInSet(s, {'/',':','%','_','\\','\128'..'\xFF'} +
|
||||
s.len > 0 and allCharsInSet(s, {'.','/',':','%','_','\\','\128'..'\xFF'} +
|
||||
Digits + Letters + WhiteSpace)
|
||||
|
||||
var options = ""
|
||||
|
|
|
|||
|
|
@ -24,7 +24,6 @@ type
|
|||
c_oflag*: Cflag # output mode flags
|
||||
c_cflag*: Cflag # control mode flags
|
||||
c_lflag*: Cflag # local mode flags
|
||||
c_line*: cuchar # line discipline
|
||||
c_cc*: array[NCCS, cuchar] # control characters
|
||||
|
||||
# cc characters
|
||||
|
|
|
|||
|
|
@ -124,7 +124,8 @@ export Port, SocketFlag
|
|||
##
|
||||
## * The effect system (``raises: []``) does not work with async procedures.
|
||||
## * Can't await in a ``except`` body
|
||||
|
||||
## * Forward declarations for async procs are broken,
|
||||
## link includes workaround: https://github.com/nim-lang/Nim/issues/3182.
|
||||
|
||||
# TODO: Check if yielded future is nil and throw a more meaningful exception
|
||||
|
||||
|
|
@ -1412,12 +1413,12 @@ proc getName(node: NimNode): string {.compileTime.} =
|
|||
else:
|
||||
error("Unknown name.")
|
||||
|
||||
macro async*(prc: stmt): stmt {.immediate.} =
|
||||
## Macro which processes async procedures into the appropriate
|
||||
## iterators and yield statements.
|
||||
proc asyncSingleProc(prc: NimNode): NimNode {.compileTime.} =
|
||||
## This macro transforms a single procedure into a closure iterator.
|
||||
## The ``async`` macro supports a stmtList holding multiple async procedures.
|
||||
if prc.kind notin {nnkProcDef, nnkLambda}:
|
||||
error("Cannot transform this node kind into an async proc." &
|
||||
" Proc definition or lambda node expected.")
|
||||
error("Cannot transform this node kind into an async proc." &
|
||||
" Proc definition or lambda node expected.")
|
||||
|
||||
hint("Processing " & prc[0].getName & " as an async proc.")
|
||||
|
||||
|
|
@ -1504,9 +1505,19 @@ macro async*(prc: stmt): stmt {.immediate.} =
|
|||
result[6] = outerProcBody
|
||||
|
||||
#echo(treeRepr(result))
|
||||
if prc[0].getName == "getAsync":
|
||||
if prc[0].getName == "hubConnectionLoop":
|
||||
echo(toStrLit(result))
|
||||
|
||||
macro async*(prc: stmt): stmt {.immediate.} =
|
||||
## Macro which processes async procedures into the appropriate
|
||||
## iterators and yield statements.
|
||||
if prc.kind == nnkStmtList:
|
||||
for oneProc in prc:
|
||||
result = newStmtList()
|
||||
result.add asyncSingleProc(oneProc)
|
||||
else:
|
||||
result = asyncSingleProc(prc)
|
||||
|
||||
proc recvLine*(socket: AsyncFD): Future[string] {.async.} =
|
||||
## Reads a line of data from ``socket``. Returned future will complete once
|
||||
## a full line is read or an error occurs.
|
||||
|
|
|
|||
|
|
@ -126,8 +126,11 @@ proc parseHeader(line: string): tuple[key, value: string] =
|
|||
var i = 0
|
||||
i = line.parseUntil(result.key, ':')
|
||||
inc(i) # skip :
|
||||
i += line.skipWhiteSpace(i)
|
||||
i += line.parseUntil(result.value, {'\c', '\L'}, i)
|
||||
if i < len(line):
|
||||
i += line.skipWhiteSpace(i)
|
||||
i += line.parseUntil(result.value, {'\c', '\L'}, i)
|
||||
else:
|
||||
result.value = ""
|
||||
|
||||
proc parseProtocol(protocol: string): tuple[orig: string, major, minor: int] =
|
||||
var i = protocol.skipIgnoreCase("HTTP/")
|
||||
|
|
|
|||
|
|
@ -226,6 +226,10 @@ proc `$`*[A, B](t: Table[A, B]): string =
|
|||
## The `$` operator for hash tables.
|
||||
dollarImpl()
|
||||
|
||||
proc hasKey*[A, B](t: TableRef[A, B], key: A): bool =
|
||||
## returns true iff `key` is in the table `t`.
|
||||
result = t[].hasKey(key)
|
||||
|
||||
template equalsImpl() =
|
||||
if s.counter == t.counter:
|
||||
# different insertion orders mean different 'data' seqs, so we have
|
||||
|
|
@ -293,10 +297,6 @@ proc hasKeyOrPut*[A, B](t: var TableRef[A, B], key: A, val: B): bool =
|
|||
## returns true iff `key` is in the table, otherwise inserts `value`.
|
||||
t[].hasKeyOrPut(key, val)
|
||||
|
||||
proc hasKey*[A, B](t: TableRef[A, B], key: A): bool =
|
||||
## returns true iff `key` is in the table `t`.
|
||||
result = t[].hasKey(key)
|
||||
|
||||
proc contains*[A, B](t: TableRef[A, B], key: A): bool =
|
||||
## alias of `hasKey` for use with the `in` operator.
|
||||
return hasKey[A, B](t, key)
|
||||
|
|
|
|||
|
|
@ -402,7 +402,7 @@ proc request*(url: string, httpMethod: string, extraHeaders = "",
|
|||
|
||||
headers.add(" HTTP/1.1\c\L")
|
||||
|
||||
add(headers, "Host: " & r.hostname & "\c\L")
|
||||
add(headers, "Host: " & parseUri(url).hostname & "\c\L")
|
||||
if userAgent != "":
|
||||
add(headers, "User-Agent: " & userAgent & "\c\L")
|
||||
if proxy != nil and proxy.auth != "":
|
||||
|
|
|
|||
|
|
@ -608,29 +608,29 @@ proc newJArray*(): JsonNode =
|
|||
proc getStr*(n: JsonNode, default: string = ""): string =
|
||||
## Retrieves the string value of a `JString JsonNode`.
|
||||
##
|
||||
## Returns ``default`` if ``n`` is not a ``JString``.
|
||||
if n.kind != JString: return default
|
||||
## Returns ``default`` if ``n`` is not a ``JString``, or if ``n`` is nil.
|
||||
if n.isNil or n.kind != JString: return default
|
||||
else: return n.str
|
||||
|
||||
proc getNum*(n: JsonNode, default: BiggestInt = 0): BiggestInt =
|
||||
## Retrieves the int value of a `JInt JsonNode`.
|
||||
##
|
||||
## Returns ``default`` if ``n`` is not a ``JInt``.
|
||||
if n.kind != JInt: return default
|
||||
## Returns ``default`` if ``n`` is not a ``JInt``, or if ``n`` is nil.
|
||||
if n.isNil or n.kind != JInt: return default
|
||||
else: return n.num
|
||||
|
||||
proc getFNum*(n: JsonNode, default: float = 0.0): float =
|
||||
## Retrieves the float value of a `JFloat JsonNode`.
|
||||
##
|
||||
## Returns ``default`` if ``n`` is not a ``JFloat``.
|
||||
if n.kind != JFloat: return default
|
||||
## Returns ``default`` if ``n`` is not a ``JFloat``, or if ``n`` is nil.
|
||||
if n.isNil or n.kind != JFloat: return default
|
||||
else: return n.fnum
|
||||
|
||||
proc getBVal*(n: JsonNode, default: bool = false): bool =
|
||||
## Retrieves the bool value of a `JBool JsonNode`.
|
||||
##
|
||||
## Returns ``default`` if ``n`` is not a ``JBool``.
|
||||
if n.kind != JBool: return default
|
||||
## Returns ``default`` if ``n`` is not a ``JBool``, or if ``n`` is nil.
|
||||
if n.isNil or n.kind != JBool: return default
|
||||
else: return n.bval
|
||||
|
||||
proc getFields*(n: JsonNode,
|
||||
|
|
@ -638,15 +638,15 @@ proc getFields*(n: JsonNode,
|
|||
seq[tuple[key: string, val: JsonNode]] =
|
||||
## Retrieves the key, value pairs of a `JObject JsonNode`.
|
||||
##
|
||||
## Returns ``default`` if ``n`` is not a ``JObject``.
|
||||
if n.kind != JObject: return default
|
||||
## Returns ``default`` if ``n`` is not a ``JObject``, or if ``n`` is nil.
|
||||
if n.isNil or n.kind != JObject: return default
|
||||
else: return n.fields
|
||||
|
||||
proc getElems*(n: JsonNode, default: seq[JsonNode] = @[]): seq[JsonNode] =
|
||||
## Retrieves the int value of a `JArray JsonNode`.
|
||||
##
|
||||
## Returns ``default`` if ``n`` is not a ``JArray``.
|
||||
if n.kind != JArray: return default
|
||||
## Returns ``default`` if ``n`` is not a ``JArray``, or if ``n`` is nil.
|
||||
if n.isNil or n.kind != JArray: return default
|
||||
else: return n.elems
|
||||
|
||||
proc `%`*(s: string): JsonNode =
|
||||
|
|
|
|||
|
|
@ -427,10 +427,12 @@ proc `^`*[T](x, y: T): T =
|
|||
var (x, y) = (x, y)
|
||||
result = 1
|
||||
|
||||
while y != 0:
|
||||
while true:
|
||||
if (y and 1) != 0:
|
||||
result *= x
|
||||
y = y shr 1
|
||||
if y == 0:
|
||||
break
|
||||
x *= x
|
||||
|
||||
proc gcd*[T](x, y: T): T =
|
||||
|
|
|
|||
|
|
@ -11,6 +11,9 @@
|
|||
##
|
||||
## This module provides support for `memory mapped files`:idx:
|
||||
## (Posix's `mmap`:idx:) on the different operating systems.
|
||||
##
|
||||
## It also provides some fast iterators over lines in text files (or
|
||||
## other "line-like", variable length, delimited records).
|
||||
|
||||
when defined(windows):
|
||||
import winlean
|
||||
|
|
@ -245,3 +248,96 @@ proc close*(f: var MemFile) =
|
|||
|
||||
if error: raiseOSError(lastErr)
|
||||
|
||||
type MemSlice* = object ## represent slice of a MemFile for iteration over delimited lines/records
|
||||
data*: pointer
|
||||
size*: int
|
||||
|
||||
proc c_memcpy(a, b: pointer, n: int) {.importc: "memcpy", header: "<string.h>".}
|
||||
|
||||
proc `$`*(ms: MemSlice): string {.inline.} =
|
||||
## Return a Nim string built from a MemSlice.
|
||||
var buf = newString(ms.size)
|
||||
c_memcpy(addr(buf[0]), ms.data, ms.size)
|
||||
buf[ms.size] = '\0'
|
||||
result = buf
|
||||
|
||||
iterator memSlices*(mfile: MemFile, delim='\l', eat='\r'): MemSlice {.inline.} =
|
||||
## Iterates over [optional `eat`] `delim`-delimited slices in MemFile `mfile`.
|
||||
##
|
||||
## Default parameters parse lines ending in either Unix(\\l) or Windows(\\r\\l)
|
||||
## style on on a line-by-line basis. I.e., not every line needs the same ending.
|
||||
## Unlike readLine(File) & lines(File), archaic MacOS9 \\r-delimited lines
|
||||
## are not supported as a third option for each line. Such archaic MacOS9
|
||||
## files can be handled by passing delim='\\r', eat='\\0', though.
|
||||
##
|
||||
## Delimiters are not part of the returned slice. A final, unterminated line
|
||||
## or record is returned just like any other.
|
||||
##
|
||||
## Non-default delimiters can be passed to allow iteration over other sorts
|
||||
## of "line-like" variable length records. Pass eat='\\0' to be strictly
|
||||
## `delim`-delimited. (Eating an optional prefix equal to '\\0' is not
|
||||
## supported.)
|
||||
##
|
||||
## This zero copy, memchr-limited interface is probably the fastest way to
|
||||
## iterate over line-like records in a file. However, returned (data,size)
|
||||
## objects are not Nim strings, bounds checked Nim arrays, or even terminated
|
||||
## C strings. So, care is required to access the data (e.g., think C mem*
|
||||
## functions, not str* functions). Example:
|
||||
##
|
||||
## .. code-block:: nim
|
||||
## var count = 0
|
||||
## for slice in memSlices(memfiles.open("foo")):
|
||||
## if slice.size > 0 and cast[cstring](slice.data)[0] != '#':
|
||||
## inc(count)
|
||||
## echo count
|
||||
|
||||
proc c_memchr(cstr: pointer, c: char, n: csize): pointer {.
|
||||
importc: "memchr", header: "<string.h>" .}
|
||||
proc `-!`(p, q: pointer): int {.inline.} = return cast[int](p) -% cast[int](q)
|
||||
var ms: MemSlice
|
||||
var ending: pointer
|
||||
ms.data = mfile.mem
|
||||
var remaining = mfile.size
|
||||
while remaining > 0:
|
||||
ending = c_memchr(ms.data, delim, remaining)
|
||||
if ending == nil: # unterminated final slice
|
||||
ms.size = remaining # Weird case..check eat?
|
||||
yield ms
|
||||
break
|
||||
ms.size = ending -! ms.data # delim is NOT included
|
||||
if eat != '\0' and ms.size > 0 and cast[cstring](ms.data)[ms.size - 1] == eat:
|
||||
dec(ms.size) # trim pre-delim char
|
||||
yield ms
|
||||
ms.data = cast[pointer](cast[int](ending) +% 1) # skip delim
|
||||
remaining = mfile.size - (ms.data -! mfile.mem)
|
||||
|
||||
iterator lines*(mfile: MemFile, buf: var TaintedString, delim='\l', eat='\r'): TaintedString {.inline.} =
|
||||
## Replace contents of passed buffer with each new line, like
|
||||
## `readLine(File) <system.html#readLine,File,TaintedString>`_.
|
||||
## `delim`, `eat`, and delimiting logic is exactly as for
|
||||
## `memSlices <#memSlices>`_, but Nim strings are returned. Example:
|
||||
##
|
||||
## .. code-block:: nim
|
||||
## var buffer: TaintedString = ""
|
||||
## for line in lines(memfiles.open("foo"), buffer):
|
||||
## echo line
|
||||
|
||||
for ms in memSlices(mfile, delim, eat):
|
||||
buf.setLen(ms.size)
|
||||
c_memcpy(addr(buf[0]), ms.data, ms.size)
|
||||
buf[ms.size] = '\0'
|
||||
yield buf
|
||||
|
||||
iterator lines*(mfile: MemFile, delim='\l', eat='\r'): TaintedString {.inline.} =
|
||||
## Return each line in a file as a Nim string, like
|
||||
## `lines(File) <system.html#lines.i,File>`_.
|
||||
## `delim`, `eat`, and delimiting logic is exactly as for
|
||||
## `memSlices <#memSlices>`_, but Nim strings are returned. Example:
|
||||
##
|
||||
## .. code-block:: nim
|
||||
## for line in lines(memfiles.open("foo")):
|
||||
## echo line
|
||||
|
||||
var buf = TaintedString(newStringOfCap(80))
|
||||
for line in lines(mfile, buf, delim, eat):
|
||||
yield buf
|
||||
|
|
|
|||
|
|
@ -1244,7 +1244,14 @@ iterator walkFiles*(pattern: string): string {.tags: [ReadDirEffect].} =
|
|||
while true:
|
||||
if not skipFindData(f) and
|
||||
(f.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) == 0'i32:
|
||||
yield splitFile(pattern).dir / extractFilename(getFilename(f))
|
||||
# Windows bug/gotcha: 't*.nim' matches 'tfoo.nims' -.- so we check
|
||||
# that the file extensions have the same length ...
|
||||
let ff = getFilename(f)
|
||||
let dotPos = searchExtPos(pattern)
|
||||
let idx = ff.len - pattern.len + dotPos
|
||||
if dotPos < 0 or idx >= ff.len or ff[idx] == '.' or
|
||||
pattern[dotPos+1] == '*':
|
||||
yield splitFile(pattern).dir / extractFilename(ff)
|
||||
if findNextFile(res, f) == 0'i32: break
|
||||
findClose(res)
|
||||
else: # here we use glob
|
||||
|
|
|
|||
|
|
@ -168,9 +168,8 @@ proc cmpIgnoreStyle*(a, b: string): int {.noSideEffect,
|
|||
inc(i)
|
||||
inc(j)
|
||||
|
||||
{.pop.}
|
||||
|
||||
proc strip*(s: string, leading = true, trailing = true, chars: set[char] = Whitespace): string
|
||||
proc strip*(s: string, leading = true, trailing = true, chars: set[char] = Whitespace): string
|
||||
{.noSideEffect, rtl, extern: "nsuStrip".} =
|
||||
## Strips `chars` from `s` and returns the resulting string.
|
||||
##
|
||||
|
|
@ -1396,6 +1395,62 @@ proc format*(formatstr: string, a: varargs[string, `$`]): string {.noSideEffect,
|
|||
|
||||
{.pop.}
|
||||
|
||||
proc removeSuffix*(s: var string, chars: set[char] = Newlines) {.
|
||||
rtl, extern: "nsuRemoveSuffixCharSet".} =
|
||||
## Removes the first matching character from the string (in-place) given a
|
||||
## set of characters. If the set of characters is only equal to `Newlines`
|
||||
## then it will remove both the newline and return feed.
|
||||
## .. code-block:: nim
|
||||
## var
|
||||
## userInput = "Hello World!\r\n"
|
||||
## otherInput = "Hello!?!"
|
||||
## userInput.removeSuffix
|
||||
## userInput == "Hello World!"
|
||||
## userInput.removeSuffix({'!', '?'})
|
||||
## userInput == "Hello World"
|
||||
## otherInput.removeSuffix({'!', '?'})
|
||||
## otherInput == "Hello!?"
|
||||
|
||||
var last = len(s) - 1
|
||||
|
||||
if chars == Newlines:
|
||||
if s[last] == '\10':
|
||||
last -= 1
|
||||
|
||||
if s[last] == '\13':
|
||||
last -= 1
|
||||
|
||||
else:
|
||||
if s[last] in chars:
|
||||
last -= 1
|
||||
|
||||
s.setLen(last + 1)
|
||||
|
||||
proc removeSuffix*(s: var string, c: char) {.rtl, extern: "nsuRemoveSuffixChar".} =
|
||||
## Removes a single character (in-place) from a string.
|
||||
## .. code-block:: nim
|
||||
## var
|
||||
## table = "users"
|
||||
## table.removeSuffix('s')
|
||||
## table == "user"
|
||||
removeSuffix(s, chars = {c})
|
||||
|
||||
proc removeSuffix*(s: var string, suffix: string) {.
|
||||
rtl, extern: "nsuRemoveSuffixString".} =
|
||||
## Remove the first matching suffix (in-place) from a string.
|
||||
## .. code-block:: nim
|
||||
## var
|
||||
## answers = "yeses"
|
||||
## answers.removeSuffix("es")
|
||||
## answers == "yes"
|
||||
|
||||
var newLen = s.len
|
||||
|
||||
if s.endsWith(suffix):
|
||||
newLen -= len(suffix)
|
||||
|
||||
s.setLen(newLen)
|
||||
|
||||
when isMainModule:
|
||||
doAssert align("abc", 4) == " abc"
|
||||
doAssert align("a", 0) == "a"
|
||||
|
|
|
|||
|
|
@ -26,11 +26,6 @@ type
|
|||
WeekDay* = enum ## represents a weekday
|
||||
dMon, dTue, dWed, dThu, dFri, dSat, dSun
|
||||
|
||||
when not defined(JS):
|
||||
var
|
||||
timezone {.importc, header: "<time.h>".}: int
|
||||
tzname {.importc, header: "<time.h>" .}: array[0..1, cstring]
|
||||
|
||||
when defined(posix) and not defined(JS):
|
||||
type
|
||||
TimeImpl {.importc: "time_t", header: "<time.h>".} = int
|
||||
|
|
@ -49,6 +44,9 @@ when defined(posix) and not defined(JS):
|
|||
proc posix_gettimeofday(tp: var Timeval, unused: pointer = nil) {.
|
||||
importc: "gettimeofday", header: "<sys/time.h>".}
|
||||
|
||||
var
|
||||
timezone {.importc, header: "<time.h>".}: int
|
||||
tzname {.importc, header: "<time.h>" .}: array[0..1, cstring]
|
||||
# we also need tzset() to make sure that tzname is initialized
|
||||
proc tzset() {.importc, header: "<time.h>".}
|
||||
# calling tzset() implicitly to initialize tzname data.
|
||||
|
|
@ -60,12 +58,20 @@ elif defined(windows):
|
|||
when defined(vcc):
|
||||
# newest version of Visual C++ defines time_t to be of 64 bits
|
||||
type TimeImpl {.importc: "time_t", header: "<time.h>".} = int64
|
||||
# visual c's c runtime exposes these under a different name
|
||||
var
|
||||
timezone {.importc: "_timezone", header: "<time.h>".}: int
|
||||
tzname {.importc: "_tzname", header: "<time.h>"}: array[0..1, cstring]
|
||||
else:
|
||||
type TimeImpl {.importc: "time_t", header: "<time.h>".} = int32
|
||||
var
|
||||
timezone {.importc, header: "<time.h>".}: int
|
||||
tzname {.importc, header: "<time.h>" .}: array[0..1, cstring]
|
||||
|
||||
type
|
||||
Time* = distinct TimeImpl
|
||||
|
||||
|
||||
elif defined(JS):
|
||||
type
|
||||
Time* {.importc.} = object
|
||||
|
|
@ -542,11 +548,9 @@ elif defined(JS):
|
|||
## get the milliseconds from the start of the program
|
||||
return int(getTime() - startMilsecs)
|
||||
|
||||
proc valueOf(time: Time): float {.importcpp: "getTime", tags:[]}
|
||||
proc fromSeconds(since1970: float): Time = result = newDate(since1970 * 1000)
|
||||
|
||||
proc fromSeconds(since1970: float): Time = result = newDate(since1970)
|
||||
|
||||
proc toSeconds(time: Time): float = result = time.valueOf() / 1000
|
||||
proc toSeconds(time: Time): float = result = time.getTime() / 1000
|
||||
|
||||
proc getTimezone(): int = result = newDate().getTimezoneOffset()
|
||||
|
||||
|
|
@ -1046,6 +1050,120 @@ proc parse*(value, layout: string): TimeInfo =
|
|||
info.weekday = getLocalTime(timeInfoToTime(info)).weekday
|
||||
return info
|
||||
|
||||
# Leap year calculations are adapted from:
|
||||
# from http://www.codeproject.com/Articles/7358/Ultra-fast-Algorithms-for-Working-with-Leap-Years
|
||||
# The dayOfTheWeek procs are adapated from:
|
||||
# from http://stason.org/TULARC/society/calendars/2-5-What-day-of-the-week-was-2-August-1953.html
|
||||
|
||||
# Note: for leap years, start date is assumed to be 1 AD.
|
||||
# counts the number of leap years up to January 1st of a given year.
|
||||
# Keep in mind that if specified year is a leap year, the leap day
|
||||
# has not happened before January 1st of that year.
|
||||
proc countLeapYears(yearSpan: int): int =
|
||||
(((yearSpan - 1) / 4) - ((yearSpan - 1) / 100) + ((yearSpan - 1)/400)).int
|
||||
|
||||
proc countDays(yearSpan: int): int =
|
||||
(yearSpan - 1) * 365 + countLeapYears(yearSpan)
|
||||
|
||||
proc countYears(daySpan: int): int =
|
||||
# counts the number of years spanned by a given number of days.
|
||||
((daySpan - countLeapYears(daySpan div 365)) div 365)
|
||||
|
||||
proc countYearsAndDays(daySpan: int): tuple[years: int, days: int] =
|
||||
# counts the number of years spanned by a given number of days and the remainder as days.
|
||||
let days = daySpan - countLeapYears(daySpan div 365)
|
||||
result.years = days div 365
|
||||
result.days = days mod 365
|
||||
|
||||
const
|
||||
secondsInMin = 60
|
||||
secondsInHour = 60*60
|
||||
secondsInDay = 60*60*24
|
||||
epochStartYear = 1970
|
||||
|
||||
proc getDayOfWeek*(day, month, year: int): WeekDay =
|
||||
## Returns the day of the week enum from day, month and year.
|
||||
# Day & month start from one.
|
||||
let
|
||||
a = (14 - month) div 12
|
||||
y = year - a
|
||||
m = month + (12*a) - 2
|
||||
d = (day + y + (y div 4) - (y div 100) + (y div 400) + (31*m) div 12) mod 7
|
||||
# The value of d is 0 for a Sunday, 1 for a Monday, 2 for a Tuesday, etc. so we must correct
|
||||
# for the WeekDay type.
|
||||
if d == 0: return dSun
|
||||
result = (d-1).WeekDay
|
||||
|
||||
proc getDayOfWeekJulian*(day, month, year: int): WeekDay =
|
||||
## Returns the day of the week enum from day, month and year, according to the Julian calender.
|
||||
# Day & month start from one.
|
||||
let
|
||||
a = (14 - month) div 12
|
||||
y = year - a
|
||||
m = month + (12*a) - 2
|
||||
d = (5 + day + y + (y div 4) + (31*m) div 12) mod 7
|
||||
result = d.WeekDay
|
||||
|
||||
proc timeToTimeInfo*(t: Time): TimeInfo =
|
||||
## Converts a Time to TimeInfo.
|
||||
let
|
||||
secs = t.toSeconds().int
|
||||
daysSinceEpoch = secs div secondsInDay
|
||||
(yearsSinceEpoch, daysRemaining) = countYearsAndDays(daysSinceEpoch)
|
||||
daySeconds = secs mod secondsInDay
|
||||
|
||||
y = yearsSinceEpoch + epochStartYear
|
||||
|
||||
var
|
||||
mon = mJan
|
||||
days = daysRemaining
|
||||
daysInMonth = getDaysInMonth(mon, y)
|
||||
|
||||
# calculate month and day remainder
|
||||
while days > daysInMonth and mon <= mDec:
|
||||
days -= daysInMonth
|
||||
mon.inc
|
||||
daysInMonth = getDaysInMonth(mon, y)
|
||||
|
||||
let
|
||||
yd = daysRemaining
|
||||
m = mon # month is zero indexed enum
|
||||
md = days
|
||||
# NB: month is zero indexed but dayOfWeek expects 1 indexed.
|
||||
wd = getDayOfWeek(days, mon.int + 1, y).Weekday
|
||||
h = daySeconds div secondsInHour + 1
|
||||
mi = (daySeconds mod secondsInHour) div secondsInMin
|
||||
s = daySeconds mod secondsInMin
|
||||
result = TimeInfo(year: y, yearday: yd, month: m, monthday: md, weekday: wd, hour: h, minute: mi, second: s)
|
||||
|
||||
proc timeToTimeInterval*(t: Time): TimeInterval =
|
||||
## Converts a Time to a TimeInterval.
|
||||
|
||||
let
|
||||
secs = t.toSeconds().int
|
||||
daysSinceEpoch = secs div secondsInDay
|
||||
(yearsSinceEpoch, daysRemaining) = countYearsAndDays(daysSinceEpoch)
|
||||
daySeconds = secs mod secondsInDay
|
||||
|
||||
result.years = yearsSinceEpoch + epochStartYear
|
||||
|
||||
var
|
||||
mon = mJan
|
||||
days = daysRemaining
|
||||
daysInMonth = getDaysInMonth(mon, result.years)
|
||||
|
||||
# calculate month and day remainder
|
||||
while days > daysInMonth and mon <= mDec:
|
||||
days -= daysInMonth
|
||||
mon.inc
|
||||
daysInMonth = getDaysInMonth(mon, result.years)
|
||||
|
||||
result.months = mon.int + 1 # month is 1 indexed int
|
||||
result.days = days
|
||||
result.hours = daySeconds div secondsInHour + 1
|
||||
result.minutes = (daySeconds mod secondsInHour) div secondsInMin
|
||||
result.seconds = daySeconds mod secondsInMin
|
||||
# Milliseconds not available from Time
|
||||
|
||||
when isMainModule:
|
||||
# $ date --date='@2147483647'
|
||||
|
|
@ -1066,12 +1184,13 @@ when isMainModule:
|
|||
" ss t tt y yy yyy yyyy yyyyy z zz zzz ZZZ") ==
|
||||
"27 27 Mon Monday 4 04 16 16 6 06 1 01 Jan January 29 29 P PM 5 75 975 1975 01975 0 00 00:00 UTC"
|
||||
|
||||
when not defined(JS) and sizeof(Time) == 8:
|
||||
var t3 = getGMTime(fromSeconds(889067643645)) # Fri 7 Jun 19:20:45 BST 30143
|
||||
assert t3.format("d dd ddd dddd h hh H HH m mm M MM MMM MMMM s" &
|
||||
" ss t tt y yy yyy yyyy yyyyy z zz zzz ZZZ") ==
|
||||
"7 07 Fri Friday 6 06 18 18 20 20 6 06 Jun June 45 45 P PM 3 43 143 0143 30143 0 00 00:00 UTC"
|
||||
assert t3.format(":,[]()-/") == ":,[]()-/"
|
||||
when not defined(JS):
|
||||
when sizeof(Time) == 8:
|
||||
var t3 = getGMTime(fromSeconds(889067643645)) # Fri 7 Jun 19:20:45 BST 30143
|
||||
assert t3.format("d dd ddd dddd h hh H HH m mm M MM MMM MMMM s" &
|
||||
" ss t tt y yy yyy yyyy yyyyy z zz zzz ZZZ") ==
|
||||
"7 07 Fri Friday 6 06 18 18 20 20 6 06 Jun June 45 45 P PM 3 43 143 0143 30143 0 00 00:00 UTC"
|
||||
assert t3.format(":,[]()-/") == ":,[]()-/"
|
||||
|
||||
var t4 = getGMTime(fromSeconds(876124714)) # Mon 6 Oct 08:58:34 BST 1997
|
||||
assert t4.format("M MM MMM MMMM") == "10 10 Oct October"
|
||||
|
|
@ -1131,3 +1250,18 @@ when isMainModule:
|
|||
assert "15:04:00" in $s.parse(f)
|
||||
when not defined(testing):
|
||||
echo "Kitchen: " & $s.parse(f)
|
||||
var ti = timeToTimeInfo(getTime())
|
||||
echo "Todays date after decoding: ", ti
|
||||
var tint = timeToTimeInterval(getTime())
|
||||
echo "Todays date after decoding to interval: ", tint
|
||||
# checking dayOfWeek matches known days
|
||||
assert getDayOfWeek(21, 9, 1900) == dFri
|
||||
assert getDayOfWeek(1, 1, 1970) == dThu
|
||||
assert getDayOfWeek(21, 9, 1970) == dMon
|
||||
assert getDayOfWeek(1, 1, 2000) == dSat
|
||||
assert getDayOfWeek(1, 1, 2021) == dFri
|
||||
# Julian tests
|
||||
assert getDayOfWeekJulian(21, 9, 1900) == dFri
|
||||
assert getDayOfWeekJulian(21, 9, 1970) == dMon
|
||||
assert getDayOfWeekJulian(1, 1, 2000) == dSat
|
||||
assert getDayOfWeekJulian(1, 1, 2021) == dFri
|
||||
|
|
|
|||
|
|
@ -285,7 +285,7 @@ macro check*(conditions: stmt): stmt {.immediate.} =
|
|||
|
||||
result = getAst(rewrite(checked, checked.lineinfo, checked.toStrLit))
|
||||
|
||||
template require*(conditions: stmt): stmt {.immediate, dirty.} =
|
||||
template require*(conditions: stmt): stmt {.immediate.} =
|
||||
## Same as `check` except any failed test causes the program to quit
|
||||
## immediately. Any teardown statements are not executed and the failed
|
||||
## test output is not generated.
|
||||
|
|
|
|||
135
lib/system.nim
135
lib/system.nim
|
|
@ -153,6 +153,13 @@ proc `addr`*[T](x: var T): ptr T {.magic: "Addr", noSideEffect.} =
|
|||
## Cannot be overloaded.
|
||||
discard
|
||||
|
||||
proc unsafeAddr*[T](x: var T): ptr T {.magic: "Addr", noSideEffect.} =
|
||||
## Builtin 'addr' operator for taking the address of a memory location.
|
||||
## This works even for ``let`` variables or parameters for better interop
|
||||
## with C and so it is considered even more unsafe than the ordinary ``addr``.
|
||||
## Cannot be overloaded.
|
||||
discard
|
||||
|
||||
proc `type`*(x: expr): typeDesc {.magic: "TypeOf", noSideEffect.} =
|
||||
## Builtin 'type' operator for accessing the type of an expression.
|
||||
## Cannot be overloaded.
|
||||
|
|
@ -176,10 +183,19 @@ proc new*[T](a: var ref T) {.magic: "New", noSideEffect.}
|
|||
## creates a new object of type ``T`` and returns a safe (traced)
|
||||
## reference to it in ``a``.
|
||||
|
||||
proc new*(T: typedesc): ref T =
|
||||
proc new*(T: typedesc): auto =
|
||||
## creates a new object of type ``T`` and returns a safe (traced)
|
||||
## reference to it as result value
|
||||
new(result)
|
||||
## reference to it as result value.
|
||||
##
|
||||
## When ``T`` is a ref type then the resulting type will be ``T``,
|
||||
## otherwise it will be ``ref T``.
|
||||
when (T is ref):
|
||||
var r: T
|
||||
else:
|
||||
var r: ref T
|
||||
new(r)
|
||||
return r
|
||||
|
||||
|
||||
proc internalNew*[T](a: var ref T) {.magic: "New", noSideEffect.}
|
||||
## leaked implementation detail. Do not use.
|
||||
|
|
@ -331,7 +347,7 @@ const
|
|||
|
||||
include "system/inclrtl"
|
||||
|
||||
const NoFakeVars* = defined(NimrodVM) ## true if the backend doesn't support \
|
||||
const NoFakeVars* = defined(nimscript) ## true if the backend doesn't support \
|
||||
## "fake variables" like 'var EBADF {.importc.}: cint'.
|
||||
|
||||
const ArrayDummySize = when defined(cpu16): 10_000 else: 100_000_000
|
||||
|
|
@ -349,7 +365,7 @@ when not defined(JS):
|
|||
data: UncheckedCharArray
|
||||
NimString = ptr NimStringDesc
|
||||
|
||||
when not defined(JS) and not defined(NimrodVM):
|
||||
when not defined(JS) and not defined(nimscript):
|
||||
template space(s: PGenericSeq): int {.dirty.} =
|
||||
s.reserved and not seqShallowFlag
|
||||
|
||||
|
|
@ -481,6 +497,7 @@ type
|
|||
## See the full `exception hierarchy`_.
|
||||
ObjectConversionError* = object of Exception ## \
|
||||
## Raised if an object is converted to an incompatible object type.
|
||||
## You can use ``of`` operator to check if conversion will succeed.
|
||||
##
|
||||
## See the full `exception hierarchy`_.
|
||||
FloatingPointError* = object of Exception ## \
|
||||
|
|
@ -560,6 +577,9 @@ proc sizeof*[T](x: T): int {.magic: "SizeOf", noSideEffect.}
|
|||
## that one never needs to know ``x``'s size. As a special semantic rule,
|
||||
## ``x`` may also be a type identifier (``sizeof(int)`` is valid).
|
||||
|
||||
when defined(nimtypedescfixed):
|
||||
proc sizeof*(x: typedesc): int {.magic: "SizeOf", noSideEffect.}
|
||||
|
||||
proc `<`*[T](x: Ordinal[T]): T {.magic: "UnaryLt", noSideEffect.}
|
||||
## unary ``<`` that can be used for nice looking excluding ranges:
|
||||
##
|
||||
|
|
@ -1150,7 +1170,8 @@ const
|
|||
|
||||
hostCPU* {.magic: "HostCPU".}: string = ""
|
||||
## a string that describes the host CPU. Possible values:
|
||||
## "i386", "alpha", "powerpc", "powerpc64", "sparc", "amd64", "mips", "arm".
|
||||
## "i386", "alpha", "powerpc", "powerpc64", "powerpc64el", "sparc",
|
||||
## "amd64", "mips", "mipsel", "arm", "arm64".
|
||||
|
||||
seqShallowFlag = low(int)
|
||||
|
||||
|
|
@ -1239,11 +1260,11 @@ template sysAssert(cond: bool, msg: string) =
|
|||
echo "[SYSASSERT] ", msg
|
||||
quit 1
|
||||
|
||||
const hasAlloc = hostOS != "standalone" or not defined(nogc)
|
||||
const hasAlloc = (hostOS != "standalone" or not defined(nogc)) and not defined(nimscript)
|
||||
|
||||
when not defined(JS) and not defined(nimrodVm) and hostOS != "standalone":
|
||||
when not defined(JS) and not defined(nimscript) and hostOS != "standalone":
|
||||
include "system/cgprocs"
|
||||
when not defined(JS) and not defined(nimrodVm) and hasAlloc:
|
||||
when not defined(JS) and not defined(nimscript) and hasAlloc:
|
||||
proc setStackBottom(theStackBottom: pointer) {.compilerRtl, noinline, benign.}
|
||||
proc addChar(s: NimString, c: char): NimString {.compilerProc, benign.}
|
||||
|
||||
|
|
@ -1424,7 +1445,7 @@ proc substr*(s: string, first, last: int): string {.
|
|||
## is used instead: This means ``substr`` can also be used to `cut`:idx:
|
||||
## or `limit`:idx: a string's length.
|
||||
|
||||
when not defined(nimrodVM):
|
||||
when not defined(nimscript):
|
||||
proc zeroMem*(p: pointer, size: Natural) {.importc, noDecl, benign.}
|
||||
## overwrites the contents of the memory at ``p`` with the value 0.
|
||||
## Exactly ``size`` bytes will be overwritten. Like any procedure
|
||||
|
|
@ -1482,7 +1503,7 @@ when not defined(nimrodVM):
|
|||
## containing zero, so it is somewhat safer than ``createU``.
|
||||
## The allocated memory belongs to its allocating thread!
|
||||
## Use `createShared` to allocate from a shared heap.
|
||||
cast[ptr T](alloc0(T.sizeof * size))
|
||||
cast[ptr T](alloc0(sizeof(T) * size))
|
||||
proc realloc*(p: pointer, newSize: Natural): pointer {.noconv, rtl, tags: [],
|
||||
benign.}
|
||||
## grows or shrinks a given memory block. If p is **nil** then a new
|
||||
|
|
@ -1586,7 +1607,7 @@ proc `$`*(x: int64): string {.magic: "Int64ToStr", noSideEffect.}
|
|||
## The stringify operator for an integer argument. Returns `x`
|
||||
## converted to a decimal string.
|
||||
|
||||
when not defined(NimrodVM):
|
||||
when not defined(nimscript):
|
||||
when not defined(JS) and hasAlloc:
|
||||
proc `$` *(x: uint64): string {.noSideEffect.}
|
||||
## The stringify operator for an unsigned integer argument. Returns `x`
|
||||
|
|
@ -1654,7 +1675,7 @@ const
|
|||
|
||||
# GC interface:
|
||||
|
||||
when not defined(nimrodVM) and hasAlloc:
|
||||
when not defined(nimscript) and hasAlloc:
|
||||
proc getOccupiedMem*(): int {.rtl.}
|
||||
## returns the number of bytes that are owned by the process and hold data.
|
||||
|
||||
|
|
@ -1995,7 +2016,7 @@ proc `&` *[T](x: T, y: seq[T]): seq[T] {.noSideEffect.} =
|
|||
for i in 0..y.len-1:
|
||||
result[i+1] = y[i]
|
||||
|
||||
when not defined(NimrodVM):
|
||||
when not defined(nimscript):
|
||||
when not defined(JS):
|
||||
proc seqToPtr[T](x: seq[T]): pointer {.inline, nosideeffect.} =
|
||||
result = cast[pointer](x)
|
||||
|
|
@ -2214,7 +2235,7 @@ when false:
|
|||
|
||||
# ----------------- GC interface ---------------------------------------------
|
||||
|
||||
when not defined(nimrodVM) and hasAlloc:
|
||||
when not defined(nimscript) and hasAlloc:
|
||||
proc GC_disable*() {.rtl, inl, benign.}
|
||||
## disables the GC. If called n-times, n calls to `GC_enable` are needed to
|
||||
## reactivate the GC. Note that in most circumstances one should only disable
|
||||
|
|
@ -2436,10 +2457,10 @@ else:
|
|||
if x < 0: -x else: x
|
||||
{.pop.}
|
||||
|
||||
when not defined(JS): #and not defined(NimrodVM):
|
||||
when not defined(JS): #and not defined(nimscript):
|
||||
{.push stack_trace: off, profiler:off.}
|
||||
|
||||
when not defined(NimrodVM) and not defined(nogc):
|
||||
when not defined(nimscript) and not defined(nogc):
|
||||
proc initGC()
|
||||
when not defined(boehmgc) and not defined(useMalloc) and not defined(gogc):
|
||||
proc initAllocator() {.inline.}
|
||||
|
|
@ -2467,13 +2488,19 @@ when not defined(JS): #and not defined(NimrodVM):
|
|||
strDesc.kind = tyString
|
||||
strDesc.flags = {ntfAcyclic}
|
||||
|
||||
include "system/ansi_c"
|
||||
when not defined(nimscript):
|
||||
include "system/ansi_c"
|
||||
|
||||
proc cmp(x, y: string): int =
|
||||
result = int(c_strcmp(x, y))
|
||||
proc cmp(x, y: string): int =
|
||||
result = int(c_strcmp(x, y))
|
||||
else:
|
||||
proc cmp(x, y: string): int =
|
||||
if x < y: result = -1
|
||||
elif x > y: result = 1
|
||||
else: result = 0
|
||||
|
||||
const pccHack = if defined(pcc): "_" else: "" # Hack for PCC
|
||||
when not defined(NimrodVM):
|
||||
when not defined(nimscript):
|
||||
when defined(windows):
|
||||
# work-around C's sucking abstraction:
|
||||
# BUGFIX: stdin and stdout should be binary files!
|
||||
|
|
@ -2515,14 +2542,15 @@ when not defined(JS): #and not defined(NimrodVM):
|
|||
|
||||
{.deprecated: [TFile: File, TFileHandle: FileHandle, TFileMode: FileMode].}
|
||||
|
||||
# text file handling:
|
||||
var
|
||||
stdin* {.importc: "stdin", header: "<stdio.h>".}: File
|
||||
## The standard input stream.
|
||||
stdout* {.importc: "stdout", header: "<stdio.h>".}: File
|
||||
## The standard output stream.
|
||||
stderr* {.importc: "stderr", header: "<stdio.h>".}: File
|
||||
## The standard error stream.
|
||||
when not defined(nimscript):
|
||||
# text file handling:
|
||||
var
|
||||
stdin* {.importc: "stdin", header: "<stdio.h>".}: File
|
||||
## The standard input stream.
|
||||
stdout* {.importc: "stdout", header: "<stdio.h>".}: File
|
||||
## The standard output stream.
|
||||
stderr* {.importc: "stderr", header: "<stdio.h>".}: File
|
||||
## The standard error stream.
|
||||
|
||||
when defined(useStdoutAsStdmsg):
|
||||
template stdmsg*: File = stdout
|
||||
|
|
@ -2717,7 +2745,7 @@ when not defined(JS): #and not defined(NimrodVM):
|
|||
inc(i)
|
||||
dealloc(a)
|
||||
|
||||
when not defined(NimrodVM):
|
||||
when not defined(nimscript):
|
||||
proc atomicInc*(memLoc: var int, x: int = 1): int {.inline,
|
||||
discardable, benign.}
|
||||
## atomic increment of `memLoc`. Returns the value after the operation.
|
||||
|
|
@ -2728,27 +2756,27 @@ when not defined(JS): #and not defined(NimrodVM):
|
|||
|
||||
include "system/atomics"
|
||||
|
||||
type
|
||||
PSafePoint = ptr TSafePoint
|
||||
TSafePoint {.compilerproc, final.} = object
|
||||
prev: PSafePoint # points to next safe point ON THE STACK
|
||||
status: int
|
||||
context: C_JmpBuf
|
||||
hasRaiseAction: bool
|
||||
raiseAction: proc (e: ref Exception): bool {.closure.}
|
||||
SafePoint = TSafePoint
|
||||
# {.deprecated: [TSafePoint: SafePoint].}
|
||||
type
|
||||
PSafePoint = ptr TSafePoint
|
||||
TSafePoint {.compilerproc, final.} = object
|
||||
prev: PSafePoint # points to next safe point ON THE STACK
|
||||
status: int
|
||||
context: C_JmpBuf
|
||||
hasRaiseAction: bool
|
||||
raiseAction: proc (e: ref Exception): bool {.closure.}
|
||||
SafePoint = TSafePoint
|
||||
# {.deprecated: [TSafePoint: SafePoint].}
|
||||
|
||||
when declared(initAllocator):
|
||||
initAllocator()
|
||||
when hasThreadSupport:
|
||||
include "system/syslocks"
|
||||
when hostOS != "standalone": include "system/threads"
|
||||
elif not defined(nogc) and not defined(NimrodVM):
|
||||
elif not defined(nogc) and not defined(nimscript):
|
||||
when not defined(useNimRtl) and not defined(createNimRtl): initStackBottom()
|
||||
when declared(initGC): initGC()
|
||||
|
||||
when not defined(NimrodVM):
|
||||
when not defined(nimscript):
|
||||
proc setControlCHook*(hook: proc () {.noconv.} not nil)
|
||||
## allows you to override the behaviour of your application when CTRL+C
|
||||
## is pressed. Only one such hook is supported.
|
||||
|
|
@ -2777,9 +2805,9 @@ when not defined(JS): #and not defined(NimrodVM):
|
|||
{.pop.} # stack trace
|
||||
{.pop.} # stack trace
|
||||
|
||||
when hostOS != "standalone" and not defined(NimrodVM):
|
||||
when hostOS != "standalone" and not defined(nimscript):
|
||||
include "system/dyncalls"
|
||||
when not defined(NimrodVM):
|
||||
when not defined(nimscript):
|
||||
include "system/sets"
|
||||
|
||||
when defined(gogc):
|
||||
|
|
@ -2854,11 +2882,11 @@ when not defined(JS): #and not defined(NimrodVM):
|
|||
var res = TaintedString(newStringOfCap(80))
|
||||
while f.readLine(res): yield res
|
||||
|
||||
when not defined(NimrodVM) and hasAlloc:
|
||||
when not defined(nimscript) and hasAlloc:
|
||||
include "system/assign"
|
||||
include "system/repr"
|
||||
|
||||
when hostOS != "standalone" and not defined(NimrodVM):
|
||||
when hostOS != "standalone" and not defined(nimscript):
|
||||
proc getCurrentException*(): ref Exception {.compilerRtl, inl, benign.} =
|
||||
## retrieves the current exception; if there is none, nil is returned.
|
||||
result = currException
|
||||
|
|
@ -2886,14 +2914,14 @@ when not defined(JS): #and not defined(NimrodVM):
|
|||
currException = exc
|
||||
|
||||
{.push stack_trace: off, profiler:off.}
|
||||
when defined(endb) and not defined(NimrodVM):
|
||||
when defined(endb) and not defined(nimscript):
|
||||
include "system/debugger"
|
||||
|
||||
when defined(profiler) or defined(memProfiler):
|
||||
include "system/profiler"
|
||||
{.pop.} # stacktrace
|
||||
|
||||
when not defined(NimrodVM):
|
||||
when not defined(nimscript):
|
||||
proc likely*(val: bool): bool {.importc: "likely", nodecl, nosideeffect.}
|
||||
## Hints the optimizer that `val` is likely going to be true.
|
||||
##
|
||||
|
|
@ -2971,7 +2999,7 @@ elif defined(JS):
|
|||
when defined(JS):
|
||||
include "system/jssys"
|
||||
include "system/reprjs"
|
||||
elif defined(NimrodVM):
|
||||
elif defined(nimscript):
|
||||
proc cmp(x, y: string): int =
|
||||
if x == y: return 0
|
||||
if x < y: return -1
|
||||
|
|
@ -3284,7 +3312,7 @@ proc shallow*[T](s: var seq[T]) {.noSideEffect, inline.} =
|
|||
## marks a sequence `s` as `shallow`:idx:. Subsequent assignments will not
|
||||
## perform deep copies of `s`. This is only useful for optimization
|
||||
## purposes.
|
||||
when not defined(JS) and not defined(NimrodVM):
|
||||
when not defined(JS) and not defined(nimscript):
|
||||
var s = cast[PGenericSeq](s)
|
||||
s.reserved = s.reserved or seqShallowFlag
|
||||
|
||||
|
|
@ -3292,7 +3320,7 @@ proc shallow*(s: var string) {.noSideEffect, inline.} =
|
|||
## marks a string `s` as `shallow`:idx:. Subsequent assignments will not
|
||||
## perform deep copies of `s`. This is only useful for optimization
|
||||
## purposes.
|
||||
when not defined(JS) and not defined(NimrodVM):
|
||||
when not defined(JS) and not defined(nimscript):
|
||||
var s = cast[PGenericSeq](s)
|
||||
s.reserved = s.reserved or seqShallowFlag
|
||||
|
||||
|
|
@ -3381,7 +3409,7 @@ proc locals*(): RootObj {.magic: "Plugin", noSideEffect.} =
|
|||
## # -> B is 1
|
||||
discard
|
||||
|
||||
when hasAlloc and not defined(NimrodVM) and not defined(JS):
|
||||
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
|
||||
## for the implementation of ``spawn``.
|
||||
|
|
@ -3423,3 +3451,6 @@ proc xlen*[T](x: seq[T]): int {.magic: "XLenSeq", noSideEffect.} =
|
|||
discard
|
||||
|
||||
{.pop.} #{.push warning[GcMem]: off, warning[Uninit]: off.}
|
||||
|
||||
when defined(nimconfig):
|
||||
include "system/nimscript"
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ type
|
|||
# only slots that are
|
||||
# needed are allocated and not 10_000,
|
||||
# except for the global data description.
|
||||
f: Frame
|
||||
f: TFrame
|
||||
slots: array[0..10_000, VarSlot]
|
||||
{.deprecated: [TVarSlot: VarSlot, TExtendedFrame: ExtendedFrame].}
|
||||
|
||||
|
|
@ -66,7 +66,7 @@ var
|
|||
dbgBP: array[0..127, Breakpoint] # breakpoints
|
||||
dbgBPlen: int
|
||||
dbgBPbloom: int64 # we use a bloom filter to speed up breakpoint checking
|
||||
|
||||
|
||||
dbgFilenames*: array[0..300, cstring] ## registered filenames;
|
||||
## 'nil' terminated
|
||||
dbgFilenameLen: int
|
||||
|
|
@ -197,7 +197,7 @@ proc genericHashAux(dest: pointer, n: ptr TNimNode, shallow: bool,
|
|||
result = genericHashAux(cast[pointer](d +% n.offset), n.typ, shallow, h)
|
||||
of nkList:
|
||||
result = h
|
||||
for i in 0..n.len-1:
|
||||
for i in 0..n.len-1:
|
||||
result = result !& genericHashAux(dest, n.sons[i], shallow, result)
|
||||
of nkCase:
|
||||
result = h !& hash(cast[pointer](d +% n.offset), n.typ.size)
|
||||
|
|
@ -205,7 +205,7 @@ proc genericHashAux(dest: pointer, n: ptr TNimNode, shallow: bool,
|
|||
if m != nil: result = genericHashAux(dest, m, shallow, result)
|
||||
of nkNone: sysAssert(false, "genericHashAux")
|
||||
|
||||
proc genericHashAux(dest: pointer, mt: PNimType, shallow: bool,
|
||||
proc genericHashAux(dest: pointer, mt: PNimType, shallow: bool,
|
||||
h: Hash): Hash =
|
||||
sysAssert(mt != nil, "genericHashAux 2")
|
||||
case mt.kind
|
||||
|
|
@ -257,7 +257,7 @@ proc genericHashAux(dest: pointer, mt: PNimType, shallow: bool,
|
|||
|
||||
proc genericHash(dest: pointer, mt: PNimType): int =
|
||||
result = genericHashAux(dest, mt, false, 0)
|
||||
|
||||
|
||||
proc dbgRegisterWatchpoint(address: pointer, name: cstring,
|
||||
typ: PNimType) {.compilerproc.} =
|
||||
let L = watchPointsLen
|
||||
|
|
@ -285,7 +285,7 @@ var
|
|||
## Only code compiled with the ``debugger:on`` switch calls this hook.
|
||||
|
||||
dbgWatchpointHook*: proc (watchpointName: cstring) {.nimcall.}
|
||||
|
||||
|
||||
proc checkWatchpoints =
|
||||
let L = watchPointsLen
|
||||
for i in 0.. <L:
|
||||
|
|
|
|||
152
lib/system/nimscript.nim
Normal file
152
lib/system/nimscript.nim
Normal file
|
|
@ -0,0 +1,152 @@
|
|||
#
|
||||
#
|
||||
# Nim's Runtime Library
|
||||
# (c) Copyright 2015 Andreas Rumpf
|
||||
#
|
||||
# See the file "copying.txt", included in this
|
||||
# distribution, for details about the copyright.
|
||||
#
|
||||
|
||||
|
||||
# Nim's configuration system now uses Nim for scripting. This module provides
|
||||
# a few things that are required for this to work.
|
||||
|
||||
template builtin = discard
|
||||
|
||||
proc listDirs*(dir: string): seq[string] = builtin
|
||||
proc listFiles*(dir: string): seq[string] = builtin
|
||||
|
||||
proc removeDir(dir: string) = builtin
|
||||
proc removeFile(dir: string) = builtin
|
||||
proc moveFile(src, dest: string) = builtin
|
||||
proc createDir(dir: string) = builtin
|
||||
proc getOsError: string = builtin
|
||||
proc setCurrentDir(dir: string) = builtin
|
||||
proc getCurrentDir(): string = builtin
|
||||
proc paramStr*(i: int): string = builtin
|
||||
proc paramCount*(): int = builtin
|
||||
|
||||
proc switch*(key: string, val="") = builtin
|
||||
proc getCommand*(): string = builtin
|
||||
proc setCommand*(cmd: string) = builtin
|
||||
proc cmpIgnoreStyle(a, b: string): int = builtin
|
||||
|
||||
proc strip(s: string): string =
|
||||
var i = 0
|
||||
while s[i] in {' ', '\c', '\L'}: inc i
|
||||
result = s.substr(i)
|
||||
|
||||
template `--`*(key, val: untyped) = switch(astToStr(key), strip astToStr(val))
|
||||
template `--`*(key: untyped) = switch(astToStr(key), "")
|
||||
|
||||
type
|
||||
ScriptMode* {.pure.} = enum
|
||||
Silent,
|
||||
Verbose,
|
||||
Whatif
|
||||
|
||||
var
|
||||
mode*: ScriptMode ## Set this to influence how mkDir, rmDir, rmFile etc.
|
||||
## behave
|
||||
|
||||
template checkOsError =
|
||||
let err = getOsError()
|
||||
if err.len > 0: raise newException(OSError, err)
|
||||
|
||||
template log(msg: string, body: untyped) =
|
||||
if mode == ScriptMode.Verbose or mode == ScriptMode.Whatif:
|
||||
echo "[NimScript] ", msg
|
||||
if mode != ScriptMode.WhatIf:
|
||||
body
|
||||
|
||||
proc rmDir*(dir: string) {.raises: [OSError].} =
|
||||
log "rmDir: " & dir:
|
||||
removeDir dir
|
||||
checkOsError()
|
||||
|
||||
proc rmFile*(dir: string) {.raises: [OSError].} =
|
||||
log "rmFile: " & dir:
|
||||
removeFile dir
|
||||
checkOsError()
|
||||
|
||||
proc mkDir*(dir: string) {.raises: [OSError].} =
|
||||
log "mkDir: " & dir:
|
||||
createDir dir
|
||||
checkOsError()
|
||||
|
||||
proc mvFile*(`from`, to: string) {.raises: [OSError].} =
|
||||
log "mvFile: " & `from` & ", " & to:
|
||||
moveFile `from`, to
|
||||
checkOsError()
|
||||
|
||||
proc exec*(command: string, input = "", cache = "") =
|
||||
## Executes an external process.
|
||||
log "exec: " & command:
|
||||
echo staticExec(command, input, cache)
|
||||
|
||||
proc put*(key, value: string) =
|
||||
## Sets a configuration 'key' like 'gcc.options.always' to its value.
|
||||
builtin
|
||||
|
||||
proc get*(key: string): string =
|
||||
## Retrieves a configuration 'key' like 'gcc.options.always'.
|
||||
builtin
|
||||
|
||||
proc exists*(key: string): bool =
|
||||
## Checks for the existance of a configuration 'key'
|
||||
## like 'gcc.options.always'.
|
||||
builtin
|
||||
|
||||
proc nimcacheDir*(): string =
|
||||
## Retrieves the location of 'nimcache'.
|
||||
builtin
|
||||
|
||||
proc thisDir*(): string =
|
||||
## Retrieves the location of the current ``nims`` script file.
|
||||
builtin
|
||||
|
||||
proc cd*(dir: string) {.raises: [OSError].} =
|
||||
## Changes the current directory.
|
||||
##
|
||||
## The change is permanent for the rest of the execution, since this is just
|
||||
## a shortcut for `os.setCurrentDir()
|
||||
## <http://nim-lang.org/os.html#setCurrentDir,string>`_ . Use the `withDir()
|
||||
## <#withDir>`_ template if you want to perform a temporary change only.
|
||||
setCurrentDir(dir)
|
||||
checkOsError()
|
||||
|
||||
template withDir*(dir: string; body: untyped): untyped =
|
||||
## Changes the current directory temporarily.
|
||||
##
|
||||
## If you need a permanent change, use the `cd() <#cd>`_ proc. Usage example:
|
||||
##
|
||||
## .. code-block:: nimrod
|
||||
## withDir "foo":
|
||||
## # inside foo
|
||||
## #back to last dir
|
||||
var curDir = getCurrentDir()
|
||||
try:
|
||||
cd(dir)
|
||||
body
|
||||
finally:
|
||||
cd(curDir)
|
||||
|
||||
template `==?`(a, b: string): bool = cmpIgnoreStyle(a, b) == 0
|
||||
|
||||
proc writeTask(name, desc: string) =
|
||||
if desc.len > 0:
|
||||
var spaces = " "
|
||||
for i in 0 ..< 20 - name.len: spaces.add ' '
|
||||
echo name, spaces, desc
|
||||
|
||||
template task*(name: untyped; description: string; body: untyped): untyped =
|
||||
## Defines a task. Hidden tasks are supported via an empty description.
|
||||
proc `name Task`() = body
|
||||
|
||||
let cmd = getCommand()
|
||||
if cmd.len == 0 or cmd ==? "help" or cmd == "nop":
|
||||
setCommand "nop"
|
||||
writeTask(astToStr(name), description)
|
||||
elif cmd ==? astToStr(name):
|
||||
setCommand "nop"
|
||||
`name Task`()
|
||||
|
|
@ -18,11 +18,14 @@ type
|
|||
alpha, ## Alpha processor
|
||||
powerpc, ## 32 bit PowerPC
|
||||
powerpc64, ## 64 bit PowerPC
|
||||
powerpc64el, ## Little Endian 64 bit PowerPC
|
||||
sparc, ## Sparc based processor
|
||||
ia64, ## Intel Itanium
|
||||
amd64, ## x86_64 (AMD64); 64 bit x86 compatible CPU
|
||||
mips, ## Mips based processor
|
||||
mipsel, ## Little Endian Mips based processor
|
||||
arm, ## ARM based processor
|
||||
arm64, ## ARM64 based processor
|
||||
vm, ## Some Virtual machine: Nim's VM or JavaScript
|
||||
avr ## AVR based processor
|
||||
|
||||
|
|
@ -63,11 +66,14 @@ const
|
|||
elif defined(alpha): CpuPlatform.alpha
|
||||
elif defined(powerpc): CpuPlatform.powerpc
|
||||
elif defined(powerpc64): CpuPlatform.powerpc64
|
||||
elif defined(powerpc64el): CpuPlatform.powerpc64el
|
||||
elif defined(sparc): CpuPlatform.sparc
|
||||
elif defined(ia64): CpuPlatform.ia64
|
||||
elif defined(amd64): CpuPlatform.amd64
|
||||
elif defined(mips): CpuPlatform.mips
|
||||
elif defined(mipsel): CpuPlatform.mipsel
|
||||
elif defined(arm): CpuPlatform.arm
|
||||
elif defined(arm64): CpuPlatform.arm64
|
||||
elif defined(vm): CpuPlatform.vm
|
||||
elif defined(avr): CpuPlatform.avr
|
||||
else: CpuPlatform.none
|
||||
|
|
|
|||
|
|
@ -256,7 +256,10 @@ when not defined(useNimRtl):
|
|||
of tyBool: add result, reprBool(cast[ptr bool](p)[])
|
||||
of tyChar: add result, reprChar(cast[ptr char](p)[])
|
||||
of tyString: reprStrAux(result, cast[ptr string](p)[])
|
||||
of tyCString: reprStrAux(result, $(cast[ptr cstring](p)[]))
|
||||
of tyCString:
|
||||
let cs = cast[ptr cstring](p)[]
|
||||
if cs.isNil: add result, "nil"
|
||||
else: reprStrAux(result, $cs)
|
||||
of tyRange: reprAux(result, p, typ.base, cl)
|
||||
of tyProc, tyPointer:
|
||||
if cast[PPointer](p)[] == nil: add result, "nil"
|
||||
|
|
|
|||
|
|
@ -19,9 +19,9 @@ proc countBits32(n: int32): int {.compilerproc.} =
|
|||
v = (v and 0x33333333'i32) +% ((v shr 2'i32) and 0x33333333'i32)
|
||||
result = ((v +% (v shr 4'i32) and 0xF0F0F0F'i32) *% 0x1010101'i32) shr 24'i32
|
||||
|
||||
proc countBits64(n: int64): int {.compilerproc.} =
|
||||
result = countBits32(toU32(n and 0xffff'i64)) +
|
||||
countBits32(toU32(n shr 16'i64))
|
||||
proc countBits64(n: int64): int {.compilerproc.} =
|
||||
result = countBits32(toU32(n and 0xffffffff'i64)) +
|
||||
countBits32(toU32(n shr 32'i64))
|
||||
|
||||
proc cardSet(s: NimSet, len: int): int {.compilerproc.} =
|
||||
result = 0
|
||||
|
|
|
|||
|
|
@ -182,7 +182,10 @@ proc readAllFile(file: File): string =
|
|||
proc readAll(file: File): TaintedString =
|
||||
# Separate handling needed because we need to buffer when we
|
||||
# don't know the overall length of the File.
|
||||
let len = if file != stdin: rawFileSize(file) else: -1
|
||||
when declared(stdin):
|
||||
let len = if file != stdin: rawFileSize(file) else: -1
|
||||
else:
|
||||
let len = rawFileSize(file)
|
||||
if len > 0:
|
||||
result = readAllFile(file, len).TaintedString
|
||||
else:
|
||||
|
|
@ -216,9 +219,9 @@ proc writeLine[Ty](f: File, x: varargs[Ty, `$`]) =
|
|||
for i in items(x): write(f, i)
|
||||
write(f, "\n")
|
||||
|
||||
|
||||
proc rawEcho(x: string) {.inline, compilerproc.} = write(stdout, x)
|
||||
proc rawEchoNL() {.inline, compilerproc.} = write(stdout, "\n")
|
||||
when declared(stdout):
|
||||
proc rawEcho(x: string) {.inline, compilerproc.} = write(stdout, x)
|
||||
proc rawEchoNL() {.inline, compilerproc.} = write(stdout, "\n")
|
||||
|
||||
# interface to the C procs:
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue