summary refs log tree commit diff stats
path: root/tests/controlflow/tcontrolflow.nim
blob: 258f3f50d2de41d594e0adea1c41cd164d1e8757 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
discard """
  output: '''
10
true true
true false
false true
false false
i == 2
'''
"""


block tbreak:
  var
    x = false
    run = true

  while run:
    run = false
    block myblock:
      if true:
        break
      echo "leaving myblock"
    x = true
  doAssert(x)

  # bug #1418
  iterator foo: int =
    for x in 0 .. 9:
      for y in [10,20,30,40,50,60,70,80,90]:
        yield x + y

  for p in foo():
    echo p
    break

  iterator permutations: int =
    yield 10

  for p in permutations():
    break

  # regression:
  proc main =
    for x in [true, false]:
      for y in [true, false]:
        echo x, " ", y

  main()



block tcontinue:
  var i = 0
  while i < 400:

    if i == 10: break
    elif i == 3:
      inc i
      continue
    inc i

  var f = "failure"
  var j = 0
  while j < 300:
    for x in 0..34:
      if j < 300: continue
      if x == 10:
        echo "failure: should never happen"
        break
    f = "came here"
    break

  if i == 10:
    doAssert f == "came here"
  else:
    echo "failure"



block tnestif:
  var
      x, y: int
  x = 2
  if x == 0:
      write(stdout, "i == 0")
      if y == 0:
          writeLine(stdout, x)
      else:
          writeLine(stdout, y)
  elif x == 1:
      writeLine(stdout, "i == 1")
  elif x == 2:
      writeLine(stdout, "i == 2")
  else:
      writeLine(stdout, "looks like Python")
  #OUT i == 2