I am trying to learn more about macros, and in particular I am trying to understand how much information I can get from the compiler that is not in the AST.
In particular, there are two things that could be useful for patty :
Unfortunately, 1. does not seem to work with immediate macros. On the other hand, I was not able find a decent syntax to do ML-style pattern matching without making the macro immediate (it is difficult to make the patterns valid Nim statements in that context).
It seems like I would need the macro to be eager in the first parameter (the expression to be matched) and lazy in the second one (the statement containing the pattern)...
I do not have any good idea right now, but I will try to think of something that remains readable while not being immediate
It seems like I would need the macro to be eager in the first parameter (the expression to be matched) and lazy in the second one (the statement containing the pattern)...
But this exactly what the typed vs untyped distinction enables! For instance in lexim I use this:
macro match*(s: cstring|string; pos: int; sections: untyped): untyped =
...
proc main =
var input = "the 0909 else input elif elseo end"
var pos = 0
while pos < input.len:
let oldPos = pos
match input, pos:
of r"\d+": echo "an integer ", input.substr(oldPos, pos-1), "##"
of "else": echo "an ELSE"
of "elif": echo "an ELIF"
of "end": echo "an END"
of r"[a-zA-Z_]\w+": echo "an identifier ", input.substr(oldPos, pos-1), "##"
of r".": echo "something else ", input.substr(oldPos, pos-1), "##"
Great! It seems that, whenever I find a stumbling block, it turns out that things are done right and I have just missed something! :-)
I have tried using typed/untyped and it seems to work like a charm!