The TL/1 Compiler

The TL/1 compiler is JBurg's primary regression test, and also serves as an example of a very simple compiler.

The TL/1 language is extremely limited. A TL/1 compilation unit is composed of a single main() procedure. Available statements are if-then-else, while, and assignment. Variables are not declared, they come into scope at their first assignment and stay in scope thereafter. Arithmetic expressions, string concatenation, and comparison expressions are available.

Running TL/1

To compile a TL/1 program, invoke jburg.test.tl1.TL1Compiler.main(), passing the name of your TL/1 program as the only argument. The TL/1 compiler is not good about removing path information, so it's best to execute the compiler in the working directory that contains the file.

For example, to compile mytl1.tl1 and save mytl1.class on disk:

java jburg.test.tl1.TL1Compiler mytl1.tl1
Running the TL/1 program

From the command line, invoke the TL/1 class as the main class, e.g.

java mytl1

From a Java program, construct a new instance of your TL/1 class and invoke its TL1main() method, e.g.

new mytl1().TL1main()

TL/1 Components


Processing Overview

TL1Compiler.main() gets its arguments from the command line. Only one argument is accepted: the name of the TL/1 program to compile. By convention, TL/1 programs use the extension ".tl1". main() passes the name of the TL/1 file to TL1Compiler.compile(inputFile).

TL1Compiler.compile() forms a class name by stripping the ".tl1" extension from the input name; the TL/1 compiler is not good about removing path information, so it's best to execute the compiler in the working directory that contains the file.

TL1Compiler.compile() creates a new BCEL ClassGen object and a new TL1Reducer, and sets the reducer's properties so it can reference the compiler's ClassGen, the class name, etc. TL1Compiler.compile() then invokes the BURM's burm() method, which labels and reduces the tree of TL1INode objects, generating BCEL InstructionList objects as it goes.

TL1Compiler.compile() then adds a simple main() and a no-args constructor to the generated code, and writes the .class file out on disk.


Front End Components

JavaCC Grammar

The parser recognizes TL/1 syntax and constructs TL1INode objects to represent the program's Abstract Syntax Tree.

When the parser recognizes a list of objects, such as a list of statements, it constructs CONS_CELL TL1INodes to represent the list as a "tree." The CONS_CELL list is "backwards" in that new elements are added to the head instead of the tail; it makes the emitter slightly more readable. If having the CONS_CELL lists in the "correct" order were important, the emitter code that processes CONS_CELL lists would need to be inverted.

TL1INode

The TL1INode class has four property access methods of interest.