Hi,
Is there a way to define a term rewriting template that will match proc definitions? Something like:
template procRewrite{a}(a: untyped{nkProcDef}): untyped =
a
Can't make it to work easily. It needs to accept proc definition as untyped argument.
Thanks a lot
Thanks for reply, The effect is exactly the same as you say. The only difference is that you don't need to annotate every function.
I think it would be useful for test coverage library for example.
Would you consider a PR?
I don't think so, if you want to apply it for multiple/all procs use the block syntax:
transform:
proc p() = discard
proc q() = discard
I know the additional indentation is annoying but {.push transform.} ... {.pop.} is worse IMO.
or indent the whole module
I would like a reserved identifier for a macro which I can optionally implement and which is automatically invoked with the entire module's AST as its argument.
I thought Nim supported "Higher-Level-Module" like higher level function?
fooMacro:
./path/to/module
Also there is the fresh out the oven compiler API, not sure if it can be use for that.
Hmm, couldn't get the "higher-order-module" solution to work, but a simple include seems to do the trick:
# main module:
import macros
macro mangleModule(modl: typed): untyped =
echo modl.treeRepr
result = modl[0].last
result.last.last[0].strVal = "Ho"
mangleModule:
include test
# module test:
echo "Hi"
# output:
StmtList
StmtList
IncludeStmt
Ident "test"
Command
Sym "echo"
HiddenStdConv
Empty
Bracket
StrLit "Hi"
...
Ho
The typed version of the AST of the include statement apparently consists of the statement itself followed by the AST of the included code. Strange, but useful :).
As to the compiler API (looked at nimeval.nim): one would have to inject a macro call as the first statement of the interpreted script, I don't see how to do that.