tcompiletimetable.nim 874 B

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