tcompiletimetable.nim 893 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. discard """
  2. nimout: '''
  3. 2
  4. 3
  5. 4:2
  6. Got Hi
  7. Got Hey
  8. '''
  9. output:'''
  10. a
  11. b
  12. c
  13. '''
  14. """
  15. # bug #404
  16. import macros, tables, strtabs
  17. var ZOOT{.compileTime.} = initTable[int, int](2)
  18. var iii {.compiletime.} = 1
  19. macro zoo: untyped =
  20. ZOOT[iii] = iii*2
  21. inc iii
  22. echo iii
  23. zoo
  24. zoo
  25. macro tupleUnpack: untyped =
  26. var (y,z) = (4, 2)
  27. echo y, ":", z
  28. tupleUnpack
  29. # bug #903
  30. var x {.compileTime.}: StringTableRef
  31. macro addStuff(stuff, body: untyped): untyped =
  32. result = newNimNode(nnkStmtList)
  33. if x.isNil:
  34. x = newStringTable(modeStyleInsensitive)
  35. x[$stuff] = ""
  36. macro dump(): untyped =
  37. result = newNimNode(nnkStmtList)
  38. for y in x.keys: echo "Got ", y
  39. addStuff("Hey"): echo "Hey"
  40. addStuff("Hi"): echo "Hi"
  41. dump()
  42. # ensure .compileTime vars can be used at runtime:
  43. import macros
  44. var xzzzz {.compileTime.}: array[3, string] = ["a", "b", "c"]
  45. for i in 0..high(xzzzz): echo xzzzz[i]