lihaoyi / macropy

Macros in Python: quasiquotes, case classes, LINQ and more!
3.28k stars 178 forks source link

Pre-compiling macros #52

Closed lihaoyi closed 11 years ago

lihaoyi commented 11 years ago

There should be a way of loading a bunch of modules, expanding all the macros inside them and saving the expanded source files. This would allow library-writers to utilize MacroPy as a dev/build/test-time dependency without having it as a run-time dependency, making it more likely that people will start using MacroPy incrementally.

Everything up to the point of hygienic quasiquotes should be extremely simple: just unparse the AST and save it somewhere. Hygienic quasiquotes maintain a registry of lexically-captured identifiers with all sorts of things inside (values, classes, packages, functions, methods, closures) to simulate lexical closure within the quasiquote block. These will need to be pickled and saved during pre-compilation, such that they can be accurately re-constituted during the macroless import process.

lihaoyi commented 11 years ago

This works

https://github.com/lihaoyi/macropy#exporting-your-expanded-code

May not work perfectly, but there's a small unit test for it that demonstrates it working in the presence of hygienic quasiquotes (i.e. all the pickling etc. described above works) and I've tested it (not it automated tests) to work on the entire MacroPy test suite.

Not sure if it will remain in this form, or if there are other utility helpers that are nice to have, but as a first approximation (pre-macro-expanding the 3000LOC super-macro-intensive MacroPy test suite and executing it flawlessly) it works.