chapter 2.4 modula 2 simple types. constants and variables constants –name –value the value of a...

30
Chapter 2.4 Modula 2 Simple Types

Upload: brett-dawson

Post on 18-Jan-2016

232 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

Chapter 2.4

Modula 2

Simple Types

Page 2: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

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 dataas well as the operations that can be performed on the data

Page 3: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

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

Page 4: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

Simple Types• Ordinal Types

– Enumerated types– BOOLEAN type– CHAR type– CARDINAL type– INTEGER type– Subrange types

• REAL Type

• POINTER Type

Page 5: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

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

Page 6: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

Enumerated Types

• Main benefit : ClarityTYPE 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;

Page 7: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

BOOLEAN Type

• Possible values: TRUE , FALSE• Operators : NOT, AND, OR

X AND YX = TRUEX = FALSE

TRUEY = TRUE Y = FALSE

FALSEFALSEFALSE

X OR YX = TRUEX = FALSE

TRUEY = TRUE Y = FALSE

FALSETRUETRUE

NOT X FALSEX = TRUE X = FALSE

TRUE

Page 8: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

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 >= b TRUE when a greater or equal b• a <= b TRUE when a less or equal b• a IN b TRUE when value of a belongs to the set b

have a BOOLEAN value

Page 9: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

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

VANANTWERPEN van Gorp

Page 10: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

Printable ASCII Character Set

032 048 0 064 @ 080 P 096 ` 112 p033 ! 049 1 065 A 081 Q 097 a 113 q034 “ 050 2 066 B 082 R 098 b 114 r035 # 051 3 067 C 083 S 099 c 115 s036 $ 052 4 068 D 084 T 100 d 116 t037 % 053 5 069 E 085 U 101 e 117 u038 & 054 6 070 F 086 V 102 f 118 v

039 ‘ 055 7 071 G 087 W 103 g 119 w040 ( 056 8 072 H 088 X 104 h 120 x041 ) 057 9 073 I 089 Y 105 i 121 y042 * 058 : 074 J 090 Z 106 j 122 z043 + 059 ; 075 K 091 [ 107 k 123 {044 , 060 < 076 L 092 \ 108 l 124 |045 - 061 = 077 M 093 ] 109 m 125 }046 . 062 > 078 N 094 ^ 110 n 126 ~

047 / 063 ? 079 O 095 _ 111 o

Page 11: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

ASCII extensions for pc

128 Ç 144 É 160 á 176 _ 192 + 208 ð 224 Ó 240 129 ü 145 æ 161 í 177 _ 193 - 209 Ð 225 ßß 241 ±130 é 146 Æ 162 ó 178 _ 194 - 210 Ê 226 Ô 242 _131 â 147 ô 163 ú 179 ¦ 195 + 211 Ë 227 Ò 243 ¾132 ä 148 ö 164 ñ 180 ¦ 196 - 212 È 228 õ 244 ¶133 à 149 ò 165 Ñ 181 Á 197 + 213 i 229 Õ 245 §134 å 150 û 166 ª 182 Â 198 ã 214 Í 230 µ 246 ÷135 ç 151 ù 167 º 183 À 199 Ã 215 Î 231 þ 247 ¸136 ê 152 ÿ 168 ¿ 184 © 200 + 216 Ï 232 Þ 248 °137 ë 153 Ö 169 ® 185 ¦ 201 + 217 + 233 Ú 249 ¨138 è 154 Ü 170 ¬ 186 ¦ 202 - 218 + 234 Û 250 ·139 ï 155 ø 171 ½ 187 + 203 - 219 _ 235 Ù 251 ¹140 î 156 £ 172 ¼ 188 + 204 ¦ 220 _ 236 ý 252 ³141 ì 157 Ø 173 ¡ 189 ¢ 205 - 221 ¦ 237 Ý 253 ²142 Ä 158 × 174 « 190 ¥ 206 + 222 Ì 238 ¯ 254 _143 Å 159 ƒ 175 » 191 + 207 ¤ 223 _ 239 ´ 255

Page 12: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

ASCII special characters

0 nul null character 16 DLE Data Link Escape1 SOH Start of Heading 17 DC1 D. Ctrl.1 (X-ON)2 STX Start of Text 18 DC2 D. Ctrl.2 (Tape on)3 ETX End of Text 19 DC3 D. Ctrl.3 (X-OFF)4 EOT End of Transmission 20 DC4 D. Ctrl.4 (Tape off)5 ENQ Enquiry 21 NAK Negative Acknow.6 ACK Acknowledge 22 SYN Synchronous Idle7 BEL Bell Attention Signal 23 ETB End Transm. Block8 BS Backspace 24 CAN Cancel9 HT Horizontal Tabulator 25 EM End of Medium10 LF Line Feed 26 SUB Substitute11 VT Vertical Tabulation 27 ESC Escape12 FF Form Feed 28 FS File Separator13 CR Carriage Return 29 GS Group Separator14 SO Shift Out Alternate 30 RS Record Separator15 SI Shift in Character Set 31 US Unit Separator

128 DEL Delete

Page 13: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

CARDINAL Type• Possible values: implementation dependant

– With 16 bit representations : 0 <= C < 216 = 65536

– With 32 bit representations : 0 <= C < 232 ~ 4.10 9

• Operators : relational ++ addition

- subtraction

* multiplication

DIV integer quotient

MOD integer 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

Page 14: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

INTEGER Type• Possible values: implementation dependant

– With 16 bit representations : -215 <= I < 215 = 32768

– With 32 bit representations : -231 <= I < 231 ~ 2.10 9

• Operators : relational ++ addition

- subtraction

* multiplication

DIV integer quotient

MOD integer remainder

• Examples :5 DIV 2 = 2 -5 DIV 2 = -2 5 DIV -2 = -2

5 MOD 2 = 1 -5 MOD 2 = ?? 5 MOD -2 = ??

Page 15: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

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 + 2n) MOD 2n

• Range:- 2 n-1 <= I < 2 n-1

Page 16: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

Internal representation of Integers

0000

0101

0011

0010

0001

1000

1100 0100

0110

1001

1101

0111

1111

1110

1010

1011

0+1

+2

+6

+5

+4

+3

-8

-7

-6

+7

-3

-4

-5

-1

-2

Page 17: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

INTEGER & CARDINAL Overflow

• With 16 bit representations:Cardinal : 60 000 + 10 000 = 4 464 !!!

Integer : 30 000 + 10 000 = - 7 232 !!!

0000

0101

0011

00100001

1000

1100 0100

0110

1001

1101

0111

11111110

1010

1011

0+1

+2

+6

+5

+4

+3

-8-7

-6

+7

-3

-4

-5

-1

-2

Page 18: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

Score: -32276

With thanks to Ariane for the score

Page 19: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

Subrange Types

• For all ordinal types subrange types can be definedTYPE

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

Page 20: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

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 *)

Page 21: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

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);

Page 22: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

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) *)

Page 23: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

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" *)

Page 24: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

REAL TypeApproximate 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

Page 25: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

REAL TypeOperators 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);

Page 26: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

REAL Type

Example CONST BEFperEURO = 40.3399;VAR EUROValue : REAL; BEFValue : CARDINAL; RealBef : REAL;.. .RealBEF := EUROValue * BEFperEURO;BEFValue := VAL(CARDINAL, RealBEF+0.5)

Page 27: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

Expression Syntax

SimpleExpression

Relational Operator SimpleExpression

SimpleExpression

Term

Additive Operator

+

-

Page 28: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

Term Syntax

Factor

Multiplicative Operator

Page 29: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

Factor Syntax

Page 30: Chapter 2.4 Modula 2 Simple Types. Constants and Variables Constants –Name –Value The value of a constant belongs to a type. Variables –Name –Type Type

Expression Evaluation

• Order of evaluation:1 Factor

2 Term

3 Simple Expression

4 Expression

• Parentheses can modify order

• Evaluation is done from left to right,

in a lazy fashion