Download presentation
Presentation is loading. Please wait.
Published byAbigayle Gardner Modified over 8 years ago
1
Data Types (1) 1 Programming Languages – Principles and Practice by Kenneth C Louden
2
Overview of topic Simple Types Type Constructors Type Equivalence Type Checking Type Conversion 2
3
What is a data type? A set of values versus A set of values + set of operations on those values 3
4
Why data types? Data abstraction ◦ Programming style ◦ Modifiability Type checking (semantic analysis) can be done at compile time Compiler uses type information to allocate space for variables Type conversion (coercion) can be done at compile time 4
5
Overview cont… Declarations ◦ Explicit type information Type declarations ◦ Give new names to types in type declararion Type checking ◦ Type Inference Rules for determining the types of constructs from available type information ◦ Type equivalence determines if two types are the same Type system ◦ Type construction methods + type inference rules + type equivalence algorithm 5
6
Simple Types Predefined types ◦ Integer, real, boolean, and char ◦ Predefined numeric types with specific precision Longint, double ◦ Different formats for reals Fixed, float 6
7
Simple Types … Enumerated types Pascal type fruit = (apple, orange, banana); C/C++ enum fruit { apple, orange, banana }; 7
8
Simple Types Subrange types Pascal type byte = 0..255; minors = 0..19; teens = 13..19; ADA subtype teens is INTEGER range 13..19; 8
9
Data Aggregrates and Type Constructors Aggregate (compound) objects and types are constructed from simple types Recursive – can also construct aggregrate objects and types from aggregrate types Predefined – records, arrays, strings…… 9
10
Constructors - Cartesian Product U X V = { (u, v) | u is in U and v is in V} Projection functions ◦ p 1 : U X V -> U and P 2 : U X V -> V Pascal family of languages - record 10 type reg_polygon = record no_of_edges: integer; edge_size: real end; var a_poly : reg_polygon; Cartesian product type INTEGER X REAL Projections a_poly.no_of_edges a_poly.edge_size
11
Constructors – Cartesian Product C/C++ 11 typedef struct { int no_of_edges; float edge_size; } reg_polygon ; reg_polygon a_poly = {3, 3.45} Projections a_poly.no_of_edges a_poly.edge_size
12
Constructors – Mapping (arrays) The array constructor defines mappings as data aggregates Mapping from an array index to the value stored in that position in the array The domain is the values of the index The range is the values stored in the array 12
13
Constructors Mapping (arrays) C/C++ 13 typedef int little_people_num[3]; little_people_num gollum_city = {0, 0, 0} gollum_city[3] = 5 typedef int matrix[10][20];
14
Constructors – Mapping (arrays)… Pascal ADA 14 Type intarray = array[2..5] of integer; little_people = (dwarf, elf, goblin); little_people_num = array[little_people] of integer; intmatrix = array[1..10, 1..20] of integer; x:array(INTEGER range 2.6) of INTEGER := (0,2,0,5,-33)
15
Constructors Union Cartesian products – conjunction of fields Union – disjunction of fields Discriminated or undiscriminated 15
16
Constructors Unions Undiscriminated C/C++ 16 typedef union { short int offset; long unsigned int absolute; } address;
17
Constructors Union… Discriminated 17 typedef struct { address location; descriptor kind; } safe_address; enum descriptor{rel, abs}; safe_address an_address; if (an_address == rel) an_address.location.offset = 255;
18
Pascal Variant Record 18 type address_range = 0..maxint; address_type = (absolute, offset); safe_address = record case kind: address_type of absolute: (abs_addr:address_range); offset: (off_addr: integer); end
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.