tcomputedgoto.nim 941 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. discard """
  2. output: '''
  3. yeah A enumB
  4. uneven
  5. yeah A enumB
  6. yeah CD enumD
  7. uneven
  8. yeah CD enumE
  9. yeah A enumB
  10. uneven
  11. yeah CD enumE
  12. yeah CD enumD
  13. uneven
  14. yeah A enumB
  15. yeah B enumC
  16. uneven
  17. yeah A enumB
  18. yeah A enumB
  19. uneven
  20. yeah A enumB
  21. '''
  22. """
  23. type
  24. MyEnum = enum
  25. enumA, enumB, enumC, enumD, enumE, enumLast
  26. proc vm() =
  27. var instructions: array[0..100, MyEnum]
  28. instructions[2] = enumC
  29. instructions[3] = enumD
  30. instructions[4] = enumA
  31. instructions[5] = enumD
  32. instructions[6] = enumC
  33. instructions[7] = enumA
  34. instructions[8] = enumB
  35. instructions[12] = enumE
  36. var pc = 0
  37. while true:
  38. {.computedGoto.}
  39. let instr = instructions[pc]
  40. let ra = instr.succ # instr.regA
  41. case instr
  42. of enumA:
  43. echo "yeah A ", ra
  44. of enumC, enumD:
  45. echo "yeah CD ", ra
  46. of enumB:
  47. echo "yeah B ", ra
  48. of enumE:
  49. break
  50. of enumLast: discard
  51. inc(pc)
  52. if pc mod 2 == 1:
  53. echo "uneven"
  54. vm()