ttemplatesymbols.nim 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import
  2. macros, algorithm, strutils
  3. proc normalProc(x: int) =
  4. echo x
  5. template templateWithtouParams =
  6. echo 10
  7. proc overloadedProc(x: int) =
  8. echo x
  9. proc overloadedProc(x: string) =
  10. echo x
  11. proc overloadedProc[T](x: T) =
  12. echo x
  13. template normalTemplate(x: int) =
  14. echo x
  15. template overloadedTemplate(x: int) =
  16. echo x
  17. template overloadedTemplate(x: string) =
  18. echo x
  19. macro normalMacro(x: int): untyped =
  20. discard
  21. macro macroWithoutParams: untyped =
  22. discard
  23. macro inspectSymbol(sym: typed, expected: static[string]): untyped =
  24. if sym.kind == nnkSym:
  25. echo "Symbol node:"
  26. let res = sym.getImpl.repr & "\n"
  27. echo res
  28. # echo "|", res, "|"
  29. # echo "|", expected, "|"
  30. if expected.len > 0: assert res == expected
  31. elif sym.kind in {nnkClosedSymChoice, nnkOpenSymChoice}:
  32. echo "Begin sym choice:"
  33. var results = newSeq[string](0)
  34. for innerSym in sym:
  35. results.add innerSym.getImpl.repr
  36. sort(results, cmp[string])
  37. let res = results.join("\n") & "\n"
  38. echo res
  39. if expected.len > 0: assert res == expected
  40. echo "End symchoice."
  41. else:
  42. echo "Non-symbol node: ", sym.kind
  43. if expected.len > 0: assert $sym.kind == expected
  44. macro inspectUntyped(sym: untyped, expected: static[string]): untyped =
  45. let res = sym.repr
  46. echo "Untyped node: ", res
  47. assert res == expected
  48. inspectSymbol templateWithtouParams, "nnkCommand"
  49. # this template is expanded, because bindSym was not used
  50. # the end result is the template body (nnkCommand)
  51. inspectSymbol bindSym("templateWithtouParams"), """
  52. template templateWithtouParams() =
  53. echo 10
  54. """
  55. inspectSymbol macroWithoutParams, "nnkEmpty"
  56. # Just like the template above, the macro was expanded
  57. inspectSymbol bindSym("macroWithoutParams"), """
  58. macro macroWithoutParams(): untyped =
  59. discard
  60. """
  61. inspectSymbol normalMacro, """
  62. macro normalMacro(x: int): untyped =
  63. discard
  64. """
  65. # Since the normalMacro has params, it's automatically
  66. # treated as a symbol here (no need for `bindSym`)
  67. inspectSymbol bindSym("normalMacro"), """
  68. macro normalMacro(x: int): untyped =
  69. discard
  70. """
  71. inspectSymbol normalTemplate, """
  72. template normalTemplate(x: int) =
  73. echo x
  74. """
  75. inspectSymbol bindSym("normalTemplate"), """
  76. template normalTemplate(x: int) =
  77. echo x
  78. """
  79. inspectSymbol overloadedTemplate, """
  80. template overloadedTemplate(x: int) =
  81. echo x
  82. template overloadedTemplate(x: string) =
  83. echo x
  84. """
  85. inspectSymbol bindSym("overloadedTemplate"), """
  86. template overloadedTemplate(x: int) =
  87. echo x
  88. template overloadedTemplate(x: string) =
  89. echo x
  90. """
  91. inspectUntyped bindSym("overloadedTemplate"), """bindSym("overloadedTemplate")"""
  92. # binSym is active only in the presence of `typed` params.
  93. # `untyped` params still get the raw AST
  94. inspectSymbol normalProc, """
  95. proc normalProc(x: int) =
  96. echo [x]
  97. """
  98. inspectSymbol bindSym("normalProc"), """
  99. proc normalProc(x: int) =
  100. echo [x]
  101. """
  102. inspectSymbol overloadedProc, """
  103. proc overloadedProc(x: int) =
  104. echo [x]
  105. proc overloadedProc(x: string) =
  106. echo [x]
  107. proc overloadedProc[T](x: T) =
  108. echo x
  109. """
  110. # XXX: There seems to be a repr rendering problem above.
  111. # Notice that `echo [x]`
  112. inspectSymbol overloadedProc[float], """
  113. proc overloadedProc(x: T) =
  114. echo [x]
  115. """
  116. # As expected, when we select a specific generic, the
  117. # AST is no longer a symChoice
  118. inspectSymbol bindSym("overloadedProc"), """
  119. proc overloadedProc(x: int) =
  120. echo [x]
  121. proc overloadedProc(x: string) =
  122. echo [x]
  123. proc overloadedProc[T](x: T) =
  124. echo x
  125. """