The following example illustrates a problem with macro parameters. Save this as a test.nim:
import macros, strutils, os
macro test_paths(path: string): stmt =
let input = slurp(path.strVal)
for line in input.splitLines:
echo "Did read ", line.strip
result = newNimNode(nnkStmtList)
when isMainModule:
test_paths("test.nim")
test_paths("."/"test.nim")
When compiled, the file will process the first macro and will fail on the second one:
stack trace: (most recent call last)
test.nim(4) test_paths
test.nim(4, 24) Error: field 'strVal' cannot be found
I understand that in the case of the second invocation rather than a string the macro is receiving the whole / operator call with two parameters. Is there a helper which goes through the input parameters and processes them to always return a string? If I try to use macro.$ on the path parameter it SIGSEVs.import macros, strutils, os
macro test_paths(path: static[string]): stmt =
let input = slurp(path)
for line in input.splitLines:
echo "Did read ", line.strip
result = newNimNode(nnkStmtList)
when isMainModule:
test_paths("test.nim")
test_paths("."/"test.nim")