tcontrolflow.nim 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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
  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