implemented 'pure' enums
This commit is contained in:
parent
46e9ff6139
commit
580d85d233
3 changed files with 21 additions and 5 deletions
|
|
@ -38,6 +38,7 @@ proc semEnum(c: PContext, n: PNode, prev: PType): PType =
|
|||
localError(n.sons[0].info, errInheritanceOnlyWithEnums)
|
||||
counter = lastOrd(base) + 1
|
||||
rawAddSon(result, base)
|
||||
let isPure = result.sym != nil and sfPure in result.sym.flags
|
||||
for i in countup(1, sonsLen(n) - 1):
|
||||
case n.sons[i].kind
|
||||
of nkEnumFieldDef:
|
||||
|
|
@ -73,12 +74,12 @@ proc semEnum(c: PContext, n: PNode, prev: PType): PType =
|
|||
else: illFormedAst(n)
|
||||
e.typ = result
|
||||
e.position = int(counter)
|
||||
if result.sym != nil and sfExported in result.sym.flags:
|
||||
incl(e.flags, sfUsed) # BUGFIX
|
||||
incl(e.flags, sfExported) # BUGFIX
|
||||
StrTableAdd(c.module.tab, e) # BUGFIX
|
||||
if result.sym != nil and sfExported in result.sym.flags:
|
||||
incl(e.flags, sfUsed)
|
||||
incl(e.flags, sfExported)
|
||||
if not isPure: StrTableAdd(c.module.tab, e)
|
||||
addSon(result.n, newSymNode(e))
|
||||
if sfGenSym notin e.flags: addDeclAt(c, e, c.tab.tos - 1)
|
||||
if sfGenSym notin e.flags and not isPure: addDeclAt(c, e, c.tab.tos - 1)
|
||||
inc(counter)
|
||||
|
||||
proc semSet(c: PContext, n: PNode, prev: PType): PType =
|
||||
|
|
|
|||
|
|
@ -795,6 +795,19 @@ values to use:
|
|||
As can be seen from the example, it is possible to both specify a field's
|
||||
ordinal value and its string value by using a tuple. It is also
|
||||
possible to only specify one of them.
|
||||
|
||||
An enum can be marked with the ``pure`` pragma so that it's fields are not
|
||||
added to the current scope, so they always need to be accessed
|
||||
via ``TMyEnum.value``:
|
||||
|
||||
.. code-block:: nimrod
|
||||
|
||||
type
|
||||
TMyEnum {.pure.} = enum
|
||||
valueA, valueB, valueC, valueD
|
||||
|
||||
echo valueA # error: Unknown identifier
|
||||
echo TMyEnum.valueA # works
|
||||
|
||||
|
||||
String type
|
||||
|
|
|
|||
2
todo.txt
2
todo.txt
|
|
@ -45,6 +45,8 @@ version 0.9.XX
|
|||
|
||||
- JS gen:
|
||||
- fix exception handling
|
||||
- object branch transitions can't work with the current 'reset'; add a 'reset'
|
||||
with an additional parameter
|
||||
- fix remaining closure bugs:
|
||||
- test evals.nim with closures
|
||||
- what about macros with closures?
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue