macrocache.nim 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2018 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module provides an API for macros to collect compile-time information
  10. ## across module boundaries. It should be used instead of global `{.compileTime.}`
  11. ## variables as those break incremental compilation.
  12. ##
  13. ## The main feature of this module is that if you create `CacheTable`s or
  14. ## any other `Cache` types with the same name in different modules, their
  15. ## content will be shared, meaning that you can fill a `CacheTable` in
  16. ## one module, and iterate over its contents in another.
  17. runnableExamples:
  18. import std/macros
  19. const mcTable = CacheTable"myTable"
  20. const mcSeq = CacheSeq"mySeq"
  21. const mcCounter = CacheCounter"myCounter"
  22. static:
  23. # add new key "val" with the value `myval`
  24. let myval = newLit("hello ic")
  25. mcTable["val"] = myval
  26. assert mcTable["val"].kind == nnkStrLit
  27. # Can access the same cache from different static contexts
  28. # All the information is retained
  29. static:
  30. # get value from `mcTable` and add it to `mcSeq`
  31. mcSeq.add(mcTable["val"])
  32. assert mcSeq.len == 1
  33. static:
  34. assert mcSeq[0].strVal == "hello ic"
  35. # increase `mcCounter` by 3
  36. mcCounter.inc(3)
  37. assert mcCounter.value == 3
  38. type
  39. CacheSeq* = distinct string
  40. ## Compile-time sequence of `NimNode`s.
  41. CacheTable* = distinct string
  42. ## Compile-time table of key-value pairs.
  43. ##
  44. ## Keys are `string`s and values are `NimNode`s.
  45. CacheCounter* = distinct string
  46. ## Compile-time counter, uses `int` for storing the count.
  47. proc value*(c: CacheCounter): int {.magic: "NccValue".} =
  48. ## Returns the value of a counter `c`.
  49. runnableExamples:
  50. static:
  51. let counter = CacheCounter"valTest"
  52. # default value is 0
  53. assert counter.value == 0
  54. inc counter
  55. assert counter.value == 1
  56. proc inc*(c: CacheCounter; by = 1) {.magic: "NccInc".} =
  57. ## Increments the counter `c` with the value `by`.
  58. runnableExamples:
  59. static:
  60. let counter = CacheCounter"incTest"
  61. inc counter
  62. inc counter, 5
  63. assert counter.value == 6
  64. proc add*(s: CacheSeq; value: NimNode) {.magic: "NcsAdd".} =
  65. ## Adds `value` to `s`.
  66. runnableExamples:
  67. import std/macros
  68. const mySeq = CacheSeq"addTest"
  69. static:
  70. mySeq.add(newLit(5))
  71. mySeq.add(newLit("hello ic"))
  72. assert mySeq.len == 2
  73. assert mySeq[1].strVal == "hello ic"
  74. proc incl*(s: CacheSeq; value: NimNode) {.magic: "NcsIncl".} =
  75. ## Adds `value` to `s`.
  76. ##
  77. ## .. hint:: This doesn't do anything if `value` is already in `s`.
  78. runnableExamples:
  79. import std/macros
  80. const mySeq = CacheSeq"inclTest"
  81. static:
  82. mySeq.incl(newLit(5))
  83. mySeq.incl(newLit(5))
  84. # still one element
  85. assert mySeq.len == 1
  86. proc len*(s: CacheSeq): int {.magic: "NcsLen".} =
  87. ## Returns the length of `s`.
  88. runnableExamples:
  89. import std/macros
  90. const mySeq = CacheSeq"lenTest"
  91. static:
  92. let val = newLit("helper")
  93. mySeq.add(val)
  94. assert mySeq.len == 1
  95. mySeq.add(val)
  96. assert mySeq.len == 2
  97. proc `[]`*(s: CacheSeq; i: int): NimNode {.magic: "NcsAt".} =
  98. ## Returns the `i`th value from `s`.
  99. runnableExamples:
  100. import std/macros
  101. const mySeq = CacheSeq"subTest"
  102. static:
  103. mySeq.add(newLit(42))
  104. assert mySeq[0].intVal == 42
  105. iterator items*(s: CacheSeq): NimNode =
  106. ## Iterates over each item in `s`.
  107. runnableExamples:
  108. import std/macros
  109. const myseq = CacheSeq"itemsTest"
  110. static:
  111. myseq.add(newLit(5))
  112. myseq.add(newLit(42))
  113. for val in myseq:
  114. # check that all values in `myseq` are int literals
  115. assert val.kind == nnkIntLit
  116. for i in 0 ..< len(s): yield s[i]
  117. proc `[]=`*(t: CacheTable; key: string, value: NimNode) {.magic: "NctPut".} =
  118. ## Inserts a `(key, value)` pair into `t`.
  119. ##
  120. ## .. warning:: `key` has to be unique! Assigning `value` to a `key` that is already
  121. ## in the table will result in a compiler error.
  122. runnableExamples:
  123. import std/macros
  124. const mcTable = CacheTable"subTest"
  125. static:
  126. # assign newLit(5) to the key "value"
  127. mcTable["value"] = newLit(5)
  128. # check that we can get the value back
  129. assert mcTable["value"].kind == nnkIntLit
  130. proc len*(t: CacheTable): int {.magic: "NctLen".} =
  131. ## Returns the number of elements in `t`.
  132. runnableExamples:
  133. import std/macros
  134. const dataTable = CacheTable"lenTest"
  135. static:
  136. dataTable["key"] = newLit(5)
  137. assert dataTable.len == 1
  138. proc `[]`*(t: CacheTable; key: string): NimNode {.magic: "NctGet".} =
  139. ## Retrieves the `NimNode` value at `t[key]`.
  140. runnableExamples:
  141. import std/macros
  142. const mcTable = CacheTable"subTest"
  143. static:
  144. mcTable["toAdd"] = newStmtList()
  145. # get the NimNode back
  146. assert mcTable["toAdd"].kind == nnkStmtList
  147. proc hasNext(t: CacheTable; iter: int): bool {.magic: "NctHasNext".}
  148. proc next(t: CacheTable; iter: int): (string, NimNode, int) {.magic: "NctNext".}
  149. iterator pairs*(t: CacheTable): (string, NimNode) =
  150. ## Iterates over all `(key, value)` pairs in `t`.
  151. runnableExamples:
  152. import std/macros
  153. const mytabl = CacheTable"values"
  154. static:
  155. mytabl["intVal"] = newLit(5)
  156. mytabl["otherVal"] = newLit(6)
  157. for key, val in mytabl:
  158. # make sure that we actually get the same keys
  159. assert key in ["intVal", "otherVal"]
  160. # all vals are int literals
  161. assert val.kind == nnkIntLit
  162. var h = 0
  163. while hasNext(t, h):
  164. let (a, b, h2) = next(t, h)
  165. yield (a, b)
  166. h = h2