14 ms·
Anyone looking to do this kind of thing, the macropy library makes it a fair bit easier: https://macropy3.readthedocs.io/en/latest/ https://macropy3.readthedocs
by traverseda 4y ago
Anyone looking to do this kind of thing, the macropy library makes it a fair bit easier: https://macropy3.readthedocs.io/en/latest/ https://macropy3.readthedocs.io/en/latest/
You really do need to be modifying the AST and not just applying a regex to the source code text. This can be a challenge if you want to modify python syntax as you first need to get an AST you can modify, personally I recommend starting with the LARK library and modifying the python syntax grammer it includes.
https://lark-parser.readthedocs.io/en/latest/examples/advanced/python_parser.html https://lark-parser.readthedocs.io/en/latest/examples/advanc...
I use similar techniques to transpile openscad code into a python AST in my "pySdfScad" project, while still theoretically getting the benefits of fancy tracebacks and debugging and the like. Probably should have gone with a simple parser instead, but what can you do.
I think they should have stopped at the "cursed way" and not the "truly cursed way", if they really wanted the syntax changes than having your own python parser like the LARK implementation I mention above is a must.
- tusharsadhwani 4y agoIt's true, the truly cursed method should never be used in a project that you actually aim to use. But, I just wanted to see how close I could get to the original idea, regardless of how "bad" the solution ended up being. The second method indeed goes very far! I've made some very cool libraries and talks with that alone.