11Programming LanguagesTevfik KoşarLecture - XVIMarch 16th, 20062Roadmap• Type Inference• Records (Structures)• Variant Records (Unions)23Type InferenceSuppose the following operation:type Atype = 0..20Btype =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. integer4Records (Structures)• In C:struct element{char name[2];int atomic_number;double atromic_weight;_Bool metallic;}• In Pascal:type element = recordname: two_chars;atomic_number: integeratomic_weight : real;metallic: Boolean;end;35Records (Structures)type element = recordname: two_chars;atomic_number: integeratomic_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 6Records (Structures)type element = packed recordname: two_chars;atomic_number: integeratomic_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 bytes47Records (Structures)• An alternative way would be rearranging record’s fields•Some compilers do this automatically• Now element record consumes 16 bytes (only 1 byte wasted)type element = recordname: two_chars;metallic: Boolean;atomic_number: integeratomic_weight : real;end;8Variant Records (Unions)• Provide two or more alternative fields, only one is valid at a given time.type element = recordname: two_chars;atomic_number: integeratomic_weight : real;metallic: Boolean;case naturally_occuring: Boolean oftrue: (source: string_ptr;prevalence: real;)false: (lifetime: real;)end;59Variant Records (Unions)10Variant Records (Unions)type tag = (is_int, is_real, is_bool);var test: recordcase which: tag ofis_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.0611Variant Records (Unions)type tag = (is_int, is_real, is_bool);var test: recordcase which: tag ofis_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!12Variant Records (Unions)type tag = (is_int, is_real, is_bool);var test: recordcase which: tag ofis_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!713Variant Records (Unions)type tag = (is_int, is_real, is_bool);var test: recordcase tag ofis_int : (i: integer);is_real: (r:real);is_bool: (b:Boolean);end;----X test.which : = is_real; not requiredtest.r := 3.0;writeln(test.i);Not an error, but the output will be junk!14Variant Records (Unions)• Variant records with tags: discriminated unons• Variant records without tags: nondiscriminated unions815Variant Records
View Full Document