Permalink
Browse files

fixes #368

  • Loading branch information...
1 parent 94becd0 commit 51e40bf8868857ed824391dbcfb3726a01d015e8 @Araq Araq committed Apr 8, 2013
Showing with 63 additions and 2 deletions.
  1. +4 −2 compiler/evals.nim
  2. +59 −0 tests/compile/tmacro2.nim
View
@@ -1495,11 +1495,13 @@ proc evalMacroCall(c: PEvalContext, n, nOrig: PNode, sym: PSym): PNode =
c.callsite = nOrig
var s = newStackFrame()
s.call = n
- setlen(s.params, n.len)
+ var L = n.safeLen
+ if L == 0: L = 1
+ setlen(s.params, L)
# return value:
s.params[0] = newNodeIT(nkNilLit, n.info, sym.typ.sons[0])
# setup parameters:
- for i in 1 .. < n.len: s.params[i] = setupMacroParam(n.sons[i])
+ for i in 1 .. < L: s.params[i] = setupMacroParam(n.sons[i])
pushStackFrame(c, s)
discard eval(c, sym.getBody)
result = s.params[0]
@@ -0,0 +1,59 @@
+import macros,json
+
+var decls{.compileTime.}: seq[PNimrodNode] = @[]
+var impls{.compileTime.}: seq[PNimrodNode] = @[]
+
+macro importImpl_forward(name, returns): stmt {.immediate.} =
+ result = newNimNode(nnkEmpty)
+ var func_name = newNimNode(nnkAccQuoted)
+ func_name.add newIdentNode("import")
+ func_name.add name
+
+ var res = newNimNode(nnkProcDef)
+ res.add newNimNode(nnkPostfix)
+ res[0].add newIdentNode("*")
+ res[0].add func_name
+ res.add newNimNode(nnkEmpty)
+ res.add newNimNode(nnkEmpty)
+ res.add newNimNode(nnkFormalParams)
+ res[3].add returns
+ var p1 = newNimNode(nnkIdentDefs)
+ p1.add newIdentNode("dat")
+ p1.add newIdentNOde("PJsonNode")
+ p1.add newNimNode(nnkEmpty)
+ res[3].add p1
+ var p2 = newNimNode(nnkIdentDefs)
+ p2.add newIdentNode("errors")
+ p2.add newNimNode(nnkVarTy)
+ p2.add newNimNode(nnkEmpty)
+ p2[1].add newNimNOde(nnkBracketExpr)
+ p2[1][0].add newIdentNode("seq")
+ p2[1][0].add newIdentNode("string")
+ res[3].add p2
+
+ res.add newNimNode(nnkEmpty)
+ res.add newNimNode(nnkEmpty)
+ res.add newNimNode(nnkEmpty)
+
+ decls.add res
+ echo(repr(res))
+
+macro importImpl(name, returns: expr, body: stmt): stmt {.immediate.} =
+ #var res = getAST(importImpl_forward(name, returns))
+ discard getAST(importImpl_forward(name, returns))
+ var res = copyNimTree(decls[decls.high])
+ res[6] = body
+ echo repr(res)
+ impls.add res
+
+macro okayy:stmt =
+ result = newNimNode(nnkStmtList)
+ for node in decls: result.add node
+ for node in impls: result.add node
+
+importimpl(Item, int):
+ echo 42
+importImpl(Foo, int16):
+ echo 77
+
+okayy

0 comments on commit 51e40bf

Please sign in to comment.