| Index | Software | Tm | About Tm | News | Examples | Documentation | Downloading | Contact |
The following pages shows the core code of a number of small programs. The first program parses lists of assignment statements such as
a = 1 + 2 * 3; b = 1 - 2; c = - 1 + 2; d = -(1+2); e = c + 3 + 4; f = c + (3 + 4);
and generates a Tm textual representation. This demonstrates the use of Tm for the representation of parse trees.
The second program reads the Tm textual representation generated by the first program, evaluates constant expressions, and generates a Tm textual representation again. This demonstrates manipulations on Tm parse trees.
The third program substitutes known variable definitions in subsequent expressions. This demonstrates the use of Tm-generated tree-walkers to implement optimizers.
Obviously, in normal circumstances it would make more sense to do all this in one program, but that way there would be little left to demonstrate.
In miniature these programs demonstrate typical applications of Tm-generated code: internal representation of parse trees, manipulations on these trees, and exchange of datastructures with other programs.
The assignment statements that these programs work on are represented with the following data structures:
|| Representation of a stream of variable assignments.
command = { lhs:tmstring, rhs:expr };
expr ~=
ExprPlus: { a:expr, b:expr } |
ExprTimes: { a:expr, b:expr } |
ExprNegate: { x:expr } |
ExprConst: { n:int } |
ExprSymbol: { s:tmstring }
;
Note that this describes the structure of only a single assignment statement. Since Tm is implicitly able to generate code for lists of arbitrary types, we don't have to specify anything special for lists of assignments.
| C templates | C parser | C optimizer | C treewalker | Macro language |
The full code of these example programs can be downloaded from here:
tmdemo-1.0.tar.gz, gzip compressed tar file
Last modified Sunday 23 December 2007 14:14:01 UT by Kees van Reeuwijk.