implemented macros.getImpl

This commit is contained in:
Araq
2015-08-21 02:34:34 +02:00
parent a2bb7d4c71
commit 695e2e970e
8 changed files with 39 additions and 2 deletions

View File

@@ -602,7 +602,7 @@ type
mNSetFloatVal, mNSetSymbol, mNSetIdent, mNSetType, mNSetStrVal, mNLineInfo,
mNNewNimNode, mNCopyNimNode, mNCopyNimTree, mStrToIdent, mIdentToStr,
mNBindSym, mLocals, mNCallSite,
mEqIdent, mEqNimrodNode, mSameNodeType,
mEqIdent, mEqNimrodNode, mSameNodeType, mGetImpl,
mNHint, mNWarning, mNError,
mInstantiationInfo, mGetTypeInfo, mNGenSym

View File

@@ -781,6 +781,14 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
regs[ra].node.add(copyTree(regs[rb].regToNode))
else:
stackTrace(c, tos, pc, errNilAccess)
of opcGetImpl:
decodeB(rkNode)
let a = regs[rb].node
if a.kind == nkSym:
regs[ra].node = if a.sym.ast.isNil: newNode(nkNilLit)
else: copyTree(a.sym.ast)
else:
stackTrace(c, tos, pc, errFieldXNotFound, "symbol")
of opcEcho:
let rb = instr.regB
if rb == 1:

View File

@@ -102,6 +102,7 @@ type
opcEqIdent,
opcStrToIdent,
opcIdentToStr,
opcGetImpl,
opcEcho,
opcIndCall, # dest = call regStart, n; where regStart = fn, arg1, ...

View File

@@ -948,6 +948,7 @@ proc genMagic(c: PCtx; n: PNode; dest: var TDest; m: TMagic) =
of mSlurp: genUnaryABC(c, n, dest, opcSlurp)
of mStaticExec: genBinaryABCD(c, n, dest, opcGorge)
of mNLen: genUnaryABI(c, n, dest, opcLenSeq)
of mGetImpl: genUnaryABC(c, n, dest, opcGetImpl)
of mNChild: genBinaryABC(c, n, dest, opcNChild)
of mNSetChild, mNDel:
unused(n, dest)

View File

@@ -213,6 +213,10 @@ proc newNimNode*(kind: NimNodeKind,
proc copyNimNode*(n: NimNode): NimNode {.magic: "NCopyNimNode", noSideEffect.}
proc copyNimTree*(n: NimNode): NimNode {.magic: "NCopyNimTree", noSideEffect.}
proc getImpl*(s: NimSym): NimNode {.magic: "GetImpl", noSideEffect.}
## retrieve the implementation of a symbol `s`. `s` can be a routine or a
## const.
proc error*(msg: string) {.magic: "NError", benign.}
## writes an error message at compile time

20
tests/macros/tgetimpl.nim Normal file
View File

@@ -0,0 +1,20 @@
discard """
msg: '''"muhaha"
proc poo(x, y: int) =
echo ["poo"]'''
"""
import macros
const
foo = "muhaha"
proc poo(x, y: int) =
echo "poo"
macro m(x: typed): untyped =
echo repr x.symbol.getImpl
result = x
discard m foo
discard m poo

View File

@@ -2,7 +2,6 @@ version 0.11.4
==============
- make tuple unpacking work in a non-var/let context
- built-in 'getImpl'
- document special cased varargs[untyped] and varargs[typed]

View File

@@ -61,6 +61,7 @@ News
- The ``expandSymlink`` proc has been added to the ``os`` module.
- The ``tailDir`` proc has been added to the ``os`` module.
Language Additions
------------------
@@ -68,6 +69,9 @@ News
variable or parameter for C interoperability. Since technically this
makes parameters and ``let`` variables mutable, it is considered even more
unsafe than the ordinary ``addr`` builtin.
- Added ``macros.getImpl`` that can be used to access the implementation of
a routine or a constant. This allows for example for user-defined inlining
of function calls.
Bugfixes