4 ms·
Absolutely :) I "discovered" this when researching implementing a new language on top of python. The decorator is completely unnecessary, so you can have a comp
by ssize_t 7y ago
Absolutely :) I "discovered" this when researching implementing a new language on top of python. The decorator is completely unnecessary, so you can have a complete language with custom features built entirely on top of the python interpreter.
- lapnitnelav 7y agoI've been toying with the idea of implementing some sort of DSL on top of python but wouldn't really know where to start. Would you have any pointers (heh) given the discovery you made?
- ssize_t 7y agoI think the `#coding:` approach is promising. You have to fit your parsing/transpilation into a pretty small time budget (which is a fun challenge on its own) to keep the startup time hit negligible. The source code gets reparsed serval times in certain cases, e.g. when printing a stack trace, so its a good idea to have some sort of caching mechanism. As far as parsing goes, if you want to stick with python I've had good success with pyparsing [1], otherwise I have a strong preference to do language-related things in OCaml with menhir [2]. I've toyed with wrapping the Python parser in an OCaml library with decent success [3]. But, of course, unless you're optimizing for fun, it's probably a good idea to stick with Python. Another, weirder, approach is creating a language that happens to be able to be parsed by the same grammar as python, then using a decorator (or similar) to get the source code or the ast to reparse and transpile. As an example you could have something like `a <- b` be the syntax for an actor model message receive dsl (which is valid python). [1] https://github.com/pyparsing/pyparsing https://github.com/pyparsing/pyparsing [2] http://gallium.inria.fr/~fpottier/menhir/ http://gallium.inria.fr/~fpottier/menhir/ [3] https://github.com/georgek42/pyparser https://github.com/georgek42/pyparser
- lapnitnelav 7y agoThanks a mil for the tips.
- j88439h84 7y agoSyntactic is a module specifically for doing this. https://github.com/metatooling/syntactic https://github.com/metatooling/syntactic
- vthriller 7y agoSpeaking of building on top of CPython, you can also go another route with compiling however crazy syntax you can come up with into its bytecode. See e.g. https://pyos.github.io/dg/ https://pyos.github.io/dg/