CSC362 2004S, Class 26: Type Checking Admin: * Train: * Comments and/or questions on yesterday's lab? * Leanred: There are different strategies one might employ * What do you expect to be different for Pascal? * Simplifying Pascal * Project, Phase 3: Type Checking (A week from Monday?) * Interested in the ACM Programming Contest? Contact Arjun Overview: * Review * Types of types /Observations on Type Checking/ * Learned: Where we type check: Where should we look, What kinds of types do we look at? * It seems inefficient to walk the tree multiple times (e.g,. for both type checking and for checking declarations). Can we be more efficient? Can we type check while parsing? * You can probably do many things on one walk through the tree (both type chck and check initialize-before-use) * The dependencies and parse method affect whether or not you can type check while parsing (most compilers do most of this stuff while parsing) * For your first comipler, it's better to separate stuff. * Helped think about kinds of errors you might encounter and strategies you might employ for recovering from those errors * Implicitly or explicitly converting values from one type to another * Choosing default values for variables * Reminded us about the implicit coercion from one type to another that happens so frequently in the languages we use * There's a lot of different stuff we have to type check; What are the things we'll need to do on the next stage of the compilers? * Strategy: You need to be careful about both operand types and return types for operations (and, therefore, for functions) * If you generalize your idea of "operation" (e.g., including assignment), almost everything is simply validating arument types. * Strategy for type checking: * Accumulate types in a hash table for easy reference * Recursively check the types at each place in the tree /Why will Pascal be more difficult than AL?/ * Need to deal with nested scopes. Typical strategy for dealing with nested scopes: Stack of hash tables * Need to deal with user defined types. (In Pascal, two types are equivalent and interchangable only when they have the same name.) * Many more kinds of types: Arrays, Records, Pointers, Functions * Oge * Pointer types type pq = pointer to q pz = pointer to z px = pointer to py py = pointer to px * Pointer strategies: * "All pointer types are equivalent" * "No pointer types are equivalent" * "A pointer to X and apointer to Y are equivalent if X is equiv to Y" * Functions not hard-codable * Many more structures to walk; lots of annoying repetitious coding /Pascal is too complicated/ No records, user-defined types, pointers, multidimensional arrays No variable parameters, array parameters No labels and gotos