DOC PREVIEW
LSU CSC 4101 - Programming Languages

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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

LSU CSC 4101 - Programming Languages

Download Programming Languages
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Programming Languages and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Programming Languages 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?