TC-5 Primitive Samples

Note

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

Note

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

Note

Use OVM to exercise your output.

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

$ echo $?
0
ovm arith.hir
$ ovm 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 ovm for a verbose execution.

ovm --trace arith.hir | ansi2txt
$ ovm --trace arith.hir | ansi2txt
Parsing arith.hir
eval seq
  eval sxp
    eval op add
      const 1
      eval op mul
        const 2
        const 3
      mul 2 3
    add 1 6
  sxp : 7
  eval sxp
    const 0
  sxp : 0
end seq
label end
Exit with code : 0

Temporaries state :
fp : 81916
sp : 81916
rv : 0
$ echo $?
0

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


Note

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