Programming Languages Tevfik Ko ar Lecture XVI March 16th 2006 1 Roadmap Type Inference Records Structures Variant Records Unions 2 1 Type Inference Suppose the following operation type Atype 0 20 Btype 10 20 var a Atype b Btype What will be the type of a b Possible values range from 10 to 40 So will the type 0 40 In usual cases the result of an arithmetic operation on a subrange has the subrange s base type eg integer 3 Records Structures In C struct element char name 2 int atomic number double atromic weight Bool metallic In Pascal type element record name two chars atomic number integer atomic weight real metallic Boolean end 4 2 Records Structures type element record name two chars atomic number integer atomic weight real metallic Boolean end Fields of a record are stored in adjacent locations in memory Compiler keeps track of the offset of each field within each record type The element record occupies 20 bytes of memory 5 bytes are wasted In an array of elements compiler will denote 20 bytes for each member 5 Records Structures type element packed record name two chars atomic number integer atomic weight real metallic Boolean end Space optimization by pushing fields together To access a nonaligned field compiler has to issue a multi instruction sequence retrieve multiple pieces and reassemble Now element record consumes only 15 bytes 6 3 Records Structures type element record name two chars metallic Boolean atomic number integer atomic weight real end An alternative way would be rearranging record s fields Some compilers do this automatically Now element record consumes 16 bytes only 1 byte wasted 7 Variant Records Unions Provide two or more alternative fields only one is valid at a given time type element record name two chars atomic number integer atomic weight real metallic Boolean case naturally occuring Boolean of true source string ptr prevalence real false lifetime real end 8 4 Variant Records Unions 9 Variant Records Unions type tag is int is real is bool var test record case which tag of is int i integer is real r real is bool b Boolean end test which is real test r 3 0 writeln test r Output 3 0 10 5 Variant Records Unions type tag is int is real is bool var test record case which tag of is int i integer is real r real is bool b Boolean end test which is real test r 3 0 writeln test i Dynamic semantic error 11 Variant Records Unions type tag is int is real is bool var test record case which tag of is int i integer is real r real is bool b Boolean end test which is real test r 3 0 test which is int writeln test i Not an error but the output will be junk 12 6 Variant Records Unions type tag is int is real is bool var test record case tag of is int i integer is real r real is bool b Boolean end X test which is real not required test r 3 0 writeln test i Not an error but the output will be junk 13 Variant Records Unions Variant records with tags discriminated unons Variant records without tags nondiscriminated unions 14 7 Variant Records Unions 15 8
View Full Document
Unlocking...