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 = 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
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
Simple Types Ordinal Types –Enumerated types –BOOLEAN type –CHAR type –CARDINAL type –INTEGER type –Subrange types REAL Type POINTER 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
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;
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
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
CHAR Type Possible values: all characters of a system specific alphabet. In many systems : extended ASCII –128 standardized characters 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
Printable ASCII Character Set 047/063?079O095_111o
ASCII extensions for pc 207 ¤ 223 _ 239 ´ 255
ASCII special characters 128DELDelete
CARDINAL Type Possible values: implementation dependant –With 16 bit representations : 0 <= C < 2 16 = –With 32 bit representations : 0 <= C < 2 32 ~ 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
INTEGER Type Possible values: implementation dependant –With 16 bit representations : <= I < 2 15 = –With 32 bit representations : <= I < 2 31 ~ 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 = ??
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
Internal representation of Integers
INTEGER & CARDINAL Overflow With 16 bit representations: Cardinal : = !!! Integer : = !!!
Score: With thanks to Ariane for the score
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
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 *)
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);
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) *)
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" *)
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: –Largest value : –Relative error: < Double precision (64 bit) "LONGREAL" –Smallest value: –Largest value : –Relative error: <
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);
REAL Type Example CONST BEFperEURO = ; VAR EUROValue : REAL; BEFValue : CARDINAL; RealBef : REAL;... RealBEF := EUROValue * BEFperEURO; BEFValue := VAL(CARDINAL, RealBEF+0.5)
Expression Syntax SimpleExpression Relational OperatorSimpleExpression Term Additive Operator + -
Term Syntax Factor Multiplicative Operator
Factor Syntax
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