template_issues.nim 3.9 KB

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