From 7744c6ce3172856b0fc0c6a04f7a5fbd956da8be Mon Sep 17 00:00:00 2001 From: Fredrik Høisæther Rasch Date: Thu, 2 Nov 2017 22:54:03 +0100 Subject: Moving asArray to future module As per [suggestion](https://github.com/nim-lang/Nim/pull/6640#issuecomment-341565453) made by @Araq --- lib/pure/future.nim | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'lib/pure/future.nim') diff --git a/lib/pure/future.nim b/lib/pure/future.nim index f6592df71..be62f8879 100644 --- a/lib/pure/future.nim +++ b/lib/pure/future.nim @@ -197,4 +197,32 @@ macro dump*(x: typed): untyped = let s = x.toStrLit let r = quote do: debugEcho `s`, " = ", `x` - return r \ No newline at end of file + return r + +macro asArray*(targetType: typedesc, values: typed): untyped = + ## applies a type conversion to each of the elements in the specified + ## array literal. Each element is converted to the ``targetType`` type.. + ## + ## Example: + ## + ## .. code-block:: + ## let x = asArray(int, [0.1, 1.2, 2.3, 3.4]) + ## doAssert x is array[4, int] + ## + ## Short notation for: + ## + ## .. code-block:: + ## let x = [(0.1).int, (1.2).int, (2.3).int, (3.4).int] + let tNode = getType(targetType)[1] + values.expectKind(nnkBracket) + result = newNimNode(nnkBracket, lineInfoFrom=values) + for i in 0 ..< len(values): + var dot = newNimNode(nnkDotExpr, lineInfoFrom=values[i]) + dot.add newPar(values[i]) + dot.add tNode + result.add dot + +when isMainModule: + block: # asArray tests + let x = asArray(int, [1.2, 2.3, 3.4, 4.5]) + doAssert x is array[4, int] -- cgit 1.4.1-2-gfad0 From a312693250e0e523814579bd6ef756dde7ea03ee Mon Sep 17 00:00:00 2001 From: Fredrik Høisæther Rasch Date: Mon, 6 Nov 2017 14:07:42 +0100 Subject: asArray poduces a cal expression instead of a dot expression --- lib/pure/future.nim | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lib/pure/future.nim') diff --git a/lib/pure/future.nim b/lib/pure/future.nim index be62f8879..f88ff7738 100644 --- a/lib/pure/future.nim +++ b/lib/pure/future.nim @@ -217,10 +217,10 @@ macro asArray*(targetType: typedesc, values: typed): untyped = values.expectKind(nnkBracket) result = newNimNode(nnkBracket, lineInfoFrom=values) for i in 0 ..< len(values): - var dot = newNimNode(nnkDotExpr, lineInfoFrom=values[i]) - dot.add newPar(values[i]) - dot.add tNode - result.add dot + var call = newNimNode(nnkCall, lineInfoFrom=values[i]) + call.add tNode + call.add values[i] + result.add call when isMainModule: block: # asArray tests -- cgit 1.4.1-2-gfad0 From e334a257fa97b6d6da88b0231346677e9f70933d Mon Sep 17 00:00:00 2001 From: Fredrik Høisæther Rasch Date: Mon, 6 Nov 2017 14:19:41 +0100 Subject: asarray targetType parameter does not need to be a typedesc. --- lib/pure/future.nim | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'lib/pure/future.nim') diff --git a/lib/pure/future.nim b/lib/pure/future.nim index f88ff7738..de074bd52 100644 --- a/lib/pure/future.nim +++ b/lib/pure/future.nim @@ -199,7 +199,7 @@ macro dump*(x: typed): untyped = debugEcho `s`, " = ", `x` return r -macro asArray*(targetType: typedesc, values: typed): untyped = +macro asArray*(targetType: untyped, values: typed): untyped = ## applies a type conversion to each of the elements in the specified ## array literal. Each element is converted to the ``targetType`` type.. ## @@ -213,16 +213,18 @@ macro asArray*(targetType: typedesc, values: typed): untyped = ## ## .. code-block:: ## let x = [(0.1).int, (1.2).int, (2.3).int, (3.4).int] - let tNode = getType(targetType)[1] values.expectKind(nnkBracket) result = newNimNode(nnkBracket, lineInfoFrom=values) for i in 0 ..< len(values): var call = newNimNode(nnkCall, lineInfoFrom=values[i]) - call.add tNode + call.add targetType call.add values[i] result.add call + echo result.repr() when isMainModule: block: # asArray tests let x = asArray(int, [1.2, 2.3, 3.4, 4.5]) doAssert x is array[4, int] + let y = asArray(`$`, [1.2, 2.3, 3.4, 4.5]) + doAssert y is array[4, string] -- cgit 1.4.1-2-gfad0 From e56db3561068cb1cf15ab3d055b0722d863df117 Mon Sep 17 00:00:00 2001 From: Fredrik Høisæther Rasch Date: Wed, 8 Nov 2017 11:09:32 +0100 Subject: Move asArray and ListComprehension to new sugar module --- lib/pure/future.nim | 87 ----------------------------------------------- lib/pure/sugar.nim | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 87 deletions(-) create mode 100644 lib/pure/sugar.nim (limited to 'lib/pure/future.nim') diff --git a/lib/pure/future.nim b/lib/pure/future.nim index de074bd52..3a068666e 100644 --- a/lib/pure/future.nim +++ b/lib/pure/future.nim @@ -122,63 +122,6 @@ macro `->`*(p, b: untyped): untyped = result = createProcType(p, b) -type ListComprehension = object -var lc*: ListComprehension - -macro `[]`*(lc: ListComprehension, comp, typ: untyped): untyped = - ## List comprehension, returns a sequence. `comp` is the actual list - ## comprehension, for example ``x | (x <- 1..10, x mod 2 == 0)``. `typ` is - ## the type that will be stored inside the result seq. - ## - ## .. code-block:: nim - ## - ## echo lc[x | (x <- 1..10, x mod 2 == 0), int] - ## - ## const n = 20 - ## echo lc[(x,y,z) | (x <- 1..n, y <- x..n, z <- y..n, x*x + y*y == z*z), - ## tuple[a,b,c: int]] - - expectLen(comp, 3) - expectKind(comp, nnkInfix) - expectKind(comp[0], nnkIdent) - assert($comp[0].ident == "|") - - 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].ident == "<-": - 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)))) - - macro dump*(x: typed): untyped = ## Dumps the content of an expression, useful for debugging. ## It accepts any expression and prints a textual representation @@ -198,33 +141,3 @@ macro dump*(x: typed): untyped = let r = quote do: debugEcho `s`, " = ", `x` return r - -macro asArray*(targetType: untyped, values: typed): untyped = - ## applies a type conversion to each of the elements in the specified - ## array literal. Each element is converted to the ``targetType`` type.. - ## - ## Example: - ## - ## .. code-block:: - ## let x = asArray(int, [0.1, 1.2, 2.3, 3.4]) - ## doAssert x is array[4, int] - ## - ## Short notation for: - ## - ## .. code-block:: - ## let x = [(0.1).int, (1.2).int, (2.3).int, (3.4).int] - values.expectKind(nnkBracket) - result = newNimNode(nnkBracket, lineInfoFrom=values) - for i in 0 ..< len(values): - var call = newNimNode(nnkCall, lineInfoFrom=values[i]) - call.add targetType - call.add values[i] - result.add call - echo result.repr() - -when isMainModule: - block: # asArray tests - let x = asArray(int, [1.2, 2.3, 3.4, 4.5]) - doAssert x is array[4, int] - let y = asArray(`$`, [1.2, 2.3, 3.4, 4.5]) - doAssert y is array[4, string] diff --git a/lib/pure/sugar.nim b/lib/pure/sugar.nim new file mode 100644 index 000000000..6a5ea1c89 --- /dev/null +++ b/lib/pure/sugar.nim @@ -0,0 +1,98 @@ +# +# +# Nim's Runtime Library +# (c) Copyright 2017 Dominik Picheta +# +# See the file "copying.txt", included in this +# distribution, for details about the copyright. +# + +## This module procs, operators and macros that provide *syntactic sugar* for +## the nim language. + +import macros + +type ListComprehension = object +var lc*: ListComprehension + +macro `[]`*(lc: ListComprehension, comp, typ: untyped): untyped = + ## List comprehension, returns a sequence. `comp` is the actual list + ## comprehension, for example ``x | (x <- 1..10, x mod 2 == 0)``. `typ` is + ## the type that will be stored inside the result seq. + ## + ## .. code-block:: nim + ## + ## echo lc[x | (x <- 1..10, x mod 2 == 0), int] + ## + ## const n = 20 + ## echo lc[(x,y,z) | (x <- 1..n, y <- x..n, z <- y..n, x*x + y*y == z*z), + ## tuple[a,b,c: int]] + + expectLen(comp, 3) + expectKind(comp, nnkInfix) + expectKind(comp[0], nnkIdent) + assert($comp[0].ident == "|") + + 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].ident == "<-": + 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)))) + +macro asArray*(targetType: untyped, values: typed): untyped = + ## applies a type conversion to each of the elements in the specified + ## array literal. Each element is converted to the ``targetType`` type.. + ## + ## Example: + ## + ## .. code-block:: + ## let x = asArray(int, [0.1, 1.2, 2.3, 3.4]) + ## doAssert x is array[4, int] + ## + ## Short notation for: + ## + ## .. code-block:: + ## let x = [(0.1).int, (1.2).int, (2.3).int, (3.4).int] + values.expectKind(nnkBracket) + result = newNimNode(nnkBracket, lineInfoFrom=values) + for i in 0 ..< len(values): + var call = newNimNode(nnkCall, lineInfoFrom=values[i]) + call.add targetType + call.add values[i] + result.add call + +when isMainModule: + block: # asArray tests + let x = asArray(int, [1.2, 2.3, 3.4, 4.5]) + doAssert x is array[4, int] + let y = asArray(`$`, [1.2, 2.3, 3.4, 4.5]) + doAssert y is array[4, string] -- cgit 1.4.1-2-gfad0 From bd2f4d18525d9242183d17ec33730c684af6d698 Mon Sep 17 00:00:00 2001 From: Fredrik Høisæther Rasch Date: Wed, 8 Nov 2017 15:15:16 +0100 Subject: Revert "Move asArray and ListComprehension to new sugar module" This reverts commit 9bdee897747d3345d1c7722b3440d7cf38460f40. --- lib/pure/future.nim | 87 +++++++++++++++++++++++++++++++++++++++++++++++ lib/pure/sugar.nim | 98 ----------------------------------------------------- 2 files changed, 87 insertions(+), 98 deletions(-) delete mode 100644 lib/pure/sugar.nim (limited to 'lib/pure/future.nim') diff --git a/lib/pure/future.nim b/lib/pure/future.nim index 3a068666e..de074bd52 100644 --- a/lib/pure/future.nim +++ b/lib/pure/future.nim @@ -122,6 +122,63 @@ macro `->`*(p, b: untyped): untyped = result = createProcType(p, b) +type ListComprehension = object +var lc*: ListComprehension + +macro `[]`*(lc: ListComprehension, comp, typ: untyped): untyped = + ## List comprehension, returns a sequence. `comp` is the actual list + ## comprehension, for example ``x | (x <- 1..10, x mod 2 == 0)``. `typ` is + ## the type that will be stored inside the result seq. + ## + ## .. code-block:: nim + ## + ## echo lc[x | (x <- 1..10, x mod 2 == 0), int] + ## + ## const n = 20 + ## echo lc[(x,y,z) | (x <- 1..n, y <- x..n, z <- y..n, x*x + y*y == z*z), + ## tuple[a,b,c: int]] + + expectLen(comp, 3) + expectKind(comp, nnkInfix) + expectKind(comp[0], nnkIdent) + assert($comp[0].ident == "|") + + 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].ident == "<-": + 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)))) + + macro dump*(x: typed): untyped = ## Dumps the content of an expression, useful for debugging. ## It accepts any expression and prints a textual representation @@ -141,3 +198,33 @@ macro dump*(x: typed): untyped = let r = quote do: debugEcho `s`, " = ", `x` return r + +macro asArray*(targetType: untyped, values: typed): untyped = + ## applies a type conversion to each of the elements in the specified + ## array literal. Each element is converted to the ``targetType`` type.. + ## + ## Example: + ## + ## .. code-block:: + ## let x = asArray(int, [0.1, 1.2, 2.3, 3.4]) + ## doAssert x is array[4, int] + ## + ## Short notation for: + ## + ## .. code-block:: + ## let x = [(0.1).int, (1.2).int, (2.3).int, (3.4).int] + values.expectKind(nnkBracket) + result = newNimNode(nnkBracket, lineInfoFrom=values) + for i in 0 ..< len(values): + var call = newNimNode(nnkCall, lineInfoFrom=values[i]) + call.add targetType + call.add values[i] + result.add call + echo result.repr() + +when isMainModule: + block: # asArray tests + let x = asArray(int, [1.2, 2.3, 3.4, 4.5]) + doAssert x is array[4, int] + let y = asArray(`$`, [1.2, 2.3, 3.4, 4.5]) + doAssert y is array[4, string] diff --git a/lib/pure/sugar.nim b/lib/pure/sugar.nim deleted file mode 100644 index 6a5ea1c89..000000000 --- a/lib/pure/sugar.nim +++ /dev/null @@ -1,98 +0,0 @@ -# -# -# Nim's Runtime Library -# (c) Copyright 2017 Dominik Picheta -# -# See the file "copying.txt", included in this -# distribution, for details about the copyright. -# - -## This module procs, operators and macros that provide *syntactic sugar* for -## the nim language. - -import macros - -type ListComprehension = object -var lc*: ListComprehension - -macro `[]`*(lc: ListComprehension, comp, typ: untyped): untyped = - ## List comprehension, returns a sequence. `comp` is the actual list - ## comprehension, for example ``x | (x <- 1..10, x mod 2 == 0)``. `typ` is - ## the type that will be stored inside the result seq. - ## - ## .. code-block:: nim - ## - ## echo lc[x | (x <- 1..10, x mod 2 == 0), int] - ## - ## const n = 20 - ## echo lc[(x,y,z) | (x <- 1..n, y <- x..n, z <- y..n, x*x + y*y == z*z), - ## tuple[a,b,c: int]] - - expectLen(comp, 3) - expectKind(comp, nnkInfix) - expectKind(comp[0], nnkIdent) - assert($comp[0].ident == "|") - - 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].ident == "<-": - 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)))) - -macro asArray*(targetType: untyped, values: typed): untyped = - ## applies a type conversion to each of the elements in the specified - ## array literal. Each element is converted to the ``targetType`` type.. - ## - ## Example: - ## - ## .. code-block:: - ## let x = asArray(int, [0.1, 1.2, 2.3, 3.4]) - ## doAssert x is array[4, int] - ## - ## Short notation for: - ## - ## .. code-block:: - ## let x = [(0.1).int, (1.2).int, (2.3).int, (3.4).int] - values.expectKind(nnkBracket) - result = newNimNode(nnkBracket, lineInfoFrom=values) - for i in 0 ..< len(values): - var call = newNimNode(nnkCall, lineInfoFrom=values[i]) - call.add targetType - call.add values[i] - result.add call - -when isMainModule: - block: # asArray tests - let x = asArray(int, [1.2, 2.3, 3.4, 4.5]) - doAssert x is array[4, int] - let y = asArray(`$`, [1.2, 2.3, 3.4, 4.5]) - doAssert y is array[4, string] -- cgit 1.4.1-2-gfad0 From b02ecda5a052b828d8baed3ba6ff1ce58b660a44 Mon Sep 17 00:00:00 2001 From: Fredrik Høisæther Rasch Date: Wed, 8 Nov 2017 15:18:45 +0100 Subject: Move asArray macro back to sequtils This reverts commit 72f653c2daa5c629ed8a57a4f53dcef56432aa26. --- lib/pure/collections/sequtils.nim | 30 ++++++++++++++++++++++++++++++ lib/pure/future.nim | 30 ------------------------------ 2 files changed, 30 insertions(+), 30 deletions(-) (limited to 'lib/pure/future.nim') diff --git a/lib/pure/collections/sequtils.nim b/lib/pure/collections/sequtils.nim index 0eb8e6704..ee89ce17b 100644 --- a/lib/pure/collections/sequtils.nim +++ b/lib/pure/collections/sequtils.nim @@ -20,6 +20,8 @@ include "system/inclrtl" +import macros + when not defined(nimhygiene): {.pragma: dirty.} @@ -704,6 +706,28 @@ template newSeqWith*(len: int, init: untyped): untyped = result[i] = init result +macro asArray*(targetType: untyped, values: typed): untyped = + ## applies a type conversion to each of the elements in the specified + ## array literal. Each element is converted to the ``targetType`` type.. + ## + ## Example: + ## + ## .. code-block:: + ## let x = asArray(int, [0.1, 1.2, 2.3, 3.4]) + ## doAssert x is array[4, int] + ## + ## Short notation for: + ## + ## .. code-block:: + ## let x = [(0.1).int, (1.2).int, (2.3).int, (3.4).int] + values.expectKind(nnkBracket) + result = newNimNode(nnkBracket, lineInfoFrom=values) + for i in 0 ..< len(values): + var call = newNimNode(nnkCall, lineInfoFrom=values[i]) + call.add targetType + call.add values[i] + result.add call + when isMainModule: import strutils block: # concat test @@ -992,5 +1016,11 @@ when isMainModule: seq2D[0][1] = true doAssert seq2D == @[@[true, true], @[true, false], @[false, false], @[false, false]] + block: # asArray tests + let x = asArray(int, [1.2, 2.3, 3.4, 4.5]) + doAssert x is array[4, int] + let y = asArray(`$`, [1.2, 2.3, 3.4, 4.5]) + doAssert y is array[4, string] + when not defined(testing): echo "Finished doc tests" diff --git a/lib/pure/future.nim b/lib/pure/future.nim index de074bd52..1a3ab688d 100644 --- a/lib/pure/future.nim +++ b/lib/pure/future.nim @@ -198,33 +198,3 @@ macro dump*(x: typed): untyped = let r = quote do: debugEcho `s`, " = ", `x` return r - -macro asArray*(targetType: untyped, values: typed): untyped = - ## applies a type conversion to each of the elements in the specified - ## array literal. Each element is converted to the ``targetType`` type.. - ## - ## Example: - ## - ## .. code-block:: - ## let x = asArray(int, [0.1, 1.2, 2.3, 3.4]) - ## doAssert x is array[4, int] - ## - ## Short notation for: - ## - ## .. code-block:: - ## let x = [(0.1).int, (1.2).int, (2.3).int, (3.4).int] - values.expectKind(nnkBracket) - result = newNimNode(nnkBracket, lineInfoFrom=values) - for i in 0 ..< len(values): - var call = newNimNode(nnkCall, lineInfoFrom=values[i]) - call.add targetType - call.add values[i] - result.add call - echo result.repr() - -when isMainModule: - block: # asArray tests - let x = asArray(int, [1.2, 2.3, 3.4, 4.5]) - doAssert x is array[4, int] - let y = asArray(`$`, [1.2, 2.3, 3.4, 4.5]) - doAssert y is array[4, string] -- cgit 1.4.1-2-gfad0