Lecture 4: Modula-2 & InTouch Modula-2 overview InTouch demo [RTCS Ch. 6.2; DMOC Ch. 2] 1 History Niklaus Wirth, ETH, Switzerland 1970 Pascal 1975 Modula 1979 Modula-2, first implementation on PDP-11 1980 Language definition, Technical Report 1981 Compilers outside ETH Oberon operative system language less real-time emphasis Modula-3 DEC object-oriented 2 garbage collection Aims Capitals Modula-2 Facilitate the construction of large programs. The module concept. Separate Compilation. Be a hardware close systems programming language. Control over allocation. ADDRESS... Support concurrent programming. NEWPROCESS, TRANSFER, IOTRANSFER A more systematic syntax than in Pascal Reserved words are written with CAPITALS. The compiler distinguishes between capitals and non-capital characters. Comments Can be nested. (* This is a comment *) (* (* *) *) 3 4
Data Types INTEGER, CARDINAL, BOOLEAN, CHAR REAL ch: CHAR; ch := 'a'; ORD('A') = 65 CHR(65) = 'A' x: REAL; x := 5; (* Wrong *) x := 5.0; (* Correct *) Enumeration types optype = (plus,minus,times,divide); op : optype; op := plus; INC(op); (* minus *) DEC(op); (* plus again *) INC(op,3); (* divide *) op := VAL(optype,2); (* times *) ORD(minus); (* 1 *) 5 6 Arrays (* Modula-2 *) (* Pascal *) ARRAY [1..10] OF INTEGER; array [1..10] of integer; type Index = [1..10]; Index = 1..10; var a: ARRAY Index OF INTEGER; a: array [Index] of integer; ARRAY [1..m],[1..n] OF REAL array[1..m,1..n] of real; Strings Represented as ARRAY OF CHAR Library module Strings Records PersonType = RECORD name: ARRAY [0..79] OF CHAR: age: [0..110]; sex: (male,female); anders: PersonType; anders.age := 28; 7 8
ComponentType = (monitor,printer); StringType = ARRAY [0..19] OF CHAR; MechanismType = (dotmatrix,laser); Records with variant part SystemComponent = RECORD manufacturer: StringType; model: StringType; CASE kind: ComponentType OF monitor: colour: BOOLEAN; numlines, numcolumns: CARDINAL printer: mechanism: MechanismType; speed: CARDINAL Thetagfieldcanbeomitted RECORD CASE : BOOLEAN OF TRUE: a: INTEGER; FALSE:b: REAL END A record may have more than one variant part. Variant parts can be nested. component: SystemComponent; Set Types As in Pascal component.manufacturer := 'IBM'; component.kind := printer; component.speed := 10000; 9 10 Pointers Procedure Types p,q: POINTER TO CARDINAL; c: CARDINAL; c := p^; q^ := c; q^ := p^; q := p; NEW(p) allocates memory for a dynamic object of the type pointed to by p. DISPOSE(p) deallocates the memory. To use NEW and DISPOSE one must import STORAGE.ALLOCATE and STORAGE.DEALLOCATE 11 WriteProcedure = PROCEDURE(INTEGER); PROCEDURE WriteDecimal(i: INTEGER); BEGIN END WriteDecimal; PROCEDURE WriteHex(i: INTEGER); BEGIN END WriteHex; WP: WriteProcedure; BEGIN WP := WriteDecimal; WP(10); WP := WriteHex; WP(10); 12
Statements Conditions Assignment a := b + 3; Repetition WHILE i <= limit DO REPEAT UNTIL i > limit; 13 IF a = b THEN ELSIF a < b THEN ELSE Multiple Choice CASE color OF green: blue: red: ELSE END 14 Iteration FOR i := 1 TO n DO FOR i := 1 TO n BY 2 DO Infinite loop LOOP Statements not in Pascal Procedure call Exit from infinite loop P(a,b) EXIT; Function call Return from function procedure x := F(a,b) + 3; RETURN a + b; With reference Alternative return from procedure WITH p DO 15 RETURN 16
Procedures Statements not found in Modula-2 Function procedures and "ordinary" procedures Two kinds of formal parameters: variable parameters and value parameters Variable parameters preceded by go to L1; A change of a variable formal parameter affects the actual parameter (values are passed out of the procedure). (Call by reference) A value parameter gets its value by copying the value of the actual parameter. 17 18 Open array parameters PROCEDURE Sum(s: ARRAY OF CARDINAL): CARDINAL; i, sum: CARDINAL; BEGIN sum := 0; FOR i := 0 TO HIGH(s) DO sum := sum + s[i]; RETURN sum; END Sum; Called by v := ARRAY [m..n] OF CARDINAL; k := Sum(v); v[m] is mapped to s[0] v[n] is mapped to s[n-m] HIGH(s) = n - m Only the first index in a multi-dimensional array 19 Systems programming WORD BYTE ADDRESS (* assignment compatible with all pointer types *) PROCEDURE ADR(x:AnyType): ADDRESS; (* Returns the address of x *) PROCEDURE SIZE( v:anytype): CARDINAL; (* Returns the size needed for v *) PROCEDURE TSIZE(AnyType): CARDINAL; (* Returns the size needed for a variable of the given type *) Plus NEWPROCESS, TRANSFER, IOTRANSFER 20
Modules The primary difference between Pascal and Modula-2 A program consists of modules. One of the modules is the main program. The others are library modules. Separately compiled. Local modules are modules defined inside other modules not so much used. Modules act as walls. Entities declared inside a module are not visible outside the module unless they have been exported by the module. In order for another module to use an exported entity the module must import the entity. A module may import from multiple other modules. A complete Modula-2 program consists of a set of interconnected modules = dependency graph. 21 22 Library modules Two parts: a definition part and an implementation part. The definition part specifies the entities that the module exports. Constitutes the externally visible interface of the module. The implementation part contains the full declaration of the entity, i.e., the implementation. MODULE Main; END Main. Library modules DEFINITION MODULE M; END M. M.def IMPLEMENTATION MODULE M; END M. M.mod Main program: Main.mod 23 24
Import MODULE name ";" {import} {declarations} [BEGIN StatementSequence] END name "." Main Module The items that can be imported by another module must be defined in the Definition module (exported by default) import = [FROM name] IMPORT namelist ";" namelist could contain modules, procedures, variables, types, constants IMPORT M1; FROM M1 IMPORT x, y; Selective "dequalification" x and y can be referenced directly. Other exported entities of M1 can be referenced with M1.entity 25 26 Definition modules Implementation modules DEFINITION MODULE name ";" {import} {definition} END name ";" definition = CONST {constant decl. ";"} {name ["=" type] ";"} {name : type ";"} ProcedureHead ";" 27 CompilationUnit = DefinitionModule [IMPLEMENTATION] ProgramModule ProgramModule = MODULE name ";" {import} {declaration} [BEGIN StatementSequence] END name "." Example IMPLEMENTATION MODULE TreeConstructor; BEGIN END TreeConstructor. 28
DEFINITION MODULE TreeConstructor; Opaque types A data type whose structure is hidden inside a module. The value of an opaque type must be a pointer. Thus the compiler knows how much space to allocate for a variable of an opaque type. FROM RuleHandler IMPORT GetConsequent, GetAntecedent; TreePtr; PROCEDURE MakeTree( T: Treeptr); PROCEDURE Traverse(T: TreePtr); END TreeConstructor. 29 30 IMPLEMENTATION MODULE TreeConstructor; Tree = RECORD TreePtr = POINTER TO Tree; END TreeConstructor. Separate compilation A change in a definition module means that all modules that import from the changed definition module must be recompiled. Then, all modules that import from the recompiled modules must be recompiled, etc. A change in an implementation module only results in a recompilation of that module. 31 32
A Dependency Graph IMPL DEF Main Program MODULE M; Modules A powerful tool for creating large programs. B DEF IMPL Help make programs easier to modify by supporting information hiding. Help make programs more portable. Make separate compilation possible. C DEF IMPL D DEF IMPL Enable the development of libraries of resuable code. 33 34 DEFINITION MODULE Stack; ElementType = ; StackType; PROCEDURE Push( S: StackType, value: ElemenType); PROCEDURE Pop( S: StackType): ElementType; PROCEDURE Init( S: StackType); END Stack. DEFINITION MODULE Stack; FROM User IMPORT ElementType; StackType; PROCEDURE Push( S: StackType, value: ElemenType); PROCEDURE Pop( S: StackType): ElementType; PROCEDURE Init( S: StackType); END Stack. 35 36
IMPLEMENTATION MODULE Stack; FROM Storage IMPORT ALLOCATE; CONST StackMax = 100; StackRange = [0..StackMax]; StackType = POINTER TO RECORD top : StackRange; s : ARRAY StackRange OF ElementType; PROCEDURE Push( S: StackType; value : ElementType); BEGIN WITH S^ DO END Push 37 InTouch Graphical User Interface (GUI) package from Wonderware Corp. Two parts: WindowMaker: graphical editor WindowViewer: run-time environment Animation Links: animated objects two types: TouchLinks DisplayLinks Scripts: command language 38 Tagname Data Dictionary Run-time database The variable of applications Variable: tagname type DDE tag types Memory tag types DDE Dynamic Data Exchange Message-based protocol from Microsoft Client-server system server: provides data client: requests data 39 40
A DDE Conversation Addressing Cold links: one-time requests, snapshot Hot links: the server automatically supply data to the clients whenever the data has changed Warm links: the server informs the cliens that the data has changed. The client may then request the data if needed. Three parts: Application/server name Topic name Item name 41 42 Type definitions: Server; Topic; RealItem; BooleanItem; InTouch WindowViewer InTouch (NT process) DDE Server Module Tagname Data Dictionary DDE communication DDE server Kernel Process (NT thread) Real Time Kernel (NT process) 43 PROCEDURE NewServer( s name DDE primitives : Server; : ARRAY OF CHAR); This primitive creates and returns a server, s. The parameter name defines the application name for the server. PROCEDURE DisposeServer( s : Server); The server is deleted. PROCEDURE NewTopic( t : Topic; s : Server; name : ARRAY OF CHAR); A topic belonging to server s is created and returned. The name of the topic is given by name. PROCEDURE DisposeTopic( t : Topic); A topic is deleted. 44
PROCEDURE NewReal( r : RealItem; t : Topic; name : ARRAY OF CHAR); An item of type real belonging to topic t is created and returned. The name of the item is given by name. PROCEDURE DisposeReal( r : RealItem); An item of type real is deleted. PROCEDURE GetReal(r : RealItem) : LONGREAL; Returns the current value of the real type item r. The operation is nonblocking. PROCEDURE SetReal(r : RealItem; value : LONGREAL); Sets the value of the real type item r. If the item is requested by a DDE-client (e.g., InTouch) and the value has changed then a DDE message is sent to the client. PROCEDURE NewBoolean( r : BooleanItem; t : Topic; name : ARRAY OF CHAR); An item of type boolean belonging to topic t is created and returned. The name of the item is given by name. PROCEDURE DisposeBoolean( r : BooleanItem); An item of type boolean is deleted. PROCEDURE GetBoolean(r : BooleanItem) : BOOLEAN; Returns the current value of the boolean type item r. The operation is nonblocking. PROCEDURE SetBoolean(r : BooleanItem; value : BOOLEAN); Sets the value of the boolean type item r. If the item is requested by a DDE-client (e.g., InTouch) and the value has changed then a DDE message is sent to the client. 45 46 Application: Topic: Item Item Item InTouch Information DDE message Topic: GetReal DMOC Chapter 2 incl. example InTouch Getting Started InTouch User s Guide DDE message Topic: SetReal 47 48