Rename PNimrodNode to NimNode

This commit is contained in:
def
2015-03-17 17:50:32 +01:00
parent 8e651fa0d4
commit fd4e629905
30 changed files with 508 additions and 518 deletions

View File

@@ -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

View File

@@ -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])]
#===============================================================================

View File

@@ -46,13 +46,13 @@ echotest()
# bug #1103
type
type
Td = tuple
a:string
b:int
proc get_data(d: Td) : string {.compileTime.} =
result = d.a # Works if a literal string is used here.
result = d.a # Works if a literal string is used here.
# Bugs if line A or B is active. Works with C
result &= "aa" # A
#result.add("aa") # B
@@ -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

View File

@@ -1,4 +1,4 @@
# Dump the contents of a PNimrodNode
# Dump the contents of a NimNode
import macros
@@ -7,7 +7,7 @@ template plus(a, b: expr): expr {.dirty} =
macro call(e: expr): expr =
result = newCall("foo", newStrLitNode("bar"))
macro dumpAST(n: stmt): stmt {.immediate.} =
# dump AST as a side-effect and return the inner node
let n = callsite()
@@ -24,10 +24,10 @@ macro dumpAST(n: stmt): stmt {.immediate.} =
echo e.lispRepr
result = n[1]
dumpAST:
proc add(x, y: int): int =
return x + y
proc sub(x, y: int): int = return x - y

View File

@@ -1,13 +1,13 @@
# 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, "(")
case n.kind
of nnkEmpty: discard # same as nil node in this representation
of nnkEmpty: discard # same as nil node in this representation
of nnkNilLit: add(result, "nil")
of nnkCharLit..nnkInt64Lit: add(result, $n.intVal)
of nnkFloatLit..nnkFloat64Lit: add(result, $n.floatVal)
@@ -20,17 +20,17 @@ proc dumpit(n: PNimrodNode): string {.compileTime.} =
add(result, ", ")
add(result, dumpit(n[j]))
add(result, ")")
macro dumpAST(n: stmt): stmt {.immediate.} =
macro dumpAST(n: stmt): stmt {.immediate.} =
# dump AST as a side-effect and return the inner node
let n = callsite()
echo dumpit(n)
result = n[1]
dumpAST:
proc add(x, y: int): int =
return x + y
proc sub(x, y: int): int = return x - y

View File

@@ -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])
@@ -19,19 +19,19 @@ macro async2(prc: stmt): stmt {.immediate.} =
# -> var retFuture = newFuture[T]()
var retFutureSym = newIdentNode("retFuture") #genSym(nskVar, "retFuture")
outerProcBody.add(
newVarStmt(retFutureSym,
newVarStmt(retFutureSym,
newCall(
newNimNode(nnkBracketExpr).add(
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

View File

@@ -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

View File

@@ -3,17 +3,17 @@ import macros
from uri import `/`
macro test*(a: stmt): stmt {.immediate.} =
var nodes: tuple[a, b: int]
var nodes: tuple[a, b: int]
nodes.a = 4
nodes[1] = 45
type
TTypeEx = object
x, y: int
case b: bool
of false: nil
of true: z: float
var t: TTypeEx
t.b = true
t.z = 4.5

View File

@@ -4,7 +4,7 @@ discard """
import macros
type
type
TA = tuple[a: int]
PA = ref TA
@@ -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:

View File

@@ -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)

View File

@@ -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)
@@ -38,7 +38,7 @@ macro importImpl_forward(name, returns): stmt {.immediate.} =
decls.add res
echo(repr(res))
macro importImpl(name, returns: expr, body: stmt): stmt {.immediate.} =
macro importImpl(name, returns: expr, body: stmt): stmt {.immediate.} =
#var res = getAST(importImpl_forward(name, returns))
discard getAST(importImpl_forward(name, returns))
var res = copyNimTree(decls[decls.high])
@@ -56,4 +56,4 @@ importImpl(Item, int):
importImpl(Foo, int16):
echo 77
okayy
okayy

View File

@@ -15,10 +15,10 @@ macro outterMacro*(n: stmt): stmt {.immediate.} =
expectKind(n, TNimrodNodeKind.nnkCall)
if n.len != 3 or n[1].kind != TNimrodNodeKind.nnkIdent:
error("Macro " & callNode.repr &
" requires the ident passed as parameter (eg: " & callNode.repr &
" 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)

View File

@@ -3,7 +3,7 @@ discard """
"""
import macros
proc makeMacro: PNimrodNode =
proc makeMacro: NimNode =
result = nil
var p = makeMacro()

View File

@@ -19,7 +19,7 @@ template processInterpolations(e: expr) =
macro formatStyleInterpolation(e: expr): expr =
let e = callsite()
var
var
formatString = ""
arrayNode = newNimNode(nnkBracket)
idx = 1
@@ -27,14 +27,14 @@ 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
proc addDollar() =
formatString.add("$$")
processInterpolations(e)
result = parseExpr("\"x\" % [y]")
@@ -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)
@@ -59,7 +59,7 @@ macro concatStyleInterpolation(e: expr): expr =
proc sum(a, b, c: int): int =
return (a + b + c)
var
var
alice = "Alice"
bob = "Bob"
a = 10

View File

@@ -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))

View File

@@ -18,9 +18,9 @@ proc `$`*[T](x: seq[T]): string =
result.add($x[i])
result.add ']'
macro defPacket*(typeNameN: expr, typeFields: expr): stmt {.immediate.} =
macro defPacket*(typeNameN: expr, typeFields: expr): stmt {.immediate.} =
result = newNimNode(nnkStmtList)
let
let
typeName = quoted2ident(typeNameN)
packetID = ^"p"
streamID = ^"s"
@@ -66,7 +66,7 @@ macro defPacket*(typeNameN: expr, typeFields: expr): stmt {.immediate.} =
readBody = newNimNode(nnkStmtList)
lenNames = 0
for i in 0.. typeFields.len - 1:
let
let
name = typeFields[i][0]
dotName = packetID.dot(name)
resName = newIdentNode(!"result").dot(name)
@@ -91,11 +91,11 @@ macro defPacket*(typeNameN: expr, typeFields: expr): stmt {.immediate.} =
newNimNode(nnkDiscardStmt).und(
newCall("readData", streamID, newNimNode(nnkAddr).und(resName), newCall("sizeof", resName))))
packBody.add(
newCall("writeData", streamID, newNimNode(nnkAddr).und(dotName), newCall("sizeof", dotName)))
newCall("writeData", streamID, newNimNode(nnkAddr).und(dotName), newCall("sizeof", dotName)))
of "seq":
## let lenX = readInt16(s)
newLenName()
let
let
item = ^"item" ## item name in our iterators
seqType = typeFields[i][1][1] ## type of seq
readName = newIdentNode("read"& $seqType.ident)
@@ -107,7 +107,7 @@ macro defPacket*(typeNameN: expr, typeFields: expr): stmt {.immediate.} =
readBody.add( ## result.name = @[]
resName := ("@".prefix(newNimNode(nnkBracket))),
newNimNode(nnkForStmt).und( ## for item in 1..len:
item,
item,
infix(1.lit, "..", lenName),
newNimNode(nnkStmtList).und(
newCall( ## add(result.name, unpack[seqType](stream))
@@ -117,7 +117,7 @@ macro defPacket*(typeNameN: expr, typeFields: expr): stmt {.immediate.} =
newNimNode(nnkVarSection).und(newNimNode(nnkIdentDefs).und(
lenName, ## var lenName = int16(len(p.name))
newIdentNode("int16"),
newCall("int16", newCall("len", dotName)))),
newCall("int16", newCall("len", dotName)))),
newCall("writeData", streamID, newNimNode(nnkAddr).und(lenName), 2.lit),
newNimNode(nnkForStmt).und( ## for item in 0..length - 1: pack(p.name[item], stream)
item,
@@ -143,8 +143,8 @@ macro defPacket*(typeNameN: expr, typeFields: expr): stmt {.immediate.} =
readBody.add(resName := newCall("read"& $typeFields[i][1].ident, streamID))
else:
error("I dont know what to do with: "& treerepr(typeFields[i]))
var
var
toStringFunc = newNimNode(nnkProcDef).und(
newNimNode(nnkPostfix).und(
^"*",
@@ -161,12 +161,12 @@ macro defPacket*(typeNameN: expr, typeFields: expr): stmt {.immediate.} =
emptyNode(),
newNimNode(nnkStmtList).und(#[6]
newNimNode(nnkAsgn).und(
^"result", ## result =
^"result", ## result =
newNimNode(nnkCall).und(#[6][0][1]
^"format", ## format
emptyNode())))) ## "[TypeName $1 $2]"
formatStr = "["& $typeName.ident
const emptyFields = {nnkEmpty, nnkNilLit}
var objFields = newNimNode(nnkRecList)
for i in 0.. < len(typeFields):
@@ -186,10 +186,10 @@ macro defPacket*(typeNameN: expr, typeFields: expr): stmt {.immediate.} =
prefix("$", packetID.dot(fname)))
formatStr.add " $"
formatStr.add($(i + 1))
formatStr.add ']'
toStringFunc[6][0][1][1] = formatStr.lit()
result.add(
newNimNode(nnkTypeSection).und(
newNimNode(nnkTypeDef).und(
@@ -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(),
@@ -227,7 +227,7 @@ proc newProc*(name: string, params: varargs[PNimrodNode], resultType: PNimrodNod
macro forwardPacket*(typeName: expr, underlyingType: typedesc): stmt {.immediate.} =
result = newNimNode(nnkStmtList).und(
newProc(
"read"& $typeName.ident,
"read"& $typeName.ident,
["s" -> "PStream" -> newNimNode(nnkNilLit)],
typeName),
newProc(
@@ -258,21 +258,21 @@ when isMainModule:
A = 0'i8,
B, C
forwardPacket(SomeEnum, int8)
defPacket(Foo, tuple[x: array[0..4, int8]])
var f = newFoo([4'i8, 3'i8, 2'i8, 1'i8, 0'i8])
var s2 = newStringStream("")
f.pack(s2)
assert s2.data == "\4\3\2\1\0"
var s = newStringStream()
s.flushImpl = proc(s: PStream) =
var z = PStringStream(s)
z.setPosition(0)
z.data.setLen(0)
s.setPosition(0)
s.data.setLen(0)
var o = B
@@ -283,7 +283,7 @@ when isMainModule:
o.pack(s)
assert s.data == "\1\0\2"
s.flush
defPacket(Y, tuple[z: int8])
proc `$`(z: Y): string = result = "Y("& $z.z &")"
defPacket(TestPkt, tuple[x: seq[Y]])
@@ -292,4 +292,4 @@ when isMainModule:
for itm in test.x:
echo(itm)
test.pack(s)
echo(repr(s.data))
echo(repr(s.data))

View File

@@ -9,9 +9,9 @@ template defPacketImports*(): stmt {.immediate, dirty.} =
import macros, macro_dsl, estreams
from strutils import format
macro defPacket*(typeNameN: expr, typeFields: expr): stmt {.immediate.} =
macro defPacket*(typeNameN: expr, typeFields: expr): stmt {.immediate.} =
result = newNimNode(nnkStmtList)
let
let
typeName = quoted2ident(typeNameN)
packetID = ^"p"
streamID = ^"s"
@@ -57,7 +57,7 @@ macro defPacket*(typeNameN: expr, typeFields: expr): stmt {.immediate.} =
readBody = newNimNode(nnkStmtList)
lenNames = 0
for i in 0.. typeFields.len - 1:
let
let
name = typeFields[i][0]
dotName = packetID.dot(name)
resName = newIdentNode(!"result").dot(name)
@@ -67,7 +67,7 @@ macro defPacket*(typeNameN: expr, typeFields: expr): stmt {.immediate.} =
of "seq":
## let lenX = readInt16(s)
newLenName()
let
let
item = ^"item" ## item name in our iterators
seqType = typeFields[i][1][1] ## type of seq
readName = newIdentNode("read"& $seqType.ident)
@@ -79,7 +79,7 @@ macro defPacket*(typeNameN: expr, typeFields: expr): stmt {.immediate.} =
readBody.add( ## result.name = @[]
resName := ("@".prefix(newNimNode(nnkBracket))),
newNimNode(nnkForStmt).und( ## for item in 1..len:
item,
item,
infix(1.lit, "..", lenName),
newNimNode(nnkStmtList).und(
newCall( ## add(result.name, unpack[seqType](stream))
@@ -89,7 +89,7 @@ macro defPacket*(typeNameN: expr, typeFields: expr): stmt {.immediate.} =
newNimNode(nnkVarSection).und(newNimNode(nnkIdentDefs).und(
lenName, ## var lenName = int16(len(p.name))
newIdentNode("int16"),
newCall("int16", newCall("len", dotName)))),
newCall("int16", newCall("len", dotName)))),
newCall("writeBE", streamID, lenName),
newNimNode(nnkForStmt).und( ## for item in 0..length - 1: pack(p.name[item], stream)
item,
@@ -115,8 +115,8 @@ macro defPacket*(typeNameN: expr, typeFields: expr): stmt {.immediate.} =
readBody.add(resName := newCall("read"& $typeFields[i][1].ident, streamID))
else:
error("I dont know what to do with: "& treerepr(typeFields[i]))
var
var
toStringFunc = newNimNode(nnkProcDef).und(
newNimNode(nnkPostfix).und(
^"*",
@@ -133,12 +133,12 @@ macro defPacket*(typeNameN: expr, typeFields: expr): stmt {.immediate.} =
emptyNode(),
newNimNode(nnkStmtList).und(#[6]
newNimNode(nnkAsgn).und(
^"result", ## result =
^"result", ## result =
newNimNode(nnkCall).und(#[6][0][1]
^"format", ## format
emptyNode())))) ## "[TypeName $1 $2]"
formatStr = "["& $typeName.ident
const emptyFields = {nnkEmpty, nnkNilLit}
var objFields = newNimNode(nnkRecList)
for i in 0.. < len(typeFields):
@@ -158,10 +158,10 @@ macro defPacket*(typeNameN: expr, typeFields: expr): stmt {.immediate.} =
prefix("$", packetID.dot(fname)))
formatStr.add " $"
formatStr.add($(i + 1))
formatStr.add ']'
toStringFunc[6][0][1][1] = formatStr.lit()
result.add(
newNimNode(nnkTypeSection).und(
newNimNode(nnkTypeDef).und(
@@ -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.} =
@@ -206,7 +206,7 @@ macro forwardPacket*(typeName: expr, underlyingType: expr): stmt {.immediate.} =
streamID = ^"s"
result = newNimNode(nnkStmtList).und(
newProc(
(^("read"& $typeName.ident)).postfix("*"),
(^("read"& $typeName.ident)).postfix("*"),
[ iddefs("s", "PBuffer", newNimNode(nnkNilLit)) ],
typeName),
newProc(
@@ -218,7 +218,7 @@ macro forwardPacket*(typeName: expr, underlyingType: expr): stmt {.immediate.} =
readBody = result[0][6]
packBody = result[1][6]
resName = ^"result"
case underlyingType.kind
of nnkBracketExpr:
case $underlyingType[0].ident
@@ -250,21 +250,21 @@ when isMainModule:
A = 0'i8,
B, C
forwardPacket(SomeEnum, int8)
defPacket(Foo, tuple[x: array[0..4, int8]])
var f = newFoo([4'i8, 3'i8, 2'i8, 1'i8, 0'i8])
var s2 = newStringStream("")
f.pack(s2)
assert s2.data == "\4\3\2\1\0"
var s = newStringStream()
s.flushImpl = proc(s: PStream) =
var z = PStringStream(s)
z.setPosition(0)
z.data.setLen(0)
s.setPosition(0)
s.data.setLen(0)
var o = B
@@ -275,7 +275,7 @@ when isMainModule:
o.pack(s)
assert s.data == "\1\0\2"
s.flush
defPacket(Y, tuple[z: int8])
proc `$`(z: Y): string = result = "Y("& $z.z &")"
defPacket(TestPkt, tuple[x: seq[Y]])

View File

@@ -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 = ""

View File

@@ -13,7 +13,7 @@ var g = 70
++g
g ++ 7
g.`++`(10, 20)
echo g
echo g
#let lv = stdin.readline
@@ -56,7 +56,7 @@ type
fkLit, ## element is a literal like 0.1
fkAdd, ## element is an addition operation
fkMul, ## element is a multiplication operation
fkExp ## element is an exponentiation operation
fkExp ## element is an exponentiation operation
type
Formula = ref object
@@ -78,16 +78,16 @@ proc evaluate(n: Formula, varToVal: proc (name: string): float): float =
echo evaluate(Formula(kind: fkLit, value: 0.4), nil)
proc isPolyTerm(n: Formula): bool =
n.kind == fkMul and n.left.kind == fkLit and (let e = n.right;
n.kind == fkMul and n.left.kind == fkLit and (let e = n.right;
e.kind == fkExp and e.left.kind == fkVar and e.right.kind == fkLit)
proc isPolynomial(n: Formula): bool =
isPolyTerm(n) or
isPolyTerm(n) or
(n.kind == fkAdd and isPolynomial(n.left) and isPolynomial(n.right))
let myFormula = Formula(kind: fkMul,
left: Formula(kind: fkLit, value: 2.0),
right: Formula(kind: fkExp,
right: Formula(kind: fkExp,
left: Formula(kind: fkVar, name: "x"),
right: Formula(kind: fkLit, value: 5.0)))
@@ -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)))

View File

@@ -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

View File

@@ -3,13 +3,13 @@ discard """
output: "Using test.Closing test."
"""
import
import
macros
# This macro mimics the using statement from C#
#
# It's kept only as a test for the macro system
# Nim's destructors offer a mechanism for automatic
# Nim's destructors offer a mechanism for automatic
# disposal of resources.
#
macro autoClose(e: expr): stmt {.immediate.} =
@@ -20,19 +20,19 @@ macro autoClose(e: expr): stmt {.immediate.} =
var args = e
var body = e[2]
var
variables : seq[PNimrodNode]
closingCalls : seq[PNimrodNode]
var
variables : seq[NimNode]
closingCalls : seq[NimNode]
newSeq(variables, 0)
newSeq(closingCalls, 0)
for i in countup(1, args.len-2):
if args[i].kind == nnkExprEqExpr:
var varName = args[i][0]
var varValue = args[i][1]
var varAssignment = newNimNode(nnkIdentDefs)
varAssignment.add(varName)
varAssignment.add(newNimNode(nnkEmpty)) # empty means no type
@@ -43,7 +43,7 @@ macro autoClose(e: expr): stmt {.immediate.} =
else:
error "Using statement: Unexpected expression. Got " &
$args[i].kind & " instead of assignment."
var varSection = newNimNode(nnkVarSection)
varSection.add(variables)
@@ -67,10 +67,10 @@ macro autoClose(e: expr): stmt {.immediate.} =
targetAst[0][1][0] = varSection
targetAst[0][1][1][0] = body
targetAst[0][1][1][1][0] = finallyBlock
result = targetAst
type
type
TResource* = object
field*: string

View File

@@ -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] = @[]
@@ -40,7 +40,7 @@ proc buildSuiteContents(suiteName, suiteDesc, suiteBloc: PNimrodNode): tuple[tes
discard
return (tests: tests)
macro suite(suiteName, suiteDesc: expr, suiteBloc: stmt): stmt {.immediate.} =
let contents = buildSuiteContents(suiteName, suiteDesc, suiteBloc)