TC-3 Samples

Binding is relating a name use to its definition.

Warning

You must strictly follow the format specified below when displaying binding addresses. Addresses must be displayed right after declarations or usages identifiers:

identifier /* 0x55bbe7bdc230 */

Display addresses only when needed. Look closely at the samples.

me.tig
let
  var me := 0
in
  me
end
tc -XbBA me.tig
$ tc -XbBA me.tig
/* == Abstract Syntax Tree. == */

function _main /* 0x55fda6b3f0c0 */() =
  (
    let
      var me /* 0x55fda6b3e770 */ := 0
    in
      me /* 0x55fda6b3e770 */
    end;
    ()
  )
$ echo $?
0

This is harder when there are several occurrences of the same name. Note that primitive types are accepted, but have no pre-declaration, contrary to primitive functions.

meme.tig
let
  var me := 0
  function id(me : int) : int = me
in
  print_int(me)
end
tc -bBA meme.tig
$ tc -bBA meme.tig
/* == Abstract Syntax Tree. == */

primitive print /* 0x55652b37efd0 */(string /* 0x55652b37e310 */ : string /* 0 */)
primitive print_err /* 0x55652b37eb80 */(string /* 0x55652b37e1a0 */ : string /* 0 */)
primitive print_int /* 0x55652b37b350 */(int /* 0x55652b380a60 */ : int /* 0 */)
primitive flush /* 0x55652b378310 */()
primitive getchar /* 0x55652b381480 */() : string /* 0 */
primitive ord /* 0x55652b380e90 */(string /* 0x55652b3807f0 */ : string /* 0 */) : int /* 0 */
primitive chr /* 0x55652b392510 */(code /* 0x55652b37f980 */ : int /* 0 */) : string /* 0 */
primitive size /* 0x55652b3919a0 */(string /* 0x55652b3b2b80 */ : string /* 0 */) : int /* 0 */
primitive streq /* 0x55652b3944c0 */(s1 /* 0x55652b3823e0 */ : string /* 0 */, s2 /* 0x55652b382540 */ : string /* 0 */) : int /* 0 */
primitive strcmp /* 0x55652b394870 */(s1 /* 0x55652b394610 */ : string /* 0 */, s2 /* 0x55652b394780 */ : string /* 0 */) : int /* 0 */
primitive substring /* 0x55652b394db0 */(string /* 0x55652b3949c0 */ : string /* 0 */, start /* 0x55652b394b80 */ : int /* 0 */, length /* 0x55652b394cc0 */ : int /* 0 */) : string /* 0 */
primitive concat /* 0x55652b392b20 */(fst /* 0x55652b394f50 */ : string /* 0 */, snd /* 0x55652b392a30 */ : string /* 0 */) : string /* 0 */
primitive not /* 0x55652b392e30 */(boolean /* 0x55652b392cc0 */ : int /* 0 */) : int /* 0 */
primitive exit /* 0x55652b393050 */(status /* 0x55652b392fd0 */ : int /* 0 */)
function _main /* 0x55652b393380 */() =
  (
    let
      var me /* 0x55652b37e770 */ := 0
      function id /* 0x55652b37f0c0 */(me /* 0x55652b37e450 */ : int /* 0 */) : int /* 0 */ =
        me /* 0x55652b37e450 */
    in
      print_int /* 0x55652b37b350 */(me /* 0x55652b37e770 */)
    end;
    ()
  )
$ echo $?
0

TC-3 is in charge of incorrect uses of the names, such as undefined names,

nome.tig
me
tc -bBA nome.tig
$ tc -bBA nome.tig
nome.tig:1.1-2: undeclared variable: me
$ echo $?
4

or redefined names.

tome.tig
let
  type me = {}
  type me = {}
  function twice(a: int, a: int) : int = a + a
in
  me {} = me {}
end
tc -bBA tome.tig
$ tc -bBA tome.tig
tome.tig:3.3-14: redefinition: me
tome.tig:2.3-14: first definition
tome.tig:4.25-31: redefinition: a
tome.tig:4.18-23: first definition
$ echo $?
4

In addition to binding names, --bindings-compute is also in charge of binding the break to their corresponding loop construct.

breaks-in-embedded-loops.tig
let var x := 0 in
  while 1 do
  (
    for i := 0 to 10 do
    (
      x := x + i;
      if x >= 42 then
        break
    );
    if x >= 51 then
      break
  )
end
tc -XbBA breaks-in-embedded-loops.tig
$ tc -XbBA breaks-in-embedded-loops.tig
/* == Abstract Syntax Tree. == */

function _main /* 0x563f1b2c20c0 */() =
  (
    let
      var x /* 0x563f1b2c3a60 */ := 0
    in
      while /* 0x563f1b2c2060 */ 1 do
        (
          for /* 0x563f1b2c54c0 */ i /* 0x563f1b2c1310 */ := 0 to 10 do
            (
              x /* 0x563f1b2c3a60 */ := x /* 0x563f1b2c3a60 */ + i /* 0x563f1b2c1310 */;
              if x /* 0x563f1b2c3a60 */ >= 42
                then break /* 0x563f1b2c54c0 */
                else ()
            );
          if x /* 0x563f1b2c3a60 */ >= 51
            then break /* 0x563f1b2c2060 */
            else ()
        )
    end;
    ()
  )
$ echo $?
0
break.tig
break
tc -b break.tig
$ tc -b break.tig
break.tig:1.1-5: `break' outside any loop
$ echo $?
4

Embedded loops show that there is scoping for breaks. Beware that there are places, apparently inside loops, where breaks make no sense.

Although it is a matter of definitions and uses of names, record members are not bound here, because it is easier to implement during type checking. Likewise, duplicate fields are to be reported during type checking.

box.tig
let
  type     box = { value : int }
  type     dup = { value : int, value : string }
  var      box := box { value = 51 }
in
  box.head
end
tc -XbBA box.tig
$ tc -XbBA box.tig
/* == Abstract Syntax Tree. == */

function _main /* 0x55b5dfa800c0 */() =
  (
    let
      type box /* 0x55b5dfa81f70 */ = { value : int /* 0 */ }
      type dup /* 0x55b5dfa81110 */ = {
        value : int /* 0 */,
        value : string /* 0 */
      }
      var box /* 0x55b5dfa7f770 */ := box /* 0x55b5dfa81f70 */ { value = 51 }
    in
      box /* 0x55b5dfa7f770 */.head
    end;
    ()
  )
$ echo $?
0
tc -T box.tig
$ tc -T box.tig
box.tig:3.33-46: identifier multiply defined: value
box.tig:6.3-10: invalid field: head
$ echo $?
5

But apart from these field-specific checks delayed at TC-4, TC-3 should report other name-related errors. In particular, a field with an invalid type name is a binding error (related to the field’s type, not the field itself) to be reported at TC-3.

unknown-field-type.tig
let
  type rec = { a : unknown }
in
  rec { a = 42 }
end
tc -XbBA unknown-field-type.tig
$ tc -XbBA unknown-field-type.tig
unknown-field-type.tig:2.20-26: undeclared type: unknown
$ echo $?
4

Likewise, class members (both attributes and methods) are not to be bound at TC-3, but at the type-checking stage (see TC-4, Type Checking). Therefore, no bindings are to be displayed in regards to object at TC-3.

bad-member-bindings.tig
let
  type C = class {}
  var c := new C
in
  c.missing_method();
  c.missing_attribute
end
tc -X --object-bindings-compute -BA bad-member-bindings.tig
$ tc -X --object-bindings-compute -BA bad-member-bindings.tig
/* == Abstract Syntax Tree. == */

function _main /* 0x5652cf8a5b80 */() =
  (
    let
      type C /* 0x5652cf8bb720 */ = 
      class extends Object /* 0 */
      {
      }
      var c /* 0x5652cf8a5770 */ := new C /* 0x5652cf8bb720 */
    in
      (
        c /* 0x5652cf8a5770 */.missing_method();
        c /* 0x5652cf8a5770 */.missing_attribute
      )
    end;
    ()
  )
$ echo $?
0
tc --object-types-compute bad-member-bindings.tig
$ tc --object-types-compute bad-member-bindings.tig
bad-member-bindings.tig:5.3-20: unknown method: missing_method
bad-member-bindings.tig:6.3-21: unknown attribute: missing_attribute
$ echo $?
5

Concerning the super class type, the compiler should just check that this type exists in the environment at TC-3, Bindings. Other checks are left to TC-4 (see TC-4 Samples).

missing-super-class.tig
let
  /* Super class doesn't exist.  */
  class Z extends Ghost {}
in
end
tc -X --object-bindings-compute -BA missing-super-class.tig
$ tc -X --object-bindings-compute -BA missing-super-class.tig
missing-super-class.tig:3.19-23: undeclared type: Ghost
$ echo $?
4

The self identifier is not a reserved keyword, it can be used to name variables, functions and types, even inside a class. When used inside a method, self refers to the current instance of the object if no variable named self is present in the current scope.

self.tig
let
  class self
  {
    var self : self := nil
    method self() : self = self.self
  }

  var self : self := new self

  class foo
  {
    var self := self
    method self(self : self) : self = self.self
  }
in
end
tc -X --object-bindings-compute -BA self.tig
$ tc -X --object-bindings-compute -BA self.tig
/* == Abstract Syntax Tree. == */

function _main /* 0x55df414a7310 */() =
  (
    let
      type self /* 0x55df414afe00 */ = 
      class extends Object /* 0 */
      {
        var self : self /* 0x55df414afe00 */ := nil
        method self() : self /* 0x55df414afe00 */ =
          self /* 0 */.self
      }
      var self /* 0x55df414ad450 */ : self /* 0x55df414afe00 */ := new self /* 0x55df414afe00 */
      type foo /* 0x55df414c12d0 */ = 
      class extends Object /* 0 */
      {
        var self := self /* 0x55df414ad450 */
        method self(self /* 0x55df414ad1a0 */ : self /* 0x55df414afe00 */) : self /* 0x55df414afe00 */ =
          self /* 0x55df414ad1a0 */.self
      }
    in
      ()
    end;
    ()
  )
$ echo $?
0