Skip to main content

Section C.23 O2: Type Checking Examples, Implementation of Type Checking

Synopsis.

  • Work through a couple of type checking examples to discuss how our typing rules work.

  • Discuss pros and cons of type systems (not in the lecture notes) by means of statically and dynamically typed languages. Statically typed language typically require more programming effort to get the types right. One can generally generate faster code from them, because types don't have to be checked at runtime. With dynamically-typed languages you typically get your code with less hassle because you don't need to care about making the code type. However, you might spend that time of fixing bugs that typing would have prevented. Dynamically-typed languages typically run slower because type checking is performed at runtime. This can be to some extent alleviated by modern just-in-time compilers.

  • Outline implementation of type checking in our small compiler.

  • For most language elements this is just bottom-up (recursive) tree traversal.

  • Interesting are variables occurrences and declarations.

  • Need appropriate implementation of type environment.

  • Use a stack of maps. Whenever entering a block, push new map with variable names to declaration AST nodes to it. Point to parent environment. When looking up a variable, traverse stack of type environments from top to bottom and look for variable in the respective map.

Sections Covered.

Section 6.6, Section 9.2