PZ06C - Polymorphism Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.3 PZ06C.

Slides:



Advertisements
Similar presentations
PZ04B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04B - Arrays and records Programming Language Design.
Advertisements

Programming Languages and Paradigms
PZ10B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10B - Garbage collection Programming Language Design.
PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09B - Parameter transmission Programming Language Design.
PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09B - Parameter transmission Programming Language Design.
Elementary Data Types Prof. Alamdeep Singh. Scalar Data Types Scalar data types represent a single object, i.e. only one value can be derived. In general,
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Chapter Four Data Types Pratt 2 Data Objects A run-time grouping of one or more pieces of data in a virtual machine a container for data it can be –system.
PZ05A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ05A - Abstract data types Programming Language Design.
PZ10A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10A - Heap storage Programming Language Design and.
PZ07B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ07B - Basic statements Programming Language Design.
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
Elementary Data Types Scalar Data Types Numerical Data Types Other
PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04A - Scalar and composite data Programming Language.
PZ05B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ05B - Type equality Programming Language Design and.
CS 2104 Prog. Lang. Concepts Subprograms
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
Basic Semantics Associating meaning with language entities.
PZ03EX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03EX - ML Programming Language Design and Implementation.
PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06C - Polymorphism Programming Language Design and.
Polymorphism Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.3.
Heap storage & Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001.
Variables reference, coding, visibility. Rules for making names  permitted character set  maximum length, significant length  case sensitivity  special.
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
1 Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
1 Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Abstract data types Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
PZ11A Programming Language design and Implementation -4th Edition
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Complex Data Types One very important measure of the “goodness” of a PL is the capability of its data types to model the problem space variables Design.
PZ05A - Abstract data types
CSC 533: Programming Languages Spring 2015
PZ09A - Activation records
PZ04A - Scalar and composite data
Abstract data types Programming Language Design and Implementation
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Languages and Compilers (SProg og Oversættere)
Polymorphism Programming Language Design and Implementation
Heap storage & Garbage collection
Abstract data types Programming Language Design and Implementation
Course Overview PART I: overview material PART II: inside a compiler
Runtime Environments What is in the memory?.
Parameter transmission
Polymorphism Programming Language Design and Implementation
PZ09B - Parameter transmission
Abstract data types Programming Language Design and Implementation
Type equality Programming Language Design and Implementation
Parameter transmission
Type equality Programming Language Design and Implementation
Heap storage & Garbage collection
Type equality Programming Language Design and Implementation
Parameter transmission
Polymorphism Programming Language Design and Implementation
Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CSC 533: Programming Languages Spring 2018
Parameter transmission
Type equality Programming Language Design and Implementation
CSC 533: Programming Languages Spring 2019
Types and Related Issues
Type equality Programming Language Design and Implementation
PZ07B - Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
PZ05B - Type equality Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 6.4 PZ05B.
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Parameter transmission
Polymorphism Programming Language Design and Implementation
Presentation transcript:

PZ06C - Polymorphism Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.3 PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000

Polymorphism The use of parameters to subprograms is one of the oldest characteristics of programming languages But they have an l-value. That is, they are a data object that requires storage. Polymorphism means a single operator or subprogram name can to refer to any of a number of function definitions depending on the data types of the arguments and results. Similar to overloading It is generally applied to functions where a type is one of the arguments. PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000

Polymorphism in arrays Note: Implementation of arrays differs in most languages: Pascal - arrays are explicit and bounds are part of type. Not polymorphic. type matrix10 = array[1..10] of integer; var x: matrix10; Ada - polymorphic array bounds type matrix is array (integer range <>) of integer; y: matrix(1..30); C arrays don't really exist - shorthand for pointer variables. int C[10]; C[5] means *C+5 PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000

Create polymorphism Can often build parameterized objects for true polymorphism, e.g., stack of objects: Y: stack(int, 10)  up to 10 ints Z: stack(float, 20)  up to 20 reals Can do this in ML and Ada for any data types that don't involve calculations (i.e., only for operations that use pointer manipulation (stacking and unstacking) Why? Macros can be used to simulate this for languages without polymorphism, e.g. In Pascal: sum(int,A,B)  can be implemented as a macro with 4 sequences depending upon types of arguments: trunc(A)+B A+trunc(B) A+B trunc(A)+trunc(B) PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000

Implementation For statically typed languages (ML, C++), polymorphism is easy: Keep track of function argument types in symbol table of compiler. For dynamically typed languages: Two forms of arguments can be passed to a polymorphic function: 1. An immediate descriptor - when the value to a function is smaller than the size of the fixed field. For example, passing a Boolean, character, or small integer to a function uses less space than the fixed-size object permits. The actual value is placed in the argument location, and the extra bits in the field are used to tell the function what the type actually is. 2. A boxed descriptor occurs in all other cases. PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000

Boxed descriptors The argument field will contain a type indicator stating the argument is boxed. The rest of the field will be the address of the actual object (which will be elsewhere, such as in heap storage). At this address, the complete type information will be given, such as giving the entire structure of the composite data object. PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000

Example of dynamic polymorphism Assume argument descriptor field is 5 bytes. Byte 1 is a data type indicator and Bytes 2 to 5 are the data. The following arguments can be passed to a polymorphic function: 1. 32-bit Integer data. Byte 1=0 signifying integer, bytes 2 to 5 are the 32-bit integer value. 2. 8-bit character data. Byte 1=1 signifying character, byte 2= actual character argument, bytes 3 to 5 unused. 3. 1-bit Boolean data. Byte 1=2 and byte 2= 0 or 1. 4. Complex record structure. Byte 1=3 and bytes 2 to 5 are pointer to structure. The r-value at this pointer address contains more information about the actual argument. PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, 2000