Skip to content

Instantly share code, notes, and snippets.

@Vindaar
Last active July 21, 2018 14:21
Show Gist options
  • Save Vindaar/e803d2e5705febd8de018821fbeb7f6b to your computer and use it in GitHub Desktop.
Save Vindaar/e803d2e5705febd8de018821fbeb7f6b to your computer and use it in GitHub Desktop.
import macros
macro enumExt(e: typed, nums: varargs[untyped]): untyped =
# get type to access nodes of given enum
let t = e.getType
# seq to store syms to add to new enum
var toAdd = newSeq[NimNode]()
for x in t[1]:
case x.kind
of nnkSym:
# add symbol to enum part
toAdd.add x
else:
discard
# add additional fields
for x in nums:
toAdd.add x
echo "New enum fields ", toAdd
# create new enum
result = nnkEnumTy.newTree(
newEmptyNode()
)
# add old enum's fields + new ones
for x in toAdd:
result.add x
type
miranNum = enum
miOne, miTwo, miThree
miranNumExt = enumExt(
miranNum,
miFour,
miFive
)
let a = miranNumExt.miFour
let b = miranNumExt.miOne
echo a
echo b
static: echo miranNumExt.getType.treeRepr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment