summary refs log tree commit diff stats
path: root/lib/core/macros.nim
diff options
context:
space:
mode:
authorArne Döring <arne.doering@gmx.net>2018-07-19 16:26:07 +0200
committerArne Döring <arne.doering@gmx.net>2018-10-18 15:39:22 +0200
commitabe8ac196234f1cc10a2e0d197441b97faadf54c (patch)
treecdb7fc82c615fe97bdfcc699354c2132b005ed8c /lib/core/macros.nim
parentbe77710c8c189bf797162e1844676c780d8ee920 (diff)
downloadNim-abe8ac196234f1cc10a2e0d197441b97faadf54c.tar.gz
or on NimNode
Diffstat (limited to 'lib/core/macros.nim')
-rw-r--r--lib/core/macros.nim17
1 files changed, 16 insertions, 1 deletions
diff --git a/lib/core/macros.nim b/lib/core/macros.nim
index 2b0d74e6a..e4424690a 100644
--- a/lib/core/macros.nim
+++ b/lib/core/macros.nim
@@ -176,6 +176,22 @@ proc `[]=`*(n: NimNode, i: BackwardsIndex, child: NimNode) =
   ## set `n`'s `i`'th child to `child`.
   n[n.len - i.int] = child
 
+template `or`*(a,b: NimNode): NimNode =
+  ## Evalutuate ``a`` and when it is not an empty node, return return
+  ## it. Otherwise evaluate to ``b``. Can be used to chain several
+  ## expressions that evaluates to the first expression that is not
+  ## empty.
+  ##
+  ## .. code-block:: nim
+  ##
+  ##   let node = mightBeEmpty() or mightAlsoBeEmpty() or fallbackNode
+
+  let arg = a
+  if arg != nil and arg.kind != nnkEmpty:
+    arg
+  else:
+    b
+
 proc add*(father, child: NimNode): NimNode {.magic: "NAdd", discardable,
   noSideEffect, locks: 0.}
   ## Adds the `child` to the `father` node. Returns the
@@ -1433,4 +1449,3 @@ proc getProjectPath*(): string = discard
   ## Returns the path to the currently compiling project, not to
   ## be confused with ``system.currentSourcePath`` which returns
   ## the path of the current module.
-