[Python-ideas] Python dialect that compiles into python

Anders Hovmöller boxed at killingar.net
Fri Sep 7 08:38:48 EDT 2018



Many features on this list propose different syntax to python, producing 
> different python "dialects" that can statically be transformed to python : 
>
> - a,b += f(x) → _t = f(x); a += _t; b += _t;  (augmented assignement 
> unpacking)
> - a = 2x + 1 → a = 2*x + 1  (juxtaposition is product)
> - f(*, x, y) → f(x=x, y=y)  (simplekwargs)
> - DSL specific language
> - all def become @partially def
> - etc...
>
> Using a modified version of ast, it is relatively easy to modifiy the 
> syntax tree of a program to produce another program. So one could compile 
> the "python dialect" into regular python. The last example with partially 
> for example doesn't even need new syntax.
>

For my specific suggestion (simplekwargs), it's really not worth doing at 
all if it's not a part of standard Python in my opinion. The reason is 
tooling: you wouldn't get tools like Jedi and PyCharm to read these files 
and interpret them correctly.

That being said, it could absolutely be a nice way to prototype things or 
for very edge cases. I'd recommend looking into parso (the AST lib that 
underlies Jedi). I used it for the analysis tool I wrote for simplekwargs 
and I've also used it to write my mutation tester mutmut. It has several 
advantages:

- it's a roundtrip AST so you wouldn't lose any formatting or comments etc
- it has an error recovery mode where parse errors are isolated in the AST 
as error nodes. You could probably use this to just enumerate error nodes 
and doing .get_code() on them and then have all your special parsing in 
there.
- it is very liberal in what it accepts putting into an AST that it renders 
out to source again so it's quite easy to hack something together that works

/ Anders
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180907/c82b0368/attachment.html>


More information about the Python-ideas mailing list