Rename PNimrodNode to NimNode
This commit is contained in:
parent
8e651fa0d4
commit
fd4e629905
30 changed files with 508 additions and 518 deletions
|
|
@ -120,12 +120,10 @@ const
|
|||
nnkCallKinds* = {nnkCall, nnkInfix, nnkPrefix, nnkPostfix, nnkCommand,
|
||||
nnkCallStrLit}
|
||||
|
||||
{.push warning[deprecated]: off.}
|
||||
|
||||
proc `[]`*(n: PNimrodNode, i: int): PNimrodNode {.magic: "NChild", noSideEffect.}
|
||||
proc `[]`*(n: NimNode, i: int): NimNode {.magic: "NChild", noSideEffect.}
|
||||
## get `n`'s `i`'th child.
|
||||
|
||||
proc `[]=`*(n: PNimrodNode, i: int, child: PNimrodNode) {.magic: "NSetChild",
|
||||
proc `[]=`*(n: NimNode, i: int, child: NimNode) {.magic: "NSetChild",
|
||||
noSideEffect.}
|
||||
## set `n`'s `i`'th child to `child`.
|
||||
|
||||
|
|
@ -141,34 +139,34 @@ proc `$`*(s: NimSym): string {.magic: "IdentToStr", noSideEffect.}
|
|||
proc `==`*(a, b: NimIdent): bool {.magic: "EqIdent", noSideEffect.}
|
||||
## compares two Nim identifiers
|
||||
|
||||
proc `==`*(a, b: PNimrodNode): bool {.magic: "EqNimrodNode", noSideEffect.}
|
||||
proc `==`*(a, b: NimNode): bool {.magic: "EqNimrodNode", noSideEffect.}
|
||||
## compares two Nim nodes
|
||||
|
||||
proc len*(n: PNimrodNode): int {.magic: "NLen", noSideEffect.}
|
||||
proc len*(n: NimNode): int {.magic: "NLen", noSideEffect.}
|
||||
## returns the number of children of `n`.
|
||||
|
||||
proc add*(father, child: PNimrodNode): PNimrodNode {.magic: "NAdd", discardable,
|
||||
proc add*(father, child: NimNode): NimNode {.magic: "NAdd", discardable,
|
||||
noSideEffect, locks: 0.}
|
||||
## Adds the `child` to the `father` node. Returns the
|
||||
## father node so that calls can be nested.
|
||||
|
||||
proc add*(father: PNimrodNode, children: varargs[PNimrodNode]): PNimrodNode {.
|
||||
proc add*(father: NimNode, children: varargs[NimNode]): NimNode {.
|
||||
magic: "NAddMultiple", discardable, noSideEffect, locks: 0.}
|
||||
## Adds each child of `children` to the `father` node.
|
||||
## Returns the `father` node so that calls can be nested.
|
||||
|
||||
proc del*(father: PNimrodNode, idx = 0, n = 1) {.magic: "NDel", noSideEffect.}
|
||||
proc del*(father: NimNode, idx = 0, n = 1) {.magic: "NDel", noSideEffect.}
|
||||
## deletes `n` children of `father` starting at index `idx`.
|
||||
|
||||
proc kind*(n: PNimrodNode): TNimrodNodeKind {.magic: "NKind", noSideEffect.}
|
||||
proc kind*(n: NimNode): NimNodeKind {.magic: "NKind", noSideEffect.}
|
||||
## returns the `kind` of the node `n`.
|
||||
|
||||
proc intVal*(n: PNimrodNode): BiggestInt {.magic: "NIntVal", noSideEffect.}
|
||||
proc floatVal*(n: PNimrodNode): BiggestFloat {.magic: "NFloatVal", noSideEffect.}
|
||||
proc symbol*(n: PNimrodNode): NimSym {.magic: "NSymbol", noSideEffect.}
|
||||
proc ident*(n: PNimrodNode): NimIdent {.magic: "NIdent", noSideEffect.}
|
||||
proc intVal*(n: NimNode): BiggestInt {.magic: "NIntVal", noSideEffect.}
|
||||
proc floatVal*(n: NimNode): BiggestFloat {.magic: "NFloatVal", noSideEffect.}
|
||||
proc symbol*(n: NimNode): NimSym {.magic: "NSymbol", noSideEffect.}
|
||||
proc ident*(n: NimNode): NimIdent {.magic: "NIdent", noSideEffect.}
|
||||
|
||||
proc getType*(n: PNimrodNode): PNimrodNode {.magic: "NGetType", noSideEffect.}
|
||||
proc getType*(n: NimNode): NimNode {.magic: "NGetType", noSideEffect.}
|
||||
## with 'getType' you can access the node's `type`:idx:. A Nim type is
|
||||
## mapped to a Nim AST too, so it's slightly confusing but it means the same
|
||||
## API can be used to traverse types. Recursive types are flattened for you
|
||||
|
|
@ -176,30 +174,30 @@ proc getType*(n: PNimrodNode): PNimrodNode {.magic: "NGetType", noSideEffect.}
|
|||
## resolve recursive types, you have to call 'getType' again. To see what
|
||||
## kind of type it is, call `typeKind` on getType's result.
|
||||
|
||||
proc typeKind*(n: PNimrodNode): NimTypeKind {.magic: "NGetType", noSideEffect.}
|
||||
proc typeKind*(n: NimNode): NimTypeKind {.magic: "NGetType", noSideEffect.}
|
||||
## Returns the type kind of the node 'n' that should represent a type, that
|
||||
## means the node should have been obtained via `getType`.
|
||||
|
||||
proc strVal*(n: PNimrodNode): string {.magic: "NStrVal", noSideEffect.}
|
||||
proc strVal*(n: NimNode): string {.magic: "NStrVal", noSideEffect.}
|
||||
|
||||
proc `intVal=`*(n: PNimrodNode, val: BiggestInt) {.magic: "NSetIntVal", noSideEffect.}
|
||||
proc `floatVal=`*(n: PNimrodNode, val: BiggestFloat) {.magic: "NSetFloatVal", noSideEffect.}
|
||||
proc `symbol=`*(n: PNimrodNode, val: NimSym) {.magic: "NSetSymbol", noSideEffect.}
|
||||
proc `ident=`*(n: PNimrodNode, val: NimIdent) {.magic: "NSetIdent", noSideEffect.}
|
||||
#proc `typ=`*(n: PNimrodNode, typ: typedesc) {.magic: "NSetType".}
|
||||
proc `intVal=`*(n: NimNode, val: BiggestInt) {.magic: "NSetIntVal", noSideEffect.}
|
||||
proc `floatVal=`*(n: NimNode, val: BiggestFloat) {.magic: "NSetFloatVal", noSideEffect.}
|
||||
proc `symbol=`*(n: NimNode, val: NimSym) {.magic: "NSetSymbol", noSideEffect.}
|
||||
proc `ident=`*(n: NimNode, val: NimIdent) {.magic: "NSetIdent", noSideEffect.}
|
||||
#proc `typ=`*(n: NimNode, typ: typedesc) {.magic: "NSetType".}
|
||||
# this is not sound! Unfortunately forbidding 'typ=' is not enough, as you
|
||||
# can easily do:
|
||||
# let bracket = semCheck([1, 2])
|
||||
# let fake = semCheck(2.0)
|
||||
# bracket[0] = fake # constructs a mixed array with ints and floats!
|
||||
|
||||
proc `strVal=`*(n: PNimrodNode, val: string) {.magic: "NSetStrVal", noSideEffect.}
|
||||
proc `strVal=`*(n: NimNode, val: string) {.magic: "NSetStrVal", noSideEffect.}
|
||||
|
||||
proc newNimNode*(kind: TNimrodNodeKind,
|
||||
n: PNimrodNode=nil): PNimrodNode {.magic: "NNewNimNode", noSideEffect.}
|
||||
proc newNimNode*(kind: NimNodeKind,
|
||||
n: NimNode=nil): NimNode {.magic: "NNewNimNode", noSideEffect.}
|
||||
|
||||
proc copyNimNode*(n: PNimrodNode): PNimrodNode {.magic: "NCopyNimNode", noSideEffect.}
|
||||
proc copyNimTree*(n: PNimrodNode): PNimrodNode {.magic: "NCopyNimTree", noSideEffect.}
|
||||
proc copyNimNode*(n: NimNode): NimNode {.magic: "NCopyNimNode", noSideEffect.}
|
||||
proc copyNimTree*(n: NimNode): NimNode {.magic: "NCopyNimTree", noSideEffect.}
|
||||
|
||||
proc error*(msg: string) {.magic: "NError", benign.}
|
||||
## writes an error message at compile time
|
||||
|
|
@ -210,27 +208,27 @@ proc warning*(msg: string) {.magic: "NWarning", benign.}
|
|||
proc hint*(msg: string) {.magic: "NHint", benign.}
|
||||
## writes a hint message at compile time
|
||||
|
||||
proc newStrLitNode*(s: string): PNimrodNode {.compileTime, noSideEffect.} =
|
||||
proc newStrLitNode*(s: string): NimNode {.compileTime, noSideEffect.} =
|
||||
## creates a string literal node from `s`
|
||||
result = newNimNode(nnkStrLit)
|
||||
result.strVal = s
|
||||
|
||||
proc newIntLitNode*(i: BiggestInt): PNimrodNode {.compileTime.} =
|
||||
proc newIntLitNode*(i: BiggestInt): NimNode {.compileTime.} =
|
||||
## creates a int literal node from `i`
|
||||
result = newNimNode(nnkIntLit)
|
||||
result.intVal = i
|
||||
|
||||
proc newFloatLitNode*(f: BiggestFloat): PNimrodNode {.compileTime.} =
|
||||
proc newFloatLitNode*(f: BiggestFloat): NimNode {.compileTime.} =
|
||||
## creates a float literal node from `f`
|
||||
result = newNimNode(nnkFloatLit)
|
||||
result.floatVal = f
|
||||
|
||||
proc newIdentNode*(i: NimIdent): PNimrodNode {.compileTime.} =
|
||||
proc newIdentNode*(i: NimIdent): NimNode {.compileTime.} =
|
||||
## creates an identifier node from `i`
|
||||
result = newNimNode(nnkIdent)
|
||||
result.ident = i
|
||||
|
||||
proc newIdentNode*(i: string): PNimrodNode {.compileTime.} =
|
||||
proc newIdentNode*(i: string): NimNode {.compileTime.} =
|
||||
## creates an identifier node from `i`
|
||||
result = newNimNode(nnkIdent)
|
||||
result.ident = !i
|
||||
|
|
@ -247,7 +245,7 @@ type
|
|||
|
||||
{.deprecated: [TBindSymRule: BindSymRule].}
|
||||
|
||||
proc bindSym*(ident: string, rule: BindSymRule = brClosed): PNimrodNode {.
|
||||
proc bindSym*(ident: string, rule: BindSymRule = brClosed): NimNode {.
|
||||
magic: "NBindSym", noSideEffect.}
|
||||
## creates a node that binds `ident` to a symbol node. The bound symbol
|
||||
## may be an overloaded symbol.
|
||||
|
|
@ -258,48 +256,48 @@ proc bindSym*(ident: string, rule: BindSymRule = brClosed): PNimrodNode {.
|
|||
## If ``rule == brForceOpen`` always an ``nkOpenSymChoice`` tree is
|
||||
## returned even if the symbol is not ambiguous.
|
||||
|
||||
proc genSym*(kind: NimSymKind = nskLet; ident = ""): PNimrodNode {.
|
||||
proc genSym*(kind: NimSymKind = nskLet; ident = ""): NimNode {.
|
||||
magic: "NGenSym", noSideEffect.}
|
||||
## generates a fresh symbol that is guaranteed to be unique. The symbol
|
||||
## needs to occur in a declaration context.
|
||||
|
||||
proc callsite*(): PNimrodNode {.magic: "NCallSite", benign.}
|
||||
proc callsite*(): NimNode {.magic: "NCallSite", benign.}
|
||||
## returns the AST of the invocation expression that invoked this macro.
|
||||
|
||||
proc toStrLit*(n: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc toStrLit*(n: NimNode): NimNode {.compileTime.} =
|
||||
## converts the AST `n` to the concrete Nim code and wraps that
|
||||
## in a string literal node
|
||||
return newStrLitNode(repr(n))
|
||||
|
||||
proc lineinfo*(n: PNimrodNode): string {.magic: "NLineInfo", noSideEffect.}
|
||||
proc lineinfo*(n: NimNode): string {.magic: "NLineInfo", noSideEffect.}
|
||||
## returns the position the node appears in the original source file
|
||||
## in the form filename(line, col)
|
||||
|
||||
proc internalParseExpr(s: string): PNimrodNode {.
|
||||
proc internalParseExpr(s: string): NimNode {.
|
||||
magic: "ParseExprToAst", noSideEffect.}
|
||||
|
||||
proc internalParseStmt(s: string): PNimrodNode {.
|
||||
proc internalParseStmt(s: string): NimNode {.
|
||||
magic: "ParseStmtToAst", noSideEffect.}
|
||||
|
||||
proc internalErrorFlag*(): string {.magic: "NError", noSideEffect.}
|
||||
## Some builtins set an error flag. This is then turned into a proper
|
||||
## exception. **Note**: Ordinary application code should not call this.
|
||||
|
||||
proc parseExpr*(s: string): PNimrodNode {.noSideEffect, compileTime.} =
|
||||
proc parseExpr*(s: string): NimNode {.noSideEffect, compileTime.} =
|
||||
## Compiles the passed string to its AST representation.
|
||||
## Expects a single expression. Raises ``ValueError`` for parsing errors.
|
||||
result = internalParseExpr(s)
|
||||
let x = internalErrorFlag()
|
||||
if x.len > 0: raise newException(ValueError, x)
|
||||
|
||||
proc parseStmt*(s: string): PNimrodNode {.noSideEffect, compileTime.} =
|
||||
proc parseStmt*(s: string): NimNode {.noSideEffect, compileTime.} =
|
||||
## Compiles the passed string to its AST representation.
|
||||
## Expects one or more statements. Raises ``ValueError`` for parsing errors.
|
||||
result = internalParseStmt(s)
|
||||
let x = internalErrorFlag()
|
||||
if x.len > 0: raise newException(ValueError, x)
|
||||
|
||||
proc getAst*(macroOrTemplate: expr): PNimrodNode {.magic: "ExpandToAst", noSideEffect.}
|
||||
proc getAst*(macroOrTemplate: expr): NimNode {.magic: "ExpandToAst", noSideEffect.}
|
||||
## Obtains the AST nodes returned from a macro or template invocation.
|
||||
## Example:
|
||||
##
|
||||
|
|
@ -308,10 +306,10 @@ proc getAst*(macroOrTemplate: expr): PNimrodNode {.magic: "ExpandToAst", noSideE
|
|||
## macro FooMacro() =
|
||||
## var ast = getAst(BarTemplate())
|
||||
|
||||
proc quote*(bl: stmt, op = "``"): PNimrodNode {.magic: "QuoteAst", noSideEffect.}
|
||||
proc quote*(bl: stmt, op = "``"): NimNode {.magic: "QuoteAst", noSideEffect.}
|
||||
## Quasi-quoting operator.
|
||||
## Accepts an expression or a block and returns the AST that represents it.
|
||||
## Within the quoted AST, you are able to interpolate PNimrodNode expressions
|
||||
## Within the quoted AST, you are able to interpolate NimNode expressions
|
||||
## from the surrounding scope. If no operator is given, quoting is done using
|
||||
## backticks. Otherwise, the given operator must be used as a prefix operator
|
||||
## for any interpolated expression. The original meaning of the interpolation
|
||||
|
|
@ -339,26 +337,26 @@ proc quote*(bl: stmt, op = "``"): PNimrodNode {.magic: "QuoteAst", noSideEffect.
|
|||
## if not `ex`:
|
||||
## echo `info` & ": Check failed: " & `expString`
|
||||
|
||||
proc expectKind*(n: PNimrodNode, k: TNimrodNodeKind) {.compileTime.} =
|
||||
proc expectKind*(n: NimNode, k: NimNodeKind) {.compileTime.} =
|
||||
## checks that `n` is of kind `k`. If this is not the case,
|
||||
## compilation aborts with an error message. This is useful for writing
|
||||
## macros that check the AST that is passed to them.
|
||||
if n.kind != k: error("Expected a node of kind " & $k & ", got " & $n.kind)
|
||||
|
||||
proc expectMinLen*(n: PNimrodNode, min: int) {.compileTime.} =
|
||||
proc expectMinLen*(n: NimNode, min: int) {.compileTime.} =
|
||||
## checks that `n` has at least `min` children. If this is not the case,
|
||||
## compilation aborts with an error message. This is useful for writing
|
||||
## macros that check its number of arguments.
|
||||
if n.len < min: error("macro expects a node with " & $min & " children")
|
||||
|
||||
proc expectLen*(n: PNimrodNode, len: int) {.compileTime.} =
|
||||
proc expectLen*(n: NimNode, len: int) {.compileTime.} =
|
||||
## checks that `n` has exactly `len` children. If this is not the case,
|
||||
## compilation aborts with an error message. This is useful for writing
|
||||
## macros that check its number of arguments.
|
||||
if n.len != len: error("macro expects a node with " & $len & " children")
|
||||
|
||||
proc newCall*(theProc: PNimrodNode,
|
||||
args: varargs[PNimrodNode]): PNimrodNode {.compileTime.} =
|
||||
proc newCall*(theProc: NimNode,
|
||||
args: varargs[NimNode]): NimNode {.compileTime.} =
|
||||
## produces a new call node. `theProc` is the proc that is called with
|
||||
## the arguments ``args[0..]``.
|
||||
result = newNimNode(nnkCall)
|
||||
|
|
@ -366,7 +364,7 @@ proc newCall*(theProc: PNimrodNode,
|
|||
result.add(args)
|
||||
|
||||
proc newCall*(theProc: NimIdent,
|
||||
args: varargs[PNimrodNode]): PNimrodNode {.compileTime.} =
|
||||
args: varargs[NimNode]): NimNode {.compileTime.} =
|
||||
## produces a new call node. `theProc` is the proc that is called with
|
||||
## the arguments ``args[0..]``.
|
||||
result = newNimNode(nnkCall)
|
||||
|
|
@ -374,35 +372,35 @@ proc newCall*(theProc: NimIdent,
|
|||
result.add(args)
|
||||
|
||||
proc newCall*(theProc: string,
|
||||
args: varargs[PNimrodNode]): PNimrodNode {.compileTime.} =
|
||||
args: varargs[NimNode]): NimNode {.compileTime.} =
|
||||
## produces a new call node. `theProc` is the proc that is called with
|
||||
## the arguments ``args[0..]``.
|
||||
result = newNimNode(nnkCall)
|
||||
result.add(newIdentNode(theProc))
|
||||
result.add(args)
|
||||
|
||||
proc newLit*(c: char): PNimrodNode {.compileTime.} =
|
||||
proc newLit*(c: char): NimNode {.compileTime.} =
|
||||
## produces a new character literal node.
|
||||
result = newNimNode(nnkCharLit)
|
||||
result.intVal = ord(c)
|
||||
|
||||
proc newLit*(i: BiggestInt): PNimrodNode {.compileTime.} =
|
||||
proc newLit*(i: BiggestInt): NimNode {.compileTime.} =
|
||||
## produces a new integer literal node.
|
||||
result = newNimNode(nnkIntLit)
|
||||
result.intVal = i
|
||||
|
||||
proc newLit*(f: BiggestFloat): PNimrodNode {.compileTime.} =
|
||||
proc newLit*(f: BiggestFloat): NimNode {.compileTime.} =
|
||||
## produces a new float literal node.
|
||||
result = newNimNode(nnkFloatLit)
|
||||
result.floatVal = f
|
||||
|
||||
proc newLit*(s: string): PNimrodNode {.compileTime.} =
|
||||
proc newLit*(s: string): NimNode {.compileTime.} =
|
||||
## produces a new string literal node.
|
||||
result = newNimNode(nnkStrLit)
|
||||
result.strVal = s
|
||||
|
||||
proc nestList*(theProc: NimIdent,
|
||||
x: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
x: NimNode): NimNode {.compileTime.} =
|
||||
## nests the list `x` into a tree of call expressions:
|
||||
## ``[a, b, c]`` is transformed into ``theProc(a, theProc(c, d))``.
|
||||
var L = x.len
|
||||
|
|
@ -413,11 +411,11 @@ proc nestList*(theProc: NimIdent,
|
|||
# This could easily user code and so should be fixed in evals.nim somehow.
|
||||
result = newCall(theProc, x[i], copyNimTree(result))
|
||||
|
||||
proc treeRepr*(n: PNimrodNode): string {.compileTime, benign.} =
|
||||
proc treeRepr*(n: NimNode): string {.compileTime, benign.} =
|
||||
## Convert the AST `n` to a human-readable tree-like string.
|
||||
##
|
||||
## See also `repr` and `lispRepr`.
|
||||
proc traverse(res: var string, level: int, n: PNimrodNode) {.benign.} =
|
||||
proc traverse(res: var string, level: int, n: NimNode) {.benign.} =
|
||||
for i in 0..level-1: res.add " "
|
||||
res.add(($n.kind).substr(3))
|
||||
|
||||
|
|
@ -438,7 +436,7 @@ proc treeRepr*(n: PNimrodNode): string {.compileTime, benign.} =
|
|||
result = ""
|
||||
traverse(result, 0, n)
|
||||
|
||||
proc lispRepr*(n: PNimrodNode): string {.compileTime, benign.} =
|
||||
proc lispRepr*(n: NimNode): string {.compileTime, benign.} =
|
||||
## Convert the AST `n` to a human-readable lisp-like string,
|
||||
##
|
||||
## See also `repr` and `treeRepr`.
|
||||
|
|
@ -485,56 +483,56 @@ macro dumpLispImm*(s: stmt): stmt {.immediate, deprecated.} = echo s.lispRepr
|
|||
## The ``immediate`` version of `dumpLisp`.
|
||||
|
||||
|
||||
proc newEmptyNode*(): PNimrodNode {.compileTime, noSideEffect.} =
|
||||
proc newEmptyNode*(): NimNode {.compileTime, noSideEffect.} =
|
||||
## Create a new empty node
|
||||
result = newNimNode(nnkEmpty)
|
||||
|
||||
proc newStmtList*(stmts: varargs[PNimrodNode]): PNimrodNode {.compileTime.}=
|
||||
proc newStmtList*(stmts: varargs[NimNode]): NimNode {.compileTime.}=
|
||||
## Create a new statement list
|
||||
result = newNimNode(nnkStmtList).add(stmts)
|
||||
|
||||
proc newPar*(exprs: varargs[PNimrodNode]): PNimrodNode {.compileTime.}=
|
||||
proc newPar*(exprs: varargs[NimNode]): NimNode {.compileTime.}=
|
||||
## Create a new parentheses-enclosed expression
|
||||
newNimNode(nnkPar).add(exprs)
|
||||
|
||||
proc newBlockStmt*(label, body: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc newBlockStmt*(label, body: NimNode): NimNode {.compileTime.} =
|
||||
## Create a new block statement with label
|
||||
return newNimNode(nnkBlockStmt).add(label, body)
|
||||
|
||||
proc newBlockStmt*(body: PNimrodNode): PNimrodNode {.compiletime.} =
|
||||
proc newBlockStmt*(body: NimNode): NimNode {.compiletime.} =
|
||||
## Create a new block: stmt
|
||||
return newNimNode(nnkBlockStmt).add(newEmptyNode(), body)
|
||||
|
||||
proc newVarStmt*(name, value: PNimrodNode): PNimrodNode {.compiletime.} =
|
||||
proc newVarStmt*(name, value: NimNode): NimNode {.compiletime.} =
|
||||
## Create a new var stmt
|
||||
return newNimNode(nnkVarSection).add(
|
||||
newNimNode(nnkIdentDefs).add(name, newNimNode(nnkEmpty), value))
|
||||
|
||||
proc newLetStmt*(name, value: PNimrodNode): PNimrodNode {.compiletime.} =
|
||||
proc newLetStmt*(name, value: NimNode): NimNode {.compiletime.} =
|
||||
## Create a new let stmt
|
||||
return newNimNode(nnkLetSection).add(
|
||||
newNimNode(nnkIdentDefs).add(name, newNimNode(nnkEmpty), value))
|
||||
|
||||
proc newConstStmt*(name, value: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc newConstStmt*(name, value: NimNode): NimNode {.compileTime.} =
|
||||
## Create a new const stmt
|
||||
newNimNode(nnkConstSection).add(
|
||||
newNimNode(nnkConstDef).add(name, newNimNode(nnkEmpty), value))
|
||||
|
||||
proc newAssignment*(lhs, rhs: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc newAssignment*(lhs, rhs: NimNode): NimNode {.compileTime.} =
|
||||
return newNimNode(nnkAsgn).add(lhs, rhs)
|
||||
|
||||
proc newDotExpr*(a, b: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc newDotExpr*(a, b: NimNode): NimNode {.compileTime.} =
|
||||
## Create new dot expression
|
||||
## a.dot(b) -> `a.b`
|
||||
return newNimNode(nnkDotExpr).add(a, b)
|
||||
|
||||
proc newColonExpr*(a, b: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc newColonExpr*(a, b: NimNode): NimNode {.compileTime.} =
|
||||
## Create new colon expression
|
||||
## newColonExpr(a, b) -> `a: b`
|
||||
newNimNode(nnkExprColonExpr).add(a, b)
|
||||
|
||||
proc newIdentDefs*(name, kind: PNimrodNode;
|
||||
default = newEmptyNode()): PNimrodNode {.compileTime.} =
|
||||
proc newIdentDefs*(name, kind: NimNode;
|
||||
default = newEmptyNode()): NimNode {.compileTime.} =
|
||||
## Creates a new ``nnkIdentDefs`` node of a specific kind and value.
|
||||
##
|
||||
## ``nnkIdentDefs`` need to have at least three children, but they can have
|
||||
|
|
@ -565,13 +563,13 @@ proc newIdentDefs*(name, kind: PNimrodNode;
|
|||
## newStrLitNode("Hello"))
|
||||
newNimNode(nnkIdentDefs).add(name, kind, default)
|
||||
|
||||
proc newNilLit*(): PNimrodNode {.compileTime.} =
|
||||
proc newNilLit*(): NimNode {.compileTime.} =
|
||||
## New nil literal shortcut
|
||||
result = newNimNode(nnkNilLit)
|
||||
|
||||
proc high*(node: PNimrodNode): int {.compileTime.} = len(node) - 1
|
||||
proc high*(node: NimNode): int {.compileTime.} = len(node) - 1
|
||||
## Return the highest index available for a node
|
||||
proc last*(node: PNimrodNode): PNimrodNode {.compileTime.} = node[node.high]
|
||||
proc last*(node: NimNode): NimNode {.compileTime.} = node[node.high]
|
||||
## Return the last item in nodes children. Same as `node[node.high()]`
|
||||
|
||||
|
||||
|
|
@ -581,11 +579,11 @@ const
|
|||
CallNodes* = {nnkCall, nnkInfix, nnkPrefix, nnkPostfix, nnkCommand,
|
||||
nnkCallStrLit, nnkHiddenCallConv}
|
||||
|
||||
proc expectKind*(n: PNimrodNode; k: set[TNimrodNodeKind]) {.compileTime.} =
|
||||
proc expectKind*(n: NimNode; k: set[NimNodeKind]) {.compileTime.} =
|
||||
assert n.kind in k, "Expected one of " & $k & ", got " & $n.kind
|
||||
|
||||
proc newProc*(name = newEmptyNode(); params: openArray[PNimrodNode] = [newEmptyNode()];
|
||||
body: PNimrodNode = newStmtList(), procType = nnkProcDef): PNimrodNode {.compileTime.} =
|
||||
proc newProc*(name = newEmptyNode(); params: openArray[NimNode] = [newEmptyNode()];
|
||||
body: NimNode = newStmtList(), procType = nnkProcDef): NimNode {.compileTime.} =
|
||||
## shortcut for creating a new proc
|
||||
##
|
||||
## The ``params`` array must start with the return type of the proc,
|
||||
|
|
@ -600,8 +598,8 @@ proc newProc*(name = newEmptyNode(); params: openArray[PNimrodNode] = [newEmptyN
|
|||
newEmptyNode(),
|
||||
body)
|
||||
|
||||
proc newIfStmt*(branches: varargs[tuple[cond, body: PNimrodNode]]):
|
||||
PNimrodNode {.compiletime.} =
|
||||
proc newIfStmt*(branches: varargs[tuple[cond, body: NimNode]]):
|
||||
NimNode {.compiletime.} =
|
||||
## Constructor for ``if`` statements.
|
||||
##
|
||||
## .. code-block:: nim
|
||||
|
|
@ -616,35 +614,35 @@ proc newIfStmt*(branches: varargs[tuple[cond, body: PNimrodNode]]):
|
|||
result.add(newNimNode(nnkElifBranch).add(i.cond, i.body))
|
||||
|
||||
|
||||
proc copyChildrenTo*(src, dest: PNimrodNode) {.compileTime.}=
|
||||
proc copyChildrenTo*(src, dest: NimNode) {.compileTime.}=
|
||||
## Copy all children from `src` to `dest`
|
||||
for i in 0 .. < src.len:
|
||||
dest.add src[i].copyNimTree
|
||||
|
||||
template expectRoutine(node: PNimrodNode): stmt =
|
||||
template expectRoutine(node: NimNode): stmt =
|
||||
expectKind(node, RoutineNodes)
|
||||
|
||||
proc name*(someProc: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc name*(someProc: NimNode): NimNode {.compileTime.} =
|
||||
someProc.expectRoutine
|
||||
result = someProc[0]
|
||||
proc `name=`*(someProc: PNimrodNode; val: PNimrodNode) {.compileTime.} =
|
||||
proc `name=`*(someProc: NimNode; val: NimNode) {.compileTime.} =
|
||||
someProc.expectRoutine
|
||||
someProc[0] = val
|
||||
|
||||
proc params*(someProc: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc params*(someProc: NimNode): NimNode {.compileTime.} =
|
||||
someProc.expectRoutine
|
||||
result = someProc[3]
|
||||
proc `params=`* (someProc: PNimrodNode; params: PNimrodNode) {.compileTime.}=
|
||||
proc `params=`* (someProc: NimNode; params: NimNode) {.compileTime.}=
|
||||
someProc.expectRoutine
|
||||
assert params.kind == nnkFormalParams
|
||||
someProc[3] = params
|
||||
|
||||
proc pragma*(someProc: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc pragma*(someProc: NimNode): NimNode {.compileTime.} =
|
||||
## Get the pragma of a proc type
|
||||
## These will be expanded
|
||||
someProc.expectRoutine
|
||||
result = someProc[4]
|
||||
proc `pragma=`*(someProc: PNimrodNode; val: PNimrodNode){.compileTime.}=
|
||||
proc `pragma=`*(someProc: NimNode; val: NimNode){.compileTime.}=
|
||||
## Set the pragma of a proc type
|
||||
someProc.expectRoutine
|
||||
assert val.kind in {nnkEmpty, nnkPragma}
|
||||
|
|
@ -654,7 +652,7 @@ proc `pragma=`*(someProc: PNimrodNode; val: PNimrodNode){.compileTime.}=
|
|||
template badNodeKind(k; f): stmt{.immediate.} =
|
||||
assert false, "Invalid node kind " & $k & " for macros.`" & $f & "`"
|
||||
|
||||
proc body*(someProc: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc body*(someProc: NimNode): NimNode {.compileTime.} =
|
||||
case someProc.kind:
|
||||
of RoutineNodes:
|
||||
return someProc[6]
|
||||
|
|
@ -665,7 +663,7 @@ proc body*(someProc: PNimrodNode): PNimrodNode {.compileTime.} =
|
|||
else:
|
||||
badNodeKind someProc.kind, "body"
|
||||
|
||||
proc `body=`*(someProc: PNimrodNode, val: PNimrodNode) {.compileTime.} =
|
||||
proc `body=`*(someProc: NimNode, val: NimNode) {.compileTime.} =
|
||||
case someProc.kind
|
||||
of RoutineNodes:
|
||||
someProc[6] = val
|
||||
|
|
@ -676,10 +674,10 @@ proc `body=`*(someProc: PNimrodNode, val: PNimrodNode) {.compileTime.} =
|
|||
else:
|
||||
badNodeKind someProc.kind, "body="
|
||||
|
||||
proc basename*(a: PNimrodNode): PNimrodNode {.compiletime, benign.}
|
||||
proc basename*(a: NimNode): NimNode {.compiletime, benign.}
|
||||
|
||||
|
||||
proc `$`*(node: PNimrodNode): string {.compileTime.} =
|
||||
proc `$`*(node: NimNode): string {.compileTime.} =
|
||||
## Get the string of an identifier node
|
||||
case node.kind
|
||||
of nnkIdent:
|
||||
|
|
@ -693,14 +691,14 @@ proc `$`*(node: PNimrodNode): string {.compileTime.} =
|
|||
else:
|
||||
badNodeKind node.kind, "$"
|
||||
|
||||
proc ident*(name: string): PNimrodNode {.compileTime,inline.} = newIdentNode(name)
|
||||
proc ident*(name: string): NimNode {.compileTime,inline.} = newIdentNode(name)
|
||||
## Create a new ident node from a string
|
||||
|
||||
iterator children*(n: PNimrodNode): PNimrodNode {.inline.}=
|
||||
iterator children*(n: NimNode): NimNode {.inline.}=
|
||||
for i in 0 .. high(n):
|
||||
yield n[i]
|
||||
|
||||
template findChild*(n: PNimrodNode; cond: expr): PNimrodNode {.
|
||||
template findChild*(n: NimNode; cond: expr): NimNode {.
|
||||
immediate, dirty.} =
|
||||
## Find the first child node matching condition (or nil).
|
||||
##
|
||||
|
|
@ -708,14 +706,14 @@ template findChild*(n: PNimrodNode; cond: expr): PNimrodNode {.
|
|||
## var res = findChild(n, it.kind == nnkPostfix and
|
||||
## it.basename.ident == !"foo")
|
||||
block:
|
||||
var result: PNimrodNode
|
||||
var result: NimNode
|
||||
for it in n.children:
|
||||
if cond:
|
||||
result = it
|
||||
break
|
||||
result
|
||||
|
||||
proc insert*(a: PNimrodNode; pos: int; b: PNimrodNode) {.compileTime.} =
|
||||
proc insert*(a: NimNode; pos: int; b: NimNode) {.compileTime.} =
|
||||
## Insert node B into A at pos
|
||||
if high(a) < pos:
|
||||
## add some empty nodes first
|
||||
|
|
@ -730,7 +728,7 @@ proc insert*(a: PNimrodNode; pos: int; b: PNimrodNode) {.compileTime.} =
|
|||
a[i + 1] = a[i]
|
||||
a[pos] = b
|
||||
|
||||
proc basename*(a: PNimrodNode): PNimrodNode =
|
||||
proc basename*(a: NimNode): NimNode =
|
||||
## Pull an identifier from prefix/postfix expressions
|
||||
case a.kind
|
||||
of nnkIdent: return a
|
||||
|
|
@ -738,39 +736,39 @@ proc basename*(a: PNimrodNode): PNimrodNode =
|
|||
else:
|
||||
quit "Do not know how to get basename of ("& treeRepr(a) &")\n"& repr(a)
|
||||
|
||||
proc `basename=`*(a: PNimrodNode; val: string) {.compileTime.}=
|
||||
proc `basename=`*(a: NimNode; val: string) {.compileTime.}=
|
||||
case a.kind
|
||||
of nnkIdent: macros.`ident=`(a, !val)
|
||||
of nnkPostfix, nnkPrefix: a[1] = ident(val)
|
||||
else:
|
||||
quit "Do not know how to get basename of ("& treeRepr(a)& ")\n"& repr(a)
|
||||
|
||||
proc postfix*(node: PNimrodNode; op: string): PNimrodNode {.compileTime.} =
|
||||
proc postfix*(node: NimNode; op: string): NimNode {.compileTime.} =
|
||||
newNimNode(nnkPostfix).add(ident(op), node)
|
||||
|
||||
proc prefix*(node: PNimrodNode; op: string): PNimrodNode {.compileTime.} =
|
||||
proc prefix*(node: NimNode; op: string): NimNode {.compileTime.} =
|
||||
newNimNode(nnkPrefix).add(ident(op), node)
|
||||
|
||||
proc infix*(a: PNimrodNode; op: string;
|
||||
b: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc infix*(a: NimNode; op: string;
|
||||
b: NimNode): NimNode {.compileTime.} =
|
||||
newNimNode(nnkInfix).add(ident(op), a, b)
|
||||
|
||||
proc unpackPostfix*(node: PNimrodNode): tuple[node: PNimrodNode; op: string] {.
|
||||
proc unpackPostfix*(node: NimNode): tuple[node: NimNode; op: string] {.
|
||||
compileTime.} =
|
||||
node.expectKind nnkPostfix
|
||||
result = (node[0], $node[1])
|
||||
|
||||
proc unpackPrefix*(node: PNimrodNode): tuple[node: PNimrodNode; op: string] {.
|
||||
proc unpackPrefix*(node: NimNode): tuple[node: NimNode; op: string] {.
|
||||
compileTime.} =
|
||||
node.expectKind nnkPrefix
|
||||
result = (node[0], $node[1])
|
||||
|
||||
proc unpackInfix*(node: PNimrodNode): tuple[left: PNimrodNode; op: string;
|
||||
right: PNimrodNode] {.compileTime.} =
|
||||
proc unpackInfix*(node: NimNode): tuple[left: NimNode; op: string;
|
||||
right: NimNode] {.compileTime.} =
|
||||
assert node.kind == nnkInfix
|
||||
result = (node[0], $node[1], node[2])
|
||||
|
||||
proc copy*(node: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc copy*(node: NimNode): NimNode {.compileTime.} =
|
||||
## An alias for copyNimTree().
|
||||
return node.copyNimTree()
|
||||
|
||||
|
|
@ -793,7 +791,7 @@ proc cmpIgnoreStyle(a, b: cstring): int {.noSideEffect.} =
|
|||
proc eqIdent* (a, b: string): bool = cmpIgnoreStyle(a, b) == 0
|
||||
## Check if two idents are identical.
|
||||
|
||||
proc hasArgOfName* (params: PNimrodNode; name: string): bool {.compiletime.}=
|
||||
proc hasArgOfName* (params: NimNode; name: string): bool {.compiletime.}=
|
||||
## Search nnkFormalParams for an argument.
|
||||
assert params.kind == nnkFormalParams
|
||||
for i in 1 .. <params.len:
|
||||
|
|
@ -801,7 +799,7 @@ proc hasArgOfName* (params: PNimrodNode; name: string): bool {.compiletime.}=
|
|||
if name.eqIdent( $ node[0]):
|
||||
return true
|
||||
|
||||
proc addIdentIfAbsent*(dest: PNimrodNode, ident: string) {.compiletime.} =
|
||||
proc addIdentIfAbsent*(dest: NimNode, ident: string) {.compiletime.} =
|
||||
## Add ident to dest if it is not present. This is intended for use
|
||||
## with pragmas.
|
||||
for node in dest.children:
|
||||
|
|
@ -825,5 +823,3 @@ when not defined(booting):
|
|||
macro payload: stmt {.gensym.} =
|
||||
result = parseStmt(e)
|
||||
payload()
|
||||
|
||||
{.pop.}
|
||||
|
|
|
|||
|
|
@ -1064,13 +1064,13 @@ proc accept*(socket: TAsyncFD,
|
|||
|
||||
# -- Await Macro
|
||||
|
||||
proc skipUntilStmtList(node: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc skipUntilStmtList(node: NimNode): NimNode {.compileTime.} =
|
||||
# Skips a nest of StmtList's.
|
||||
result = node
|
||||
if node[0].kind == nnkStmtList:
|
||||
result = skipUntilStmtList(node[0])
|
||||
|
||||
proc skipStmtList(node: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc skipStmtList(node: NimNode): NimNode {.compileTime.} =
|
||||
result = node
|
||||
if node[0].kind == nnkStmtList:
|
||||
result = node[0]
|
||||
|
|
@ -1098,11 +1098,11 @@ template createCb(retFutureSym, iteratorNameSym,
|
|||
cb()
|
||||
#{.pop.}
|
||||
proc generateExceptionCheck(futSym,
|
||||
tryStmt, rootReceiver, fromNode: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
tryStmt, rootReceiver, fromNode: NimNode): NimNode {.compileTime.} =
|
||||
if tryStmt.kind == nnkNilLit:
|
||||
result = rootReceiver
|
||||
else:
|
||||
var exceptionChecks: seq[tuple[cond, body: PNimrodNode]] = @[]
|
||||
var exceptionChecks: seq[tuple[cond, body: NimNode]] = @[]
|
||||
let errorNode = newDotExpr(futSym, newIdentNode("error"))
|
||||
for i in 1 .. <tryStmt.len:
|
||||
let exceptBranch = tryStmt[i]
|
||||
|
|
@ -1110,7 +1110,7 @@ proc generateExceptionCheck(futSym,
|
|||
exceptionChecks.add((newIdentNode("true"), exceptBranch[0]))
|
||||
else:
|
||||
var exceptIdentCount = 0
|
||||
var ifCond: PNimrodNode
|
||||
var ifCond: NimNode
|
||||
for i in 0 .. <exceptBranch.len:
|
||||
let child = exceptBranch[i]
|
||||
if child.kind == nnkIdent:
|
||||
|
|
@ -1144,10 +1144,10 @@ proc generateExceptionCheck(futSym,
|
|||
)
|
||||
result.add elseNode
|
||||
|
||||
template createVar(result: var PNimrodNode, futSymName: string,
|
||||
asyncProc: PNimrodNode,
|
||||
template createVar(result: var NimNode, futSymName: string,
|
||||
asyncProc: NimNode,
|
||||
valueReceiver, rootReceiver: expr,
|
||||
fromNode: PNimrodNode) =
|
||||
fromNode: NimNode) =
|
||||
result = newNimNode(nnkStmtList, fromNode)
|
||||
var futSym = genSym(nskVar, "future")
|
||||
result.add newVarStmt(futSym, asyncProc) # -> var future<x> = y
|
||||
|
|
@ -1155,9 +1155,9 @@ template createVar(result: var PNimrodNode, futSymName: string,
|
|||
valueReceiver = newDotExpr(futSym, newIdentNode("read")) # -> future<x>.read
|
||||
result.add generateExceptionCheck(futSym, tryStmt, rootReceiver, fromNode)
|
||||
|
||||
proc processBody(node, retFutureSym: PNimrodNode,
|
||||
proc processBody(node, retFutureSym: NimNode,
|
||||
subTypeIsVoid: bool,
|
||||
tryStmt: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
tryStmt: NimNode): NimNode {.compileTime.} =
|
||||
#echo(node.treeRepr)
|
||||
result = node
|
||||
case node.kind
|
||||
|
|
@ -1183,7 +1183,7 @@ proc processBody(node, retFutureSym: PNimrodNode,
|
|||
result = newNimNode(nnkYieldStmt, node).add(node[1]) # -> yield x
|
||||
of nnkCall, nnkCommand:
|
||||
# await foo(p, x)
|
||||
var futureValue: PNimrodNode
|
||||
var futureValue: NimNode
|
||||
result.createVar("future" & $node[1][0].toStrLit, node[1], futureValue,
|
||||
futureValue, node)
|
||||
else:
|
||||
|
|
@ -1232,8 +1232,8 @@ proc processBody(node, retFutureSym: PNimrodNode,
|
|||
# working in ``except``?
|
||||
tryBody[1] = processBody(n[1], retFutureSym, subTypeIsVoid, nil)
|
||||
|
||||
proc processForTry(n: PNimrodNode, i: var int,
|
||||
res: PNimrodNode): bool {.compileTime.} =
|
||||
proc processForTry(n: NimNode, i: var int,
|
||||
res: NimNode): bool {.compileTime.} =
|
||||
## Transforms the body of the tryStmt. Does not transform the
|
||||
## body in ``except``.
|
||||
## Returns true if the tryStmt node was transformed into an ifStmt.
|
||||
|
|
@ -1275,7 +1275,7 @@ proc processBody(node, retFutureSym: PNimrodNode,
|
|||
for i in 0 .. <result.len:
|
||||
result[i] = processBody(result[i], retFutureSym, subTypeIsVoid, tryStmt)
|
||||
|
||||
proc getName(node: PNimrodNode): string {.compileTime.} =
|
||||
proc getName(node: NimNode): string {.compileTime.} =
|
||||
case node.kind
|
||||
of nnkPostfix:
|
||||
return $node[1].ident
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@
|
|||
|
||||
import macros
|
||||
|
||||
proc createProcType(p, b: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc createProcType(p, b: NimNode): NimNode {.compileTime.} =
|
||||
#echo treeRepr(p)
|
||||
#echo treeRepr(b)
|
||||
result = newNimNode(nnkProcTy)
|
||||
|
|
@ -62,7 +62,7 @@ macro `=>`*(p, b: expr): expr {.immediate.} =
|
|||
|
||||
#echo treeRepr(p)
|
||||
#echo(treeRepr(b))
|
||||
var params: seq[PNimrodNode] = @[newIdentNode("auto")]
|
||||
var params: seq[NimNode] = @[newIdentNode("auto")]
|
||||
|
||||
case p.kind
|
||||
of nnkPar:
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ const
|
|||
"onmouseover onmousemove onmouseout onkeypress onkeydown onkeyup "
|
||||
commonAttr* = coreAttr & eventAttr
|
||||
|
||||
proc getIdent(e: PNimrodNode): string {.compileTime.} =
|
||||
proc getIdent(e: NimNode): string {.compileTime.} =
|
||||
case e.kind
|
||||
of nnkIdent: result = normalize($e.ident)
|
||||
of nnkAccQuoted:
|
||||
|
|
@ -53,8 +53,8 @@ proc delete[T](s: var seq[T], attr: T): bool =
|
|||
setLen(s, L-1)
|
||||
result = true
|
||||
|
||||
proc xmlCheckedTag*(e: PNimrodNode, tag: string, optAttr = "", reqAttr = "",
|
||||
isLeaf = false): PNimrodNode {.compileTime.} =
|
||||
proc xmlCheckedTag*(e: NimNode, tag: string, optAttr = "", reqAttr = "",
|
||||
isLeaf = false): NimNode {.compileTime.} =
|
||||
## use this procedure to define a new XML tag
|
||||
|
||||
# copy the attributes; when iterating over them these lists
|
||||
|
|
|
|||
|
|
@ -644,7 +644,7 @@ proc `%`*(elements: openArray[JsonNode]): JsonNode =
|
|||
newSeq(result.elems, elements.len)
|
||||
for i, p in pairs(elements): result.elems[i] = p
|
||||
|
||||
proc toJson(x: PNimrodNode): PNimrodNode {.compiletime.} =
|
||||
proc toJson(x: NimNode): NimNode {.compiletime.} =
|
||||
case x.kind
|
||||
of nnkBracket:
|
||||
result = newNimNode(nnkBracket)
|
||||
|
|
|
|||
|
|
@ -144,7 +144,7 @@ macro check*(conditions: stmt): stmt {.immediate.} =
|
|||
when compiles(string($value)):
|
||||
checkpoint(name & " was " & $value)
|
||||
|
||||
proc inspectArgs(exp: PNimrodNode) =
|
||||
proc inspectArgs(exp: NimNode) =
|
||||
for i in 1 .. <exp.len:
|
||||
if exp[i].kind notin nnkLiterals:
|
||||
inc counter
|
||||
|
|
@ -202,7 +202,7 @@ template require*(conditions: stmt): stmt {.immediate, dirty.} =
|
|||
macro expect*(exceptions: varargs[expr], body: stmt): stmt {.immediate.} =
|
||||
let exp = callsite()
|
||||
template expectBody(errorTypes, lineInfoLit: expr,
|
||||
body: stmt): PNimrodNode {.dirty.} =
|
||||
body: stmt): NimNode {.dirty.} =
|
||||
try:
|
||||
body
|
||||
checkpoint(lineInfoLit & ": Expect Failed, no exception was thrown.")
|
||||
|
|
|
|||
|
|
@ -263,7 +263,7 @@ proc newXmlTree*(tag: string, children: openArray[XmlNode],
|
|||
for i in 0..children.len-1: result.s[i] = children[i]
|
||||
result.fAttr = attributes
|
||||
|
||||
proc xmlConstructor(e: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc xmlConstructor(e: NimNode): NimNode {.compileTime.} =
|
||||
expectLen(e, 2)
|
||||
var a = e[1]
|
||||
if a.kind == nnkCall:
|
||||
|
|
|
|||
|
|
@ -3149,15 +3149,9 @@ proc shallow*(s: var string) {.noSideEffect, inline.} =
|
|||
type
|
||||
NimNodeObj = object
|
||||
|
||||
when defined(nimnode):
|
||||
type
|
||||
NimNode* {.magic: "PNimrodNode".} = ref NimNodeObj
|
||||
## represents a Nim AST node. Macros operate on this type.
|
||||
{.deprecated: [PNimrodNode: NimNode].}
|
||||
else:
|
||||
type
|
||||
PNimrodNode* {.magic: "PNimrodNode".} = ref NimNodeObj
|
||||
## represents a Nim AST node. Macros operate on this type.
|
||||
NimNode* {.magic: "PNimrodNode".} = ref NimNodeObj
|
||||
## represents a Nim AST node. Macros operate on this type.
|
||||
{.deprecated: [PNimrodNode: NimNode].}
|
||||
|
||||
when false:
|
||||
template eval*(blk: stmt): stmt =
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ macro expect*(exceptions: varargs[expr], body: stmt): stmt {.immediate.} =
|
|||
## Expect docstrings
|
||||
let exp = callsite()
|
||||
template expectBody(errorTypes, lineInfoLit: expr,
|
||||
body: stmt): PNimrodNode {.dirty.} =
|
||||
body: stmt): NimNode {.dirty.} =
|
||||
try:
|
||||
body
|
||||
assert false
|
||||
|
|
|
|||
|
|
@ -20,13 +20,13 @@ import strutils
|
|||
# This serves the same purpose as D's `alias` parameters for types, used heavily
|
||||
# in its popular `ranges` and `algorithm` modules.
|
||||
|
||||
var exprNodes {.compileTime.} = newSeq[PNimrodNode]()
|
||||
var exprNodes {.compileTime.} = newSeq[NimNode]()
|
||||
|
||||
proc refExpr(exprNode: PNimrodNode): string {.compileTime.} =
|
||||
proc refExpr(exprNode: NimNode): string {.compileTime.} =
|
||||
exprNodes.add exprNode.copy
|
||||
"expr" & $(exprNodes.len - 1)
|
||||
|
||||
proc derefExpr(exprRef: string): PNimrodNode {.compileTime.} =
|
||||
proc derefExpr(exprRef: string): NimNode {.compileTime.} =
|
||||
exprNodes[parseInt(exprRef[4 .. -1])]
|
||||
|
||||
#===============================================================================
|
||||
|
|
|
|||
|
|
@ -69,7 +69,7 @@ m(s)
|
|||
|
||||
# bug #933
|
||||
|
||||
proc nilcheck(): PNimrodNode {.compileTime.} =
|
||||
proc nilcheck(): NimNode {.compileTime.} =
|
||||
echo(result == nil) # true
|
||||
echo(result.isNil) # true
|
||||
echo(repr(result)) # nil
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
# Dump the contents of a PNimrodNode
|
||||
# Dump the contents of a NimNode
|
||||
|
||||
import macros
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
# Dump the contents of a PNimrodNode
|
||||
# Dump the contents of a NimNode
|
||||
|
||||
import macros
|
||||
|
||||
proc dumpit(n: PNimrodNode): string {.compileTime.} =
|
||||
proc dumpit(n: NimNode): string {.compileTime.} =
|
||||
if n == nil: return "nil"
|
||||
result = $n.kind
|
||||
add(result, "(")
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import rawsockets, asyncdispatch, macros
|
|||
var p = newDispatcher()
|
||||
var sock = newAsyncRawSocket()
|
||||
|
||||
proc convertReturns(node, retFutureSym: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc convertReturns(node, retFutureSym: NimNode): NimNode {.compileTime.} =
|
||||
case node.kind
|
||||
of nnkReturnStmt:
|
||||
result = newCall(newIdentNode("complete"), retFutureSym, node[0])
|
||||
|
|
@ -25,13 +25,13 @@ macro async2(prc: stmt): stmt {.immediate.} =
|
|||
newIdentNode("newFuture"),
|
||||
prc[3][0][1])))) # Get type from return type of this proc.
|
||||
|
||||
# -> iterator nameIter(): PFutureBase {.closure.} = <proc_body>
|
||||
# -> iterator nameIter(): FutureBase {.closure.} = <proc_body>
|
||||
# Changing this line to: newIdentNode($prc[0].ident & "Iter") # will make it work.
|
||||
var iteratorNameSym = genSym(nskIterator, $prc[0].ident & "Iter")
|
||||
#var iteratorNameSym = newIdentNode($prc[0].ident & "Iter")
|
||||
var procBody = prc[6].convertReturns(retFutureSym)
|
||||
|
||||
var closureIterator = newProc(iteratorNameSym, [newIdentNode("PFutureBase")],
|
||||
var closureIterator = newProc(iteratorNameSym, [newIdentNode("FutureBase")],
|
||||
procBody, nnkIteratorDef)
|
||||
closureIterator[4] = newNimNode(nnkPragma).add(newIdentNode("closure"))
|
||||
outerProcBody.add(closureIterator)
|
||||
|
|
@ -55,8 +55,8 @@ macro async2(prc: stmt): stmt {.immediate.} =
|
|||
|
||||
result[6] = outerProcBody
|
||||
|
||||
proc readStuff(): PFuture[string] {.async2.} =
|
||||
var fut = connect(sock, "irc.freenode.org", TPort(6667))
|
||||
proc readStuff(): Future[string] {.async2.} =
|
||||
var fut = connect(sock, "irc.freenode.org", Port(6667))
|
||||
yield fut
|
||||
var fut2 = recv(sock, 50)
|
||||
yield fut2
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
import parseutils, macros
|
||||
|
||||
proc parse_until_symbol(node: PNimrodNode, value: string, index: var int): bool {.compiletime.} =
|
||||
proc parse_until_symbol(node: NimNode, value: string, index: var int): bool {.compiletime.} =
|
||||
var splitValue: string
|
||||
var read = value.parseUntil(splitValue, '$', index)
|
||||
|
||||
|
|
@ -15,7 +15,7 @@ proc parse_until_symbol(node: PNimrodNode, value: string, index: var int): bool
|
|||
if splitValue.len > 0:
|
||||
node.insert node.len, newCall("add", ident("result"), newStrLitNode(splitValue))
|
||||
|
||||
proc parse_template(node: PNimrodNode, value: string) {.compiletime.} =
|
||||
proc parse_template(node: NimNode, value: string) {.compiletime.} =
|
||||
var index = 0
|
||||
while index < value.len and
|
||||
parse_until_symbol(node, value, index): discard
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ test:
|
|||
macro test2*(a: stmt): stmt {.immediate.} =
|
||||
proc testproc(recurse: int) =
|
||||
echo "Thats weird"
|
||||
var o : PNimrodNode = nil
|
||||
var o : NimNode = nil
|
||||
echo " no its not!"
|
||||
o = newNimNode(nnkNone)
|
||||
if recurse > 0:
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import
|
|||
|
||||
macro test_macro*(n: stmt): stmt {.immediate.} =
|
||||
result = newNimNode(nnkStmtList)
|
||||
var ass : PNimrodNode = newNimNode(nnkAsgn)
|
||||
var ass : NimNode = newNimNode(nnkAsgn)
|
||||
add(ass, newIdentNode("str"))
|
||||
add(ass, newStrLitNode("after"))
|
||||
add(result, ass)
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import macros,json
|
||||
|
||||
var decls{.compileTime.}: seq[PNimrodNode] = @[]
|
||||
var impls{.compileTime.}: seq[PNimrodNode] = @[]
|
||||
var decls{.compileTime.}: seq[NimNode] = @[]
|
||||
var impls{.compileTime.}: seq[NimNode] = @[]
|
||||
|
||||
macro importImpl_forward(name, returns): stmt {.immediate.} =
|
||||
result = newNimNode(nnkEmpty)
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ macro outterMacro*(n: stmt): stmt {.immediate.} =
|
|||
" requires the ident passed as parameter (eg: " & callNode.repr &
|
||||
"(the_name_you_want)): statements.")
|
||||
result = newNimNode(TNimrodNodeKind.nnkStmtList)
|
||||
var ass : PNimrodNode = newNimNode(nnkAsgn)
|
||||
var ass : NimNode = newNimNode(nnkAsgn)
|
||||
ass.add(newIdentNode(n[1].ident))
|
||||
ass.add(newStrLitNode(innerProc(4)))
|
||||
result.add(ass)
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ discard """
|
|||
"""
|
||||
|
||||
import macros
|
||||
proc makeMacro: PNimrodNode =
|
||||
proc makeMacro: NimNode =
|
||||
result = nil
|
||||
|
||||
var p = makeMacro()
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ macro formatStyleInterpolation(e: expr): expr =
|
|||
proc addString(s: string) =
|
||||
formatString.add(s)
|
||||
|
||||
proc addExpr(e: PNimrodNode) =
|
||||
proc addExpr(e: NimNode) =
|
||||
arrayNode.add(e)
|
||||
formatString.add("$" & $(idx))
|
||||
inc idx
|
||||
|
|
@ -43,11 +43,11 @@ macro formatStyleInterpolation(e: expr): expr =
|
|||
|
||||
macro concatStyleInterpolation(e: expr): expr =
|
||||
let e = callsite()
|
||||
var args: seq[PNimrodNode]
|
||||
var args: seq[NimNode]
|
||||
newSeq(args, 0)
|
||||
|
||||
proc addString(s: string) = args.add(newStrLitNode(s))
|
||||
proc addExpr(e: PNimrodNode) = args.add(e)
|
||||
proc addExpr(e: NimNode) = args.add(e)
|
||||
proc addDollar() = args.add(newStrLitNode"$")
|
||||
|
||||
processInterpolations(e)
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ discard """
|
|||
|
||||
import macros
|
||||
|
||||
proc test(f: var PNimrodNode) {.compileTime.} =
|
||||
proc test(f: var NimNode) {.compileTime.} =
|
||||
f = newNimNode(nnkStmtList)
|
||||
f.add newCall(newIdentNode("echo"), newLit(10))
|
||||
|
||||
|
|
|
|||
|
|
@ -206,15 +206,15 @@ macro defPacket*(typeNameN: expr, typeFields: expr): stmt {.immediate.} =
|
|||
when defined(GenPacketShowOutput):
|
||||
echo(repr(result))
|
||||
|
||||
proc `->`(a: string, b: string): PNimrodNode {.compileTime.} =
|
||||
proc `->`(a: string, b: string): NimNode {.compileTime.} =
|
||||
result = newNimNode(nnkIdentDefs).und(^a, ^b, newNimNode(nnkEmpty))
|
||||
proc `->`(a: string, b: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc `->`(a: string, b: NimNode): NimNode {.compileTime.} =
|
||||
result = newNimNode(nnkIdentDefs).und(^a, b, newNimNode(nnkEmpty))
|
||||
proc `->`(a, b: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc `->`(a, b: NimNode): NimNode {.compileTime.} =
|
||||
a[2] = b
|
||||
result = a
|
||||
|
||||
proc newProc*(name: string, params: varargs[PNimrodNode], resultType: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc newProc*(name: string, params: varargs[NimNode], resultType: NimNode): NimNode {.compileTime.} =
|
||||
result = newNimNode(nnkProcDef).und(
|
||||
^name,
|
||||
emptyNode(),
|
||||
|
|
|
|||
|
|
@ -178,7 +178,7 @@ macro defPacket*(typeNameN: expr, typeFields: expr): stmt {.immediate.} =
|
|||
when defined(GenPacketShowOutput):
|
||||
echo(repr(result))
|
||||
|
||||
proc newProc*(name: PNimrodNode; params: varargs[PNimrodNode]; resultType: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc newProc*(name: NimNode; params: varargs[NimNode]; resultType: NimNode): NimNode {.compileTime.} =
|
||||
result = newNimNode(nnkProcDef).und(
|
||||
name,
|
||||
emptyNode(),
|
||||
|
|
@ -189,15 +189,15 @@ proc newProc*(name: PNimrodNode; params: varargs[PNimrodNode]; resultType: PNimr
|
|||
newNimNode(nnkStmtList))
|
||||
result[3].add(params)
|
||||
|
||||
proc body*(procNode: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc body*(procNode: NimNode): NimNode {.compileTime.} =
|
||||
assert procNode.kind == nnkProcDef and procNode[6].kind == nnkStmtList
|
||||
result = procNode[6]
|
||||
|
||||
proc iddefs*(a, b: string; c: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc iddefs*(a, b: string; c: NimNode): NimNode {.compileTime.} =
|
||||
result = newNimNode(nnkIdentDefs).und(^a, ^b, c)
|
||||
proc iddefs*(a: string; b: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc iddefs*(a: string; b: NimNode): NimNode {.compileTime.} =
|
||||
result = newNimNode(nnkIdentDefs).und(^a, b, emptyNode())
|
||||
proc varTy*(a: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc varTy*(a: NimNode): NimNode {.compileTime.} =
|
||||
result = newNimNode(nnkVarTy).und(a)
|
||||
|
||||
macro forwardPacket*(typeName: expr, underlyingType: expr): stmt {.immediate.} =
|
||||
|
|
|
|||
|
|
@ -1,42 +1,42 @@
|
|||
import macros
|
||||
{.deadCodeElim: on.}
|
||||
#Inline macro.add() to allow for easier nesting
|
||||
proc und*(a: PNimrodNode; b: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc und*(a: NimNode; b: NimNode): NimNode {.compileTime.} =
|
||||
a.add(b)
|
||||
result = a
|
||||
proc und*(a: PNimrodNode; b: varargs[PNimrodNode]): PNimrodNode {.compileTime.} =
|
||||
proc und*(a: NimNode; b: varargs[NimNode]): NimNode {.compileTime.} =
|
||||
a.add(b)
|
||||
result = a
|
||||
|
||||
proc `^`*(a: string): PNimrodNode {.compileTime.} =
|
||||
proc `^`*(a: string): NimNode {.compileTime.} =
|
||||
## new ident node
|
||||
result = newIdentNode(!a)
|
||||
proc `[]`*(a, b: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc `[]`*(a, b: NimNode): NimNode {.compileTime.} =
|
||||
## new bracket expression: node[node] not to be confused with node[indx]
|
||||
result = newNimNode(nnkBracketExpr).und(a, b)
|
||||
proc `:=`*(left, right: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc `:=`*(left, right: NimNode): NimNode {.compileTime.} =
|
||||
## new Asgn node: left = right
|
||||
result = newNimNode(nnkAsgn).und(left, right)
|
||||
|
||||
proc lit*(a: string): PNimrodNode {.compileTime.} =
|
||||
proc lit*(a: string): NimNode {.compileTime.} =
|
||||
result = newStrLitNode(a)
|
||||
proc lit*(a: int): PNimrodNode {.compileTime.} =
|
||||
proc lit*(a: int): NimNode {.compileTime.} =
|
||||
result = newIntLitNode(a)
|
||||
proc lit*(a: float): PNimrodNode {.compileTime.} =
|
||||
proc lit*(a: float): NimNode {.compileTime.} =
|
||||
result = newFloatLitNode(a)
|
||||
proc lit*(a: char): PNimrodNode {.compileTime.} =
|
||||
proc lit*(a: char): NimNode {.compileTime.} =
|
||||
result = newNimNode(nnkCharLit)
|
||||
result.intval = a.ord
|
||||
|
||||
proc emptyNode*(): PNimrodNode {.compileTime.} =
|
||||
proc emptyNode*(): NimNode {.compileTime.} =
|
||||
result = newNimNode(nnkEmpty)
|
||||
|
||||
proc dot*(left, right: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc dot*(left, right: NimNode): NimNode {.compileTime.} =
|
||||
result = newNimNode(nnkDotExpr).und(left, right)
|
||||
proc prefix*(a: string, b: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc prefix*(a: string, b: NimNode): NimNode {.compileTime.} =
|
||||
result = newNimNode(nnkPrefix).und(newIdentNode(!a), b)
|
||||
|
||||
proc quoted2ident*(a: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc quoted2ident*(a: NimNode): NimNode {.compileTime.} =
|
||||
if a.kind != nnkAccQuoted:
|
||||
return a
|
||||
var pname = ""
|
||||
|
|
|
|||
|
|
@ -104,7 +104,7 @@ proc pat2kind(pattern: string): FormulaKind =
|
|||
|
||||
import macros
|
||||
|
||||
proc matchAgainst(n, pattern: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
proc matchAgainst(n, pattern: NimNode): NimNode {.compileTime.} =
|
||||
template `@`(current, field: expr): expr =
|
||||
newDotExpr(current, newIdentNode(astToStr(field)))
|
||||
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ const identChars = {'a'..'z', 'A'..'Z', '0'..'9', '_'}
|
|||
|
||||
|
||||
# Procedure Declarations
|
||||
proc parse_template(node: PNimrodNode, value: string) {.compiletime.}
|
||||
proc parse_template(node: NimNode, value: string) {.compiletime.}
|
||||
|
||||
|
||||
# Procedure Definitions
|
||||
|
|
@ -166,7 +166,7 @@ iterator parse_compound_statements(value, identifier: string, index: int): strin
|
|||
get_next_ident(["try", "$except", "$finally"])
|
||||
|
||||
|
||||
proc parse_complex_stmt(value, identifier: string, index: var int): PNimrodNode {.compiletime.} =
|
||||
proc parse_complex_stmt(value, identifier: string, index: var int): NimNode {.compiletime.} =
|
||||
## Parses if/when/try /elif /else /except /finally statements
|
||||
|
||||
# Build up complex statement string
|
||||
|
|
@ -218,7 +218,7 @@ proc parse_complex_stmt(value, identifier: string, index: var int): PNimrodNode
|
|||
inc(resultIndex)
|
||||
|
||||
|
||||
proc parse_simple_statement(value: string, index: var int): PNimrodNode {.compiletime.} =
|
||||
proc parse_simple_statement(value: string, index: var int): NimNode {.compiletime.} =
|
||||
## Parses for/while
|
||||
|
||||
# Detect indentation
|
||||
|
|
@ -252,7 +252,7 @@ proc parse_simple_statement(value: string, index: var int): PNimrodNode {.compil
|
|||
inc(index, value.parse_thru_eol(index))
|
||||
|
||||
|
||||
proc parse_until_symbol(node: PNimrodNode, value: string, index: var int): bool {.compiletime.} =
|
||||
proc parse_until_symbol(node: NimNode, value: string, index: var int): bool {.compiletime.} =
|
||||
## Parses a string until a $ symbol is encountered, if
|
||||
## two $$'s are encountered in a row, a split will happen
|
||||
## removing one of the $'s from the resulting output
|
||||
|
|
@ -311,7 +311,7 @@ proc parse_until_symbol(node: PNimrodNode, value: string, index: var int): bool
|
|||
node.insert insertionPoint, newCall("add", ident("result"), newStrLitNode(splitValue))
|
||||
|
||||
|
||||
proc parse_template(node: PNimrodNode, value: string) =
|
||||
proc parse_template(node: NimNode, value: string) =
|
||||
## Parses through entire template, outputing valid
|
||||
## Nim code into the input `node` AST.
|
||||
var index = 0
|
||||
|
|
|
|||
|
|
@ -22,8 +22,8 @@ macro autoClose(e: expr): stmt {.immediate.} =
|
|||
var body = e[2]
|
||||
|
||||
var
|
||||
variables : seq[PNimrodNode]
|
||||
closingCalls : seq[PNimrodNode]
|
||||
variables : seq[NimNode]
|
||||
closingCalls : seq[NimNode]
|
||||
|
||||
newSeq(variables, 0)
|
||||
newSeq(closingCalls, 0)
|
||||
|
|
|
|||
|
|
@ -8,9 +8,9 @@ type
|
|||
suiteDesc: string
|
||||
testName: string
|
||||
testDesc: string
|
||||
testBlock: PNimrodNode
|
||||
testBlock: NimNode
|
||||
|
||||
proc buildSuiteContents(suiteName, suiteDesc, suiteBloc: PNimrodNode): tuple[tests: seq[SuiteTest]] {.compileTime.} =
|
||||
proc buildSuiteContents(suiteName, suiteDesc, suiteBloc: NimNode): tuple[tests: seq[SuiteTest]] {.compileTime.} =
|
||||
var
|
||||
tests:seq[SuiteTest] = @[]
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue