made tests green again

This commit is contained in:
Araq 2012-08-31 00:00:14 +02:00
commit b56df72a32
11 changed files with 16 additions and 8 deletions

View file

@ -154,6 +154,7 @@ template require*(conditions: stmt): stmt {.dirty.} =
check conditions check conditions
macro expect*(exp: stmt): stmt = macro expect*(exp: stmt): stmt =
let exp = callsite()
template expectBody(errorTypes, lineInfoLit: expr, template expectBody(errorTypes, lineInfoLit: expr,
body: stmt): PNimrodNode {.dirty.} = body: stmt): PNimrodNode {.dirty.} =
try: try:

View file

@ -258,7 +258,7 @@ proc xmlConstructor(e: PNimrodNode): PNimrodNode {.compileTime.} =
else: else:
result = newCall("newXmlTree", toStrLit(a)) result = newCall("newXmlTree", toStrLit(a))
macro `<>`*(x: expr): expr = macro `<>`*(x: expr): expr {.immediate.} =
## Constructor macro for XML. Example usage: ## Constructor macro for XML. Example usage:
## ##
## .. code-block:: nimrod ## .. code-block:: nimrod

View file

@ -11,7 +11,7 @@ var x = if 4 != 5:
else: else:
"no" "no"
macro mymacro(n: expr): expr = result = n[1][0] macro mymacro(n: expr): expr = result = n[0]
mymacro: mymacro:
echo "test" echo "test"

View file

@ -8,7 +8,7 @@ import macros
var gid {.compileTime.} = 3 var gid {.compileTime.} = 3
macro genId(invokation: expr): expr = macro genId(): expr =
result = newIntLitNode(gid) result = newIntLitNode(gid)
inc gid inc gid

View file

@ -13,6 +13,7 @@ proc testBlock(): string {.compileTime.} =
result = "ta-da" result = "ta-da"
macro mac(n: expr): expr = macro mac(n: expr): expr =
let n = callsite()
expectKind(n, nnkCall) expectKind(n, nnkCall)
expectLen(n, 2) expectLen(n, 2)
expectKind(n[1], nnkStrLit) expectKind(n[1], nnkStrLit)

View file

@ -1,11 +1,12 @@
discard """ discard """
output: "Got: 'nnkIntLit' hi" output: "Got: 'nnkMacroStmt' hi"
""" """
import import
macros, strutils macros, strutils
macro outterMacro*(n: stmt): stmt = macro outterMacro*(n: stmt): stmt =
let n = callsite()
var j : string = "hi" var j : string = "hi"
proc innerProc(i: int): string = proc innerProc(i: int): string =
echo "Using arg ! " & n.repr echo "Using arg ! " & n.repr

View file

@ -35,7 +35,8 @@ proc init(my: var TRectangle) =
my.height = 10 my.height = 10
my.draw = cast[proc (my: var TFigure) {.nimcall.}](drawRectangle) my.draw = cast[proc (my: var TFigure) {.nimcall.}](drawRectangle)
macro `!` (n: expr): stmt = macro `!` (n: expr): stmt {.immediate.} =
let n = callsite()
result = newNimNode(nnkCall, n) result = newNimNode(nnkCall, n)
var dot = newNimNode(nnkDotExpr, n) var dot = newNimNode(nnkDotExpr, n)
dot.add(n[1]) # obj dot.add(n[1]) # obj

View file

@ -6,7 +6,8 @@ discard """
import macros import macros
macro quoteWords(n: expr): expr = macro quoteWords(n: expr): expr {.immediate.} =
let n = callsite()
result = newNimNode(nnkBracket, n) result = newNimNode(nnkBracket, n)
for i in 1..n.len-1: for i in 1..n.len-1:
expectKind(n[i], nnkIdent) expectKind(n[i], nnkIdent)

View file

@ -18,6 +18,7 @@ template ProcessInterpolations(e: expr) =
of ikVar, ikExpr: addExpr(newCall("$", parseExpr(f.value))) of ikVar, ikExpr: addExpr(newCall("$", parseExpr(f.value)))
macro formatStyleInterpolation(e: expr): expr = macro formatStyleInterpolation(e: expr): expr =
let e = callsite()
var var
formatString = "" formatString = ""
arrayNode = newNimNode(nnkBracket) arrayNode = newNimNode(nnkBracket)
@ -41,6 +42,7 @@ macro formatStyleInterpolation(e: expr): expr =
result[2] = arrayNode result[2] = arrayNode
macro concatStyleInterpolation(e: expr): expr = macro concatStyleInterpolation(e: expr): expr =
let e = callsite()
var args: seq[PNimrodNode] var args: seq[PNimrodNode]
newSeq(args, 0) newSeq(args, 0)

View file

@ -26,7 +26,8 @@ import
# `opened` here could be an overloaded proc which any type can define. # `opened` here could be an overloaded proc which any type can define.
# A common practice can be returing an Optional[Resource] obj for which # A common practice can be returing an Optional[Resource] obj for which
# `opened` is defined to `optional.hasValue` # `opened` is defined to `optional.hasValue`
macro using(e: expr): stmt = macro using(e: expr): stmt {.immediate.} =
let e = callsite()
if e.len != 2: if e.len != 2:
error "Using statement: unexpected number of arguments. Got " & error "Using statement: unexpected number of arguments. Got " &
$e.len & ", expected: 1 or more variable assignments and a block" $e.len & ", expected: 1 or more variable assignments and a block"

View file

@ -112,7 +112,7 @@ Changes affecting backwards compatibility
the ``inheritable`` pragma to introduce new object roots apart the ``inheritable`` pragma to introduce new object roots apart
from ``TObject``. from ``TObject``.
- Macros now receive parameters like templates do; use the ``callsite`` builtin - Macros now receive parameters like templates do; use the ``callsite`` builtin
to gain access to the invokating AST. to gain access to the invocation AST.
Compiler Additions Compiler Additions