TC-5 Primitive Samples

This example is probably the simplest Tiger program.

0.tig
0
tc --hir-display 0.tig
$ tc --hir-display 0.tig
/* == High Level Intermediate representation. == */
# Routine: _main
label main
# Prologue
# Body
seq
  sxp
    const 0
  sxp
    const 0
seq end
# Epilogue
label end
$ echo $?
0

You should then probably try to make more difficult programs with literals only. Arithmetics is one of the easiest tasks.

arith.tig
1 + 2 * 3
tc -H arith.tig
$ tc -H arith.tig
/* == High Level Intermediate representation. == */
# Routine: _main
label main
# Prologue
# Body
seq
  sxp
    binop add
      const 1
      binop mul
        const 2
        const 3
  sxp
    const 0
seq end
# Epilogue
label end
$ echo $?
0

Use HAVM to exercise your output.

tc -H arith.tig > arith.hir
$ tc -H arith.tig > arith.hir

$ echo $?
0
havm arith.hir
$ havm arith.hir

$ echo $?
0

Unfortunately, without actually printing something, you won’t see the final result, which means you need to implement function calls. Fortunately, you can ask havm for a verbose execution.

havm --trace arith.hir
$ havm --trace arith.hir
checkingLow
plaining
unparsing
checking
evaling
 call ( name main ) []
9.6-9.13:  const 1
11.8-11.15:  const 2
12.8-12.15:  const 3
10.6-12.15:  binop mul 2 3
8.4-12.15:  binop add 1 6
7.2-12.15:  sxp 7
14.4-14.11:  const 0
13.2-14.11:  sxp 0
 end call ( name main ) [] = 0
$ echo $?
0

If you look carefully, you will find an sxp 7 in there…

Then you are encouraged to implement control structures.

if-101.tig
if 101 then 102 else 103
tc -H if-101.tig
$ tc -H if-101.tig
/* == High Level Intermediate representation. == */
# Routine: _main
label main
# Prologue
# Body
seq
  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
  sxp
    const 0
seq end
# Epilogue
label end
$ echo $?
0

And even more difficult control structure uses.

while-101.tig
while 101
  do (if 102 then break)
tc -H while-101.tig
$ tc -H while-101.tig
/* == High Level Intermediate representation. == */
# Routine: _main
label main
# Prologue
# Body
seq
  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
  sxp
    const 0
seq end
# Epilogue
label end
$ echo $?
0

Beware that HAVM has some known bugs with its handling of break.