[Python-ideas] Python dialect that compiles into python

Robert Vanden Eynde robertvandeneynde at hotmail.com
Fri Sep 7 07:57:50 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.

Those solutions that are too specific would then be provided as a module on pip that has a common interface for "compiling" :

$ cat test.dialect.py<http://test.dialect.py>
#! dialect: juxtaposition
a = 2x + 1

$ python -m compile test.dialect.py<http://test.dialect.py>
$ cat test.py
#! compiled with dialect juxtaposition
a = 2x + 1

The generated file should also be read only if the filesystem provides the option.

In the web world, it's very common to compile into html, css or js. One of the reason was that the web must be veeeery generic and will not try to fit everyone needs.

- less compiles scss into css
- coffeescript into js
- source map provides a standard way to map each line of the new file into lines of the old files (useful for exceptions !)

One useful feature of those compilers is the --watch Option that allows to avoid to launch the compilation manually.

Of course, in the js world, the syntax was improved in the end after a long maturation of the compiling and not compiling libraries.

In the java world, languages such as Scala compile into bytecode, that's another idea.

If a standard module like "compile" is written, users can write their own module that will automatically be read by "compile" (for example, pip install compile_juxtaposition would allow the juxtaposition dialect). Compile doesn't even have to be on the standard python, it can be a lib.

One could write a module using multiple dialect like dialect: juxtaposition, simplekwargs

The order would be technically important but functionally non important.

Actually, I might start to write this lib, that looks fun.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180907/fd82b419/attachment-0001.html>


More information about the Python-ideas mailing list