summary refs log tree commit diff stats
path: root/lib/core
diff options
context:
space:
mode:
authorWhiteDuke <Wh1teDuke@users.noreply.github.com>2017-11-19 00:38:44 +0100
committerAndreas Rumpf <rumpf_a@web.de>2017-11-19 00:38:44 +0100
commit963184fea6858b270e1d6a69707c94b2511674dc (patch)
tree4f1e066fd5d9f1a5e348dafe5a49d023016fc497 /lib/core
parentd072229975f4b092130e17f7cd91cb0fc4c1dec9 (diff)
downloadNim-963184fea6858b270e1d6a69707c94b2511674dc.tar.gz
'newEnum' proc added to macros.nim (#6000)
Diffstat (limited to 'lib/core')
-rw-r--r--lib/core/macros.nim42
1 files changed, 42 insertions, 0 deletions
diff --git a/lib/core/macros.nim b/lib/core/macros.nim
index e51b79abd..b2ff3e4ea 100644
--- a/lib/core/macros.nim
+++ b/lib/core/macros.nim
@@ -895,6 +895,48 @@ proc newIfStmt*(branches: varargs[tuple[cond, body: NimNode]]):
   for i in branches:
     result.add(newNimNode(nnkElifBranch).add(i.cond, i.body))
 
+proc newEnum*(name: NimNode, fields: openArray[NimNode],
+              public, pure: bool): NimNode {.compileTime.} =
+    
+  ## Creates a new enum. `name` must be an ident. Fields are allowed to be
+  ## either idents or EnumFieldDef
+  ##
+  ## .. code-block:: nim
+  ##
+  ##    newEnum(
+  ##      name    = ident("Colors"),
+  ##      fields  = [ident("Blue"), ident("Red")],
+  ##      public  = true, pure = false)
+  ##
+  ##    # type Colors* = Blue Red
+  ##
+    
+  expectKind name, nnkIdent
+  doAssert len(fields) > 0, "Enum must contain at least one field"
+  for field in fields:
+    expectKind field, {nnkIdent, nnkEnumFieldDef}
+
+  let enumBody = newNimNode(nnkEnumTy).add(newEmptyNode()).add(fields)
+  var typeDefArgs = [name, newEmptyNode(), enumBody]
+
+  if public:
+    let postNode = newNimNode(nnkPostfix).add(
+      newIdentNode("*"), typeDefArgs[0])
+
+    typeDefArgs[0] = postNode
+
+  if pure:
+    let pragmaNode = newNimNode(nnkPragmaExpr).add(
+      typeDefArgs[0],
+      add(newNimNode(nnkPragma), newIdentNode(!"pure")))
+
+    typeDefArgs[0] = pragmaNode
+
+  let
+    typeDef   = add(newNimNode(nnkTypeDef), typeDefArgs)
+    typeSect  = add(newNimNode(nnkTypeSection), typeDef)
+
+  return typeSect
 
 proc copyChildrenTo*(src, dest: NimNode) {.compileTime.}=
   ## Copy all children from `src` to `dest`