modula 2.1

66
Modula 2 Presented by: Mohsan Naqi Roll no MS-10-06 25/12/2021 1

Upload: mohsan-naqi

Post on 21-Apr-2015

65 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: modula 2.1

Modula 2

Presented by:

Mohsan Naqi

Roll no MS-10-06 11/04/2023 1

Page 2: modula 2.1

Introduction

• Modula-2– Procedural Programming Language– Block-structured– Design was based on Pascal and Modula

• Improvements over Pascal– Modules– Low-level features– Coroutines– Syntactic features

11/04/2023 2

Page 3: modula 2.1

Design and Development

• Modules– A tool for expressing the relations between

major parts of programs and provide support for abstract data types

• Low-Level Features– a small language, which can be

implemented on a wide variety of microcomputers• Contains facilities for high-level and low-level

programming

11/04/2023 3

Page 4: modula 2.1

Design and Development

• Coroutines– Used for embedded systems that require

concurrency without the overhead of large operating systems or large languages

• Syntactic Features– Have a small vocabulary and consists of

few sentence structures• Contains simple and consistent rules of syntax,

which makes it easy to learn

11/04/2023 4

Page 5: modula 2.1

Uses of Modula-2

• Suitable for learning programming– Large projects written and maintained in the fashion

of professional software engineers– real-time embedded systems

• Safety-critical areas– Traffic control systems

• It is small, expressive, easy to learn, and to read

11/04/2023 5

Page 6: modula 2.1

History• The history of Modula-2 is the history of its designer, Dr.

Niklaus Wirth.• Dr. Wirth was a member of a large committee whose

task was to design a successor to the language ALGOL-60 in 1960.

• Modula-2 is a computer programming language designed and developed between 1977 and 1980 by Niklaus Wirth at ETH Zurich as a revision of Pascal to serve as the sole programming language for the operating system and application software for the personal workstation Lilith

• The first Modula-2 compiler, written by K. van Le in 1977 • Modula-2 compiler was designed in 1979-80 by L.

Geissmann and Ch. Jacobi with the PDP-11 compiler .• 1981, Modula-2 was in daily use and quickly proved its

worth as a powerful, efficiently implemented language

611/04/2023

Page 7: modula 2.1

Current compilers• ACK Modula-2 for Minix (freeware)• ADW Modula-2 ADW Modula-2 for Windows, ISO

compliant, ISO/IEC 10514-1, ISO/IEC 10514-2 (OO extension), ISO/IEC 10514-3 (Generic extension) (freeware)

• Aglet Modula-2 for Amiga OS 4.0/PPC (freeware)• Cambridge Modula-2 for various micro-controllers and

embedded MINOS operating system (commercial + proprietary software)

• Canterbury Modula-2 generates Java source code• FST Fitted Software Tools Modula-2 for MS-DOS

(freeware)• Gardens Point Modula-2 for BSD, Linux, OS/2, Solaris

and .NET - ISO compliant (freeware)• GNU Modula-2 for GCC platforms, version 1.0 released

December 11, 2010; PIM2, PIM3, PIM4, and ISO compliant (free software, GPLed)

• M2Amiga for Amiga (free software)• M2M by N. Wirth and collaborators from ETH Zurich,

platform independent, generates M-code for virtual machine (freeware)

• The Karlsruhe Modula-2 Compiler MOCKA for various platforms, PIM compliant (commercial, freeware

711/04/2023

Page 8: modula 2.1

11/04/2023 8

Modula 2• Has keywords: words from the

language• Always in CAPITAL letters!

• DO, FOR, IF, WHILE, END, THEN, UNTIL, LOOP, ELSE, RECORD, ARRAY, OF, TYPE, CONST, IMPORT, FROM, SET, VAR, WITH, CASE, MODULE, REAL, INTEGER…

Page 9: modula 2.1

11/04/2023 9

Program structure

• Program is called a MODULEMODULE name;

FROM …. IMPORT …;CONST pi = 3.1459;TYPE euro = REAL;

BEGIN …. (* comment *)

END name.

Page 10: modula 2.1

11/04/2023 10

Syntax• Keywords completely in CAPITAL

letters• Spaces do not matter• White lines do not matter• Signal the end of a command with

semi colon ;

• End of program with a period .

Page 11: modula 2.1

11/04/2023 11

Variables

• Convenient way to:– Store values– Manipulate values

• Closely related to the variable concept used in mathematics!

Page 12: modula 2.1

11/04/2023 12

Variables

– Need to be declared:• Name• Type

VARX : CARDINAL;

– Change the value (assignment)

X := 451;

:=

:

Page 13: modula 2.1

11/04/2023 13

Variables

• Single types:– Characters (‘a’) : CHAR– Integers (-40) : INTEGER– Reals (2.7) : REAL– Positive number (3) :

CARDINAL– Logical (TRUE/FALSE) :

BOOLEAN

Page 14: modula 2.1

11/04/2023 14

Variables

MODULE demo;VAR

a : CHAR; b : INTEGER; c : REAL; d : CARDINAL; e : BOOLEAN:

BEGIN a := ‘z’; b := -345; c := 0.35E-3; d := 345; e := FALSE;

END demo.

:=

Page 15: modula 2.1

11/04/2023 15

Variables vs. Literals

• Variable can have several values (not at one time)

• Literals are the values themselves:– -3– 123– ‘z’– 4.342– TRUE

Page 16: modula 2.1

11/04/2023 16

Working with numbers

• INTEGER, CARDINAL and REAL• Operators:

– Sum using +– Substract using –– Multiply using *

• Divide using– DIV for CARDINAL and INTEGER– / for REAL

• Modulo using MOD

Page 17: modula 2.1

11/04/2023 17

Working with numbers

MODULE demo;VAR

a, b : INTEGER; d : REAL; f : CARDINAL;

BEGIN a := -3; b := a * 2; d := 5.0 / 3.0; f := 5 DIV 3;

END demo.

Page 18: modula 2.1

11/04/2023 18

Conversions

• Numbers can not be mixed!

• CARDINAL REAL INTEGER

• Conversion is needed– TRUNC(real) => CARDINAL– FLOAT(cardinal/integer) => REAL

Page 19: modula 2.1

11/04/2023 19

BOOLEAN algebra

• Built-in type: BOOLEAN

• 2 values: TRUE, FALSE

• OPERATORS:– Logical : AND, OR, NOT– Comparison : <, >, =, <=, >=, <>, #

Page 20: modula 2.1

11/04/2023 20

BOOLEAN algebra

MODULE demo;VAR

a, b : INTEGER; c, d : BOOLEAN;

BEGIN a := -3; b := 33; c := a > b; d := NOT c;

END demo.

Page 21: modula 2.1

11/04/2023 21

Characters

• Built-in type: CHAR

• Values: all the entries of the ASCII table

• Character literals have to be surrounded by single quotes :– ‘z’

Page 22: modula 2.1

11/04/2023 22

Constant

• Keyword CONST

• Usage identical to variable• Assignment is impossible

MODULE demo;CONST

pi = 3.14159; code = 9342;

=

Page 23: modula 2.1

11/04/2023 23

Constant Example

MODULE demo;CONST

Pi = 3.14159;VAR

x, r : REAL;BEGIN

r := 5.0;x := 2.0 * Pi * r;

END demo.

Page 24: modula 2.1

11/04/2023 24

Writing on screen

• Writing on the computer screen can be done using the following procedures– String : WrStr(x);– INTEGER: WrInt(x, 0);– REAL: WrReal(x, 7, 0);– CARDINAL: WrCard(x, 0);– BOOLEAN: WrBool(x);– CHAR: WrChar(x);

Page 25: modula 2.1

• MODULE IntVar; • FROM InOut IMPORT WriteLn, WriteString, WriteInt; • VAR Count : INTEGER; x,y : INTEGER; BEGIN x := 12; y := 13; Count := x + y; WriteString("The sum of them ="); WriteInt(Count,6); WriteLn; END IntVar.

2511/04/2023

Page 26: modula 2.1

11/04/2023 26

Control instruction

• Normal program execution– Top down– Line by line

• Statements that influence the execution of the program

• 3 groups:– Branches– Loops– Procedures

Page 27: modula 2.1

11/04/2023 27

IF

• Choose an alternative based on result of the boolean expression

IF boolean_express1 THENstatements…

ELIF boolean_express2 THENstatements…

ELSEstatements…

END;

Page 28: modula 2.1

11/04/2023 28

IF

MODULE demo;FROM InOut IMPORT WrStr;VAR

x : CARDINAL;BEGIN

x := 3;IF (x < 5) THEN

WrStr(" x is smaller than 5");END;

END demo.

Page 29: modula 2.1

11/04/2023 29

IFMODULE demo;FROM IO IMPORT WrStr;VAR

x : CARDINAL;BEGIN

x := 3;IF (x < 5) THEN

WrStr(" x is smaller than 5");ELSE

WrStr(" x is >= 5");END;

END demo.

Page 30: modula 2.1

11/04/2023 30

IFMODULE demo;FROM IO IMPORT WrStr;VAR

x : CARDINAL;BEGIN

x := 3;IF (x < 5) THEN

WrStr(" x is smaller than 5");ELIF (x > 5) THEN

WrStr(" x is bigger than 5");ELSE

WrStr("x equals 5");END;

END demo.

Page 31: modula 2.1

11/04/2023 31

CASE

• Choose one alternative using a label, no test!!

CASE expression OFlabel1, label2 : statements… |label3 : staments… |

ELSEstatements…

END;

Page 32: modula 2.1

11/04/2023 32

CASEMODULE demo;VAR

menu : (junior, normal, maxi);price : REAL;

BEGINmenu := junior;CASE menu OF

junior : price := 1.25; |normal : price := 2.25; |maxi : price := 3.00; |

ELSEprice := -99.999;

END;END demo.

Page 33: modula 2.1

11/04/2023 33

FOR

• Loop a fixed number of times using a counter variable

FOR counter := start TO stop BY step DOstatements…

END;

• BY step is optional

Page 34: modula 2.1

11/04/2023 34

FOR• The counter will be

changed automatically by the FOR loop

• If BY step is omitted a default step of 1 is used

• start < stop if step is > 0

FOR counter := 10 TO 1 BY –1 DO

Page 35: modula 2.1

11/04/2023 35

FORMODULE demo;VAR

i : CARDINAL;sum : CARDINAL;

BEGINsum := 0;FOR i := 1 TO 20 DO

sum := sum + i;END;

END demo.

Page 36: modula 2.1

11/04/2023 36

WHILE

• Loop as long as the condition return TRUE

WHILE boolean_express DOstatements…

END;

Page 37: modula 2.1

11/04/2023 37

WHILEMODULE demo;VAR

i : CARDINAL;sum : CARDINAL;

BEGINsum := 0;i := 1;WHILE (i <= 20) DO

sum := sum + i;INC(i);

END;END demo.

Page 38: modula 2.1

11/04/2023 38

REPEAT

• Loop until condition becomes TRUE

REPEATstatements…

UNTIL boolean_express;

• Always at least 1 loop!!

Page 39: modula 2.1

11/04/2023 39

REPEAT

MODULE demo;VAR

i : CARDINAL;sum : CARDINAL;

BEGINsum := 0;i := 1;REPEAT

INC(sum, i);INC(i);

UNTIL (i > 20);END demo.

Page 40: modula 2.1

11/04/2023 40

LOOP

• The most basic loop– No counter– No stop condition

LOOPstatements…

END;

• Use EXIT to escape the loop

Page 41: modula 2.1

11/04/2023 41

Array

• DeclarationVAR name : ARRAY index_type OF type;

• Array contains one or more values depending on the index_type

VAR x : ARRAY [1..10] OF REAL;

Page 42: modula 2.1

11/04/2023 42

Array

VAR price : ARRAY (junior, normal, maxi)

OF REAL;

BEGIN price[normal] := 2.25; price[maxi] := 3.00;

Page 43: modula 2.1

11/04/2023 43

Strings

• Character can be represented using the built-in CHAR type– One character at a time

• There is no seperate type in Modula 2 for strings of characters!

VAR text : ARRAY [1..30] OF CHAR;

Page 44: modula 2.1

11/04/2023 44

N-dimensional array

• Modula 2 arrays can have more than one index => more than one dimension

VAR x : ARRAY [1..5] OF ARRAY [1..3] OF REAL;BEGIN x[5][2] := 5.0;

Page 45: modula 2.1

11/04/2023 45

N-dimensional array

• Declaration short hand for n-dim arrays:– x : ARRAY type1 OF ARRAY type2 OF ARRAY

type3 OF type– x : ARRAY type1,type2,type3 OF type

• Indexing short hand for n-dim arrays:– x[3][3][4]– x[3,3,4]

Page 46: modula 2.1

11/04/2023 46

PROCEDURE

• Frequently used statement can be seperated and put into a procedure => code reuse

• In Modula 2 a procedure a is little program on its own

• Difference with program:– Has a name– Possibly some input values– Possibly some output values

Page 47: modula 2.1

11/04/2023 47

PROCEDURE

• 2 aspect:

– Definition: which computation are being performed, which inputs and outputs

– Procedure call: correct use of the procedure in the program

Page 48: modula 2.1

11/04/2023 48

PROCEDURE

• Definition– Name– Number of parameters– Type of each parameter

– Does the procedure return a value:• Type of the value return value

Page 49: modula 2.1

11/04/2023 49

PROCEDURE

• 2 types of procedures– Procedures that compute a value

and return it• Function procedure

– Procedures that have a side effect (write to disk, reset a value, …)

Page 50: modula 2.1

11/04/2023 50

PROCEDURE

PROCEDURE name (arg1 : TYPE1; arg2 : TYPE2; …);(* Declarations just like for a MODULE *)CONST ….TYPE ….VAR ….BEGIN

(* The code comes here *)….

END name;

Page 51: modula 2.1

11/04/2023 51

PROCEDURE

PROCEDURE name (arg1 : TYPE1; arg2 : TYPE2; …) : TYPE6;

(* Declarations just like for a MODULE *)CONST ….TYPE ….VAR ….BEGIN

(* The code comes here *)….

END name;

Page 52: modula 2.1

11/04/2023 52

PROCEDURE

• Keyword RETURN

• Used to:– Jump out of the procedure

– Return a value and jump out of the procedure

Page 53: modula 2.1

11/04/2023 53

PROCEDURE

MODULE demo;PROCEDURE add ( a , b :CARDINAL) :

CARDINAL;BEGIN RETURN a+b;END add;VARt sum : CARDINAL;`BEGIN sum := add( 4, 2);END demo.

Page 54: modula 2.1

11/04/2023 54

PROCEDURE

MODULE demo;FROM IO IMPORT WrStr;PROCEDURE odd( x : REAL);BEGINIF (x < 4.5) THEN RETURN;END;WrStr("x is not < 4.5");END odd;

BEGIN odd(3.); odd(5.5);END demo.

Page 55: modula 2.1

11/04/2023 55

SCOPING

• Procedure are little programs on their own:– Own declarations : types, constants,

variables and even other procedures

• Which variables, constants and procedure can one use inside procedure?

Scoping rules

Page 56: modula 2.1

11/04/2023 56

SCOPING

• Global vs. Local delcarations– Global ~ defined in the main part of

the module– Local ~ defined in procedures

• Top down• Modules• Hiding phenomenon

Page 57: modula 2.1

11/04/2023 57

SCOPINGMODULE demo;VAR v : CHAR;CONST c = 0;PROCEDURE p;VAR z : CHAR;…(* v, c, z, p *)END p;PROCEDURE q;CONST v = 6;…(* v, c, p, q *)END q;BEGIN

END demo.

Page 58: modula 2.1

11/04/2023 58

Structured types

• Store more than one value per variable

– RECORD: possible to combine different types in one variable

– ARRAY: represent several values of the same type in one variable

Page 59: modula 2.1

11/04/2023 59

Record

• DelcarationVAR name: RECORD field1 : type1;

. .

fieldN : typeN; END;

Page 60: modula 2.1

11/04/2023 60

Record

• DeclarationVAR

date :RECORD

day : [1..31];month : [1..12];year : [1900..3000];

END;

Page 61: modula 2.1

11/04/2023 61

Record

• Since a record contains several values we use the field name to access the value:– record.field_name

date.day := 8;date.month := 10;date.year := 2002;

.

Page 62: modula 2.1

11/04/2023 62

Record

VAR point1, point2 : RECORD x, y : REAL; END;BEGIN point1.x := 5.4; point1.y := 2.3; point2 := point1;

Page 63: modula 2.1

11/04/2023 63

RECURSION• f(x) = g(f(x))• Example: factorial

fac(n) := n * fac(n-1); (n>1)fac(n) := 1; (n <=1)

PROCEDURE fac(n : CARDINAL): CARDINAL;BEGIN IF (n <= 1) THEN RETURN n * fac(n-1); ELSE RETURN 1; END;END fac;

Page 64: modula 2.1

MODULE IfDemo; FROM InOut IMPORT WriteString, WriteInt, WriteLn; VAR Index1 : INTEGER; BEGIN FOR Index1 := 1 TO 8 DO IF Index1 < 4 THEN (* Simple IF statement *) WriteString("Index1 is less than 4."); WriteInt(Index1,4); WriteLn; END; (* end of first IF statement *) IF Index1 = 5 THEN (* two way IF statement *) WriteString("Index1 is 5"); ELSE WriteString("Index1 is not 5"); END; (* end of second IF statement *) WriteLn; IF Index1 = 2 THEN (* multiple way IF statement

*) WriteString("Index1 is 2"); ELSIF Index1 = 6 THEN WriteString("Index1 is 6"); ELSE WriteString("I really don't care what Index1 is"); END; WriteLn; END; (* of big FOR loop *) END IfDemo. 6411/04/2023

Page 65: modula 2.1

MODULE Proced1; FROM InOut IMPORT WriteString, WriteLn; VAR Count : INTEGER; PROCEDURE WriteHeader; BEGIN WriteString("This is the header"); WriteLn; END WriteHeader; PROCEDURE WriteMessage; BEGIN WriteString("This is the message"); WriteLn; END WriteMessage; BEGIN (* Main program *) WriteHeader; FOR Count := 1 TO 8 DO WriteMessage; END;END Proced1.

6511/04/2023

Page 66: modula 2.1

Procedure with return typeMODULE Function;

FROM InOut IMPORT WriteString, WriteInt, WriteLn; PROCEDURE QuadOfSum(Number1, Number2 : INTEGER) : INTEGER;

BEGIN RETURN(4*(Number1 + Number2)); END QuadOfSum; VAR Dogs, Cats, Feet : INTEGER; BEGIN (* Main program *) Dogs := 4; Cats := 3; Feet := QuadOfSum(Dogs,Cats); WriteString("There are a total of"); WriteInt(Feet,3); WriteString(" paws."); WriteLn; END Function. 6611/04/2023