avoid asArray macros import when using nimscript

This commit is contained in:
Fredrik Høisæther Rasch 2017-10-31 12:33:35 +01:00 • committed by Andreas Rumpf
commit 70f3226836

View file

@ -20,8 +20,6 @@
include "system/inclrtl" include "system/inclrtl"
import macros
when not defined(nimhygiene): when not defined(nimhygiene):
{.pragma: dirty.} {.pragma: dirty.}
@ -706,28 +704,30 @@ template newSeqWith*(len: int, init: untyped): untyped =
result[i] = init result[i] = init
result result
macro asArray*(values: typed, targetType: typedesc): untyped = when not defined(nimscript):
## converts a static array to an array of type ``targetType`` by converting import macros
## each value in the array to the specified type. macro asArray*(values: typed, targetType: typedesc): untyped =
## ## converts a static array to an array of type ``targetType`` by converting
## Example: ## each value in the array to the specified type.
## ##
## .. code-block:: ## Example:
## let x = asArray([0.1, 1.2, 2.3, 3.4], int) ##
## doAssert x is array[4, int] ## .. code-block::
## ## let x = asArray([0.1, 1.2, 2.3, 3.4], int)
## Short notation for: ## doAssert x is array[4, int]
## ##
## .. code-block:: ## Short notation for:
## let x = [(0.1).int, (1.2).int, (2.3).int, (3.4).int] ##
let tNode = getType(targetType)[1] ## .. code-block::
values.expectKind(nnkBracket) ## let x = [(0.1).int, (1.2).int, (2.3).int, (3.4).int]
result = newNimNode(nnkBracket, lineInfoFrom=values) let tNode = getType(targetType)[1]
for i in 0 ..< len(values): values.expectKind(nnkBracket)
var dot = newNimNode(nnkDotExpr, lineInfoFrom=values[i]) result = newNimNode(nnkBracket, lineInfoFrom=values)
dot.add newPar(values[i]) for i in 0 ..< len(values):
dot.add tNode var dot = newNimNode(nnkDotExpr, lineInfoFrom=values[i])
result.add dot dot.add newPar(values[i])
dot.add tNode
result.add dot
when isMainModule: when isMainModule:
import strutils import strutils
@ -1017,7 +1017,7 @@ when isMainModule:
seq2D[0][1] = true seq2D[0][1] = true
doAssert seq2D == @[@[true, true], @[true, false], @[false, false], @[false, false]] doAssert seq2D == @[@[true, true], @[true, false], @[false, false], @[false, false]]
block: # asArray tests when not defined(nimscript): # asArray tests
let x = asArray([1.2, 2.3, 3.4, 4.5], int) let x = asArray([1.2, 2.3, 3.4, 4.5], int)
doAssert x is array[4, int] doAssert x is array[4, int]