Project 2: A type checker

In this project, you will write a type checker for Jay in Python, completing the typechecker we saw in class.

1. Setup

In a directory for this class and project, copy and untar the files for this project

cp ~tvandrun/Public/cs365/proj2.tar .
tar xvf proj2.tar

This includes a variety of files in jay and samples folders. The only one you will be working on is jay/typeCheckerVisitor.py.

2. Writing the type checker

The file typeCheckerVisitor.py contains the code for the type-checker, which you need to finish. We will have gone over this code in class.

Quite a bit of this is written for you. Here are the methods which you need to complete:

1. visit_Declaration(). Finish this method by adding code in the loop over all variables in the list of identifiers. You need to do three things: check if each identifer is new (not already declared); record its type in the type map; add it to the set of uninitialized variables. (If it has already been declared, then don't do the other two parts but instead handle it as an error.)

2. visit_Assignment(). Finish the method to verify assignment statements. Be sure to catch type errors of an undeclared target and a type mismatch between the expression and the target. Also, this should update the uninitialized set.

3. visit_Loop(). Using the method for conditionals (immediately above) as a model, write the method to verify loops. Be sure to catch the type error of a non-boolean guard. Be sure to update and return the uninitialized variable set.

4. visit_BinaryExpr() (the hard one). Remember that when visiting expressions, you must do two things: catch and record errors, and infer a type. Thus not only does this potentially write an error message and increment self.errors; it also must write its result to self.currentType (possible ERR if no type could be inferred). Using the other visit methods (especially unary expressions) as examples, write this method. Be sure to catch the type error of mismatched operands. Important information: A BinaryExpr has an instance variable called op which is a triple containing the types of the two operators and the return types. You can see details in the file ast_values.py, specifically in the three pre-defined triples for that instance variable:

iii = ("int", "int", "int")
iib = ("int", "int", "boolean")
bbb = ("boolean", "boolean", "boolean")

If the type of a subexpression of some binary expression is "ERR", then you do not need to report that subexpression as a further error. That is, for an expression like (true + 7) * 12, your type checker needs to report only the type error of trying to add a boolean and an int, not the subsequent error of trying to multiply an ERR and an int.

3. Testing

I have provided to sample Jay programs (one that types correctly, one that has a bunch of errors) in the folder samples. You should also write testfiles of your own.

To run your program, type (for example)

python jay/typeCheck.py samples/BadTypes.jay

See the project 1 description for more details about running a Python program from the commandline.

4. Turn-in

Copy your typeCheckerVisitor.py to

/cslab.all/linux/class/cs365/(your id)/proj2

I will grade your project by running it against a collection of test files.

DUE: Friday, Feb 12, 5:00 pm. Note that this is the same as when project 1 is due.


Thomas VanDrunen
Last modified: Mon Feb 1 11:21:11 CST 2016