Introduction

Pylayers teaches people about the compilation and execution of computer programs using Python. The computer programs are in a simple high level language that is similar to Python.

Compilation can be understood as converting a source code layer to an intermediate code layer, then to an assembly code layer, and finally, to a machine code layer which is executed:

Pylayers teaches about these layers and numerous related topics, all with working Python code, including: intermediate code generators, assembly code generators, machine code generators (assemblers), grammars, lexers, parsers, tokens, productions, abstract syntax trees, registers, instruction sets and more. An extensively documented simple compiler and virtual computer implemented in Python are provided. See all Pylayers software here.

I hope you find Pylayers useful. I the author, Dr. Christian Seberino, welcome all questions, comments and suggestions. You can reach me by email here.

Examples

Here are all the layers in the compilation or transformation of source code into machine code. Source code is typically the only layer humans see. Machine code is the only layer computers execute:

Layer Example Description
source code
x = 1 + 2
print(x)
                                
The source code layer is typically the only layer the human sees. Here the source code is in a high level Pythonic language.
abstract syntax tree
('program', ('statement', ('stat_semicol', ('semicol_base', ('expression',
('exp_log_and', ('exp_log_not', ('exp_comp', ('exp_bit_or', ('exp_bit_xor',
('exp_bit_and', ('exp_shift', ('exp_sum', ('exp_prod', ('exp_pdbn', ('exp_pow',
('exp_inv_elems', ('exp_base', ('VARIABLE', 'x'))))))))))))))), ('assign_op',
('EQUALS', '=')), ('expression', ('exp_log_and', ('exp_log_not', ('exp_comp',
('exp_bit_or', ('exp_bit_xor', ('exp_bit_and', ('exp_shift', ('exp_sum',
('exp_prod', ('exp_pdbn', ('exp_pow', ('exp_inv_elems', ('exp_base', ('NATURAL',
'1')))))), ('PLUS', '+'), ('exp_prod', ('exp_pdbn', ('exp_pow',
('exp_inv_elems', ('exp_base', ('NATURAL', '2')))))))))))))))), ('NEWLINE',
'\n'))), ('statement', ('stat_semicol', ('semicol_base', ('expression',
('exp_log_and', ('exp_log_not', ('exp_comp', ('exp_bit_or', ('exp_bit_xor',
('exp_bit_and', ('exp_shift', ('exp_sum', ('exp_prod', ('exp_pdbn', ('exp_pow',
('exp_inv_elems', ('exp_base', ('VARIABLE', 'print')), ('L_PAREN', '('),
('expression', ('exp_log_and', ('exp_log_not', ('exp_comp', ('exp_bit_or',
('exp_bit_xor', ('exp_bit_and', ('exp_shift', ('exp_sum', ('exp_prod',
('exp_pdbn', ('exp_pow', ('exp_inv_elems', ('exp_base', ('VARIABLE',
'x'))))))))))))))), ('R_PAREN', ')'))))))))))))))), ('NEWLINE', '\n'))))
                                
The abstract syntax tree layer is created by parsing the source code with a parser. For more info click here.
intermediate code
(def print (func (e) (list "__PRINT__" e)))

(def  (noeval <>))

"=============================================================================="

(def x (+ 1 2))

(print x)
                                
The intermediate code layer is formed from the abstract syntax tree layer. It is a program in a different programming language with less syntax. For more info click here.
assembly code The screen goes here. The assembly code layer is created by transforming the intermediate code.
machine code The screen goes here. The machine code layer is created by transforming the assembly code.