Today in CS362: Type Checking Missing Ananta, Daren, Erik, Mark, Yaw Admin Lab tomorrow: Type checking in Pascal Questions on project, phase 2? Sorry for rough notes in the outline Pascal user manual and report. Put on "Compilers Shelf" Overview What is a type? What is type checking? Why type check? When should we type check? What kinds of things have types? Where should we type check? What are types, revisited? Type equivalence basics What is a type? A primitive data structure, containing only one element of information? NOT QUITE What kinds of things have elements/members? * Classes * Lists * Sets The data perspective: A type is a set of values The use perspective: A type is a set of restrictions on when values can be used If types are sets, we have standard ways of combining sets to make new types (0) List the elements (enumeration types, in some languages) (1) Union (2) Intersection (3) Pairwise product If A,B are sets, AxB is a set Elements of AxB are (a,b) s.t. a in A, b in B (4) Kleene Star empty set union A union AxA union AxAxA union AxAxAxA ... vector or list Why have types? * "So we don't screw up" - Make sure that we perform the right sort of operations on data * Organization: Clarifies code * Adds a level of abstraction + Abstraction is good * Allows us to convert values when appropriate Type checking: * Assign types to objects (explicitly and implicitly) * Verify that objects are used in a type-safe fashion When should you type check? * Compile time + Catches lots of errors early + Shows errors to developers + Probably less overhead + May have to make more explicit * Run time + Allows more freedom of input + "More elegant" + Frees programmers: You can't always know types in advance + Allows cool run-time type coercion In CSC362 we'll do compile-time type checking * Standard for Pascal * Good first example of traversing parse trees * Probably easier than generating code to do run-time type check. What kinds of things have types in Pascal programs? variables have types functions have types any terminal? or selected terminals (constants) What kinds of types can they have? built-in primitive types (integer, real, char) arrays, records strings (in some versions of Pascal) How do we determine types? Build a symbol table for variables, functions, procs Rely on the lexer to assign types to literals Write rules to compute the types of expressions Where should we check for valid types? Any time you try to apply an operation +,*,/,-: Make sure both parameters are numbers Procedure calls: parameters are appropriate types Assignment statements: "Return" statements Array indices case statements and for loops enumerated types When are two types equivalent? Example: E0 -> id '[' E1 ']' if (id.type != array) crashandburn("What makes you think you can index non arrays?") if (E1.type != the argument type of the array) crashandburn("You indexed an array with the wrong type") E0.type = check the result type of the array type arraytype : array[type0] of type1 type0 can be subrange type val1 ... val2 any enumerated type weekdays : (monday,tuesday,wednesday,thursday,friday); classesperday : array[weekdays] of integer; What's the value of day or x after each of these loops? for day := monday to friday do begin end; for x := 1 to 5 do begin end; in C for (x = 1; x <= 5; x++) { }