I'm having some trouble with a macro setupDepFile() I'm working on. I'm trying to call it from a proc setupDepFilesProc() but getting a compilation error. I want to use a proc since I want to do some additional work before calling the macro. However, it doesn't like the parameter types I pass to setupDepFile().
Error: type mismatch: got <string, bool>
but expected one of:
macro setupDepFile(filename: static[string]; autowrite: static[bool]): untyped
first type mismatch at position: 1
required type: static[string]
but expression 'filename' is of type: string
Also a secondary issue where I'm not getting the correct return value on fileExists() in setupDepFiles().
Code below:-
import macros
import os
import strutils
proc writeDepFile*(constname, filename: string) =
let pdir = parentDir(filename)
if not dirExists(pdir):
try:
createDir(pdir)
except OSError:
echo "Failed to create directory: " & pdir
quit()
if not existsFile(filename) or getFileSize(filename) != constname.len:
writeFile(filename, constname)
while not existsFile(filename):
sleep(10)
macro setupDepFile*(filename: static[string], autowrite: static[bool]): untyped =
result = newNimNode(nnkStmtList)
echo staticRead(filename)
if not fileExists(filename): # <========== this always returns failed even if file exists and is read in line 22
echo "Could not find " & filename
quit()
var
vname = filename.multiReplace([
("/", "_"), ("\\", "_"), (":", "_"), (".", "_")
]).toUpperAscii()
ivname = ident(vname)
if vname[0] == '_': vname = "V" & vname
result.add(quote do:
const `ivname` = staticRead `filename`
)
if autowrite:
result.add(quote do:
writeDepFile(`ivname`, `filename`)
)
macro setupDepFiles*(filenames: static[seq[string]], autowrite: bool): untyped =
result = newNimNode(nnkStmtList)
for filename in filenames:
result.add(quote do:
setupDepFile(`filename`, `autowrite`)
)
proc setupDepFilesProc*(filenames: seq[string], autowrite: bool) =
for filename in filenames:
setupDepFile(filename, autowrite) # <========== compilation error as below
setupDepFiles(@["data.txt"], true)
setupDepFilesProc(@["data.txt"], true)
If I use untyped params instead, is it possible? Further, how do you get to value of the param if untyped? If I use strVal(), it says strVal() not found.
Also am still wondering why fileExists() always fails.
Thank you.