Introduction to Modula-2 - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to Modula-2

Description:

Pos number (3) : CARDINAL. Logical (TRUE/FALSE) : BOOLEAN. Why the name ' single types ' ... CHAR, REAL, BOOLEAN, CARDINAL, INTEGER. Self defined types: ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 92
Provided by: paralle
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Modula-2


1
Introduction to Modula-2
  • Johan Parent

2
Introduction
  • 3 hours of total immersion into the programming
    language Modula-2
  • We start from the very basics
  • Johan Parent
  • Johan_at_info.vub.ac.be
  • 4K216

3
Modula 2 why?
  • Why not Java, C, C, C ?!
  • Learning
  • Looks like English
  • Very structured
  • Safe (easy?)
  • Has all the modern features
  • All programming languages are equal!!

4
Programming
  • Way to control a computer
  • Write down the program (editor)
  • Translate using special program (compiler)
  • Execution by the computer
  • Compiler is clever
  • Translates in machine language
  • Reports mistakes

5
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

6
Program structure
  • Program is called a MODULE
  • MODULE name
  • FROM . IMPORT
  • CONST pi 3.1459
  • TYPE euro REAL
  • BEGIN
  • . ( comment )
  • END name.

7
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 .

8
Variables
  • Convenient way to
  • Store values
  • Manipulate values
  • Closely related to the variable concept used in
    mathematics!

9
Variables
  • Need to be declared
  • Name
  • Type
  • VAR
  • X CARDINAL
  • Change the value (assignment)
  • X 451




10
Variables
  • Single types
  • Characters (a) CHAR
  • Integers (-40) INTEGER
  • Reals (2.7) REAL
  • Pos number (3) CARDINAL
  • Logical (TRUE/FALSE) BOOLEAN
  • Why the name  single types ?

11
Variables
Keyword
  • 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.

Declarations
Assignments

12
Variables vs. Literals
  • Variable can have several values (not at one
    time)
  • Literals are the values themselves
  • -3
  • 123
  • z
  • 4.342
  • TRUE

13
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

14
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.

15
Conversions
  • Numbers can not be mixed!
  • CARDINAL ? REAL ? INTEGER
  • Conversion is needed
  • TRUNC(real) gt CARDINAL
  • FLOAT(cardinal/integer) gt REAL

16
BOOLEAN algebra
  • Built-in type BOOLEAN
  • 2 values TRUE, FALSE
  • OPERATORS
  • Logical AND, OR, NOT
  • Comparison lt, gt, , lt, gt, ltgt,

17
BOOLEAN algebra
  • MODULE demo
  • VAR
  • a, b INTEGER
  • c, d BOOLEAN
  • BEGIN
  • a -3
  • b 33
  • c a gt b
  • d NOT c
  • END demo.

18
Precedence rules
  • Define which operator is evaluated first.
  • Multiplication, division, sum,
  • 13 DIV 4 3 TRUE
  • Is this correct?
  • What is the value then?
  • Use brackets to ensure the order of evalution gt
  • ((13 DIV 4) 3) TRUE)

19
Characters
  • Built-in type CHAR
  • Values all the entries of the ASCII table
  • Character literals have to be surrounded by
    single quotes
  • z
  • How can we represent text later

20
Constant
  • Keyword CONST
  • Usage identical to variable
  • Assignment is impossible
  • MODULE demo
  • CONST
  • pi 3.14159
  • code 9342


21
Constant
  • What is the type of the constant?
  • Can I use constant together with variables and
    literals?
  • MODULE demo
  • CONST
  • Pi 3.14159
  • VAR
  • x, r REAL
  • BEGIN
  • r 5.0
  • x 2.0 Pi r
  • END demo.

22
Enumeration
  • First flexible type defined by you
  • Explicit definition of the possible values of a
    variable during the declaration
  • VAR
  • x ( value1, value2, , valueN)
  • Variable x can only have the values listed
    between brackets

23
Enumeration
  • MODULE demo
  • VAR
  • continent ( europe, asia, africa, antartica,
    america)
  • BEGIN
  • continent antartica
  • END demo.
  • Danger values of enumeration can not be used as
    name for other constants or variable

24
Subrange
  • Equivalent to interval in mathematics x ? 0, 10
  • Again defined by you during the declaration
  • VAR
  • x 0 .. 10
  • c a .. d
  • Not for real valued intervals!!

..
25
Subrange
  • MODULE demo
  • VAR
  • a, b, c 2..15
  • BEGIN
  • a 2
  • b 8
  • c a b
  • END demo.

26
Structured types
  • Most types are built-in
  • CHAR, REAL, BOOLEAN, CARDINAL, INTEGER
  • Self defined types
  • Enumeration
  • Subrange
  • Until now simple types i.e. one variable one
    value at a time

27
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

28
Record
  • Delcaration
  • VAR
  • name RECORD
  • field1 type1
  • .
  • .
  • fieldN typeN
  • END

29
Record
  • Declaration
  • VAR
  • date
  • RECORD
  • day 1..31
  • month 1..12
  • year 1900..3000
  • END

30
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

.
31
Record
  • VAR
  • point1, point2 RECORD
  • x, y REAL
  • END
  • BEGIN
  • point1.x 5.4
  • point1.y 2.3
  • point2 point1

Possible?
32
Array
  • Declaration
  • VAR
  • name ARRAY index_type OF type
  • Array contains one or more values depending on
    the index_type
  • VAR
  • x ARRAY 1..10 OF REAL

33
Array
  • As with RECORDs with need more work to take one
    value out of an array gt indexing
  • arrayposition
  • x8 4.3
  • x2 x8 1.2

34
Array
  • VAR
  • price ARRAY (junior, normal, maxi) OF REAL
  • BEGIN
  • pricenormal 2.25
  • pricemaxi 3.00

35
Array
  • VAR
  • taste ARRAY (bread, rice) OF (bad, normal,
    good)
  • class ARRAY(john, alan) OF RECORD

  • age 0..200

  • weight REAL

  • END
  • BEGIN
  • tastebread good
  • tasterice good
  • classjohn.age 25

36
Array
  • Respect the index type
  • VAR x ARRAY 3..10 OF BOOLEAN
  • BEGIN
  • x1 TRUE
  • Arrays can not be copied in 1 step
  • VAR x, y ARRAY 3..10 OF BOOLEAN
  • BEGIN
  • x y

37
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

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

39
N-dimensional array
  • Modula 2 arrays can have more than one index gt
    more than one dimension
  • VAR
  • x ARRAY 1..5 OF
  • ARRAY 1..3 OF REAL
  • BEGIN
  • x52 5.0

40
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
  • x334
  • ?
  • x3,3,4

41
TYPE
  • Modula 2 comes with built-in types
  • You can also define new types
  • MODULE demo
  • TYPE
  • Mp3 RECORD
  • name ARRAY 1..40 OF
  • CHAR
  • length 1..3600
  • END


42
TYPE
  • MODULE demo
  • TYPE
  • Mp3 RECORD
  • name ARRAY 1..40 OF CHAR
  • length 1..3600
  • END
  • VAR
  • song Mp3
  • BEGIN
  • song.name "4 seasons winter"
  • END demo.

43
Control instruction
  • Normal program execution
  • Top down
  • Line by line
  • Statements that influence the execution of the
    program
  • 3 groups
  • Branches
  • Loops
  • Procedures

44
IF
  • Choose an alternative based on result of the
    boolean expression
  • IF boolean_express1 THEN
  • statements
  • ELIF boolean_express2 THEN
  • statements
  • ELSE
  • statements
  • END

Optional
45
IF
  • MODULE demo
  • FROM IO IMPORT WrStr
  • VARx CARDINAL
  • BEGINx 3
  • IF (x lt 5) THEN
  • WrStr(" x is smaller than 5")
  • END
  • END demo.

46
IF
  • MODULE demo
  • FROM IO IMPORT WrStr
  • VARx CARDINAL
  • BEGINx 3
  • IF (x lt 5) THEN
  • WrStr(" x is smaller than 5")
  • ELSE
  • WrStr(" x is gt 5")
  • END
  • END demo.

47
IF
  • MODULE demo
  • FROM IO IMPORT WrStr
  • VARx CARDINAL
  • BEGINx 3
  • IF (x lt 5) THEN
  • WrStr(" x is smaller than 5")
  • ELIF (x gt 5) THEN
  • WrStr(" x is bigger than 5")
  • ELSE
  • WrStr("x equals 5")
  • END
  • END demo.

48
CASE
  • Choose one alternative using a label, no test!!
  • CASE expression OF
  • label1, label2 statements
  • label3 staments
  • ELSE
  • statements
  • END

Optional
49
CASE
  • MODULE demo
  • VARmenu (junior, normal, maxi)
  • price REAL
  • BEGINmenu junior
  • CASE menu OF
  • junior price 1.25
  • normal price 2.25
  • maxi price 3.00
  • ELSE
  • price -99.999
  • END
  • END demo.

50
FOR
  • Loop a fixed number of times using a counter
    variable
  • FOR counter start TO stop BY step DO
  • statements
  • END
  • BY step is optional

51
FOR
  • The counter will be changed automatically by the
    FOR loop
  • If BY step is omitted a default step of 1 is used
  • start lt stop if step is gt 0
  • FOR counter 10 TO 1 BY 1 DO

52
FOR
  • MODULE demo
  • VARi CARDINAL
  • sum CARDINAL
  • BEGINsum 0
  • FOR i 1 TO 20 DO
  • sum sum i
  • END
  • END demo.

53
WHILE
  • Loop as long as the condition return TRUE
  • WHILE boolean_express DO
  • statements
  • END

54
WHILE
  • MODULE demo
  • VARi CARDINAL
  • sum CARDINAL
  • BEGINsum 0
  • i 1
  • WHILE (i lt 20) DO
  • sum sum i
  • INC(i)
  • END
  • END demo.

55
REPEAT
  • Loop until condition becomes TRUE
  • REPEAT
  • statements
  • UNTIL boolean_express
  • Always at least 1 loop!!

!
56
REPEAT
  • MODULE demo
  • VARi CARDINAL
  • sum CARDINAL
  • BEGINsum 0
  • i 1
  • REPEAT
  • INC(sum, i)
  • INC(i)
  • UNTIL (i gt 20)
  • END demo.

57
LOOP
  • The most basic loop
  • No counter
  • No stop condition
  • LOOP
  • statements
  • END
  • Use EXIT to escape the loop

58
LOOP
  • MODULE demo
  • VARi CARDINAL
  • sum CARDINAL
  • BEGINsum 0
  • i 1
  • LOOP
  • INC(sum, i)
  • INC(i)
  • IF (i gt20) THEN
  • EXIT
  • END
  • END
  • END demo.

59
PROCEDURE
  • Frequently used statement can be seperated and
    put into a procedure gt 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

60
PROCEDURE
  • 2 aspect
  • Definition which computation are being
    performed, which inputs and outputs
  • Procedure call correct use of the procedure in
    the program

61
PROCEDURE
  • Definition
  • Name
  • Number of parameters
  • Type of each parameter
  • Does the procedure return a value
  • Type of the value return value

62
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, )

63
PROCEDURE
  • PROCEDURE name (arg1 TYPE1 arg2 TYPE2 )
  • ( Declarations just like for a MODULE )
  • CONST .
  • TYPE .
  • VAR .
  • BEGIN
  • ( The code comes here )
  • .
  • END name

64
PROCEDURE
  • PROCEDURE name (arg1 TYPE1 arg2 TYPE2 )
    TYPE6
  • ( Declarations just like for a MODULE )
  • CONST .
  • TYPE .
  • VAR .
  • BEGIN
  • ( The code comes here )
  • .
  • END name

65
PROCEDURE
  • Keyword RETURN
  • Used to
  • Jump out of the procedure
  • Return a value and jump out of the procedure

66
PROCEDURE
  • MODULE demo
  • PROCEDURE add ( a , b CARDINAL) CARDINAL
  • BEGIN
  • RETURN ab
  • END add
  • VAR
  • sum CARDINAL
  • BEGIN
  • sum add( 4, 2)
  • END demo.

67
PROCEDURE
  • MODULE demo
  • FROM IO IMPORT WrStr
  • PROCEDURE odd( x REAL)
  • BEGIN
  • IF (x lt 4.5) THEN
  • RETURN
  • END
  • WrStr("x is not lt 4.5")
  • END odd
  • BEGIN
  • odd(3.)
  • odd(5.5)
  • END demo.

68
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

69
SCOPING
  • Global vs. Local delcarations
  • Global defined in the main part of the module
  • Local defined in procedures
  • Top down
  • Modules
  • Hiding phenomenon

70
SCOPING
  • MODULE 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.

71
VAL/VAR
  • MODULE demo
  • PROCEDURE reset(c REAL)
  • BEGIN
  • c 0.0
  • END zero
  • VAR
  • r REAL
  • BEGIN
  • r 99.99
  • reset(r)
  • END demo.

72
VAL/VAR
  • The previous program does not work, why?
  • Parameter passing mechanism
  • Makes copy of the arguments (value parameter VAL)
  • Execute the procedure code
  • VAR parameters are not copied!

73
VAL/VAR
  • MODULE demo
  • PROCEDURE zero (VAR c REAL)
  • BEGIN
  • c 0.0
  • END zero
  • VARx REAL
  • BEGINreset(x)
  • END demo.
  • Works as was expected!

74
VAL/VAR
  • VAR parameters are not copied but a reference to
    the original variable is passed instead
  • VAR parameter required variables during the
    procedure call, why?

75
OPEN ARRAY
  • Passing arrays as parameters is not easy?!
  • PROCEDURE p(a ARRAY 1..10 OF REAL)
  • To make it work
  • Declare a new type first
  • Use type in declaration
  • TYPE Arr ARRAY 1..10 OF REAL
  • PROCEDURE p(a Arr)

76
OPEN ARRAY
  • Not convenient
  • Declaration
  • Not flexible in terms of the size arrays passed
  • Solution open array parameters

77
OPEN ARRAY
  • Advantage
  • No need to declare a type
  • Flexible in terms of size
  • ARRAY OF type
  • PROCEDURE p(a ARRAY OF REAL)
  • Procedure p can accept arrays of any size

78
OPEN ARRAY
  • All arrays start at index 0
  • HIGH returns the index of the last position

79
OPEN ARRAY
  • PROCEDURE sum(a ARRAY OF REAL) REAL
  • VAR
  • i CARDINAL
  • tot REAL
  • BEGIN
  • tot 0.0
  • FOR i 0 TO HIGH(a) DO
  • tot tot ai
  • END
  • RETURN tot
  • END sum

80
OPEN ARRAY
  • VAR
  • firstAr ARRAY 30..35 OF CHAR
  • secondAr ARRAY 1..10 OF CHAR
  • PROCEDURE myWrStr( str ARRAY OF CHAR)
  • VAR
  • i CARDINAL
  • BEGIN
  • FOR i 0 TO HIGH(str) DO
  • WrChar(stri)
  • END
  • END myWrStr

81
RECURSION
  • f(x) g(f(x))
  • Example factorial
  • fac(n) n fac(n-1) (ngt1)
  • fac(n) 1 (n lt1)
  • PROCEDURE fac(n CARDINAL) CARDINAL
  • BEGIN
  • IF (n lt 1) THEN
  • RETURN n fac(n-1)
  • ELSE
  • RETURN 1
  • END
  • END fac

82
POINTERS
  • Pointers are variables containing memory
    addresses
  • Declaration
  • VAR
  • a POINTER TO INTEGER
  • a contains the address of a memory block which
    contains an INTEGER value.

83
POINTER
  • Adresses of memory locations are not interesting
  • The value stored at the memory location is gt
    dereference
  • VAR
  • a POINTER TO INTEGER
  • BEGIN
  • a -3

84
POINTERS
  • Pointers are used for dynamic memory allocation!
    But pointers do not do the allocation.
  • There are special Modula2 procedures to work with
    memory
  • IMPOR from the SYSTEM module
  • NEW used to ask memory
  • DISPOSE to release the memory you got

85
Pointers DEMO
  • MODULE memory
  • FROM SYSTEM IMPORT NEW, DISPOSE
  • FROM IO IMPORT WrStr, WrLn
  • VAR
  • i_ptr POINTER TO INTEGER
  • i INTEGER
  • BEGIN
  • WrStr("Start") WrLn
  • ( We reserve some memory for 1 integer
    )
  • NEW(i_ptr)
  • i_ptr 9
  • i i_ptr
  • ( We tell the computer we will not use
    this memory anymore )
  • DISPOSE(i_ptr)
  • WrStr("Stop") WrLn
  • END memory.

86
POINTERS
  • Dynamic memory allocation is the main reason for
    using pointers.
  • Special structures are used to do this
  • Linked lists
  • Trees
  • These are RECURSIVE DATA STRUCTURES

87
POINTERS
  • Linked list
  • TYPE
  • LinkPtr POINTER TO LinkRc
  • LinkRc RECORD
  • data REAL
  • next LinkPtr
  • END
  • Tree
  • TYPE
  • NodePtr POINTER TO NodeRc
  • NodeRc RECORD
  • data REAL
  • left, right NodePtr
  • END

88
POINTERS
  • These structures can be contected to each other!
  • Thereby allowing us to build big structures where
    each element contains data!
  • In this way we can use as much memory as we need!
    And this without knowing in advance how much
    memory we would need.

89
Pointers memory allocation
  • We can thus ask memory explicitly using NEW but
  • the computer may run out of memory!!!
  • We can know that when NEW returns NIL as value
    for our pointer

90
Pointers memory allocation (2)
  • In order to save memory we have to recycle the
    memory we do not use anymore
  • we have to deallocate the memory using DISPOSE
  • we have to be careful and avoid using memory we
    deallocated!!

91
MODULES
Write a Comment
User Comments (0)
About PowerShow.com