Introduction

Pylayers helps you learn all the layers in the compilation and execution of a Pythonic high level language program. It provides a simple working compiler and virtual computer, implemented in Python, for educational purposes.

Pylayers will introduce you to numerous computer science topics, all with working Python code. These include tokens, productions, grammars, abstract syntax trees, parsers, parser generators, intermediate code, intermediate code generators, assembly code, assemblers, computer processor registers, computer processor instruction sets, machine code and more. You can see the entire Pylayers code base here .

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

Example

Here are all the layers in the transformation of a Pythonic high level language program to machine code. The source code layer is typically the only layer the human sees. The machine code layer is the only layer the computer executes:

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.