Today’s Topics 1. Procedures and Procedure Call 2. Array and Record Types
Procedure and Function Calls • How to type check? - Example-driven, modelling, coding to the spec. • Example: PROCEDURE foo (x : INTEGER; y : REAL) : BOOLEAN; Type is: func(params(integer,real), returns(boolean)) Use? Type checking args & result type in calls: b := foo(1, 2.0) (* legal? *)
Modelling Call from Top to Bottom Where do we start? What do we do?
Modelling Call from Top to Bottom This is the corresponding rule: Factor ::= Designator:procSTO '(' ArgList:argList ')' {: RESULT = ProcCall.action(procSTO, arglist); :} VarSTO elaborate(ExprSTO result, ProcSTO procSTO, List argList) { delegate to ProcType delegate to ProcType VarSTO result = new VarSTO(); if (procSTO.isCompatible(argList)) result.setType(procSTO.resultType(argList)); // args don't influence restype else … // error – but how detect which part failed? return result; What else? What else? } Comment on separation of type and scope Comment on separation of type and scope
Arrays and Records // C or C++ % Oberon int A[256]; A : ARRAY 256 OF INTEGER; int B[256], C[256]; B, C : ARRAY 256 OF INTEGER; struct { char name[30]; int ssnum; } joe, fran; Type equal? struct ssrec { char name[30]; B, C? int ssnum; } A, C? struct ssrec bill, dan; fran, joe? struct { bill, joe? char phonenumber[30]; int identifier; one, bill? } one, two;
“Complete” Oberon Type Rules Same types (Oberon is “alias” or “name” equivalent, except for pointers) Two variables a and b with types T a and T b are of the same type if 1. T a and T b are both denoted by the same type identifier, or 1. T a and T b are both denoted by the same type identifier, or 2. T a is declared to equal T b in a type declaration of the form T a = T b (e.g., TYPE TEMPERATURE = REAL; ), or • Coding to the • Coding to the 3. a and b appear in the same identifier list in a variable, spec suggests spec suggests creating record field, or formal parameter declaration and are creating not open arrays. methods methods sameType sameType 4. T a and T b are POINTER TO T a' and POINTER TO T b‘ & equalType & equalType respectively and T a' and T b' are the same type . • What classes • What classes Equal types (note that equality is weaker than sameness) do they go in? do they go in? Two types T a and T b are equal if E.g., ARRAY OF INTEGER E.g., ARRAY OF INTEGER 1. T a and T b are the same type , or Only appear in param decls Only appear in param decls 2. T a and T b are open array types with equal element types, or 3. T a and T b are procedure types whose formal parameter lists match.
“Complete” Oberon Type Rules Assignment compatible ( v := e ) An expression e of type T e is assignment compatible with a variable v of type T v if one of the following conditions hold: 1. T e and T v are the same type ; 2. T e and T T v are numeric numeric types and types and T T v includes T T e ; 2. T e and v are v includes e ; 3. T v is a pointer type and T e is NIL type; Array compatible (array parameter passing) An actual parameter a of type T a is array compatible with a formal parameter f of type T f if: 1. T f and T a are the same type , or 2. T f is an open array, T a is any array, and their element types are array compatible , or 3. T f is ARRAY OF CHAR and a is a string.
Expression Compatibility (excerpt) operator first operand second operand result type = # BOOLEAN BOOLEAN BOOLEAN NIL, pointer type NIL, pointer type BOOLEAN to T 0 to T 1 where T 0 and T 1 are the same type
Recommend
More recommend