Renaming & Typechecking
1. Renaming
An optional second phase of a compiler is renaming, designed to ensure the program is well-scoped. This involves distingushing between variables with the same name, and ensuring that each variable is declared before use.
A renamer needs to globally keep track of teh variables in the scope, and perform a counter on how many times a variable with the same name has been referenced.
2. Typechecking
Typechecking may be done in a bottom-up or top-down way:
- A bottom-up approach will return the type of child expressions and compare at every level to see if they match up properly.
- A top-down approach will push information about expected types down the tree, and check if the types match up at the leaves.
We want to "parse" not "validate" - i.e. gain information about the types instead of just checking if they match up. In general, we always want to gain as much information as we can about the program at each stage of compilation.