diff options
-rw-r--r-- | compiler/trees.nim | 1 | ||||
-rw-r--r-- | compiler/vm.nim | 2 | ||||
-rw-r--r-- | lib/core/macros.nim | 8 | ||||
-rw-r--r-- | tests/macros/tnodecompare.nim | 38 |
4 files changed, 29 insertions, 20 deletions
diff --git a/compiler/trees.nim b/compiler/trees.nim index 8f0af89d3..aa5738195 100644 --- a/compiler/trees.nim +++ b/compiler/trees.nim @@ -41,6 +41,7 @@ proc exprStructuralEquivalent*(a, b: PNode; strictSymEquality=false): bool = of nkCharLit..nkUInt64Lit: result = a.intVal == b.intVal of nkFloatLit..nkFloat64Lit: result = a.floatVal == b.floatVal of nkStrLit..nkTripleStrLit: result = a.strVal == b.strVal + of nkCommentStmt: result = a.comment == b.comment of nkEmpty, nkNilLit, nkType: result = true else: if sonsLen(a) == sonsLen(b): diff --git a/compiler/vm.nim b/compiler/vm.nim index bc5873ff5..b8e6467b5 100644 --- a/compiler/vm.nim +++ b/compiler/vm.nim @@ -1406,7 +1406,7 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg = if dest.kind in {nkStrLit..nkTripleStrLit} and regs[rb].kind in {rkNode}: 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 else: stackTrace(c, tos, pc, errFieldXNotFound, "strVal") diff --git a/lib/core/macros.nim b/lib/core/macros.nim index 13f341350..af1e9de28 100644 --- a/lib/core/macros.nim +++ b/lib/core/macros.nim @@ -255,6 +255,11 @@ proc newStrLitNode*(s: string): NimNode {.compileTime, noSideEffect.} = result = newNimNode(nnkStrLit) 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.} = ## creates a int literal node from `i` result = newNimNode(nnkIntLit) @@ -275,6 +280,7 @@ proc newIdentNode*(i: string): NimNode {.compileTime.} = result = newNimNode(nnkIdent) result.ident = !i + type BindSymRule* = enum ## specifies how ``bindSym`` behaves brClosed, ## only the symbols in current scope are bound @@ -835,6 +841,8 @@ proc `$`*(node: NimNode): string {.compileTime.} = result = $node[0] of nnkAccQuoted: result = $node[0] + of nnkCommentStmt: + result = node.strVal else: badNodeKind node.kind, "$" diff --git a/tests/macros/tnodecompare.nim b/tests/macros/tnodecompare.nim index 3870c7559..b9cf7df48 100644 --- a/tests/macros/tnodecompare.nim +++ b/tests/macros/tnodecompare.nim @@ -1,33 +1,33 @@ -discard """ -output: '''true -false -true -false -true -false -true -false''' -""" - 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 = newLit(a == b) -echo test(1, 1) -echo test(1, 2) +doAssert test(1, 1) == true +doAssert test(1, 2) == false type Obj = object of RootObj Other = object of RootObj -echo test(Obj, Obj) -echo test(Obj, Other) +doAssert test(Obj, Obj) == true +doAssert test(Obj, Other) == false var a, b: int -echo test(a, a) -echo test(a, b) +doAssert test(a, a) == true +doAssert test(a, b) == false macro test2: expr = newLit(bindSym"Obj" == bindSym"Obj") @@ -35,5 +35,5 @@ macro test2: expr = macro test3: expr = newLit(bindSym"Obj" == bindSym"Other") -echo test2() -echo test3() +doAssert test2() == true +doAssert test3() == false |