COMP313A Programming Languages - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

COMP313A Programming Languages

Description:

enum descriptor{rel, abs}; safe_address an_address; if (an_address == rel) ... (Note we are just using the syntax of C as an example. ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 20
Provided by: margaretj4
Category:

less

Transcript and Presenter's Notes

Title: COMP313A Programming Languages


1
COMP313A Programming Languages
  • Data Types (2)

2
Overview
  • Type Constructors
  • Type Equivalence
  • Type Checking
  • Type Conversion

3
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

4
Constructors Mapping (arrays)
  • C/C

typedef int little_people_num3 little_people_n
um gollum_city 0, 0, 0 gollum_city3
5 typedef int matrix1020
5
Constructors Mapping (arrays)
  • Pascal
  • ADA

Type intarray array2..5 of integer
little_people (dwarf, elf, goblin)
little_people_num arraylittle_people of
integer intmatrix array1..10,
1..20 of integer
xarray(INTEGER range 2.6) of INTEGER
(0,2,0,5,-33)
6
Constructors Union
  • Cartesian products conjunction of fields
  • Union disjunction of fields
  • Discriminated or undiscriminated

7
Constructors Unions
  • Undiscriminated
  • C/C

typedef union short int offset long
unsigned int absolute address

8
Constructors Union
  • Discriminated

typedef struct address location
descriptor kind
safe_address
enum descriptorrel, abs safe_address
an_address if (an_address rel)
an_address.location.offset 255
9
  • Pascal Variant Record

type address_range 0..maxint
address_type (absolute, offset)
safe_address record
case kind address_type of
absolute
(abs_addraddress_range)
offset (off_addr integer)
end
10
Constructors Pointer and Recursive Types
  • Recursion used to define aggregates whose size
    can grow arbitrarily
  • Recursive datatype T is defined as a structure
    that can contain components of type T
  • Implemented via pointers

11
Constructors Pointer and Recursive Types
  • Linked list of integers

C/C
Ada
typedef struct int val
int_list next int_list
int-list head
type int_list_node type int_list_ref is access
node int_list_node type int_list_node is
record val integer next
int_list_ref end head int_list_ref
12
Constructors Pointer and Recursive Types
  • Lacks .

typedef struct int val
int_list next int_list
int-list head
13
Constructors Pointer and Recursive Types
  • Think of the the different possibilities for an
    int_list as
  • emptylist U int U (int X int) U (int X int X
    int)

14
Constructors Pointer and Recursive Types
  • Some languages (Pascal, Ada) require pointers to
    be typed
  • PL/1 treated pointers as untyped data
    objectsWhat is the significance of this for a
    type checker?
  • C pointers are typed but C allows arithmetic
    operations on them unlike Pascal and Ada

15
Type Equivalence
  • When are two types the same
  • Structural equivalence
  • Declaration equivalence
  • Name equivalence

16
Structural Equivalence
  • Two types are the same if they have the same
    structure
  • i.e. they are constructed in exactly the same way
    using the same type constructors from the same
    simple types

17
Structural Equivalence
typedef int A5 typedef A B typedef int
C5 typedef int D10
18
Structural Type Equivalence
(Note we are just using the syntax of C as an
example. C does NOT use structural equivalence
for structs
typedef int anarray10 typedef struct
anarray x int y struct1
typedef struct int x10
int y struct2
typedef int anarray10 typedef struct int
b anarray a struct4
typedef int anarray10 typedef struct
anarray a int b struct3
19
Structural Equivalence
  • Check representing types as trees
  • Check equivalence recursively on subtrees
  • Consider
  • Dynamic arrays

Type array1 array-1..9 of integer
array2 array0..10 of integer
Array (INTEGER range ltgt) of INTEGER
Write a Comment
User Comments (0)
About PowerShow.com