aboutsummaryrefslogtreecommitdiff
path: root/src/lc.nim
diff options
context:
space:
mode:
authorkitsunyan2020-04-26 05:14:06 +0000
committerkitsunyan2020-04-26 05:14:06 +0000
commitc86dac32d38cf2be5faa6118911429a231de8805 (patch)
tree75e9f8fb5ee37d81a01edc35b9a4534f6b56c9cc /src/lc.nim
parent38a6904a948661802d2c744e0c2b9759e11fbd70 (diff)
Backport list comprehension
Diffstat (limited to 'src/lc.nim')
-rw-r--r--src/lc.nim30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/lc.nim b/src/lc.nim
new file mode 100644
index 0000000..0e75d28
--- /dev/null
+++ b/src/lc.nim
@@ -0,0 +1,30 @@
+import macros
+
+# backported list comprehension from earlier nim versions
+
+type ListComprehension = object
+var lc*: ListComprehension
+
+template `|`*(lc: ListComprehension, comp: untyped): untyped = lc
+
+macro `[]`*(lc: ListComprehension, comp, typ: untyped): untyped =
+ expectLen(comp, 3)
+ expectKind(comp, nnkInfix)
+ assert($comp[0] == "|")
+
+ result = newCall(newDotExpr(newIdentNode("result"), newIdentNode("add")), comp[1])
+
+ for i in countdown(comp[2].len - 1, 0):
+ let x = comp[2][i]
+ expectMinLen(x, 1)
+ if x[0].kind == nnkIdent and x[0].strVal == "<-":
+ expectLen(x, 3)
+ result = newNimNode(nnkForStmt).add(x[1], x[2], result)
+ else:
+ result = newIfStmt((x, result))
+
+ result = newNimNode(nnkCall).add(newNimNode(nnkPar)
+ .add(newNimNode(nnkLambda).add(newEmptyNode(), newEmptyNode(), newEmptyNode(),
+ newNimNode(nnkFormalParams).add(newNimNode(nnkBracketExpr).add(newIdentNode("seq"), typ)),
+ newEmptyNode(), newEmptyNode(), newStmtList(newAssignment(newIdentNode("result"),
+ newNimNode(nnkPrefix).add(newIdentNode("@"), newNimNode(nnkBracket))), result))))