merged branch overloading-for-macros

This commit is contained in:
Zahary Karadjov
2012-03-19 12:06:38 +02:00
39 changed files with 366 additions and 218 deletions

View File

@@ -50,7 +50,7 @@ proc filter*[T](seq1: seq[T], pred: proc(item: T): bool): seq[T] =
## Returns all items in a sequence that fulfilled the predicate.
accumulateResult(filter(seq1, pred))
template filterIt*(seq1, pred: expr): expr =
template filterIt*(seq1, pred: expr): expr {.immediate.} =
## Finds a specific item in a sequence as long as the
## predicate returns true. The predicate needs to be an expression
## containing ``it``: ``filterIt("abcxyz", it == 'x')``.

View File

@@ -17,15 +17,15 @@ type
proc `==` *(a, b: TColor): bool {.borrow.}
## compares two colors.
template extract(a: TColor, r, g, b: expr) =
template extract(a: TColor, r, g, b: expr) {.immediate.}=
var r = a.int shr 16 and 0xff
var g = a.int shr 8 and 0xff
var b = a.int and 0xff
template rawRGB(r, g, b: expr): expr =
template rawRGB(r, g, b: int): expr =
TColor(r shl 16 or g shl 8 or b)
template colorOp(op: expr) =
template colorOp(op: expr) {.immediate.} =
extract(a, ar, ag, ab)
extract(b, br, bg, bb)
result = rawRGB(op(ar, br), op(ag, bg), op(ab, bb))

View File

@@ -242,12 +242,12 @@ proc UnixToNativePath*(path: string): string {.
inc(i)
when defined(windows):
template wrapUnary(varname, winApiProc, arg: expr) =
template wrapUnary(varname, winApiProc, arg: expr) {.immediate.} =
var tmp = allocWideCString(arg)
var varname = winApiProc(tmp)
dealloc tmp
template wrapBinary(varname, winApiProc, arg, arg2: expr) =
template wrapBinary(varname, winApiProc, arg, arg2: expr) {.immediate.} =
var tmp2 = allocWideCString(arg)
var varname = winApiProc(tmp2, arg2)
dealloc tmp2

View File

@@ -624,15 +624,15 @@ proc `<` *[T](x, y: ref T): bool {.magic: "LtPtr", noSideEffect.}
proc `<` *[T](x, y: ptr T): bool {.magic: "LtPtr", noSideEffect.}
proc `<` *(x, y: pointer): bool {.magic: "LtPtr", noSideEffect.}
template `!=` * (x, y: expr): expr =
template `!=` * (x, y: expr): expr {.immediate.} =
## unequals operator. This is a shorthand for ``not (x == y)``.
not (x == y)
template `>=` * (x, y: expr): expr =
template `>=` * (x, y: expr): expr {.immediate.} =
## "is greater or equals" operator. This is the same as ``y <= x``.
y <= x
template `>` * (x, y: expr): expr =
template `>` * (x, y: expr): expr {.immediate.} =
## "is greater" operator. This is the same as ``y < x``.
y < x
@@ -655,11 +655,11 @@ proc contains*[T](x: set[T], y: T): bool {.magic: "InSet", noSideEffect.}
## is achieved by reversing the parameters for ``contains``; ``in`` then
## passes its arguments in reverse order.
template `in` * (x, y: expr): expr = contains(y, x)
template `not_in` * (x, y: expr): expr = not contains(y, x)
template `in` * (x, y: expr): expr {.immediate.} = contains(y, x)
template `not_in` * (x, y: expr): expr {.immediate.} = not contains(y, x)
proc `is` *[T, S](x: T, y: S): bool {.magic: "Is", noSideEffect.}
template `is_not` *(x, y: expr): expr = not (x is y)
template `is_not` *(x, y: expr): expr {.immediate.} = not (x is y)
proc `of` *[T, S](x: T, y: S): bool {.magic: "Of", noSideEffect.}
@@ -842,7 +842,7 @@ proc quit*(errorcode: int = QuitSuccess) {.
## It does *not* call the garbage collector to free all the memory,
## unless a quit procedure calls ``GC_collect``.
template sysAssert(cond, msg: expr) =
template sysAssert(cond: bool, msg: string) =
when defined(useSysAssert):
if not cond:
echo "[SYSASSERT] ", msg
@@ -1090,11 +1090,11 @@ proc swap*[T](a, b: var T) {.magic: "Swap", noSideEffect.}
## swaps the values `a` and `b`. This is often more efficient than
## ``tmp = a; a = b; b = tmp``. Particularly useful for sorting algorithms.
template `>=%` *(x, y: expr): expr = y <=% x
template `>=%` *(x, y: expr): expr {.immediate.} = y <=% x
## treats `x` and `y` as unsigned and compares them.
## Returns true iff ``unsigned(x) >= unsigned(y)``.
template `>%` *(x, y: expr): expr = y <% x
template `>%` *(x, y: expr): expr {.immediate.} = y <% x
## treats `x` and `y` as unsigned and compares them.
## Returns true iff ``unsigned(x) > unsigned(y)``.
@@ -1590,7 +1590,7 @@ proc echo*[Ty](x: openarray[Ty]) {.magic: "Echo", noSideEffect.}
## Unlike other IO operations this is guaranteed to be thread-safe as
## ``echo`` is very often used for debugging convenience.
template newException*(exceptn, message: expr): expr =
template newException*(exceptn: typeDesc, message: string): expr =
## creates an exception object of type ``exceptn`` and sets its ``msg`` field
## to `message`. Returns the new exception object.
block: # open a new scope
@@ -2033,7 +2033,7 @@ proc `[]`*(s: string, x: TSlice[int]): string {.inline.} =
## slice operation for strings. Negative indexes are supported.
result = s.substr(x.a-|s, x.b-|s)
template spliceImpl(s, a, L, b: expr): stmt =
template spliceImpl(s, a, L, b: expr): stmt {.immediate.} =
# make room for additional elements or cut:
var slen = s.len
var shift = b.len - L
@@ -2176,7 +2176,7 @@ proc InstantiationInfo*(index = -1): tuple[filename: string, line: int] {.
proc raiseAssert(msg: string) {.noinline.} =
raise newException(EAssertionFailed, msg)
template assert*(cond: expr, msg = "") =
template assert*(cond: bool, msg = "") =
## provides a means to implement `programming by contracts`:idx: in Nimrod.
## ``assert`` evaluates expression ``cond`` and if ``cond`` is false, it
## raises an ``EAssertionFailure`` exception. However, the compiler may
@@ -2188,7 +2188,7 @@ template assert*(cond: expr, msg = "") =
if not cond:
raiseAssert(astToStr(cond) & ' ' & msg)
template doAssert*(cond: expr, msg = "") =
template doAssert*(cond: bool, msg = "") =
## same as `assert` but is always turned on and not affected by the
## ``--assertions`` command line switch.
bind raiseAssert, InstantiationInfo

View File

@@ -251,7 +251,7 @@ when defined(endb):
dbgAborting: bool # whether the debugger wants to abort
proc signalHandler(sig: cint) {.exportc: "signalHandler", noconv.} =
template processSignal(s, action: expr) =
template processSignal(s, action: expr) {.immediate.} =
if s == SIGINT: action("SIGINT: Interrupted by Ctrl-C.\n")
elif s == SIGSEGV:
action("SIGSEGV: Illegal storage access. (Attempt to read from nil?)\n")

View File

@@ -170,7 +170,7 @@ when traceGC:
cfprintf(cstdout, "Allocations: %ld; ZCT freed: %ld; CYC freed: %ld\n",
e, z, y)
template gcTrace(cell, state: expr): stmt =
template gcTrace(cell, state: expr): stmt {.immediate.} =
when traceGC: traceCell(cell, state)
# -----------------------------------------------------------------------------

View File

@@ -1284,9 +1284,9 @@ type # This is the system-independent thread info struc
TProcedure* = proc ()
type TEventSeq = set[TEventKind]
template evconv(procName: expr, ptrName: typeDesc, assertions: TEventSeq): stmt =
template evconv(procName: expr, ptrName: typeDesc, assertions: TEventSeq): stmt {.immediate.} =
proc `procName`*(event: PEvent): ptrName =
assert(assertions.contains(event.kind))
assert(contains(assertions, event.kind))
result = cast[ptrName](event)
evconv(EvActive, PActiveEvent, {ACTIVEEVENT})