I'm trying to perform the equivalent of the eval
method from Python in nim.
I was under the impression that parseStmt
from the macros
package should help me with this, but I'm facing a compilation issue that I don't understand.
import macros
echo parseStmt("1 + 2")
I would have expected this to print 3
when executed, but instead the compilation complains that
Error: request to generate code for .compileTime proc: $
I found this thread, and the examples there work, and following this, I was able to make the following program that works as I would expect:
import macros
import strformat
macro eval(value: string): untyped =
result = parseStmt fmt"{value}"
echo eval("1+2")
But I don't undertand why it needs to be written in this way at all. If I inline the statement, let value = "1 + 2"; echo parseStmt fmt"{value}"
, I get the same compile error as above.
Also, why is parseStmt value
different from parseStmt fmt"{value}"
, in the context of the eval
macro above?
What am I missing here?
Thank you in advance for any clarifications!