tenum.nim 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 Bconc1valueCabc3abc
  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. doAssert $x == $valueD, $x
  91. doAssert $x == "abc", $x
  92. block tfakeOptions:
  93. type
  94. TFakeOption = enum
  95. fakeNone, fakeForceFullMake, fakeBoehmGC, fakeRefcGC, fakeRangeCheck,
  96. fakeBoundsCheck, fakeOverflowCheck, fakeNilCheck, fakeAssert, fakeLineDir,
  97. fakeWarns, fakeHints, fakeListCmd, fakeCompileOnly,
  98. fakeSafeCode, # only allow safe code
  99. fakeStyleCheck, fakeOptimizeSpeed, fakeOptimizeSize, fakeGenDynLib,
  100. fakeGenGuiApp, fakeStackTrace
  101. TFakeOptionset = set[TFakeOption]
  102. var
  103. gFakeOptions: TFakeOptionset = {fakeRefcGC, fakeRangeCheck, fakeBoundsCheck,
  104. fakeOverflowCheck, fakeAssert, fakeWarns, fakeHints, fakeLineDir, fakeStackTrace}
  105. compilerArgs: int
  106. gExitcode: int8
  107. block nonzero: # bug #6959
  108. type SomeEnum = enum
  109. A = 10
  110. B
  111. C
  112. let slice = SomeEnum.low..SomeEnum.high
  113. block size_one_byte: # bug #15752
  114. type
  115. Flag = enum
  116. Disabled = 0x00
  117. Enabled = 0xFF
  118. static:
  119. assert 1 == sizeof(Flag)
  120. block: # bug #12589
  121. when not defined(i386):
  122. type
  123. OGRwkbGeometryType {.size: sizeof(cuint).} = enum
  124. wkbPoint25D = 0x80000001.cuint, wkbLineString25D = 0x80000002,
  125. wkbPolygon25D = 0x80000003
  126. proc typ(): OGRwkbGeometryType =
  127. return wkbPoint25D
  128. when not defined(gcRefc):
  129. doAssert $typ() == "wkbPoint25D"
  130. block: # bug #21280
  131. type
  132. Test = enum
  133. B = 19
  134. A = int64.high()
  135. doAssert ord(A) == int64.high()