From 324834dc23c9639c402ba70b92c191ff5b4aacae Mon Sep 17 00:00:00 2001 From: Joey Payne Date: Wed, 7 Oct 2015 23:35:17 -0600 Subject: [PATCH] Added ability for macros, converters, and templates to be added in a class. --- oop_macro.nim | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/oop_macro.nim b/oop_macro.nim index b9e5ca1..2a0bbe1 100644 --- a/oop_macro.nim +++ b/oop_macro.nim @@ -174,10 +174,12 @@ macro class*(head: untyped, body: untyped): untyped = setNodeName(n2, procName, typeName) # add the base pragma when it's a base method - n2[4] = newNimNode(nnkPragma).add(ident"base") + if node.kind == nnkMethodDef: + n2[4] = newNimNode(nnkPragma).add(ident"base") else: discard + var numRes = result.len() # Iterate over the statements, adding `self: T` # to the parameters of functions for node in body.children: @@ -217,6 +219,14 @@ macro class*(head: untyped, body: untyped): untyped = result.add(n) + of nnkIteratorDef, nnkConverterDef, nnkMacroDef, nnkTemplateDef: + # inject `self: T` into the arguments + let n = copyNimTree(node) + echo n.treeRepr + n[3].insert(1, newIdentDefs(ident(objReference), typeName)) + result.insert(numRes, n) + numRes += 1 + of nnkVarSection: # variables get turned into fields of the type. for n in node.children: @@ -281,6 +291,7 @@ macro class*(head: untyped, body: untyped): untyped = # Empty <= We want to replace self typeDecl[0][0][2][0][2] = recList result.insert(0, typeDecl) + echo result.toStrLit() when isMainModule: class Animal of RootObj: