tnestedclosures.nim 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. '''
  29. """
  30. block: #24094
  31. echo "Test 1:"
  32. proc foo() =
  33. let x = 12
  34. iterator bar2(): int {.closure.} =
  35. yield x
  36. proc bar() =
  37. let z = bar2
  38. for y in z(): # just doing bar2() gives param not in env: x
  39. echo y
  40. bar()
  41. foo()
  42. block: #24094
  43. echo "Test 2:"
  44. iterator foo(): int {.closure.} =
  45. let x = 23
  46. iterator bar2(): int {.closure.} =
  47. yield x
  48. proc bar() =
  49. let z = bar2
  50. for y in z():
  51. echo y
  52. bar()
  53. yield x
  54. for x in foo(): echo x
  55. block: #24094
  56. echo "Test 3:"
  57. iterator foo(): int {.closure.} =
  58. let x = 34
  59. proc bar() =
  60. echo x
  61. iterator bar2(): int {.closure.} =
  62. bar()
  63. yield x
  64. for y in bar2():
  65. yield y
  66. for x in foo(): echo x
  67. block:
  68. echo "Test 4:"
  69. proc foo() =
  70. var x = 45
  71. iterator bar2(): int {.closure.} =
  72. yield x
  73. yield x + 3
  74. let b1 = bar2
  75. let b2 = bar2
  76. echo b1()
  77. echo b2()
  78. x = 47
  79. echo b1()
  80. echo b2()
  81. foo()
  82. block:
  83. echo "Test 5:"
  84. proc foo() =
  85. var x = 45
  86. iterator bar2(): int {.closure.} =
  87. yield x
  88. yield x + 3
  89. proc bar() =
  90. var y = 123
  91. iterator bar3(): int {.closure.} =
  92. yield x
  93. yield y
  94. let b3 = bar3
  95. for z in b3():
  96. echo z
  97. x = 47
  98. let b2 = bar2
  99. for z in b2():
  100. echo z
  101. bar()
  102. foo()
  103. block: #19154
  104. echo "Test 6:"
  105. proc test(s: string): proc(): iterator(): string =
  106. iterator it(): string = yield s
  107. proc f(): iterator(): string = it
  108. return f
  109. let it = test("hi")()
  110. for s in it():
  111. echo "<", s, ">"
  112. block: #3824
  113. echo "Test 7:"
  114. proc main =
  115. iterator factory(): int {.closure.} =
  116. iterator bar(): int {.closure.} =
  117. yield 0
  118. yield 1
  119. yield 2
  120. for x in bar(): yield x
  121. for x in factory():
  122. echo x
  123. main()