tmacro1.nim 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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)