TC-4 Code to Write

What is to be done:

lib/misc/singleton.hh
src/type/builtin-types.*

Implement the Singletons type::String, type::Int, and type::Void. Use templates to factor the code between the three singleton classes, see singleton.


src/ast/typable.*
src/ast/type-constructor.*
src/ast/*
ast::Typable
ast::TypeConstructor

Because many AST nodes will be annotated with their type, the feature is factored by these two classes. See ast::Typable, and ast::TypeConstructor, for details.

ast::Exp
ast::Dec
ast::Ty

These are typable.

ast::FunctionDec
ast::TypeDec
ast::Ty

These build types.


src/type/type.*
src/type/array.*
src/type/class.*
src/type/function.*
src/type/method.*
src/type/named.*
src/type/nil.*
src/type/record.*

The remaining classes are incomplete.

Pay extra attention to type::operator==(const Type& a, const Type& b) and type::Type::compatible_with.

Warning

Types are considered compatible under specific rules. Here is a summary of the main types considered to have type compatibility.

Types Compatibility (in sense of strict equality, partial compatibility, etc.)

Types

array

class

function

method

nil

record

array

Same.

class

Same or
Inheritance.
Assign or
Compare.

function

Result
& formals.

method

Result
& formals.

nil

Assign or
Compare.
Assign or
Compare.

record

Assign or
Compare.

Same.


src/type/default-visitor.hxx
type::GenVisitor
type::GenDefaultVisitor

type::Types are visitable. You must implement the default visitor class template, which walks through the tree of types doing nothing. It’s used as a base class for the type visitors.

src/type/type-checker.*

Of course this is the most tricky part. We hope there are enough comments in there so that you understand what is to be done. Please, post your questions and help us improve it.

It is also the type::TypeChecker’s job to set the record_type in the type::Nil class. record_type is holding some information about the type::Record type associated to the type::Nil type. We choose to handle the record_type only when no error occurred in the type-checking process.

src/type/pretty-printer.*

In order to output nice error messages, the types need to be printed. You must implement a visitor that prints the types, similar to ast::PrettyPrinter.

src/ast/escapable.* & src/escapes/*

The implementation of TC-E, Computing the Escaping Variables, suggested at TC-3, Bindings, becomes a core assignment at TC-4, Type Checking.


src/object/type-checker.* & src/object/renamer.* (Optional)

object::TypeChecker and object::Renamer inherit from type::TypeChecker, respectively bind::Renamer so as to factor common parts.

src/astclone/cloner.* (Optional)

The astclone::Cloner class is used to duplicate an Abstract Syntax Tree. It will be useful for desugaring some constructs and handling extensions.

src/desugar/desugar-visitor.* (Optional)

The desugar::DesugarVisitor class is used to desugar some syntactic structures (e.g. for loops, string comparisons, etc.). It inherits from astclone::Cloner so as to factor common parts. See TC-D, Removing the syntactic sugar from the Abstract Syntax Tree.

src/ast/dumper-dot.* (Optional)

Continue the implementation of the ast::DumperDot by dumping the types. You will need to implement the dump_type function and call it in the relevant nodes.