template_issues.nim 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. discard """
  2. output: '''
  3. @[]
  4. 5
  5. 0
  6. a
  7. hi
  8. Hello, World!
  9. '''
  10. """
  11. import macros, json
  12. block t2057:
  13. proc mpf_get_d(x: int): float = float(x)
  14. proc mpf_cmp_d(a: int; b: float): int = 0
  15. template toFloatHelper(result, tooSmall, tooLarge: untyped) =
  16. result = mpf_get_d(a)
  17. if result == 0.0 and mpf_cmp_d(a,0.0) != 0:
  18. tooSmall
  19. if result == Inf:
  20. tooLarge
  21. proc toFloat(a: int): float =
  22. toFloatHelper(result) do:
  23. raise newException(ValueError, "number too small")
  24. do:
  25. raise newException(ValueError, "number too large")
  26. doAssert toFloat(8) == 8.0
  27. import sequtils, os
  28. block t2629:
  29. template glob_rst(basedir: string = ""): untyped =
  30. if baseDir.len == 0:
  31. to_seq(walk_files("*.rst"))
  32. else:
  33. to_seq(walk_files(basedir/"*.rst"))
  34. let rst_files = concat(glob_rst(), glob_rst("docs"))
  35. when true: echo rst_files
  36. block t5417:
  37. macro genBody: untyped =
  38. let sbx = genSym(nskLabel, "test")
  39. when true:
  40. result = quote do:
  41. block `sbx`:
  42. break `sbx`
  43. else:
  44. template foo(s1, s2) =
  45. block s1:
  46. break s2
  47. result = getAst foo(sbx, sbx)
  48. proc test() =
  49. genBody()
  50. block t909:
  51. template baz() =
  52. proc bar() =
  53. var x = 5
  54. iterator foo(): int {.closure.} =
  55. echo x
  56. var y = foo
  57. discard y()
  58. macro test(): untyped =
  59. result = getAst(baz())
  60. test()
  61. bar()
  62. block t993:
  63. type PNode = ref object of RootObj
  64. template litNode(name, ty) =
  65. type name = ref object of PNode
  66. val: ty
  67. litNode PIntNode, int
  68. template withKey(j: JsonNode; key: string; varname,
  69. body: untyped): typed =
  70. if j.hasKey(key):
  71. let varname{.inject.}= j[key]
  72. block:
  73. body
  74. var j = parsejson("{\"zzz\":1}")
  75. withkey(j, "foo", x):
  76. echo(x)
  77. block t1337:
  78. template someIt(a, pred): untyped =
  79. var it {.inject.} = 0
  80. pred
  81. proc aProc(n: auto) =
  82. n.someIt(echo(it))
  83. aProc(89)
  84. import mlt
  85. block t4564:
  86. type Bar = ref object of RootObj
  87. proc foo(a: Bar): int = 0
  88. var a: Bar
  89. let b = a.foo() > 0
  90. block t8052:
  91. type
  92. UintImpl[N: static[int], T: SomeUnsignedInt] = object
  93. raw_data: array[N, T]
  94. template genLoHi(TypeImpl: untyped): untyped =
  95. template loImpl[N: static[int], T: SomeUnsignedInt](dst: TypeImpl[N div 2, T], src: TypeImpl[N, T]) =
  96. let halfSize = N div 2
  97. for i in 0 ..< halfSize:
  98. dst.raw_data[i] = src.raw_data[i]
  99. proc lo[N: static[int], T: SomeUnsignedInt](x: TypeImpl[N,T]): TypeImpl[N div 2, T] {.inline.}=
  100. loImpl(result, x)
  101. genLoHi(UintImpl)
  102. var a: UintImpl[4, uint32]
  103. a.raw_data = [1'u32, 2'u32, 3'u32, 4'u32]
  104. doAssert a.lo.raw_data.len == 2
  105. doAssert a.lo.raw_data[0] == 1
  106. doAssert a.lo.raw_data[1] == 2
  107. block t2585:
  108. type
  109. RenderPass = object
  110. state: ref int
  111. RenderData = object
  112. fb: int
  113. walls: seq[RenderPass]
  114. Mat2 = int
  115. Vector2[T] = T
  116. Pixels=int
  117. template use(fb: int, st: untyped): untyped =
  118. echo "a ", $fb
  119. st
  120. echo "a ", $fb
  121. proc render(rdat: var RenderData; passes: var openarray[RenderPass]; proj: Mat2;
  122. indexType = 1) =
  123. for i in 0 ..< len(passes):
  124. echo "blah ", repr(passes[i])
  125. proc render2(rdat: var RenderData; screenSz: Vector2[Pixels]; proj: Mat2) =
  126. use rdat.fb:
  127. render(rdat, rdat.walls, proj, 1)
  128. block t4292:
  129. template foo(s: string): string = s
  130. proc variadicProc(v: varargs[string, foo]) = echo v[0]
  131. variadicProc("a")
  132. block t2670:
  133. template testTemplate(b: bool): typed =
  134. when b:
  135. var a = "hi"
  136. else:
  137. var a = 5
  138. echo a
  139. testTemplate(true)
  140. block t4097:
  141. var i {.compileTime.} = 2
  142. template defineId(t: typedesc) =
  143. const id {.genSym.} = i
  144. static: inc(i)
  145. proc idFor(T: typedesc[t]): int {.inline, raises: [].} = id
  146. defineId(int8)
  147. defineId(int16)
  148. doAssert idFor(int8) == 2
  149. doAssert idFor(int16) == 3
  150. block t5235:
  151. template outer(body: untyped) =
  152. template test(val: string) =
  153. const SomeConst: string = val
  154. echo SomeConst
  155. body
  156. outer:
  157. test("Hello, World!")