macrocache.nim 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. proc `[]`*(s: CacheSeq; i: BackwardsIndex): NimNode =
  106. ## Returns the `i`th last value from `s`.
  107. runnableExamples:
  108. import std/macros
  109. const mySeq = CacheSeq"backTest"
  110. static:
  111. mySeq &= newLit(42)
  112. mySeq &= newLit(7)
  113. assert mySeq[^1].intVal == 7 # Last item
  114. assert mySeq[^2].intVal == 42 # Second last item
  115. s[s.len - int(i)]
  116. iterator items*(s: CacheSeq): NimNode =
  117. ## Iterates over each item in `s`.
  118. runnableExamples:
  119. import std/macros
  120. const myseq = CacheSeq"itemsTest"
  121. static:
  122. myseq.add(newLit(5))
  123. myseq.add(newLit(42))
  124. for val in myseq:
  125. # check that all values in `myseq` are int literals
  126. assert val.kind == nnkIntLit
  127. for i in 0 ..< len(s): yield s[i]
  128. proc `[]=`*(t: CacheTable; key: string, value: NimNode) {.magic: "NctPut".} =
  129. ## Inserts a `(key, value)` pair into `t`.
  130. ##
  131. ## .. warning:: `key` has to be unique! Assigning `value` to a `key` that is already
  132. ## in the table will result in a compiler error.
  133. runnableExamples:
  134. import std/macros
  135. const mcTable = CacheTable"subTest"
  136. static:
  137. # assign newLit(5) to the key "value"
  138. mcTable["value"] = newLit(5)
  139. # check that we can get the value back
  140. assert mcTable["value"].kind == nnkIntLit
  141. proc len*(t: CacheTable): int {.magic: "NctLen".} =
  142. ## Returns the number of elements in `t`.
  143. runnableExamples:
  144. import std/macros
  145. const dataTable = CacheTable"lenTest"
  146. static:
  147. dataTable["key"] = newLit(5)
  148. assert dataTable.len == 1
  149. proc `[]`*(t: CacheTable; key: string): NimNode {.magic: "NctGet".} =
  150. ## Retrieves the `NimNode` value at `t[key]`.
  151. runnableExamples:
  152. import std/macros
  153. const mcTable = CacheTable"subTest"
  154. static:
  155. mcTable["toAdd"] = newStmtList()
  156. # get the NimNode back
  157. assert mcTable["toAdd"].kind == nnkStmtList
  158. proc hasKey*(t: CacheTable; key: string): bool =
  159. ## Returns true if `key` is in the table `t`.
  160. ##
  161. ## See also:
  162. ## * [contains proc][contains(CacheTable, string)] for use with the `in` operator
  163. runnableExamples:
  164. import std/macros
  165. const mcTable = CacheTable"hasKeyEx"
  166. static:
  167. assert not mcTable.hasKey("foo")
  168. mcTable["foo"] = newEmptyNode()
  169. # Will now be true since we inserted a value
  170. assert mcTable.hasKey("foo")
  171. discard "Implemented in vmops"
  172. proc contains*(t: CacheTable; key: string): bool {.inline.} =
  173. ## Alias of [hasKey][hasKey(CacheTable, string)] for use with the `in` operator.
  174. runnableExamples:
  175. import std/macros
  176. const mcTable = CacheTable"containsEx"
  177. static:
  178. mcTable["foo"] = newEmptyNode()
  179. # Will be true since we gave it a value before
  180. assert "foo" in mcTable
  181. t.hasKey(key)
  182. proc hasNext(t: CacheTable; iter: int): bool {.magic: "NctHasNext".}
  183. proc next(t: CacheTable; iter: int): (string, NimNode, int) {.magic: "NctNext".}
  184. iterator pairs*(t: CacheTable): (string, NimNode) =
  185. ## Iterates over all `(key, value)` pairs in `t`.
  186. runnableExamples:
  187. import std/macros
  188. const mytabl = CacheTable"values"
  189. static:
  190. mytabl["intVal"] = newLit(5)
  191. mytabl["otherVal"] = newLit(6)
  192. for key, val in mytabl:
  193. # make sure that we actually get the same keys
  194. assert key in ["intVal", "otherVal"]
  195. # all vals are int literals
  196. assert val.kind == nnkIntLit
  197. var h = 0
  198. while hasNext(t, h):
  199. let (a, b, h2) = next(t, h)
  200. yield (a, b)
  201. h = h2