Re: [Python-ideas] [Python-Dev] AST Transformation Hooks for Domain Specific Languages

(Fixed list to be python-ideas) On Sat, Apr 9, 2011 at 2:50 AM, David Malcolm <dmalcolm@redhat.com> wrote:
Ah, very interesting - indeed, compile-time metaprogramming is definitely what this idea is about (with all of the tremendous power and potentially major liabilities for readability and debugging that entails).
Yep - the registration would be with the compiler itself. Technically a "flat" namespace, but allowing dots in the names means it can mirror the general Python module namespace. An alternative would be to use the module namespace rather than a dedicated one, in which case registration might not be necessary. However, requiring an explicit registration step to support DSLs doesn't really bother me, even it means they can't be used as standalone scripts, but instead must be executed via a module that registers the DSL. With runpy.run_module and runpy.run_path available, supporting execution of other scripts isn't a great burden.
No, it's expressed by the "from dsl.sql" at the end of the line: def lookup_address(name : dsl.sql.char, dob : dsl.sql.date) from dsl.sql: The import is included in the example, simply because that was a pattern I used in my sample expansion. As Jon pointed out in his reply, it's basically a very similar idea to Mython's "quote" block, but leveraging off the existing Function node to pick up other fun tricks like arguments, decorators and annotations rather than defining a completely separate statement.
They survive if you use PyCF_ONLY_AST (or otherwise get hold of the AST from Python). The arenas are just Python lists and returning the AST for use by Python code bumps the reference count of the head node.
Yeah, sorry about that - the posting to python-dev was a mistake due to not properly checking the address auto-complete in Gmail.
Yeah, there's a way to experiment with this that's much friendlier to those systems: require that the body of the statement *also* be standard Python code. Support for non-Python syntax would then be handled by explicitly embedding it in a string (potentially even the docstring). The advantage this would have over existing string parsing techniques is that the contents would be checked at compile time rather than runtime, just as Mython does.
This is also AST-time, so the back end shouldn't make a big difference.
Indeed, in a lot of ways, this idea is really just an alternative syntax proposal for Mython "quote" statements, along with the concept of integrating it into the main compiler. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia
participants (1)
-
Nick Coghlan