tcontrolflow.nim 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. discard """
  2. output: '''
  3. 10
  4. true true
  5. true false
  6. false true
  7. false false
  8. i == 2
  9. '''
  10. """
  11. block tbreak:
  12. var
  13. x = false
  14. run = true
  15. while run:
  16. run = false
  17. block myblock:
  18. if true:
  19. break myblock
  20. echo "leaving myblock"
  21. x = true
  22. doAssert(x)
  23. # bug #1418
  24. iterator foo: int =
  25. for x in 0 .. 9:
  26. for y in [10,20,30,40,50,60,70,80,90]:
  27. yield x + y
  28. for p in foo():
  29. echo p
  30. break
  31. iterator permutations: int =
  32. yield 10
  33. for p in permutations():
  34. break
  35. # regression:
  36. proc main =
  37. for x in [true, false]:
  38. for y in [true, false]:
  39. echo x, " ", y
  40. main()
  41. block tcontinue:
  42. var i = 0
  43. while i < 400:
  44. if i == 10: break
  45. elif i == 3:
  46. inc i
  47. continue
  48. inc i
  49. var f = "failure"
  50. var j = 0
  51. while j < 300:
  52. for x in 0..34:
  53. if j < 300: continue
  54. if x == 10:
  55. echo "failure: should never happen"
  56. break
  57. f = "came here"
  58. break
  59. if i == 10:
  60. doAssert f == "came here"
  61. else:
  62. echo "failure"
  63. block tnestif:
  64. var
  65. x, y: int
  66. x = 2
  67. if x == 0:
  68. write(stdout, "i == 0")
  69. if y == 0:
  70. writeLine(stdout, x)
  71. else:
  72. writeLine(stdout, y)
  73. elif x == 1:
  74. writeLine(stdout, "i == 1")
  75. elif x == 2:
  76. writeLine(stdout, "i == 2")
  77. else:
  78. writeLine(stdout, "looks like Python")
  79. #OUT i == 2
  80. # bug https://github.com/nim-lang/RFCs/issues/451
  81. for i in 1..2: # works
  82. break
  83. block: # works
  84. for i in 1..2:
  85. break
  86. block: # works
  87. block:
  88. discard 12 + 3
  89. for i in 1..2:
  90. break
  91. block named: # works
  92. if true:
  93. break named
  94. doAssert false, "not reached"