macros.nim has parseStmt() to parse statements, and parseExpr() to parse expressions, but nothing that I see to parse statement list expressions, which is node type nnkStmtListExpr.
I'm writing macros to preprocess code at compile-time, and would love a parseStmts() proc so I could use ";" between statements within preprocessed code blocks.
import macros
macro m(): untyped =
result = parseStmt"""
echo "foo"
echo "bar"
"""
m()
Stmt can be a list of statements.
Or
import macros
macro m(): untyped =
result = parseStmt"""(echo "foo"; echo "bar")"""
m()
Thank you Araq and OderWat.
I was able to take your examples and use it to evaluate a const statement list expression string:
macro tripleParseStmt*(s: string): untyped =
let tmp = "(" & s.strVal & ")"
result = parseStmt(tmp)
tripleParseStmt("""echo "foo2"; echo "bar2"""") # works.
const s3 = """echo "foo3"; echo "bar3""""
tripleParseStmt($s3) # works if I stringify a const string.
It works outside a macro, but I was hoping to call tripleParseStmt from inside a macro, on a variable created within the macro, but that shows this message: Error: field 'strVal' cannot be found.
I also tried calling parseStmt() from within my macro using "(" .. ")":
var mybody = parseStmt("(" & res2[i] & ")")
That shows "Error: invalid indentation", which is what I was getting before when parsing strings containing ";".
I wish there was a magical, built-in parseStmts() proc to handle statement list expressions.