Rather than explain at great length, a quick bit of source code showing what I want to do:
#
# program called with nim c -d:foo=bar test.nim
#
import std / macros
const foo {.strdefine.} = "unknown"
macro jerry(fooName: untyped): untyped =
when defined(foo):
when foo == fooName.strVal:
result = quote do:
echo "foo found with a matching value"
else:
result = quote do:
echo "foo found but it does not match"
else:
result = quote do:
echo "foo not found"
jerry(bar)
It all works if I skip passing the value to the compiler: I get a "foo not found". But as soon as I try to examine the ident passed to jerry, I can't do a compile-time comparison. I get:
/home/johnd/Projects/test/test.nim(11, 17) Error: cannot evaluate at compile time: fooName
I also tried passing in a string literal ( jerry("bar") ) but that didn't help any, though clearly the compiler does know the content of a string literal at compile-time.
In other words, I am trying to change the macro output by comparing a local source-code reference to a compiler switch.
Anyone one know of a way to do something like this?
Alternatively, is it possible to get the context of the current "source code filename" at compile time?