From 587098c158ea19ee0232954d2d9277205d164df6 Mon Sep 17 00:00:00 2001 From: konqoro Date: Sun, 13 Nov 2016 14:02:52 +0200 Subject: [PATCH] Fix exporting classes This commit removes checking for missing base object in favour of being able to export a class. --- content/content/oop_macro.md | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/content/content/oop_macro.md b/content/content/oop_macro.md index 0e8f825..b97cb16 100644 --- a/content/content/oop_macro.md +++ b/content/content/oop_macro.md @@ -49,15 +49,10 @@ macro class*(head, body: untyped): untyped = var typeName, baseName: NimNode - if head.kind == nnkIdent: - # `head` is expression `typeName` - # echo head.treeRepr - # -------------------- - # Ident !"Animal" - typeName = head - baseName = newIdentNode("RootObj") + # flag if object should be exported + var exported: bool - elif head.kind == nnkInfix and $head[0] == "of": + if head.kind == nnkInfix and head[0].ident == !"of": # `head` is expression `typeName of baseClass` # echo head.treeRepr # -------------------- @@ -68,6 +63,21 @@ macro class*(head, body: untyped): untyped = typeName = head[1] baseName = head[2] + elif head.kind == nnkInfix and head[0].ident == !"*" and + head[2].kind == nnkPrefix and head[2][0].ident == !"of": + # `head` is expression `typeName* of baseClass` + # echo head.treeRepr + # -------------------- + # Infix + # Ident !"*" + # Ident !"Animal" + # Prefix + # Ident !"of" + # Ident !"RootObj" + typeName = head[1] + baseName = head[2][1] + exported = true + else: quit "Invalid node: " & head.lispRepr @@ -96,8 +106,13 @@ macro class*(head, body: untyped): untyped = # create a type section in the result result = - quote do: - type `typeName` = ref object of `baseName` + if exported: + # mark `typeName` with an asterisk + quote do: + type `typeName`* = ref object of `baseName` + else: + quote do: + type `typeName` = ref object of `baseName` # echo treeRepr(body) # --------------------