From c86dac32d38cf2be5faa6118911429a231de8805 Mon Sep 17 00:00:00 2001 From: kitsunyan Date: Sun, 26 Apr 2020 08:14:06 +0300 Subject: Backport list comprehension --- src/args.nim | 2 +- src/aur.nim | 2 +- src/common.nim | 2 +- src/feature/syncinfo.nim | 2 +- src/feature/syncinstall.nim | 2 +- src/format.nim | 2 +- src/lc.nim | 30 ++++++++++++++++++++++++++++++ src/main.nim | 2 +- src/package.nim | 3 ++- src/pacman.nim | 2 +- 10 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 src/lc.nim diff --git a/src/args.nim b/src/args.nim index fd03ee0..87fbaf3 100644 --- a/src/args.nim +++ b/src/args.nim @@ -1,6 +1,6 @@ import options, os, posix, sequtils, sets, strutils, sugar, - utils + lc, utils type ArgumentType* {.pure.} = enum diff --git a/src/aur.nim b/src/aur.nim index 399183c..cd1c744 100644 --- a/src/aur.nim +++ b/src/aur.nim @@ -1,6 +1,6 @@ import json, lists, options, re, sequtils, sets, strutils, sugar, tables, - package, utils, + lc, package, utils, "wrapper/curl" type diff --git a/src/common.nim b/src/common.nim index 8db3a79..3f9013c 100644 --- a/src/common.nim +++ b/src/common.nim @@ -1,6 +1,6 @@ import options, os, osproc, posix, sequtils, sets, strutils, sugar, tables, - args, config, format, lists, package, pacman, utils, + args, config, format, lc, lists, package, pacman, utils, "wrapper/alpm" type diff --git a/src/feature/syncinfo.nim b/src/feature/syncinfo.nim index 4f758fe..f4c6f7a 100644 --- a/src/feature/syncinfo.nim +++ b/src/feature/syncinfo.nim @@ -1,6 +1,6 @@ import options, posix, sequtils, strutils, sugar, tables, - "../args", "../aur", "../common", "../config", "../format", "../package", + "../args", "../aur", "../common", "../config", "../format", "../lc", "../package", "../pacman", "../utils", "../wrapper/alpm" diff --git a/src/feature/syncinstall.nim b/src/feature/syncinstall.nim index 7aba043..0a9170d 100644 --- a/src/feature/syncinstall.nim +++ b/src/feature/syncinstall.nim @@ -1,6 +1,6 @@ import algorithm, options, os, posix, sequtils, sets, strutils, sugar, tables, - "../args", "../aur", "../config", "../common", "../format", "../lists", "../package", + "../args", "../aur", "../config", "../common", "../format", "../lc", "../lists", "../package", "../pacman", "../utils", "../wrapper/alpm" diff --git a/src/format.nim b/src/format.nim index 2ac39dc..e8645b8 100644 --- a/src/format.nim +++ b/src/format.nim @@ -1,6 +1,6 @@ import macros, options, posix, sequtils, strutils, sugar, times, unicode, - utils + lc, utils type PackageLineFormat* = tuple[ 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)))) diff --git a/src/main.nim b/src/main.nim index 92e8887..f815f8f 100644 --- a/src/main.nim +++ b/src/main.nim @@ -1,6 +1,6 @@ import options, os, posix, re, sequtils, strutils, sugar, - args, config, format, pacman, utils + args, config, format, lc, pacman, utils import "feature/localquery", diff --git a/src/package.nim b/src/package.nim index 5b87184..b4153db 100644 --- a/src/package.nim +++ b/src/package.nim @@ -1,5 +1,6 @@ import - options, os, re, sequtils, sets, strutils, sugar, tables, utils, + options, os, re, sequtils, sets, strutils, sugar, tables, + lc, utils, "wrapper/alpm" type diff --git a/src/pacman.nim b/src/pacman.nim index 857581f..5766ee2 100644 --- a/src/pacman.nim +++ b/src/pacman.nim @@ -1,6 +1,6 @@ import macros, options, posix, sequtils, sets, strutils, sugar, tables, - args, config, utils + args, config, lc, utils type OpGroup* {.pure.} = enum -- cgit v1.2.3-70-g09d2