tnested.nim 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. discard """
  2. output: '''
  3. foo88
  4. 23 24foo 88
  5. foo88
  6. 23 24foo 88
  7. 11
  8. int: 108
  9. 0
  10. 11
  11. 1
  12. 11
  13. 2
  14. 11
  15. 3
  16. 11
  17. 4
  18. 11
  19. 5
  20. 11
  21. 6
  22. 11
  23. 7
  24. 11
  25. 8
  26. 11
  27. 9
  28. 11
  29. 11
  30. py
  31. py
  32. py
  33. py
  34. px
  35. 6
  36. '''
  37. """
  38. block tnestedclosure:
  39. proc main(param: int) =
  40. var foo = 23
  41. proc outer(outerParam: string) =
  42. var outerVar = 88
  43. echo outerParam, outerVar
  44. proc inner() =
  45. block Test:
  46. echo foo, " ", param, outerParam, " ", outerVar
  47. inner()
  48. outer("foo")
  49. # test simple closure within dummy 'main':
  50. proc dummy =
  51. proc main2(param: int) =
  52. var fooB = 23
  53. proc outer(outerParam: string) =
  54. var outerVar = 88
  55. echo outerParam, outerVar
  56. proc inner() =
  57. block Test:
  58. echo fooB, " ", param, outerParam, " ", outerVar
  59. inner()
  60. outer("foo")
  61. main2(24)
  62. dummy()
  63. main(24)
  64. # Jester + async triggered this bug:
  65. proc cbOuter() =
  66. var response = "hohoho"
  67. block:
  68. proc cbIter() =
  69. block:
  70. proc fooIter() =
  71. doAssert response == "hohoho"
  72. fooIter()
  73. cbIter()
  74. cbOuter()
  75. block tnestedproc:
  76. proc p(x, y: int): int =
  77. result = x + y
  78. echo p((proc (): int =
  79. var x = 7
  80. return x)(),
  81. (proc (): int = return 4)())
  82. block deeplynested:
  83. # bug #4070
  84. proc id(f: (proc())): auto =
  85. return f
  86. proc foo(myinteger: int): (iterator(): int) =
  87. return iterator(): int {.closure.} =
  88. proc bar() =
  89. proc kk() =
  90. echo "int: ", myinteger
  91. kk()
  92. id(bar)()
  93. discard foo(108)()
  94. block tclosure2:
  95. when true:
  96. proc ax =
  97. for xxxx in 0..9:
  98. var i = 0
  99. proc bx =
  100. if i > 10:
  101. echo xxxx
  102. return
  103. i += 1
  104. #for j in 0 .. 0: echo i
  105. bx()
  106. bx()
  107. echo i
  108. ax()
  109. when true:
  110. proc accumulator(start: int): (proc(): int {.closure.}) =
  111. var x = start-1
  112. #let dummy = proc =
  113. # discard start
  114. result = proc (): int =
  115. #var x = 9
  116. for i in 0 .. 0: x = x + 1
  117. return x
  118. var a = accumulator(3)
  119. let b = accumulator(4)
  120. echo a() + b() + a()
  121. proc outer =
  122. proc py() =
  123. # no closure here:
  124. for i in 0..3: echo "py"
  125. py()
  126. outer()
  127. when true:
  128. proc outer2 =
  129. var errorValue = 3
  130. proc fac[T](n: T): T =
  131. if n < 0: result = errorValue
  132. elif n <= 1: result = 1
  133. else: result = n * fac(n-1)
  134. proc px() {.closure.} =
  135. echo "px"
  136. proc py() {.closure.} =
  137. echo "py"
  138. let
  139. mapping = {
  140. "abc": px,
  141. "xyz": py
  142. }
  143. mapping[0][1]()
  144. echo fac(3)
  145. outer2()