. p.1/23 Today 1. Questions and discussion from lecture. 2. Type-checking Functions Arrays Records (maybe)
. p.2/23 Type-checking functions Aspects: Overview of checks 6, 7 Declaration header Declaration body Procedure call (extra!) How to do overloading
. p.3/23 Check 6 Correct number of arguments Compatible types for non-var Same type for VAR Addressable VAR Distinction between procedures and functions.
. p.4/23 Check 7 Illegal return Missing return (This could be hard...) PROCEDURE f(a : BOOLEAN) : INTEGER; BEGIN IF a THEN RETURN 1; ELSE RETURN 0; END; END f; Does this have a return?
. p.5/23 Illegal returns...but luckily you don t need to do that. Instead, just check for at least one return anywhere: PROCEDURE f(a : BOOLEAN) : INTEGER; BEGIN END f; IF FALSE THEN END; RETURN 1; Sadly, this has a return...
. p.6/23 Function header PROCEDURE f(x: INTEGER): REAL; (*... *) What needs to be checked for each part? What actions need to be performed? Procedure name Return type Parameters
. p.7/23 Header checks Duplicate procedure name? Duplicate parameter name? Valid types? Where can we do this more generally?
. p.8/23 Header actions Create a type (What is included?) Create a STO (What are its data and methods?) Add to the symbol table (How? Which? Checks?)
. p.9/23 Procedure types Return type (optional) Argument types What about VAR parameters? Argument names? Why or why not? How do we represent this? What about type equality tests?
. p.10/23 Procedure STOs Argument names? Local variables? Why keep STO and type separate?
. p.11/23 Function body PROCEDURE f(x: INTEGER): REAL; BEGIN END f; Parts: VAR z: REAL; CONST c = 3; z := c * x; RETURN z; Variable declarations. Body. Nota bene: main is like a function.
. p.12/23 Variable declarations Same as parameters? Add to symbol table (How? Which?)
. p.13/23 Function body Symbol table organization? Symbol lookup procedure? Globals, locals, and shadowing Recursive procedures Return statement: Check type and valuelessness. Check for missing return in functions.
. p.14/23 Function call PROCEDURE f(x: INTEGER): REAL; (*... *) VAR x : REAL; BEGIN x := f(5); END. Parameter passing Return value Gramma perversions
. p.15/23 Return value In Oberon, statements are not expressions = procedures are not functions. C int f(int); a = f(3); // expression f(3); Oberon // statement PROCEDUREˆ f(x: INTEGER): INTEGER; a := f(3); (* expression *) f(3); (* does not compute *)
. p.16/23 Argument passing: non-var Arguments must be assignment-compatible with parameters. PROCEDURE f(x: REAL); BEGIN (*... *) END; VAR x : INTEGER; VAR y : REAL; f(y); (* 1 *) f(x); (* 2 *) f(3); (* 3 *) f(3.5); (* 4 *)
. p.17/23 Argument passing: non-var Why assignment-compatible? Because there s an assignment! PROCEDURE f(x: REAL); BEGIN (* use x *) END; PROCEDURE f(init); BEGIN x := init; (* use x *) END;
. p.18/23 Argument passing: VAR Arguments must: have the same type (why not compatible type?) be valid designators (what s this?) PROCEDUREˆ f(var x: REAL); VAR r : REAL; VAR i : INTEGER; f(r); (* 1 *) f(i); (* 2 *) f(3.5); (* 3 *)
. p.19/23 Argument passing: VAR PROCEDURE swap(var x, y: REAL); VAR t : REAL; BEGIN t := x; x := y; y := t; END f; VAR r,s : REAL; VAR i,j : INTEGER; BEGIN swap(r,s); (* 1 *) swap(r,r); (* 2 *) swap(r,3.5); (* 3 *) swap(i,r); (* 4 *) END.
. p.20/23 VAR hints What happens (in memory) for float x = 5? seti %i0, 5 itof %f0, %i0 st x, %f0 What is being assigned to? Relation to C pointers? Java references?
. p.21/23 Ew... No-argument calls: x := f; x := f(); Procedures vs. functions: f(1,2,3); x := f(1,2,3);
. p.22/23 Aside: overloading How are overloaded STOs different? Multiple types? Yes, and multiple operations! How does op.compatible(args) change? Check for any compatible operation. What order? (think about + s return type) Could we overload on return type?
. p.23/23 Review: operator types What are the types of the following operators? How would each be declared as a procedure/function? + (addition) := (assignment) [] (array subscript) [] := (assignment to array subscript)
. p.24/23 Array declarations They re easy! VAR A : ARRAY 10 OF REAL; What are the parts of this type? Element type. Length (why is this good or bad?)
. p.25/23 Array uses VAR A : ARRAY 10 OF REAL; A[1] := A[2]; What are the types? How is A[1] different from A[2]? Can A[i] be passed to a procedure? As VAR?
. p.26/23 Arrays vs. Functions VAR A : ARRAY 10 OF REAL; A[1] := A[2]; What would A s type be as a function? Can we assign to A[i]? What about to procedure results?