mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-19 14:00:35 +00:00
Merge pull request #5877 from krux02/more-newLit
more and improved newLit procs in macros module
This commit is contained in:
@@ -432,20 +432,105 @@ proc newLit*(c: char): NimNode {.compileTime.} =
|
||||
result = newNimNode(nnkCharLit)
|
||||
result.intVal = ord(c)
|
||||
|
||||
proc newLit*(i: BiggestInt): NimNode {.compileTime.} =
|
||||
|
||||
proc newLit*(i: int): NimNode {.compileTime.} =
|
||||
## produces a new integer literal node.
|
||||
result = newNimNode(nnkIntLit)
|
||||
result.intVal = i
|
||||
|
||||
proc newLit*(i: int8): NimNode {.compileTime.} =
|
||||
## produces a new integer literal node.
|
||||
result = newNimNode(nnkInt8Lit)
|
||||
result.intVal = i
|
||||
|
||||
proc newLit*(i: int16): NimNode {.compileTime.} =
|
||||
## produces a new integer literal node.
|
||||
result = newNimNode(nnkInt16Lit)
|
||||
result.intVal = i
|
||||
|
||||
proc newLit*(i: int32): NimNode {.compileTime.} =
|
||||
## produces a new integer literal node.
|
||||
result = newNimNode(nnkInt32Lit)
|
||||
result.intVal = i
|
||||
|
||||
proc newLit*(i: int64): NimNode {.compileTime.} =
|
||||
## produces a new integer literal node.
|
||||
result = newNimNode(nnkInt64Lit)
|
||||
result.intVal = i
|
||||
|
||||
proc newLit*(i: uint): NimNode {.compileTime.} =
|
||||
## produces a new unsigned integer literal node.
|
||||
result = newNimNode(nnkUIntLit)
|
||||
result.intVal = BiggestInt(i)
|
||||
|
||||
proc newLit*(i: uint8): NimNode {.compileTime.} =
|
||||
## produces a new unsigned integer literal node.
|
||||
result = newNimNode(nnkUInt8Lit)
|
||||
result.intVal = BiggestInt(i)
|
||||
|
||||
proc newLit*(i: uint16): NimNode {.compileTime.} =
|
||||
## produces a new unsigned integer literal node.
|
||||
result = newNimNode(nnkUInt16Lit)
|
||||
result.intVal = BiggestInt(i)
|
||||
|
||||
proc newLit*(i: uint32): NimNode {.compileTime.} =
|
||||
## produces a new unsigned integer literal node.
|
||||
result = newNimNode(nnkUInt32Lit)
|
||||
result.intVal = BiggestInt(i)
|
||||
|
||||
proc newLit*(i: uint64): NimNode {.compileTime.} =
|
||||
## produces a new unsigned integer literal node.
|
||||
result = newNimNode(nnkUInt64Lit)
|
||||
result.intVal = BiggestInt(i)
|
||||
|
||||
proc newLit*(b: bool): NimNode {.compileTime.} =
|
||||
## produces a new boolean literal node.
|
||||
result = if b: bindSym"true" else: bindSym"false"
|
||||
|
||||
proc newLit*(f: BiggestFloat): NimNode {.compileTime.} =
|
||||
when false:
|
||||
# the float type is not really a distinct type as described in https://github.com/nim-lang/Nim/issues/5875
|
||||
proc newLit*(f: float): NimNode {.compileTime.} =
|
||||
## produces a new float literal node.
|
||||
result = newNimNode(nnkFloatLit)
|
||||
result.floatVal = f
|
||||
|
||||
proc newLit*(f: float32): NimNode {.compileTime.} =
|
||||
## produces a new float literal node.
|
||||
result = newNimNode(nnkFloatLit)
|
||||
result = newNimNode(nnkFloat32Lit)
|
||||
result.floatVal = f
|
||||
|
||||
proc newLit*(f: float64): NimNode {.compileTime.} =
|
||||
## produces a new float literal node.
|
||||
result = newNimNode(nnkFloat64Lit)
|
||||
result.floatVal = f
|
||||
|
||||
when compiles(float128):
|
||||
proc newLit*(f: float128): NimNode {.compileTime.} =
|
||||
## produces a new float literal node.
|
||||
result = newNimNode(nnkFloat128Lit)
|
||||
result.floatVal = f
|
||||
|
||||
proc newLit*(arg: object): NimNode {.compileTime.} =
|
||||
result = nnkObjConstr.newTree(arg.type.getTypeInst[1])
|
||||
for a, b in arg.fieldPairs:
|
||||
result.add nnkExprColonExpr.newTree( newIdentNode(a), newLit(b) )
|
||||
|
||||
proc newLit*[N,T](arg: array[N,T]): NimNode {.compileTime.} =
|
||||
result = nnkBracket.newTree
|
||||
for x in arg:
|
||||
result.add newLit(x)
|
||||
|
||||
proc newLit*[T](arg: seq[T]): NimNode {.compileTime.} =
|
||||
result = nnkBracket.newTree
|
||||
for x in arg:
|
||||
result.add newLit(x)
|
||||
result = nnkPrefix.newTree(bindSym"@", 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 newLit*(s: string): NimNode {.compileTime.} =
|
||||
## produces a new string literal node.
|
||||
result = newNimNode(nnkStrLit)
|
||||
|
||||
140
tests/macros/tnewlit.nim
Normal file
140
tests/macros/tnewlit.nim
Normal file
@@ -0,0 +1,140 @@
|
||||
import macros
|
||||
|
||||
type
|
||||
MyType = object
|
||||
a : int
|
||||
b : string
|
||||
|
||||
macro test_newLit_MyType: untyped =
|
||||
let mt = MyType(a: 123, b:"foobar")
|
||||
result = newLit(mt)
|
||||
|
||||
doAssert test_newLit_MyType == MyType(a: 123, b:"foobar")
|
||||
|
||||
macro test_newLit_array: untyped =
|
||||
let arr = [1,2,3,4,5]
|
||||
result = newLit(arr)
|
||||
|
||||
doAssert test_newLit_array == [1,2,3,4,5]
|
||||
|
||||
macro test_newLit_seq_int: untyped =
|
||||
let s: seq[int] = @[1,2,3,4,5]
|
||||
result = newLit(s)
|
||||
|
||||
block:
|
||||
let tmp: seq[int] = test_newLit_seq_int
|
||||
doAssert tmp == @[1,2,3,4,5]
|
||||
|
||||
macro test_newLit_seq_int8: untyped =
|
||||
let s: seq[int8] = @[1'i8,2,3,4,5]
|
||||
result = newLit(s)
|
||||
|
||||
block:
|
||||
let tmp: seq[int8] = test_newLit_seq_int8
|
||||
doAssert tmp == @[1'i8,2,3,4,5]
|
||||
|
||||
macro test_newLit_seq_int16: untyped =
|
||||
let s: seq[int16] = @[1'i16,2,3,4,5]
|
||||
result = newLit(s)
|
||||
|
||||
block:
|
||||
let tmp: seq[int16] = test_newLit_seq_int16
|
||||
doAssert tmp == @[1'i16,2,3,4,5]
|
||||
|
||||
macro test_newLit_seq_int32: untyped =
|
||||
let s: seq[int32] = @[1'i32,2,3,4,5]
|
||||
result = newLit(s)
|
||||
|
||||
block:
|
||||
let tmp: seq[int32] = test_newLit_seq_int32
|
||||
doAssert tmp == @[1'i32,2,3,4,5]
|
||||
|
||||
macro test_newLit_seq_int64: untyped =
|
||||
let s: seq[int64] = @[1'i64,2,3,4,5]
|
||||
result = newLit(s)
|
||||
|
||||
block:
|
||||
let tmp: seq[int64] = test_newLit_seq_int64
|
||||
doAssert tmp == @[1'i64,2,3,4,5]
|
||||
|
||||
macro test_newLit_seq_uint: untyped =
|
||||
let s: seq[uint] = @[1u,2,3,4,5]
|
||||
result = newLit(s)
|
||||
|
||||
block:
|
||||
let tmp: seq[uint] = test_newLit_seq_uint
|
||||
doAssert tmp == @[1u,2,3,4,5]
|
||||
|
||||
macro test_newLit_seq_uint8: untyped =
|
||||
let s: seq[uint8] = @[1'u8,2,3,4,5]
|
||||
result = newLit(s)
|
||||
|
||||
block:
|
||||
let tmp: seq[uint8] = test_newLit_seq_uint8
|
||||
doAssert tmp == @[1'u8,2,3,4,5]
|
||||
|
||||
macro test_newLit_seq_uint16: untyped =
|
||||
let s: seq[uint16] = @[1'u16,2,3,4,5]
|
||||
result = newLit(s)
|
||||
|
||||
block:
|
||||
let tmp: seq[uint16] = test_newLit_seq_uint16
|
||||
doAssert tmp == @[1'u16,2,3,4,5]
|
||||
|
||||
macro test_newLit_seq_uint32: untyped =
|
||||
let s: seq[uint32] = @[1'u32,2,3,4,5]
|
||||
result = newLit(s)
|
||||
|
||||
block:
|
||||
let tmp: seq[uint32] = test_newLit_seq_uint32
|
||||
doAssert tmp == @[1'u32,2,3,4,5]
|
||||
|
||||
macro test_newLit_seq_uint64: untyped =
|
||||
let s: seq[uint64] = @[1'u64,2,3,4,5]
|
||||
result = newLit(s)
|
||||
|
||||
block:
|
||||
let tmp: seq[uint64] = test_newLit_seq_uint64
|
||||
doAssert tmp == @[1'u64,2,3,4,5]
|
||||
|
||||
macro test_newLit_seq_float: untyped =
|
||||
let s: seq[float] = @[1.0, 2,3,4,5]
|
||||
result = newLit(s)
|
||||
|
||||
block:
|
||||
let tmp: seq[float] = test_newLit_seq_float
|
||||
doAssert tmp == @[1.0, 2,3,4,5]
|
||||
|
||||
macro test_newLit_seq_float32: untyped =
|
||||
let s: seq[float32] = @[1.0'f32, 2,3,4,5]
|
||||
result = newLit(s)
|
||||
|
||||
block:
|
||||
let tmp: seq[float32] = test_newLit_seq_float32
|
||||
doAssert tmp == @[1.0'f32, 2,3,4,5]
|
||||
|
||||
macro test_newLit_seq_float64: untyped =
|
||||
let s: seq[float64] = @[1.0'f64, 2,3,4,5]
|
||||
result = newLit(s)
|
||||
|
||||
block:
|
||||
let tmp: seq[float64] = test_newLit_seq_float64
|
||||
doAssert tmp == @[1.0'f64, 2,3,4,5]
|
||||
|
||||
macro test_newLit_tuple: untyped =
|
||||
let tup: tuple[a:int,b:string] = (a: 123, b: "223")
|
||||
result = newLit(tup)
|
||||
|
||||
doAssert test_newLit_tuple == (a: 123, b: "223")
|
||||
|
||||
type
|
||||
ComposedType = object
|
||||
mt: MyType
|
||||
arr: array[4,int]
|
||||
data: seq[byte]
|
||||
|
||||
macro test_newLit_ComposedType: untyped =
|
||||
let ct = ComposedType(mt: MyType(a: 123, b:"abc"), arr: [1,2,3,4], data: @[1.byte, 3, 7, 127])
|
||||
result = newLit(ct)
|
||||
|
||||
doAssert test_newLit_ComposedType == ComposedType(mt: MyType(a: 123, b:"abc"), arr: [1,2,3,4], data: @[1.byte, 3, 7, 127])
|
||||
Reference in New Issue
Block a user