Download presentation
Presentation is loading. Please wait.
Published byBrett Dawson Modified over 9 years ago
1
Chapter 2.4 Modula 2 Simple Types
2
Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type = set of all values the variable can have The type determines the internal representation of data as well as the operations that can be performed on the data
3
Types in Modula 2 Simple Types: values can’t be decomposed –Ordinal Types: bijection with natural numbers –Reals: approx. representation for real values –Pointers: addresses in data memory Structured Types: Values have # components –Arrays: all components have same type –Records: components can have # types –Sets: small sets of ordinal values –Procedures: entire subprograms
4
Simple Types Ordinal Types –Enumerated types –BOOLEAN type –CHAR type –CARDINAL type –INTEGER type –Subrange types REAL Type POINTER Type
5
Enumerated Types Possible values: User defined TYPE Day = (Monday,Tuesday,Wednesday, Thursday, Friday, Saturday, Sunday) Operators : Only relational (=, #, >, =, <=) –Ordering by enumeration Day = (Monday,Tuesday,Wednesday, Thursday, Friday, Saturday, Sunday); Monday < Sunday Day = (Sunday, Monday,Tuesday,Wednesday, Thursday, Friday, Saturday); Monday > Sunday
6
Enumerated Types Main benefit : Clarity TYPE Sex = (Male, Female); VAR SexA, SexB : Sex;... IF SexA = Male THEN SexB := Female ELSE SexB := Male END (* IF *) Replaces manual encoding (* Male encoded by 0, Female by 1 *) VAR SexA, SexB : CARDINAL;... SexB := 1 - SexA;
7
BOOLEAN Type Possible values: TRUE, FALSE Operators : NOT, AND, OR X AND Y X = TRUE X = FALSE TRUE Y = TRUEY = FALSE FALSE X OR Y X = TRUE X = FALSE TRUE Y = TRUEY = FALSE FALSETRUE NOT XFALSE X = TRUEX = FALSE TRUE
8
Relational Expressions a = bTRUE when a equals b a # bTRUE when a different from b a > b TRUE when a greater than b a < bTRUE when a less than b a >= bTRUE when a greater or equal b a <= bTRUE when a less or equal b a IN bTRUE when value of a belongs to the set b have a BOOLEAN value
9
CHAR Type Possible values: all characters of a system specific alphabet. In many systems : extended ASCII –128 standardized characters + 128 additional characters –Various extensions for allowing non-anglosaxon characters In new Microsoft & Java systems : UNICODE –65536 different characters, adequate for all languages in the world. ASCII is a subset of UNICODE. Operators : Only the relational operators –Order can be different in different alphabets. Relative position of space, letters and digits VAN ZOERSEL Van Zoersel VANANTWERPENvan Gorp
10
Printable ASCII Character Set 047/063?079O095_111o
11
ASCII extensions for pc 207 ¤ 223 _ 239 ´ 255
12
ASCII special characters 128DELDelete
13
CARDINAL Type Possible values: implementation dependant –With 16 bit representations : 0 <= C < 2 16 = 65536 –With 32 bit representations : 0 <= C < 2 32 ~ 4.10 9 Operators : relational + + addition -subtraction *multiplication DIVinteger quotient MODinteger remainder Examples : 5 DIV 2 = 2 5 DIV 7 = 0 5 DIV 1 = 5 5 MOD 2 = 1 5 MOD 7 = 5 5 MOD 1 = 0
14
INTEGER Type Possible values: implementation dependant –With 16 bit representations : -2 15 <= I < 2 15 = 32768 –With 32 bit representations : -2 31 <= I < 2 31 ~ 2.10 9 Operators : relational + + addition -subtraction *multiplication DIVinteger quotient MODinteger remainder Examples : 5 DIV 2 = 2 -5 DIV 2 = -2 5 DIV -2 = -2 5 MOD 2 = 1 -5 MOD 2 = ?? 5 MOD -2 = ??
15
Internal representation of Integers Conventions: n = number of bits in representation M = value to be represented M' = value of representation Two's complement definition: M' = (M + 2 n ) MOD 2 n Range: - 2 n-1 <= I < 2 n-1
16
Internal representation of Integers 0000 0101 0011 0010 0001 1000 11000100 0110 1001 1101 0111 1111 1110 1010 1011 0 +1 +2 +6 +5 +4 +3 -8 -7 -6 +7 -3 -4 -5 -2
17
INTEGER & CARDINAL Overflow With 16 bit representations: Cardinal : 60 000 + 10 000 = 4 464 !!! Integer :30 000 + 10 000 = - 7 232 !!! 0000 0101 0011 0010 0001 1000 11000100 0110 1001 1101 0111 1111 1110 1010 1011 0 +1 +2 +6 +5 +4 +3 -8 -7 -6 +7 -3 -4 -5 -2
18
Score: -32276 With thanks to Ariane for the score
19
Subrange Types For all ordinal types subrange types can be defined TYPE Day = (Mon,Tue,Wed,Thu,Fri,Sat,Sun); WeekDay = [Mon..Fri]; DayOfMonth = [1..31]; Month = [1..12]; Why Subranges ? –Makes out-of-range detection possible –Allows optimized memory allocation by compiler
20
Functions and Operators for ordinal types The ORD function returns the internal representation of any ordinal variable as a cardinal value. TYPE Day = (Mon,Tue,Wed,Thu,Fri,Sat,Sun); VAR X : Day; Ch : CHAR;... X := Wed; (* ORD(X) = 2 *) Ch := "A"; (* ORD(Ch) = 65 *)
21
Functions and Operators for ordinal types The CHR function returns the character represented internally by a given cardinal value. VAR Ch : CHAR; n : [0..9];... n := 3; (* Conversion of a cardinal value between 0 and 9 into the corresponding character *) Ch := CHR(ORD("0")+n);
22
Functions and Operators for ordinal types The VAL function returns the value in a specified type represented internally by a given cardinal value. TYPE Day = (Mon,Tue,Wed,Thu,Fri,Sat,Sun); VAR Ch : CHAR; Today : Day;... Today := VAL(Day,2); (* Wednesday *) Ch := VAL(CHAR,65); (* "A" *) (* VAL(CHAR,n) = CHR(n) *)
23
Functions and Operators for ordinal types The INC and DEC procedures can be used to increment or decrement ordinal variables. TYPE Day = (Mon,Tue,Wed,Thu,Fri,Sat,Sun); VAR Ch : CHAR; Today : Day;... Today:=Wed ; INC(Today); (* Today = Thu *) Ch := "A"; INC(ch,32); (* ch = "a" *)
24
REAL Type Approximate representation for real numbers Possible values and accuracy: –implementation dependant –majority of computer systems : IEEE754. Single precision (32 bit) "REAL" –Smallest value: 1.18 10 -38 –Largest value : 3.39 10 +38 –Relative error: < 3.0 10 -8 Double precision (64 bit) "LONGREAL" –Smallest value: 1.18 10 -308 –Largest value : 3.39 10 +308 –Relative error: < 1.1 10 -16
25
REAL Type Operators and type conversions Arithmetic operators : +, -, *, / Relational operators : =, #, >, >=, <, <= Expressions : No mixing of types Type conversions: –From REAL to INTEGER : Truncation ! IntVar := VAL(INTEGER,RealVal); IntVar := TRUNC(RealVal); –From INTEGER to REAL : RealVal := VAL(REAL,IntVal); RealVal := FLOAT(IntVal);
26
REAL Type Example CONST BEFperEURO = 40.3399; VAR EUROValue : REAL; BEFValue : CARDINAL; RealBef : REAL;... RealBEF := EUROValue * BEFperEURO; BEFValue := VAL(CARDINAL, RealBEF+0.5)
27
Expression Syntax SimpleExpression Relational OperatorSimpleExpression Term Additive Operator + -
28
Term Syntax Factor Multiplicative Operator
29
Factor Syntax
30
Expression Evaluation Order of evaluation: 1Factor 2Term 3Simple Expression 4Expression Parentheses can modify order Evaluation is done from left to right, in a lazy fashion
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.