This is the first set of HpcExercises that will teach you the structure of the abstract syntax of the TigerLanguage, the use of the StrategoCompiler? and writing a simple transformation on Tiger programs. The result of the final exercise is the first component to add to the TigerCompiler: TigerDesugar.
Here is an example Tiger program that will be referred to in the following exercises:
let var a:= 0 function g(a: int) : int = a + 1 in if a <> 0 then g(2) else g(3) end
The next set of exercises concerns HpcTranslationToIR.
Notes
The basic technique to use in the specification exercises is that of writing rules of the form
Lab : lhs -> rhs
where 'lhs' is a term pattern that describes the term to transform and 'rhs' is a term pattern that describes the term to which it should be transformed. The label 'Lab' is the name of the rule and can be used in strategies to invoke the rule.
The rules you write should be applied to a program by traversing it to each node. Useful strategies are 'topdown(s)' that applies a strategy 's' to each node of a tree and 'try(s)' that tries to apply a strategy but succeeds also if the strategy did not succeed.
To make a strategy into the main strategy of a transformation component you have to read a term from file, apply the strategy and write it back to some other file. These actions can be programmed using the primitives 'ReadFromFile' and 'WriteToTextFile' (see the StrategoLibrary?), but that requires writing strategies to handle command-line options and such. Therefore, the StrategoLibrary? defines a couple of standard io wrappers. The simplest one 'stdio(s)' reads a term from stdin, applies the strategy and writes the result to stdout. The more sophisticated 'iowrap(s)' can also do that, but in addition handles a couple of command-line options. The most relevant are '-i infile' to name the input file and '-o outfile' to name the output file name.
When you import a module from a different directory use the '-I dir' option to sc to extend the include path. For example if you import the Tiger signature in the Right-Associative module (in directory tiger/assoc/) use
sc -i Right-Associative -I ../sig
The path to the StrategoLibrary? is added by default. (See also */Makefile.am)