bindSym always returns all the symbols, local and larger scopes, for a variable. If I have
import macros
var a = 0
block:
var a = "block"
macro m: stmt =
let s = bindsym"a"
echo s.lisprepr
result = newstmtlist()
m()
I get
ClosedSymChoice(Sym(a), Sym(a))
How do I know which one is the actual local symbol, and which are shadowed?Ok. With the following code
import macros
var a = 0
block:
var a = "block"
block:
var a = true
block:
var a = 0.0
macro m: stmt =
let s = bindsym"a"
for n in s: echo n.gettypeinst.lisprepr
result = newstmtlist()
m()
I got
Sym(float64)
Sym(bool)
Sym(string)
Sym(int)
I guess it depends on how you implemented the symbol lookup. Is it guaranteed that the first one is the most local one?tests/<testtype>/<testfile.nim>
and the test file should start with the letter "t",
eg tests/macros/tbindsym2.nim
Maybe @jxy is asking about how to test something which happens on compile time and therefor has no output when run but when compiled.
You could do it like this:
# test bindsym order
import macros
template check(x, y) =
doAssert(x.gettypeinst.lisprepr == y)
var a = 0
block:
var a = "block"
block:
var a = true
block:
var a = 0.0
macro m: untyped =
let s = bindsym"a"
check(s[0], "Sym(float64)")
check(s[1], "Sym(bool)")
check(s[2], "Sym(string)")
check(s[3], "Sym(int)")
result = newstmtlist()
m()
Not sure if there is a better way though.