template_issues.nim 4.3 KB

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