summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2013-07-24 21:58:17 +0200
committerAraq <rumpf_a@web.de>2013-07-24 21:58:17 +0200
commitf9426bfcd54258730e14076b541cb20b7f86f6df (patch)
tree1ff145a3317e5511a0eec17c36faadbb4a1e7ee9
parentb403b41aecaa089e42be74c9a5bd70f1446f0b24 (diff)
downloadNim-f9426bfcd54258730e14076b541cb20b7f86f6df.tar.gz
added macros.genSym
-rw-r--r--compiler/ast.nim2
-rw-r--r--compiler/evals.nim10
-rw-r--r--lib/core/macros.nim13
-rw-r--r--todo.txt1
-rw-r--r--web/news.txt2
5 files changed, 23 insertions, 5 deletions
diff --git a/compiler/ast.nim b/compiler/ast.nim
index e0c3b8e86..c86ba52c6 100644
--- a/compiler/ast.nim
+++ b/compiler/ast.nim
@@ -488,7 +488,7 @@ type
     mNNewNimNode, mNCopyNimNode, mNCopyNimTree, mStrToIdent, mIdentToStr,
     mNBindSym, mLocals, mNCallSite,
     mEqIdent, mEqNimrodNode, mNHint, mNWarning, mNError,
-    mInstantiationInfo, mGetTypeInfo
+    mInstantiationInfo, mGetTypeInfo, mNGenSym
 
 # things that we can evaluate safely at compile time, even if not asked for it:
 const
diff --git a/compiler/evals.nim b/compiler/evals.nim
index f89417dc8..6266668f9 100644
--- a/compiler/evals.nim
+++ b/compiler/evals.nim
@@ -1249,6 +1249,16 @@ proc evalMagicOrCall(c: PEvalContext, n: PNode): PNode =
   of mNBindSym:
     # trivial implementation:
     result = n.sons[1]
+  of mNGenSym:
+    evalX(n.sons[1], {efLValue})
+    let k = getOrdValue(result)
+    evalX(n.sons[2], {efLValue})
+    let b = result
+    let name = if b.strVal.len == 0: ":tmp" else: b.strVal
+    if k < 0 or k > ord(high(TSymKind)):
+      internalError(n.info, "request to create a symbol with invalid kind")
+    result = newSymNode(newSym(k.TSymKind, name.getIdent, c.module, n.info))
+    incl(result.sym.flags, sfGenSym)
   of mStrToIdent: 
     result = evalAux(c, n.sons[1], {})
     if isSpecial(result): return 
diff --git a/lib/core/macros.nim b/lib/core/macros.nim
index 720b15189..9130c3ffb 100644
--- a/lib/core/macros.nim
+++ b/lib/core/macros.nim
@@ -85,11 +85,13 @@ type
   TNimTypeKinds* = set[TNimrodTypeKind]
   TNimrodSymKind* = enum
     nskUnknown, nskConditional, nskDynLib, nskParam,
-    nskGenericParam, nskTemp, nskType, nskConst,
-    nskVar, nskProc, nskMethod, nskIterator,
+    nskGenericParam, nskTemp, nskModule, nskType, nskVar, nskLet, 
+    nskConst, nskResult,
+    nskProc, nskMethod, nskIterator,
     nskConverter, nskMacro, nskTemplate, nskField,
-    nskEnumField, nskForVar, nskModule, nskLabel,
+    nskEnumField, nskForVar, nskLabel,
     nskStub
+    
   TNimSymKinds* = set[TNimrodSymKind]
 
 type
@@ -220,6 +222,11 @@ proc bindSym*(ident: string, rule: TBindSymRule = brClosed): PNimrodNode {.
   ## If ``rule == brForceOpen`` always an ``nkOpenSymChoice`` tree is
   ## returned even if the symbol is not ambiguous.
 
+proc genSym*(kind: TNimrodSymKind = nskLet; ident = ""): PNimrodNode {.
+  magic: "NGenSym".}
+  ## generates a fresh symbol that is guaranteed to be unique. The symbol
+  ## needs to occur in a declaration context.
+
 proc callsite*(): PNimrodNode {.magic: "NCallSite".}
   ## returns the AST if the invokation expression that invoked this macro.
 
diff --git a/todo.txt b/todo.txt
index 35c6a5704..f213567ec 100644
--- a/todo.txt
+++ b/todo.txt
@@ -8,7 +8,6 @@ version 0.9.4
 - mocking support with ``tyProxy`` that does: fallback for ``.`` operator
   - overloading of ``.``? Special case ``.=``?
 - built-in 'getImpl'
-- macros.gensym still missing?
 - optimize 'genericReset'; 'newException' leads to code bloat
 
 
diff --git a/web/news.txt b/web/news.txt
index 74b33e2bf..56919d56c 100644
--- a/web/news.txt
+++ b/web/news.txt
@@ -14,6 +14,8 @@ Bugfixes
 Library Additions
 -----------------
 
+- Added ``macros.genSym`` builtin for AST generation.
+
 
 Changes affecting backwards compatibility
 -----------------------------------------