added macros.genSym

This commit is contained in:
Araq 2013-07-24 21:58:17 +02:00
commit f9426bfcd5
5 changed files with 23 additions and 5 deletions

View file

@ -488,7 +488,7 @@ type
mNNewNimNode, mNCopyNimNode, mNCopyNimTree, mStrToIdent, mIdentToStr, mNNewNimNode, mNCopyNimNode, mNCopyNimTree, mStrToIdent, mIdentToStr,
mNBindSym, mLocals, mNCallSite, mNBindSym, mLocals, mNCallSite,
mEqIdent, mEqNimrodNode, mNHint, mNWarning, mNError, 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: # things that we can evaluate safely at compile time, even if not asked for it:
const const

View file

@ -1249,6 +1249,16 @@ proc evalMagicOrCall(c: PEvalContext, n: PNode): PNode =
of mNBindSym: of mNBindSym:
# trivial implementation: # trivial implementation:
result = n.sons[1] 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: of mStrToIdent:
result = evalAux(c, n.sons[1], {}) result = evalAux(c, n.sons[1], {})
if isSpecial(result): return if isSpecial(result): return

View file

@ -85,11 +85,13 @@ type
TNimTypeKinds* = set[TNimrodTypeKind] TNimTypeKinds* = set[TNimrodTypeKind]
TNimrodSymKind* = enum TNimrodSymKind* = enum
nskUnknown, nskConditional, nskDynLib, nskParam, nskUnknown, nskConditional, nskDynLib, nskParam,
nskGenericParam, nskTemp, nskType, nskConst, nskGenericParam, nskTemp, nskModule, nskType, nskVar, nskLet,
nskVar, nskProc, nskMethod, nskIterator, nskConst, nskResult,
nskProc, nskMethod, nskIterator,
nskConverter, nskMacro, nskTemplate, nskField, nskConverter, nskMacro, nskTemplate, nskField,
nskEnumField, nskForVar, nskModule, nskLabel, nskEnumField, nskForVar, nskLabel,
nskStub nskStub
TNimSymKinds* = set[TNimrodSymKind] TNimSymKinds* = set[TNimrodSymKind]
type type
@ -220,6 +222,11 @@ proc bindSym*(ident: string, rule: TBindSymRule = brClosed): PNimrodNode {.
## If ``rule == brForceOpen`` always an ``nkOpenSymChoice`` tree is ## If ``rule == brForceOpen`` always an ``nkOpenSymChoice`` tree is
## returned even if the symbol is not ambiguous. ## 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".} proc callsite*(): PNimrodNode {.magic: "NCallSite".}
## returns the AST if the invokation expression that invoked this macro. ## returns the AST if the invokation expression that invoked this macro.

View file

@ -8,7 +8,6 @@ version 0.9.4
- mocking support with ``tyProxy`` that does: fallback for ``.`` operator - mocking support with ``tyProxy`` that does: fallback for ``.`` operator
- overloading of ``.``? Special case ``.=``? - overloading of ``.``? Special case ``.=``?
- built-in 'getImpl' - built-in 'getImpl'
- macros.gensym still missing?
- optimize 'genericReset'; 'newException' leads to code bloat - optimize 'genericReset'; 'newException' leads to code bloat

View file

@ -14,6 +14,8 @@ Bugfixes
Library Additions Library Additions
----------------- -----------------
- Added ``macros.genSym`` builtin for AST generation.
Changes affecting backwards compatibility Changes affecting backwards compatibility
----------------------------------------- -----------------------------------------