improved comment satement support in macros (#5904)
This commit is contained in:
parent
da52ade86e
commit
eb8e267ff6
4 changed files with 29 additions and 20 deletions
|
|
@ -41,6 +41,7 @@ proc exprStructuralEquivalent*(a, b: PNode; strictSymEquality=false): bool =
|
||||||
of nkCharLit..nkUInt64Lit: result = a.intVal == b.intVal
|
of nkCharLit..nkUInt64Lit: result = a.intVal == b.intVal
|
||||||
of nkFloatLit..nkFloat64Lit: result = a.floatVal == b.floatVal
|
of nkFloatLit..nkFloat64Lit: result = a.floatVal == b.floatVal
|
||||||
of nkStrLit..nkTripleStrLit: result = a.strVal == b.strVal
|
of nkStrLit..nkTripleStrLit: result = a.strVal == b.strVal
|
||||||
|
of nkCommentStmt: result = a.comment == b.comment
|
||||||
of nkEmpty, nkNilLit, nkType: result = true
|
of nkEmpty, nkNilLit, nkType: result = true
|
||||||
else:
|
else:
|
||||||
if sonsLen(a) == sonsLen(b):
|
if sonsLen(a) == sonsLen(b):
|
||||||
|
|
|
||||||
|
|
@ -1406,7 +1406,7 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
|
||||||
if dest.kind in {nkStrLit..nkTripleStrLit} and
|
if dest.kind in {nkStrLit..nkTripleStrLit} and
|
||||||
regs[rb].kind in {rkNode}:
|
regs[rb].kind in {rkNode}:
|
||||||
dest.strVal = regs[rb].node.strVal
|
dest.strVal = regs[rb].node.strVal
|
||||||
elif dest.kind == nkCommentStmt:
|
elif dest.kind == nkCommentStmt and regs[rb].kind in {rkNode}:
|
||||||
dest.comment = regs[rb].node.strVal
|
dest.comment = regs[rb].node.strVal
|
||||||
else:
|
else:
|
||||||
stackTrace(c, tos, pc, errFieldXNotFound, "strVal")
|
stackTrace(c, tos, pc, errFieldXNotFound, "strVal")
|
||||||
|
|
|
||||||
|
|
@ -255,6 +255,11 @@ proc newStrLitNode*(s: string): NimNode {.compileTime, noSideEffect.} =
|
||||||
result = newNimNode(nnkStrLit)
|
result = newNimNode(nnkStrLit)
|
||||||
result.strVal = s
|
result.strVal = s
|
||||||
|
|
||||||
|
proc newCommentStmtNode*(s: string): NimNode {.compileTime, noSideEffect.} =
|
||||||
|
## creates a comment statement node
|
||||||
|
result = newNimNode(nnkCommentStmt)
|
||||||
|
result.strVal = s
|
||||||
|
|
||||||
proc newIntLitNode*(i: BiggestInt): NimNode {.compileTime.} =
|
proc newIntLitNode*(i: BiggestInt): NimNode {.compileTime.} =
|
||||||
## creates a int literal node from `i`
|
## creates a int literal node from `i`
|
||||||
result = newNimNode(nnkIntLit)
|
result = newNimNode(nnkIntLit)
|
||||||
|
|
@ -275,6 +280,7 @@ proc newIdentNode*(i: string): NimNode {.compileTime.} =
|
||||||
result = newNimNode(nnkIdent)
|
result = newNimNode(nnkIdent)
|
||||||
result.ident = !i
|
result.ident = !i
|
||||||
|
|
||||||
|
|
||||||
type
|
type
|
||||||
BindSymRule* = enum ## specifies how ``bindSym`` behaves
|
BindSymRule* = enum ## specifies how ``bindSym`` behaves
|
||||||
brClosed, ## only the symbols in current scope are bound
|
brClosed, ## only the symbols in current scope are bound
|
||||||
|
|
@ -835,6 +841,8 @@ proc `$`*(node: NimNode): string {.compileTime.} =
|
||||||
result = $node[0]
|
result = $node[0]
|
||||||
of nnkAccQuoted:
|
of nnkAccQuoted:
|
||||||
result = $node[0]
|
result = $node[0]
|
||||||
|
of nnkCommentStmt:
|
||||||
|
result = node.strVal
|
||||||
else:
|
else:
|
||||||
badNodeKind node.kind, "$"
|
badNodeKind node.kind, "$"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,33 +1,33 @@
|
||||||
discard """
|
|
||||||
output: '''true
|
|
||||||
false
|
|
||||||
true
|
|
||||||
false
|
|
||||||
true
|
|
||||||
false
|
|
||||||
true
|
|
||||||
false'''
|
|
||||||
"""
|
|
||||||
|
|
||||||
import macros
|
import macros
|
||||||
|
|
||||||
|
static:
|
||||||
|
let nodeA = newCommentStmtNode("this is a comment")
|
||||||
|
doAssert nodeA.repr == "## this is a comment"
|
||||||
|
doAssert nodeA.strVal == "this is a comment"
|
||||||
|
doAssert $nodeA == "this is a comment"
|
||||||
|
|
||||||
|
let nodeB = newCommentStmtNode("this is a comment")
|
||||||
|
doAssert nodeA == nodeB
|
||||||
|
nodeB.strVal = "this is a different comment"
|
||||||
|
doAssert nodeA != nodeB
|
||||||
|
|
||||||
macro test(a: typed, b: typed): expr =
|
macro test(a: typed, b: typed): expr =
|
||||||
newLit(a == b)
|
newLit(a == b)
|
||||||
|
|
||||||
echo test(1, 1)
|
doAssert test(1, 1) == true
|
||||||
echo test(1, 2)
|
doAssert test(1, 2) == false
|
||||||
|
|
||||||
type
|
type
|
||||||
Obj = object of RootObj
|
Obj = object of RootObj
|
||||||
Other = object of RootObj
|
Other = object of RootObj
|
||||||
|
|
||||||
echo test(Obj, Obj)
|
doAssert test(Obj, Obj) == true
|
||||||
echo test(Obj, Other)
|
doAssert test(Obj, Other) == false
|
||||||
|
|
||||||
var a, b: int
|
var a, b: int
|
||||||
|
|
||||||
echo test(a, a)
|
doAssert test(a, a) == true
|
||||||
echo test(a, b)
|
doAssert test(a, b) == false
|
||||||
|
|
||||||
macro test2: expr =
|
macro test2: expr =
|
||||||
newLit(bindSym"Obj" == bindSym"Obj")
|
newLit(bindSym"Obj" == bindSym"Obj")
|
||||||
|
|
@ -35,5 +35,5 @@ macro test2: expr =
|
||||||
macro test3: expr =
|
macro test3: expr =
|
||||||
newLit(bindSym"Obj" == bindSym"Other")
|
newLit(bindSym"Obj" == bindSym"Other")
|
||||||
|
|
||||||
echo test2()
|
doAssert test2() == true
|
||||||
echo test3()
|
doAssert test3() == false
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue