Sequence 3.2 – The binder

Pablo Oliveira & Samuel Tardieu

The binder

Checking that variables have been properly declared and linking their usages to their declaration is the role of the binder. This is part of the semantic analysis phase, which assigns meaning to the program.

Using variables in the AST

Representing scopes

This construct is also called a chain map, as mapping are chained and searched in order.

A simple example

The current scope stack is represented in comments (top of the stack is on the right side). Stack is shown as [], and mappings as {}. We represent declarations as their line numbers for simplicity.

/* [] */
let /* [{}] */
  var a := 1 /* [{a => L.3}] */
  var b := 2 /* [{a => L.3, b => L.4}] */
in
  let /* [{a => L.3, b => L.4}, {}] */
    var a := 10 /* [{a => L.3, b => L.4}, {a => L.7}] */
  in
/* If a is looked up, result will be L.7 */
  end /* [{a => L.3, b => L.4}]
end /* [] */

What about functions?

After the binder pass

Conclusion