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
|
discard """
nimout: '''"muhaha"
proc poo(x, y: int) =
let y = x
echo ["poo"]'''
"""
import macros
const
foo = "muhaha"
proc poo(x, y: int) =
let y = x
echo "poo"
macro m(x: typed): untyped =
echo repr x.getImpl
m(foo)
m(poo)
#------------
macro checkOwner(x: typed, check_id: static[int]): untyped =
let sym = case check_id:
of 0: x
of 1: x.getImpl.body[0][0][0]
of 2: x.getImpl.body[0][0][^1]
of 3: x.getImpl.body[1][0]
else: x
result = newStrLitNode($sym.owner.symKind)
macro isSameOwner(x, y: typed): untyped =
result =
if x.owner == y.owner: bindSym"true"
else: bindSym"false"
static:
doAssert checkOwner(foo, 0) == "nskModule"
doAssert checkOwner(poo, 0) == "nskModule"
doAssert checkOwner(poo, 1) == "nskProc"
doAssert checkOwner(poo, 2) == "nskProc"
doAssert checkOwner(poo, 3) == "nskModule"
doAssert isSameOwner(foo, poo)
doAssert isSameOwner(foo, echo) == false
doAssert isSameOwner(poo, len) == false
#---------------------------------------------------------------
macro check_gen_proc(ex: typed): (bool, bool) =
let lenChoice = bindsym"len"
var is_equal = false
var is_instance_of = false
for child in lenChoice:
if not is_equal:
is_equal = ex[0] == child
if not is_instance_of:
is_instance_of = isInstantiationOf(ex[0], child)
result = nnkTupleConstr.newTree(newLit(is_equal), newLit(is_instance_of))
# check that len(seq[int]) is not equal to bindSym"len", but is instance of it
let a = @[1,2,3]
assert: check_gen_proc(len(a)) == (false, true)
|