tcontrolflow.nim 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. discard """
  2. output: '''begin A
  3. elif
  4. destroyed
  5. end A
  6. begin false
  7. if
  8. destroyed
  9. end false
  10. begin true
  11. if
  12. end true
  13. 7
  14. ##index 2 not in 0 .. 1##
  15. '''
  16. cmd: "nim c --gc:arc -d:danger $file"
  17. """
  18. # we use the -d:danger switch to detect uninitialized stack
  19. # slots more reliably (there shouldn't be any, of course).
  20. type
  21. Foo = object
  22. id: int
  23. proc `=destroy`(x: var Foo) =
  24. if x.id != 0:
  25. echo "destroyed"
  26. x.id = 0
  27. proc construct(): Foo = Foo(id: 3)
  28. proc elifIsEasy(cond: bool) =
  29. echo "begin A"
  30. if cond:
  31. echo "if"
  32. elif construct().id == 3:
  33. echo "elif"
  34. else:
  35. echo "else"
  36. echo "end A"
  37. elifIsEasy(false)
  38. proc orIsHard(cond: bool) =
  39. echo "begin ", cond
  40. if cond or construct().id == 3:
  41. echo "if"
  42. else:
  43. echo "else"
  44. echo "end ", cond
  45. orIsHard(false)
  46. orIsHard(true)
  47. type
  48. Control = ref object
  49. x: int
  50. MouseEvent = ref object
  51. control: Control
  52. button: int
  53. proc run(data: Control) =
  54. var evt = MouseEvent(button: 1)
  55. evt.control = data
  56. if evt.button == 1:
  57. discard
  58. else:
  59. return
  60. echo data.x
  61. var c = Control(x: 7)
  62. run(c)
  63. proc sysFatal(exceptn: typedesc, message: string) {.inline, noreturn.} =
  64. var buf = newStringOfCap(200)
  65. add(buf, "##")
  66. add(buf, message)
  67. add(buf, "##")
  68. echo buf
  69. proc ifexpr(i, a, b: int) {.compilerproc, noinline.} =
  70. sysFatal(IndexDefect,
  71. if b < a: "index out of bounds, the container is empty"
  72. else: "index " & $i & " not in " & $a & " .. " & $b)
  73. ifexpr(2, 0, 1)