tenum.nim 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. discard """
  2. output: '''
  3. B
  4. B
  5. ABCDC
  6. foo
  7. first0second32third64
  8. my value A1my value Bconc2valueCabc4abc
  9. my value A0my value Bconc1valueCabc3valueC
  10. '''
  11. """
  12. import macros
  13. block tenum1:
  14. type E = enum a, b, c, x, y, z
  15. var en: E
  16. en = a
  17. # Bug #4066
  18. macro genEnum(): untyped = newNimNode(nnkEnumTy).add(newEmptyNode(), newIdentNode("geItem1"))
  19. type GeneratedEnum = genEnum()
  20. doAssert(type(geItem1) is GeneratedEnum)
  21. block tenum2:
  22. type
  23. TEnumHole = enum
  24. eA = 0,
  25. eB = 4,
  26. eC = 5
  27. var
  28. e: TEnumHole = eB
  29. case e
  30. of eA: echo "A"
  31. of eB: echo "B"
  32. of eC: echo "C"
  33. block tenum3:
  34. type
  35. TEnumHole {.size: sizeof(int).} = enum
  36. eA = 0,
  37. eB = 4,
  38. eC = 5
  39. var
  40. e: TEnumHole = eB
  41. case e
  42. of eA: echo "A"
  43. of eB: echo "B"
  44. of eC: echo "C"
  45. block tbasic:
  46. type
  47. MyEnum = enum
  48. A,B,C,D
  49. # trick the optimizer with an seq:
  50. var x = @[A,B,C,D]
  51. echo x[0],x[1],x[2],x[3],MyEnum(2)
  52. block talias:
  53. # bug #5148
  54. type
  55. A = enum foo, bar
  56. B = A
  57. echo B.foo
  58. block thole:
  59. type Holed = enum
  60. hFirst = (0,"first")
  61. hSecond = (32,"second")
  62. hThird = (64,"third")
  63. var x = @[0,32,64] # This is just to avoid the compiler inlining the value of the enum
  64. echo Holed(x[0]),ord Holed(x[0]),Holed(x[1]),ord Holed(x[1]),Holed(x[2]),ord Holed(x[2])
  65. block toffset:
  66. const
  67. strValB = "my value B"
  68. type
  69. TMyEnum = enum
  70. valueA = (1, "my value A"),
  71. valueB = strValB & "conc",
  72. valueC,
  73. valueD = (4, "abc")
  74. proc getValue(i:int): TMyEnum = TMyEnum(i)
  75. # trick the optimizer with a variable:
  76. var x = getValue(4)
  77. echo getValue(1), ord(valueA), getValue(2), ord(valueB), getValue(3), getValue(4), ord(valueD), x
  78. block tnamedfields:
  79. const strValB = "my value B"
  80. type
  81. TMyEnum = enum
  82. valueA = (0, "my value A"),
  83. valueB = strValB & "conc",
  84. valueC,
  85. valueD = (3, "abc"),
  86. valueE = 4
  87. # trick the optimizer with a variable:
  88. var x = valueD
  89. echo valueA, ord(valueA), valueB, ord(valueB), valueC, valueD, ord(valueD), x
  90. block toptions:
  91. type
  92. # please make sure we have under 32 options (improves code efficiency!)
  93. TOption = enum
  94. optNone, optForceFullMake, optBoehmGC, optRefcGC, optRangeCheck,
  95. optBoundsCheck, optOverflowCheck, optNilCheck, optAssert, optLineDir,
  96. optWarns, optHints, optListCmd, optCompileOnly,
  97. optSafeCode, # only allow safe code
  98. optStyleCheck, optOptimizeSpeed, optOptimizeSize, optGenDynLib,
  99. optGenGuiApp, optStackTrace
  100. TOptionset = set[TOption]
  101. var
  102. gOptions: TOptionset = {optRefcGC, optRangeCheck, optBoundsCheck,
  103. optOverflowCheck, optAssert, optWarns, optHints, optLineDir, optStackTrace}
  104. compilerArgs: int
  105. gExitcode: int8
  106. block nonzero: # bug #6959
  107. type SomeEnum = enum
  108. A = 10
  109. B
  110. C
  111. let slice = SomeEnum.low..SomeEnum.high
  112. block size_one_byte: #issue 15752
  113. type
  114. Flag = enum
  115. Disabled = 0x00
  116. Enabled = 0xFF
  117. static:
  118. assert 1 == sizeof(Flag)