Todays Topics 1. Procedures and Procedure Call 2. Array and Record - - PowerPoint PPT Presentation

today s topics
SMART_READER_LITE
LIVE PREVIEW

Todays Topics 1. Procedures and Procedure Call 2. Array and Record - - PowerPoint PPT Presentation

Todays 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;


slide-1
SLIDE 1

Today’s Topics

  • 1. Procedures and Procedure Call
  • 2. Array and Record Types
slide-2
SLIDE 2

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? *)

slide-3
SLIDE 3

Modelling Call from Top to Bottom

Where do we start? What do we do?

slide-4
SLIDE 4

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) { 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

delegate to ProcType delegate to ProcType

slide-5
SLIDE 5

Arrays and Records

% Oberon A : ARRAY 256 OF INTEGER; B, C : ARRAY 256 OF INTEGER;

Type equal? B, C? A, C? fran, joe? bill, joe?

  • ne, bill?

// C or C++ int A[256]; int B[256], C[256]; struct { char name[30]; int ssnum; } joe, fran; struct ssrec { char name[30]; int ssnum; } struct ssrec bill, dan; struct { char phonenumber[30]; int identifier; } one, two;

slide-6
SLIDE 6

“Complete” Oberon Type Rules

Same types (Oberon is “alias” or “name” equivalent, except for pointers) Two variables a and b with types Ta and Tb are of the same type if

  • 1. T
  • 1. Ta

a and T

and Tb

b are both denoted by the same type identifier, or

are both denoted by the same type identifier, or

  • 2. Ta is declared to equal Tb in a type declaration of the form

Ta = Tb (e.g., TYPE TEMPERATURE = REAL;), or

  • 3. a and b appear in the same identifier list in a variable,

record field, or formal parameter declaration and are not open arrays.

  • 4. Ta and Tb are POINTER TO Ta' and POINTER TO Tb‘

respectively and Ta' and Tb' are the same type. Equal types (note that equality is weaker than sameness) Two types Ta and Tb are equal if

  • 1. Ta and Tb are the same type, or
  • 2. Ta and Tb are open array types with equal element types, or
  • 3. Ta and Tb are procedure types whose formal parameter lists

match.

E.g., ARRAY OF INTEGER Only appear in param decls E.g., ARRAY OF INTEGER Only appear in param decls

  • Coding to the

spec suggests creating methods sameType & equalType

  • What classes

do they go in?

  • Coding to the

spec suggests creating methods sameType & equalType

  • What classes

do they go in?

slide-7
SLIDE 7

“Complete” Oberon Type Rules

Assignment compatible (v := e) An expression e of type Te is assignment compatible with a variable v of type Tv if one of the following conditions hold:

  • 1. Te and Tv are the same type;
  • 2. T
  • 2. Te

e and

and T Tv

v are

are numeric numeric types and types and T Tv

v includes

includes T Te

e;

;

  • 3. Tv is a pointer type and Te is NIL type;

Array compatible (array parameter passing) An actual parameter a of type Ta is array compatible with a formal parameter f of type Tf if:

  • 1. Tf and Ta are the same type, or
  • 2. Tf is an open array, Ta is any array, and their element types

are array compatible, or

  • 3. Tf is ARRAY OF CHAR and a is a string.
slide-8
SLIDE 8

Expression Compatibility (excerpt)

  • perator first operand second operand result type

= # BOOLEAN BOOLEAN BOOLEAN NIL, pointer type NIL, pointer type BOOLEAN to T0 to T1

where T0 and T1 are the same type