CSC362 2004S, Class 32: Translating Expressions Admin: * Questions on the project? * Q: When the program is parsed, types are assigned to variables, so why can't I tell the type when I look at a variable? * A: The parser doesn't figure anything out except the parse tree * Q: When *our type checker* traverses the declarations section, it stores the types in the symbol table; How do I tell the type of a variable? * A: Look in the symbol table. * A: Look at Devin.java, Eryn.java, and Collaborate.java * Q: What about constants? * A: When you see a constant, store a type of CONSTANT in the symbol table; when type checking an assignment, make sure the lhs is not a constant * Q: Do we have to deal with nested procedures? * A: Yes. Should be easy: call newScope() when you enter a procedure, call endScope() when you exit it * Q: Do we have to support all the builtin procedures and functions? * A: No * Q: Which ones do we have to support? * A: Single-parameter read, single parameter write * Q: Please, can't we support all of them? Please? And what if they take arrays as parameters? * A: Extra credit, but document what you're doing. * Q: What's wrong with my code? * A: See me after class * Q: What should we do about coercion from integer to real * A: return new Node(PascalNonterminals.I2R, oldNode); * Q: But PascalNontermianls.I2R doesn't exist * A: Email me. * Q: How would you typecheck expressions and procedure calls? * 1. Assignment statements. * Typecheck the rhs. Typechecking should assign a "type" attribute to the root of the rhs * Typecheck the lhs. Typechecking should assign a "type" attribute to the root of the lhs * If the lhs and the rhs have the same type, you're set * If the lhs has type real and the rhs has type integer, insert a coercion node * 2. The simplest expressions * Variable: Look it up in the symbol table * Constant: Should be obvious * 3. Array reference (var[ind]) * Typecheck the variable * Typecheck the index * The variable should have type ARRAY[TYPE0] of TYPE1 * Verify that the type of the index matches TYPE0 * Give the array reference TYPE1 (n.setAttribute("type", TYPE1)); * 4. Unop Expression a * Typecheck the subexpression * If the unop is + or -, make sure the subexpression has type int or real, set the type to the same type * If the unop is "not", make sure the subexpression has type boolean, set the type to the same type * 5. Expression Binop Expression * Typecheck the subexpressions * Do the natural thing * 6. function(Exp0, Exp1, ... Expn) * Look up the type of the function (from T0 ... Tm to ResultType) * make sure m and n match * Typecheck all the subexpressions * For each i, make sure that Exp0 has type T0 (or can be coerced) * Set the type to ResultType Q: Do we have to coerce integers to reals? A: Yes. Q: Always? A: No. Only when the semantics of teh program require it. Q: Do we have to verify that labels are not repeated in case statements? A: Only if you belong to a group which includes someone whose nickname is Choed. Q: Do we have to coerce things other than integers to reals? A: No Q: It's hot, can we go now? A: Yes Overview: * Background * Translating assignments * Translate basic expressions * Variables * Constants * Translate compound expressions * Arrays