summary refs log tree commit diff stats
path: root/tests/casestmt/tcomputedgoto.nim
blob: f7603dac3a3f9ba214387bfa5c272fdec51779ab (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
discard """
  output: '''
yeah A enumB
uneven
yeah A enumB
yeah CD enumD
uneven
yeah CD enumE
yeah A enumB
uneven
yeah CD enumE
yeah CD enumD
uneven
yeah A enumB
yeah B enumC
uneven
yeah A enumB
yeah A enumB
uneven
yeah A enumB
'''
"""

type
  MyEnum = enum
    enumA, enumB, enumC, enumD, enumE, enumLast

proc vm() =
  var instructions: array[0..100, MyEnum]
  instructions[2] = enumC
  instructions[3] = enumD
  instructions[4] = enumA
  instructions[5] = enumD
  instructions[6] = enumC
  instructions[7] = enumA
  instructions[8] = enumB

  instructions[12] = enumE
  var pc = 0
  while true:
    {.computedGoto.}
    let instr = instructions[pc]
    let ra = instr.succ # instr.regA
    case instr
    of enumA:
      echo "yeah A ", ra
    of enumC, enumD:
      echo "yeah CD ", ra
    of enumB:
      echo "yeah B ", ra
    of enumE:
      break
    of enumLast: discard
    inc(pc)

    if pc mod 2 == 1:
      echo "uneven"

vm()