| If you want to play with the algorithms at a high level [1], there are two Python AST -> bytecode implementations I've seen, and one that I'm using. (1) The 'compiler' module from Python 2. I'm using this to build my shell project Oil, so it works. See Building Oil with the OPy Bytecode Compiler: http://www.oilshell.org/blog/2018/03/04.html . The heavily refactored source is in the opy/compiler2 directory of the repo on Github. (This module was removed in Python 3 due to its redundancy with the C implementation. But there's no conceptual difference between Python 2 and 3. Some small details have changed.) (2) tailbiter, which I mentioned here: http://www.oilshell.org/blog/2018/03/27.html They are written in very different styles. Tailbiter reads like Lisp code, which may or may not be what you want. The generated bytecode is different in the sense that the values it operates on aren't just ints and floats (which hardware deals with), but they are dynamically typed objects. But this doesn't change the overall algorithm to generate bytecode from an AST. [1] which I recommend over doing it in C or C++, because ASTs are
somewhat annoying in those languages |