pl0compiler
文件大小: unknow
源码售价: 5 个金币 积分规则     积分充值
资源说明:A Compiler and Interpreter for Wirth's PL/0 Language
### A Compiler and Interpreter for Wirth's PL/0 Language


This Scala project provides an AST-based Interpreter and a TAM Bytecode Compiler for the PL/0 Programming Language by Niklaus Wirth.  

In a Nutshell, this implementation supports the following concepts:

* Lexical Scoping
* Nested Procedures
* Forward References for Procedure declarations
* Recursion 
* Declaration of Variables and Constants 
* If and While Statements
* Conditional and arithmetic operators 
* Output to Console

See http://en.wikipedia.org/wiki/PL/0 for more info on PL/0.

The Compiler generates bytecode for the Triangle Abstract Machine (TAM).  
The TAM Bytecode Interpreter and Disassembler can be obtained from this website:

http://www.dcs.gla.ac.uk/~daw/books/PLPJ/

The following is a demonstration of the interpretation and compilation of a PL/0 program that calculates the factorial of 6.

$> cat factorial.pl0
VAR f;

PROCEDURE fact;
  VAR x, result;
  
  PROCEDURE fact1;
    BEGIN
      IF x <= f THEN
        BEGIN
          result := result * x;
          x := x + 1;
          CALL fact1;
        END;
    END;
    
  BEGIN
    x := 1;
    result := 1;
    CALL fact1;
    ! result;
  END;

BEGIN
  f := 6;  
  CALL fact;
END.
#### Interpretation:
$> scala org.jetho.pl0compiler.PL0Compiler -i factorial.pl0 
720
#### Compiling to TAM bytecode:
$> scala org.jetho.pl0compiler.PL0Compiler factorial.pl0 fact.obj
#### Using the TAM Disassembler to display the generated bytecode:
$> java TAM.Disassembler fact.obj
********** TAM Disassembler (Sun Version 2.0) **********
0:  PUSH        1
1:  JUMP        28[CB]
2:  PUSH        2
3:  JUMP        18[CB]
4:  LOAD  (1)   3[L1]
5:  LOAD  (1)   0[SB]
6:  CALL        le      
7:  JUMPIF(0)   17[CB]
8:  LOAD  (1)   4[L1]
9:  LOAD  (1)   3[L1]
10:  CALL        mult    
11:  STORE (1)   4[L1]
12:  LOAD  (1)   3[L1]
13:  LOADL       1
14:  CALL        add     
15:  STORE (1)   3[L1]
16:  CALL  (L1)  4[CB]
17:  RETURN(0)   0
18:  LOADL       1
19:  STORE (1)   3[LB]
20:  LOADL       1
21:  STORE (1)   4[LB]
22:  CALL  (LB)  4[CB]
23:  LOAD  (1)   4[LB]
24:  CALL        putint  
25:  CALL        puteol  
26:  POP   (0)   2
27:  RETURN(0)   0
28:  LOADL       6
29:  STORE (1)   0[SB]
30:  CALL  (SB)  2[CB]
31:  POP   (0)   1
32:  HALT  
#### Running the program with the TAM Interpreter:
$> java TAM.Interpreter fact.obj 
********** TAM Interpreter (Java Version 2.0) **********
720

Program has halted normally.
$> 

本源码包内暂不包含可直接显示的源代码文件,请下载源码包。