TC-D Samples

String comparisons can be translated to an equivalent AST using function calls, before the translation to HIR.

string-equality.tig
"foo" = "bar"
tc --desugar-string-cmp --desugar -A string-equality.tig
$ tc --desugar-string-cmp --desugar -A string-equality.tig
/* == Abstract Syntax Tree. == */

primitive print(string_0 : string)
primitive print_err(string_1 : string)
primitive print_int(int_2 : int)
primitive flush()
primitive getchar() : string
primitive ord(string_3 : string) : int
primitive chr(code_4 : int) : string
primitive size(string_5 : string) : int
primitive streq(s1_6 : string, s2_7 : string) : int
primitive strcmp(s1_8 : string, s2_9 : string) : int
primitive substring(string_10 : string, start_11 : int, length_12 : int) : string
primitive concat(fst_13 : string, snd_14 : string) : string
primitive not(boolean_15 : int) : int
primitive exit(status_16 : int)
function _main() =
  (
    streq("foo", "bar");
    ()
  )
$ echo $?
0
string-less.tig
"foo" < "bar"
tc --desugar-string-cmp --desugar -A string-less.tig
$ tc --desugar-string-cmp --desugar -A string-less.tig
/* == Abstract Syntax Tree. == */

primitive print(string_0 : string)
primitive print_err(string_1 : string)
primitive print_int(int_2 : int)
primitive flush()
primitive getchar() : string
primitive ord(string_3 : string) : int
primitive chr(code_4 : int) : string
primitive size(string_5 : string) : int
primitive streq(s1_6 : string, s2_7 : string) : int
primitive strcmp(s1_8 : string, s2_9 : string) : int
primitive substring(string_10 : string, start_11 : int, length_12 : int) : string
primitive concat(fst_13 : string, snd_14 : string) : string
primitive not(boolean_15 : int) : int
primitive exit(status_16 : int)
function _main() =
  (
    strcmp("foo", "bar") < 0;
    ()
  )
$ echo $?
0

for loops can be seen as sugared while loops, and be transformed as such.

simple-for-loop.tig
for i := 0 to 10 do print_int(i)
tc --desugar-for --desugar -A simple-for-loop.tig
$ tc --desugar-for --desugar -A simple-for-loop.tig
/* == Abstract Syntax Tree. == */

primitive print(string_0 : string)
primitive print_err(string_1 : string)
primitive print_int(int_2 : int)
primitive flush()
primitive getchar() : string
primitive ord(string_3 : string) : int
primitive chr(code_4 : int) : string
primitive size(string_5 : string) : int
primitive streq(s1_6 : string, s2_7 : string) : int
primitive strcmp(s1_8 : string, s2_9 : string) : int
primitive substring(string_10 : string, start_11 : int, length_12 : int) : string
primitive concat(fst_13 : string, snd_14 : string) : string
primitive not(boolean_15 : int) : int
primitive exit(status_16 : int)
function _main() =
  (
    let
      var _lo := 0
      var _hi := 10
      var i_17 := _lo
    in
      if _lo <= _hi
        then while 1 do
            (
              print_int(i_17);
              if i_17 = _hi
                then break
                else ();
              i_17 := i_17 + 1
            )
        else ()
    end;
    ()
  )
$ echo $?
0