This example is probably the simplest Tiger program.
0
File 58: 0.tig
$ tc --hir-display 0.tig /* == High Level Intermediate representation. == */ # Routine: Main label Main # Prologue # Body sxp const 0 # Epilogue label end Example 59: tc --hir-display 0.tig
You should then probably try to make more difficult programs with literals only. Arithmetics is one of the easiest tasks.
1 + 2 * 3
File 60: arith.tig
$ tc -H arith.tig /* == High Level Intermediate representation. == */ # Routine: Main label Main # Prologue # Body sxp binop (+) const 1 binop (*) const 2 const 3 # Epilogue label end Example 61: tc -H arith.tig
You should use havm to exercise your output.
$ tc -H arith.tig >arith.hir Example 62: tc -H arith.tig >arith.hir
$ havm arith.hir Example 63: havm arith.hir
Unfortunately, without actually printing something, you won't see the final result, which means you need to implement calls. Fortunately, you can ask havm for a verbose execution:
$ havm --trace arith.hir error-->plaining error-->unparsing error-->checking error-->checkingLow error-->evaling error--> call ( name Main ) [] error-->8.8-8.15: const 1 error-->10.12-10.19: const 2 error-->11.12-11.19: const 3 error-->9.8-11.19: binop (*) 2 3 error-->7.4-11.19: binop (+) 1 6 error-->6.0-11.19: sxp 7 error--> end call ( name Main ) [] = 0 Example 64: havm --trace arith.hir
If you look carefully, you will find an sxp 7 in there...
Then you are encouraged to implement control structures.
if 101 then 102 else 103
File 65: if-101.tig
$ tc -H if-101.tig /* == High Level Intermediate representation. == */ # Routine: Main label Main # Prologue # Body seq cjump ne const 101 const 0 name l0 name l1 label l0 sxp const 102 jump name l2 label l1 sxp const 103 label l2 seq end # Epilogue label end Example 66: tc -H if-101.tig
And even more difficult control structure uses:
while 101
do (if 102 then break)
File 67: while-101.tig
$ tc -H while-101.tig /* == High Level Intermediate representation. == */ # Routine: Main label Main # Prologue # Body seq label l1 cjump ne const 101 const 0 name l2 name l0 label l2 seq cjump ne const 102 const 0 name l3 name l4 label l3 jump name l0 jump name l5 label l4 sxp const 0 label l5 seq end jump name l1 label l0 seq end # Epilogue label end Example 68: tc -H while-101.tig