Permalink
Please sign in to comment.
Browse files
This pure Python compiler can now compile print("Hello World")!
I glued together the pure Python pgen2 parser (from lib2to3) and the
Python2.7 compiler module (deprecated in Python 3).
parse.py is a front end to them both. The shell functions in run.sh
use it to generate .pyc file that can be executed by the Python 3.4
interpreter.
Details:
- Copy in the Python 2.7 grammar and use it. The 2to3 grammar from
Python 3 has some productions that the compiler module can't handle.
Note that deleting the print production at runtime allows us to parse
at least some Python 3 code.
- Make the grammar pure input data, rather than being baked in:
- Remove pygram.py from lib2to3. Choose python2 vs python3 grammar in
the parse.py tool.
- Move the Symbols class to parse.py to remove host dependency on the
stdlib 'symbol' module.
- Initialize pytree.py from Symbols, i.e. the production IDs extracted
by pgen2 from the grammar
- Initialize compiler/transformer.py from Symbols
- Create a Pgen2Transformer class that uses the parse tree created by
pgen2 rather than by the stdlib parsermodule.c. The compiler module
had always used the latter interface ('import parser').
- The small py2st() function in parse.py does adaption to tuples.
- Implement pretty-printing of the tuple-based parse tree.
- Add start_symbol parameter to ensure we're parsing the 'file_input'
rule (not just the first rule in the grammar file!)
- Fix bad 2to3 conversion (self.next was a member)
- Fix string/unicode problems in types.CodeType instantiation.
- Update the .pyc header to include the file size added in Python 3.
- Shell script automation and tests.
- Cleanup: Remove compileFile.
Status: Hello world runs, but ./parse.py doesn't. LOAD_LOCALS bytecode
is not found. There's still a host dependency with the 'dis' module.- Loading branch information...
Showing
with
695 additions
and 245 deletions.
- 0 opy/{Grammar.txt → 2to3.grammar}
- +1 −1 opy/compiler/__init__.py
- +48 −13 opy/compiler/pyassem.py
- +29 −37 opy/compiler/pycodegen.py
- +214 −108 opy/compiler/transformer.py
- +143 −26 opy/parse.py
- +2 −2 opy/pgen2/driver.py
- +1 −1 opy/pgen2/grammar.py
- +6 −1 opy/pgen2/parse.py
- +1 −1 opy/pgen2/pgen.py
- +2 −2 opy/pgen2/tokenize.py
- +142 −0 opy/py27.grammar
- +0 −40 opy/pygram.py
- +14 −8 opy/pytree.py
- +90 −5 opy/run.sh
- +1 −0 opy/testdata/hello_py2.py
- +1 −0 opy/testdata/hello_py3.py
File renamed without changes.
Oops, something went wrong.
0 comments on commit
31c63a7