syntax compatibility between do blocks and stmt blocks

See the section `do notation` in the manual for more info.

* nkMacroStmt has been removed
   Macro statements are now mapped to regular nkCall nodes.
   The support for additional clauses (such as else, except, of, etc)
   have been restored - they will now appear as additional arguments
   for the nkCall node (as nkElse, nkExcept, etc nodes)

* fixed some regressions in the `is` operator and semCompiles
This commit is contained in:
Zahary Karadjov
2012-10-03 21:11:41 +03:00
parent a6d5707faf
commit d9d82fb0af
21 changed files with 142 additions and 168 deletions

View File

@@ -1,5 +1,5 @@
discard """
output: "Got: 'nnkMacroStmt' hi"
output: "Got: 'nnkCall' hi"
"""
import
@@ -11,17 +11,15 @@ macro outterMacro*(n: stmt): stmt {.immediate.} =
proc innerProc(i: int): string =
echo "Using arg ! " & n.repr
result = "Got: '" & $n.kind & "' " & $j
if n.kind != TNimrodNodeKind.nnkMacroStmt:
error("Macro " & n[0].repr & " requires a block.")
var callNode = n[0]
expectKind(callNode, TNimrodNodeKind.nnkCall)
if callNode.len != 2 or callNode[1].kind != TNimrodNodeKind.nnkIdent:
expectKind(n, TNimrodNodeKind.nnkCall)
if n.len != 3 or n[1].kind != TNimrodNodeKind.nnkIdent:
error("Macro " & callNode.repr &
" requires the ident passed as parameter (eg: " & callNode.repr &
"(the_name_you_want)): statements.")
result = newNimNode(TNimrodNodeKind.nnkStmtList)
var ass : PNimrodNode = newNimNode(TNimrodNodeKind.nnkAsgn)
ass.add(newIdentNode(callNode[1].ident))
var ass : PNimrodNode = newNimNode(nnkAsgn)
ass.add(newIdentNode(n[1].ident))
ass.add(newStrLitNode(innerProc(4)))
result.add(ass)

View File

@@ -28,12 +28,12 @@ import
# `opened` is defined to `optional.hasValue`
macro using(e: expr): stmt {.immediate.} =
let e = callsite()
if e.len != 2:
if e.len != 3:
error "Using statement: unexpected number of arguments. Got " &
$e.len & ", expected: 1 or more variable assignments and a block"
var args = e[0]
var body = e[1]
var args = e
var body = e[2]
var
variables : seq[PNimrodNode]
@@ -41,8 +41,8 @@ macro using(e: expr): stmt {.immediate.} =
newSeq(variables, 0)
newSeq(closingCalls, 0)
for i in countup(1, args.len-1):
for i in countup(1, args.len-2):
if args[i].kind == nnkExprEqExpr:
var varName = args[i][0]
var varValue = args[i][1]