tmacro1.nim 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import macros
  2. macro test*(a: untyped): untyped =
  3. var nodes: tuple[a, b: int]
  4. nodes.a = 4
  5. nodes[1] = 45
  6. type
  7. TTypeEx = object
  8. x, y: int
  9. case b: bool
  10. of false: nil
  11. of true: z: float
  12. var t: TTypeEx
  13. t.b = true
  14. t.z = 4.5
  15. test:
  16. "hi"
  17. template assertNot(arg: untyped): untyped =
  18. assert(not(arg))
  19. proc foo(arg: int): void =
  20. discard
  21. proc foo(arg: float): void =
  22. discard
  23. static:
  24. ## test eqIdent
  25. let a = "abc_def"
  26. let b = "abcDef"
  27. let c = "AbcDef"
  28. let d = nnkBracketExpr.newTree() # not an identifier at all
  29. assert eqIdent( a , b )
  30. assert eqIdent(newIdentNode(a), b )
  31. assert eqIdent( a , newIdentNode(b))
  32. assert eqIdent(newIdentNode(a), newIdentNode(b))
  33. assert eqIdent( a , b )
  34. assert eqIdent(genSym(nskLet, a), b )
  35. assert eqIdent( a , genSym(nskLet, b))
  36. assert eqIdent(genSym(nskLet, a), genSym(nskLet, b))
  37. assert eqIdent(newIdentNode( a), newIdentNode( b))
  38. assert eqIdent(genSym(nskLet, a), newIdentNode( b))
  39. assert eqIdent(newIdentNode( a), genSym(nskLet, b))
  40. assert eqIdent(genSym(nskLet, a), genSym(nskLet, b))
  41. assertNot eqIdent( c , b )
  42. assertNot eqIdent(newIdentNode(c), b )
  43. assertNot eqIdent( c , newIdentNode(b))
  44. assertNot eqIdent(newIdentNode(c), newIdentNode(b))
  45. assertNot eqIdent( c , b )
  46. assertNot eqIdent(genSym(nskLet, c), b )
  47. assertNot eqIdent( c , genSym(nskLet, b))
  48. assertNot eqIdent(genSym(nskLet, c), genSym(nskLet, b))
  49. assertNot eqIdent(newIdentNode( c), newIdentNode( b))
  50. assertNot eqIdent(genSym(nskLet, c), newIdentNode( b))
  51. assertNot eqIdent(newIdentNode( c), genSym(nskLet, b))
  52. assertNot eqIdent(genSym(nskLet, c), genSym(nskLet, b))
  53. # eqIdent on non identifier at all
  54. assertNot eqIdent(a,d)
  55. # eqIdent on sym choice
  56. let fooSym = bindSym"foo"
  57. assert fooSym.kind in {nnkOpenSymChoice, nnkClosedSymChoice}
  58. assert fooSym.eqIdent("fOO")
  59. assertNot fooSym.eqIdent("bar")
  60. # eqIdent on exported and backtick quoted identifiers
  61. let procName = ident("proc")
  62. let quoted = nnkAccQuoted.newTree(procName)
  63. let exported = nnkPostfix.newTree(ident"*", procName)
  64. let exportedQuoted = nnkPostfix.newTree(ident"*", quoted)
  65. let nodes = @[procName, quoted, exported, exportedQuoted]
  66. for i in 0 ..< nodes.len:
  67. for j in 0 ..< nodes.len:
  68. doAssert eqIdent(nodes[i], nodes[j])
  69. for node in nodes:
  70. doAssert eqIdent(node, "proc")
  71. var empty: NimNode
  72. var myLit = newLit("str")
  73. assert( (empty or myLit) == myLit )
  74. empty = newEmptyNode()
  75. assert( (empty or myLit) == myLit )
  76. proc bottom(): NimNode =
  77. quit("may not be evaluated")
  78. assert( (myLit or bottom()) == myLit )
  79. type
  80. Fruit = enum
  81. apple
  82. banana
  83. orange
  84. macro foo(x: typed) =
  85. doAssert Fruit(x.intVal) == banana
  86. foo(banana)
  87. block: # bug #17733
  88. macro repr2(s: typed): string =
  89. result = newLit(s.repr)
  90. macro symHash(s: typed): string =
  91. result = newLit(symBodyHash(s))
  92. type A[T] = object
  93. x: T
  94. proc fn1(x: seq) = discard
  95. proc fn2(x: ref) = discard
  96. proc fn2b(x: ptr) = discard
  97. proc fn3(x: pointer | ref | ptr) = discard
  98. proc fn4(x: A) = discard
  99. proc fn0(x: pointer) = discard
  100. proc fn5[T](x: A[T]) = discard
  101. proc fn6(x: tuple) = discard
  102. proc fn7(x: pointer | tuple) = discard
  103. # ok
  104. discard symHash(fn0)
  105. discard symHash(fn5)
  106. discard symHash(fn6)
  107. discard symHash(fn7)
  108. # ok
  109. discard repr2(fn2)
  110. discard repr2(fn2b)
  111. discard repr2(fn3)
  112. # BUG D20210415T162824: Error: 'fn4' doesn't have a concrete type, due to unspecified generic parameters.
  113. discard symHash(fn4)
  114. discard symHash(fn1) # ditto
  115. # BUG D20210415T162832: Error: unhandled exception: index out of bounds, the container is empty [IndexDefect]
  116. discard symHash(fn3)
  117. discard symHash(fn2) # ditto
  118. discard symHash(fn2b) # ditto