summary refs log tree commit diff stats
path: root/tests/vm/tnimnode.nim
blob: f1c4d5e5a5f953a7304abb30f97c9988619b9b70 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import macros

proc assertEq(arg0,arg1: string): void =
  if arg0 != arg1:
    raiseAssert("strings not equal:\n" & arg0 & "\n" & arg1)

# a simple assignment of stmtList to another variable
var node {.compileTime.}: NimNode
# an assignment of stmtList into an array
var nodeArray {.compileTime.}: array[1, NimNode]
# an assignment of stmtList into a seq
var nodeSeq {.compileTime.} = newSeq[NimNode](2)

proc checkNode(arg: NimNode; name: string): void {. compileTime .} =
  echo "checking ", name

  assertEq arg.lispRepr, """(StmtList (DiscardStmt (Empty)))"""

  node = arg
  nodeArray = [arg]
  nodeSeq[0] = arg
  var seqAppend = newSeq[NimNode](0)
  seqAppend.add([arg]) # at the time of this writing this works
  seqAppend.add(arg)   # bit this creates a copy
  arg.add newCall(ident"echo", newLit("Hello World"))

  assertEq arg.lispRepr,          """(StmtList (DiscardStmt (Empty)) (Call (Ident "echo") (StrLit "Hello World")))"""
  assertEq node.lispRepr,         """(StmtList (DiscardStmt (Empty)) (Call (Ident "echo") (StrLit "Hello World")))"""
  assertEq nodeArray[0].lispRepr, """(StmtList (DiscardStmt (Empty)) (Call (Ident "echo") (StrLit "Hello World")))"""
  assertEq nodeSeq[0].lispRepr,   """(StmtList (DiscardStmt (Empty)) (Call (Ident "echo") (StrLit "Hello World")))"""
  assertEq seqAppend[0].lispRepr, """(StmtList (DiscardStmt (Empty)) (Call (Ident "echo") (StrLit "Hello World")))"""
  assertEq seqAppend[1].lispRepr, """(StmtList (DiscardStmt (Empty)) (Call (Ident "echo") (StrLit "Hello World")))"""

  echo "OK"

# the root node that is used to generate the Ast
var stmtList {.compileTime.}: NimNode

static:
  stmtList = newStmtList(nnkDiscardStmt.newTree(newEmptyNode()))

  checkNode(stmtList, "direct construction")


macro foo(stmtList: untyped): untyped =
  checkNode(stmtList, "untyped macro argument")

foo:
  discard


static:
  stmtList = quote do:
    discard

  checkNode(newTree(nnkStmtList, stmtList), "create with quote")


static:
  echo "testing body from loop"
  var loop = quote do:
    for i in 0 ..< 10:
      discard

  let innerBody = loop[2]
  innerBody.add newCall(ident"echo", newLit("Hello World"))

  assertEq loop[2].lispRepr, innerBody.lispRepr

  echo "OK"


static:
  echo "testing creation of comment node"
  var docComment: NimNode = newNimNode(nnkCommentStmt)
  docComment.strVal = "This is a doc comment"

  assertEq repr(docComment), "## This is a doc comment"

  echo "OK"