From abe8ac196234f1cc10a2e0d197441b97faadf54c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Thu, 19 Jul 2018 16:26:07 +0200 Subject: [PATCH 1/5] or on NimNode --- lib/core/macros.nim | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/core/macros.nim b/lib/core/macros.nim index 2b0d74e6a5..e4424690a4 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. - From 48697dc5bb5d23761e9b1679f064d03de4e7bdc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Thu, 19 Jul 2018 22:42:48 +0200 Subject: [PATCH 2/5] added test case for `or` operator --- lib/core/macros.nim | 6 +++--- tests/macros/tmacro1.nim | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/core/macros.nim b/lib/core/macros.nim index e4424690a4..a3b02de5f5 100644 --- a/lib/core/macros.nim +++ b/lib/core/macros.nim @@ -176,7 +176,7 @@ 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 = +template `or`*(x, y: 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 @@ -186,11 +186,11 @@ template `or`*(a,b: NimNode): NimNode = ## ## let node = mightBeEmpty() or mightAlsoBeEmpty() or fallbackNode - let arg = a + let arg = x if arg != nil and arg.kind != nnkEmpty: arg else: - b + y proc add*(father, child: NimNode): NimNode {.magic: "NAdd", discardable, noSideEffect, locks: 0.} diff --git a/tests/macros/tmacro1.nim b/tests/macros/tmacro1.nim index ac2bf90945..5388e861c0 100644 --- a/tests/macros/tmacro1.nim +++ b/tests/macros/tmacro1.nim @@ -79,3 +79,17 @@ static: assert fooSym.kind in {nnkOpenSymChoice, nnkClosedSymChoice} assert fooSym.eqIdent("fOO") assertNot fooSym.eqIdent("bar") + + var empty: NimNode + var myLit = newLit("str") + + assert( (empty or myLit) == myLit ) + + empty = newEmptyNode() + + assert( (empty or myLit) == myLit ) + + proc bottom(): NimNode = + quit("may not be evaluated") + + assert( (myLit or bottom()) == myLit ) From 358b5225df88a439e6654ef47ddfcbc1669fc6ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Sun, 22 Jul 2018 19:56:48 +0200 Subject: [PATCH 3/5] fixed comment --- lib/core/macros.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/core/macros.nim b/lib/core/macros.nim index a3b02de5f5..6720a3f992 100644 --- a/lib/core/macros.nim +++ b/lib/core/macros.nim @@ -177,8 +177,8 @@ proc `[]=`*(n: NimNode, i: BackwardsIndex, child: NimNode) = n[n.len - i.int] = child template `or`*(x, y: 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 + ## Evalutuate ``x`` and when it is not an empty node, return return + ## it. Otherwise evaluate to ``y``. Can be used to chain several ## expressions that evaluates to the first expression that is not ## empty. ## From 016e0b1aac1a2287afc2dbd61920cd826ef18f74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Thu, 18 Oct 2018 15:40:50 +0200 Subject: [PATCH 4/5] fix typo --- lib/core/macros.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/core/macros.nim b/lib/core/macros.nim index 6720a3f992..0ae3a811f7 100644 --- a/lib/core/macros.nim +++ b/lib/core/macros.nim @@ -177,7 +177,7 @@ proc `[]=`*(n: NimNode, i: BackwardsIndex, child: NimNode) = n[n.len - i.int] = child template `or`*(x, y: NimNode): NimNode = - ## Evalutuate ``x`` and when it is not an empty node, return return + ## Evaluate ``x`` and when it is not an empty node, return ## it. Otherwise evaluate to ``y``. Can be used to chain several ## expressions that evaluates to the first expression that is not ## empty. From f9bc4d014a84ae0aad7b174e6987451e6204caa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Thu, 18 Oct 2018 16:07:15 +0200 Subject: [PATCH 5/5] changelog entry --- changelog.md | 2 ++ lib/core/macros.nim | 3 +-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/changelog.md b/changelog.md index 1f2655d7ad..9a56717571 100644 --- a/changelog.md +++ b/changelog.md @@ -25,6 +25,8 @@ - Added `split`, `splitWhitespace`, `size`, `alignLeft`, `align`, `strip`, `repeat` procs and iterators to `unicode.nim`. +- Added `or` for `NimNode` in `macros`. + ### Library changes diff --git a/lib/core/macros.nim b/lib/core/macros.nim index 0ae3a811f7..79195966ed 100644 --- a/lib/core/macros.nim +++ b/lib/core/macros.nim @@ -179,8 +179,7 @@ proc `[]=`*(n: NimNode, i: BackwardsIndex, child: NimNode) = template `or`*(x, y: NimNode): NimNode = ## Evaluate ``x`` and when it is not an empty node, return ## it. Otherwise evaluate to ``y``. Can be used to chain several - ## expressions that evaluates to the first expression that is not - ## empty. + ## expressions to get the first expression that is not empty. ## ## .. code-block:: nim ##