Thank you both.
For my needs, I must be able to specify the enum values; so I created the
sequence of `nnkEnumFieldDef` to give to `newEnum()` which involved copying
leaves of the `Asgn` node.
For future me: `dumpTree` is your friend. For other learners, here is what I
came up with. For current talent, I welcome suggestions for improvement.
import std/macros
macro declareEnum(enumName: untyped, enumPairsStmtList: untyped) =
enumPairsStmtList.expectKind(nnkStmtList)
var fields: seq[NimNode]
for asgnNode in enumPairsStmtList:
var node = newNimNode(nnkEnumFieldDef)
node.add(asgnNode[0])
node.add(asgnNode[1])
fields.add(node)
newEnum(enumName, fields, true, true)
declareEnum(Foo):
Alpha = 1
Bravo = 2
Charlie = 3
proc main =
let v: Foo = Alpha
echo v
echo Charlie.int
when isMainModule:
main()
Run