Wirth's PL/0

Implementation of programming language PL/0

A reference PL/0 compiler, published by Niklaus Wirth in 1975-76 in “Algorithms + Data Structures = Programs” and “Compilerbau” books and updated in the following editions of the latter book. Written in Pascal.

This compiler validates the program, converts it to Assembly-like language and executes it. Note that everything — the original program, the Assembly-like code and the actual execution results (i.e., the values of all variables changed during its execution) — is printed.

Examples:

Factorial:

Example for versions Wirth's PL/0 (1976)

The language makes it impossible to print any characters, so the program just outputs the pairs number-factorial without any separators. Running this program results in the following output:

    0 var n, f;  
    1 begin  
    2    n := 0;  
    4    f := 1;  
    6    while n # 16 do  
   10    begin  
   10       n := n + 1;  
   14       f := f * n;  
   18    end;  
   19 end.  
    0  jmp  1    1  
    1  int  1    5  
    2  lit  1    0  
    3  sto  1    3  
    4  lit  1    1  
    5  sto  1    4  
    6  lod  1    3  
    7  lit  1   16  
    8  opr  1    9  
    9  jpc  1   19  
   10  lod  1    3  
   11  lit  1    1  
   12  opr  1    2  
   13  sto  1    3  
   14  lod  1    4  
   15  lod  1    3  
   16  opr  1    4  
   17  sto  1    4  
   18  jmp  1    6  
   19  opr  1    0  
 start pl/0
0
1
1
1
2
2
3
6
4
24
5
120
6
720
7
5040
8
40320
9
362880
10
3628800
11
39916800
12
479001600
13
1932053504
14
1278945280
15
2004310016
16
2004189184
 end pl/0
var n, f;
begin
   n := 0;
   f := 1;
   while n # 16 do
   begin
      n := n + 1;
      f := f * n;
   end;
end.