tnestedclosures.nim 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. discard """
  2. targets: "c"
  3. output: '''
  4. Test 1:
  5. 12
  6. Test 2:
  7. 23
  8. 23
  9. Test 3:
  10. 34
  11. 34
  12. Test 4:
  13. 45
  14. 45
  15. 50
  16. 50
  17. Test 5:
  18. 45
  19. 123
  20. 47
  21. 50
  22. Test 6:
  23. <hi>
  24. Test 7:
  25. 0
  26. 1
  27. 2
  28. Test 8:
  29. 123
  30. 456
  31. '''
  32. """
  33. block: #24094
  34. echo "Test 1:"
  35. proc foo() =
  36. let x = 12
  37. iterator bar2(): int {.closure.} =
  38. yield x
  39. proc bar() =
  40. let z = bar2
  41. for y in z(): # just doing bar2() gives param not in env: x
  42. echo y
  43. bar()
  44. foo()
  45. block: #24094
  46. echo "Test 2:"
  47. iterator foo(): int {.closure.} =
  48. let x = 23
  49. iterator bar2(): int {.closure.} =
  50. yield x
  51. proc bar() =
  52. let z = bar2
  53. for y in z():
  54. echo y
  55. bar()
  56. yield x
  57. for x in foo(): echo x
  58. block: #24094
  59. echo "Test 3:"
  60. iterator foo(): int {.closure.} =
  61. let x = 34
  62. proc bar() =
  63. echo x
  64. iterator bar2(): int {.closure.} =
  65. bar()
  66. yield x
  67. for y in bar2():
  68. yield y
  69. for x in foo(): echo x
  70. block:
  71. echo "Test 4:"
  72. proc foo() =
  73. var x = 45
  74. iterator bar2(): int {.closure.} =
  75. yield x
  76. yield x + 3
  77. let b1 = bar2
  78. let b2 = bar2
  79. echo b1()
  80. echo b2()
  81. x = 47
  82. echo b1()
  83. echo b2()
  84. foo()
  85. block:
  86. echo "Test 5:"
  87. proc foo() =
  88. var x = 45
  89. iterator bar2(): int {.closure.} =
  90. yield x
  91. yield x + 3
  92. proc bar() =
  93. var y = 123
  94. iterator bar3(): int {.closure.} =
  95. yield x
  96. yield y
  97. let b3 = bar3
  98. for z in b3():
  99. echo z
  100. x = 47
  101. let b2 = bar2
  102. for z in b2():
  103. echo z
  104. bar()
  105. foo()
  106. block: #19154
  107. echo "Test 6:"
  108. proc test(s: string): proc(): iterator(): string =
  109. iterator it(): string = yield s
  110. proc f(): iterator(): string = it
  111. return f
  112. let it = test("hi")()
  113. for s in it():
  114. echo "<", s, ">"
  115. block: #3824
  116. echo "Test 7:"
  117. proc main =
  118. iterator factory(): int {.closure.} =
  119. iterator bar(): int {.closure.} =
  120. yield 0
  121. yield 1
  122. yield 2
  123. for x in bar(): yield x
  124. for x in factory():
  125. echo x
  126. main()
  127. block: # issue #12487
  128. iterator FaiReader(): string {.closure.} =
  129. yield "something"
  130. template toClosure(i): auto =
  131. iterator j: string {.closure.} =
  132. for x in FaiReader():
  133. yield x
  134. j
  135. proc main =
  136. var reader = toClosure(FaiReader())
  137. var s: seq[string] = @[]
  138. for x in reader():
  139. s.add(x)
  140. doAssert s == @["something"]
  141. main()
  142. block: # minimized issue #24863
  143. echo "Test 8:"
  144. proc c() =
  145. iterator b(): int {.closure.} =
  146. let r = 456
  147. yield 123
  148. proc n() =
  149. echo r
  150. let a = proc () = n()
  151. a()
  152. let j = b
  153. echo j()
  154. discard j()
  155. c()