mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-30 01:44:37 +00:00
Update tuple newLit
This commit is contained in:
@@ -760,7 +760,7 @@ proc newLit*(arg: enum): NimNode {.compileTime.} =
|
||||
proc newLit*[N,T](arg: array[N,T]): NimNode {.compileTime.}
|
||||
proc newLit*[T](arg: seq[T]): NimNode {.compileTime.}
|
||||
proc newLit*[T](s: set[T]): NimNode {.compileTime.}
|
||||
proc newLit*(arg: tuple): NimNode {.compileTime.}
|
||||
proc newLit*[T: tuple](arg: T): NimNode {.compileTime.}
|
||||
|
||||
proc newLit*(arg: object): NimNode {.compileTime.} =
|
||||
result = nnkObjConstr.newTree(arg.type.getTypeInst[1])
|
||||
@@ -800,10 +800,17 @@ proc newLit*[T](s: set[T]): NimNode {.compileTime.} =
|
||||
var typ = getTypeInst(typeof(s))[1]
|
||||
result = newCall(typ,result)
|
||||
|
||||
proc newLit*(arg: tuple): NimNode {.compileTime.} =
|
||||
result = nnkPar.newTree
|
||||
for a,b in arg.fieldPairs:
|
||||
result.add nnkExprColonExpr.newTree(newIdentNode(a), newLit(b))
|
||||
proc isNamedTuple(T: typedesc): bool {.magic: "TypeTrait".}
|
||||
## See typetraits.isNamedTuple
|
||||
|
||||
proc newLit*[T: tuple](arg: T): NimNode {.compileTime.} =
|
||||
result = nnkTupleConstr.newTree
|
||||
when isNamedTuple(T):
|
||||
for a,b in arg.fieldPairs:
|
||||
result.add nnkExprColonExpr.newTree(newIdentNode(a), newLit(b))
|
||||
else:
|
||||
for b in arg.fields:
|
||||
result.add newLit(b)
|
||||
|
||||
proc nestList*(op: NimNode; pack: NimNode): NimNode {.compileTime.} =
|
||||
## Nests the list `pack` into a tree of call expressions:
|
||||
|
||||
@@ -194,3 +194,35 @@ static:
|
||||
doAssert(v == a)
|
||||
|
||||
echo "macrocache ok"
|
||||
|
||||
block tupleNewLitTests:
|
||||
macro t0(): untyped =
|
||||
result = newLit(())
|
||||
doAssert t0 == ()
|
||||
macro t1(): untyped =
|
||||
result = newLit((5,))
|
||||
doAssert t1 == (5,)
|
||||
macro t2(): untyped =
|
||||
result = newLit((a: 5))
|
||||
doAssert t2 == (a: 5)
|
||||
macro t3(): untyped =
|
||||
result = newLit((5, "5"))
|
||||
doAssert t3 == (5, "5")
|
||||
macro t4(): untyped =
|
||||
result = newLit((a: 5, b: "5"))
|
||||
doAssert t4 == (a: 5, b: "5")
|
||||
macro t5(): untyped =
|
||||
result = newLit(@[(5,)])
|
||||
doAssert t5 == @[(5,)]
|
||||
macro t6(): untyped =
|
||||
result = newLit(@[(a: 5)])
|
||||
doAssert t6 == @[(a: 5)]
|
||||
macro t7(): untyped =
|
||||
result = newLit(@[(5, "5")])
|
||||
doAssert t7 == @[(5, "5")]
|
||||
macro t8(): untyped =
|
||||
result = newLit(@[(a: 5, b: "5")])
|
||||
doAssert t8 == @[(a: 5, b: "5")]
|
||||
macro t9(): untyped =
|
||||
result = newLit(@[(a: (5, 6), b: ())])
|
||||
doAssert t9 == @[(a: (5, 6), b: ())]
|
||||
|
||||
Reference in New Issue
Block a user