Top Banner
Structure of Ada Structure of Ada Programs Programs Building a program from Building a program from components components Programming Languages Programming Languages Spring 2004 Spring 2004
28

Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

Structure of Ada Structure of Ada ProgramsPrograms

Building a program from Building a program from componentscomponents

Programming LanguagesProgramming LanguagesSpring 2004Spring 2004

Page 2: Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

Structure of a ProgramStructure of a Program

A program is a collection of separately A program is a collection of separately compiled unitscompiled units

One unit is a distinguished main One unit is a distinguished main procedureprocedure

The units must be bound together to The units must be bound together to form an executable programform an executable program

The binder checks for type The binder checks for type consistency and elaboration order consistency and elaboration order consistency.consistency.

Page 3: Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

Units 1 (Subprograms)Units 1 (Subprograms)

A unit can be a stand alone function A unit can be a stand alone function or procedure.or procedure.

Such a unit may or may not have a Such a unit may or may not have a separate spec (which is a separate separate spec (which is a separate unit)unit)

Generally good to have this separate Generally good to have this separate spec since it means recompiling body spec since it means recompiling body does not require clients to be does not require clients to be recompiledrecompiled

Page 4: Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

Units 2 (Package Specs)Units 2 (Package Specs)

A library package is a collection of A library package is a collection of declarations of types, (global) declarations of types, (global) variables, and subprogram specs.variables, and subprogram specs.

Package x isPackage x is declarations declarationsend x;end x;

If any subprogram specs present, If any subprogram specs present, then there must be a corresponding then there must be a corresponding bodybody

Page 5: Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

Units 3 (Package bodies)Units 3 (Package bodies)

A package body contains the bodies of A package body contains the bodies of all subprograms whose spec is in the all subprograms whose spec is in the package spec.package spec.

It can also contain local subprograms It can also contain local subprograms (with or without separate specs) that can (with or without separate specs) that can only be called within the body.only be called within the body.

It can also have local types, variables etcIt can also have local types, variables etcA package body variable is the A package body variable is the

equivalent of a static variable in a C equivalent of a static variable in a C function.function.

Page 6: Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

Private Sections in PackagesPrivate Sections in Packages

A package can have a private partA package can have a private partpackage Stuff ispackage Stuff is

public declarations public declarations … …privateprivate

private declarationsprivate declarationsend Stuffend Stuff

Page 7: Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

Private PartPrivate Part

Must contain full declarations of any Must contain full declarations of any private typesprivate types

e.g.e.g.

type Name is private type Name is private

then in private partthen in private part

type Name is new String (1 .. 20); type Name is new String (1 .. 20);

Page 8: Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

Private Parts (continued)Private Parts (continued)

Must contain declarations of any deferred Must contain declarations of any deferred constantsconstants

type Set is private; type Set is private; Empty_Set : constant Set; Empty_Set : constant Set;

privateprivate type Set is array type Set is array (integer range<>) of Integer; (integer range<>) of Integer;

Empty_Set : constant Set := (1 .. 0 => Empty_Set : constant Set := (1 .. 0 => 0);0);

Page 9: Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

Private PartsPrivate Parts

Can contain any other declarations Can contain any other declarations (types, variables, subprograms).(types, variables, subprograms).

These declarations can be referenced These declarations can be referenced in the body (but they could be in the in the body (but they could be in the body anyway in that case)body anyway in that case)

But can also be referenced in child But can also be referenced in child packages.packages.

Page 10: Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

Child PackagesChild Packages

Can be used to extend a package in Can be used to extend a package in a hierarchical mannera hierarchical manner

package Calendar is ….package Calendar is ….

package Calendar.Alarms is …package Calendar.Alarms is … declarations extending Calendar declarations extending Calendarend Calendar.Alarms;end Calendar.Alarms;

Page 11: Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

Child PackagesChild Packages

The child can see all declarations of The child can see all declarations of the parent (without a WITH clause, the parent (without a WITH clause, since it is really part of the parent).since it is really part of the parent).

The private part and the body of the The private part and the body of the child can see the private part (but child can see the private part (but not the body) of the parent.not the body) of the parent.

Page 12: Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

Clients of Child PackagesClients of Child Packages

A client can see just the parent’s A client can see just the parent’s declarations:declarations:

with Calendar; with Calendar; package Client is … package Client is …

Or it can see any or all of its childrenOr it can see any or all of its children

with Calendar; with Calendar.Alarms; with Calendar; with Calendar.Alarms; package Client is … package Client is …

Page 13: Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

Private PackagesPrivate Packages

A private child packageA private child package

private package Calendar.Internal is … private package Calendar.Internal is …

Can only be with’ed by other children of Can only be with’ed by other children of Calendar. It allows separation of Calendar. It allows separation of functions into a separate package functions into a separate package without exporting the outside the without exporting the outside the hierarchy.hierarchy.

Page 14: Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

Bottom Up Program StructureBottom Up Program Structure

From primitives, build larger From primitives, build larger abstractions,abstractions,in the form of reusable packages.in the form of reusable packages.

These reusable packages can be used These reusable packages can be used to build higher level abstractions.to build higher level abstractions.

Eventually the level of abstraction Eventually the level of abstraction (and power) is sufficient to allow the (and power) is sufficient to allow the program to be written as a single main program to be written as a single main program that makes appropriate calls.program that makes appropriate calls.

Page 15: Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

Top Down StructureTop Down Structure

We write the entire program, but We write the entire program, but leave sections for which we fill in the leave sections for which we fill in the detail later.detail later.

Then we write these sections, if Then we write these sections, if necessary leaving sections of them in necessary leaving sections of them in turn to be filled in later.turn to be filled in later.

We continue until we can write the We continue until we can write the sections directly in terms of available sections directly in terms of available primitives.primitives.

Page 16: Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

Subprogram SubunitsSubprogram Subunits procedure Main isprocedure Main is

Data : array …. Data : array …. procedure Input; procedure Input; procedure Calc; procedure Calc; procedure Output; procedure Output;

procedure Input is separate; procedure Input is separate; procedure Calc is separate; procedure Calc is separate; procedure Output is separate; procedure Output is separate;

beginbegin Input; Input; Calc; Calc; Output; Output;end;end;

Page 17: Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

Filling in the DetailsFilling in the Details

Now we provide separate subunitsNow we provide separate subunits

separate (Main)separate (Main)procedure Calc isprocedure Calc is … …end;end;

Note that procedure Calc has full visibility Note that procedure Calc has full visibility of its environment (e.g. can access Data, of its environment (e.g. can access Data, or call Output or Input).or call Output or Input).

Page 18: Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

Package SubunitsPackage Subunits

package body Sets ispackage body Sets is type Set_Implementation is …. type Set_Implementation is …. … … package Set_Utilities is package Set_Utilities is procedure Q; procedure Q; … … end Utilities; end Utilities;

package body Set_Utilities is package body Set_Utilities is separate;separate;end Client;end Client;

Page 19: Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

Filling in the detailsFilling in the details

The package body subunit is a separately The package body subunit is a separately compiled unit:compiled unit:

separate (Sets)separate (Sets)package body Set_Utilities ispackage body Set_Utilities is procedure Q is procedure Q is … … end Q; end Q; … …end Set_Utilities;end Set_Utilities;

Again, the subunit inherits its context (and can for Again, the subunit inherits its context (and can for example reference type Set_Implementation;example reference type Set_Implementation;

Page 20: Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

Generic UnitsGeneric Units

A library unit can be a generic unitA library unit can be a generic unit Generic subprogramsGeneric subprograms Generic packagesGeneric packages Generic children of generic packagesGeneric children of generic packages

Can instantiate at library level to make a usable Can instantiate at library level to make a usable unit, e.g.unit, e.g.

with Text_IO; with Text_IO; package Int_IO is new Integer_IO (Integer); package Int_IO is new Integer_IO (Integer);

Or can instantiate within another unitOr can instantiate within another unit

Page 21: Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

Main ProgramMain Program

Typically a parameterless procedureTypically a parameterless procedureCan be a child procedure of a packageCan be a child procedure of a packageCan also be a function (input Can also be a function (input

arguments might be argv/argc style arguments might be argv/argc style command parameters, result might be command parameters, result might be return code)return code)

Binder is told main program, and Binder is told main program, and creates the transitive closure of all creates the transitive closure of all units with’ed verifying consistency.units with’ed verifying consistency.

Page 22: Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

Distributed ProgramsDistributed Programs

Another step (Annex E of reference Another step (Annex E of reference manual).manual).

A program is a collection of partitionsA program is a collection of partitionsEach partition is essentially what we Each partition is essentially what we

have called a program so farhave called a program so farPartitions can communicate with Partitions can communicate with

remote procedure callsremote procedure callsConsistency is checkedConsistency is checked

Page 23: Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

Building an abstraction (card Building an abstraction (card games)games)

package Cards ispackage Cards is type Rank is range (‘2’, ‘3’ ,’4’… Jack, Queen, King, Ace); type Rank is range (‘2’, ‘3’ ,’4’… Jack, Queen, King, Ace);

type Suit is (Clubs, Hearts, Diamonds, Spades); type Suit is (Clubs, Hearts, Diamonds, Spades);

type Card is recordtype Card is record R : Rank; R : Rank; S : Suit; S : Suit; end record; end record;

-- We decided to make card a non-private type so-- We decided to make card a non-private type so -- that we have aggregate notation as in: -- that we have aggregate notation as in: -- -- -- Face_Card : Card := (King, Spades); -- Face_Card : Card := (King, Spades); -- -- -- If we made it private then we would have to write -- If we made it private then we would have to write -- -- -- Face_Card : Card := Make_Card (King, Spades); -- Face_Card : Card := Make_Card (King, Spades);

Page 24: Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

Building an abstraction (card Building an abstraction (card games)games)

We decide to make Dec privateWe decide to make Dec private

type Deck is private; type Deck is private;

Subprograms operating on Decks:Subprograms operating on Decks:

function Empty_Deck return Deck; function Empty_Deck return Deck; function Full_Deck return Deck; function Full_Deck return Deck; procedure Shuffle (D : in out Deck); procedure Shuffle (D : in out Deck); procedure Put (C : Card; D : in out Deck); procedure Put (C : Card; D : in out Deck); procedure Remove (C : Card; D : in out Deck); procedure Remove (C : Card; D : in out Deck);

An exception raised if card to be removed is not in deckAn exception raised if card to be removed is not in deck

Not_There : exception; Not_There : exception;end Cards;end Cards;

Page 25: Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

A child Package for DisplayA child Package for Display

package Cards.Graphics ispackage Cards.Graphics is type Size is (Small, Medium, Large); type Size is (Small, Medium, Large); type Location is record type Location is record X, Y : Float range 0.0 .. 10.0; X, Y : Float range 0.0 .. 10.0; end record; end record; procedure Display_Face_Up procedure Display_Face_Up (C : Card; (C : Card; P : Position; P : Position; S : Size := Medium); S : Size := Medium); … …end Cards.Graphics;end Cards.Graphics;

Page 26: Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

Playing KlondikePlaying Klondike

with Cards; with Cards.Graphics;with Cards; with Cards.Graphics;package Klondike ispackage Klondike is type Column is range 1 .. 7; type Column is range 1 .. 7; procedure Deal_Tableau; procedure Deal_Tableau; function Top_Card return Card; function Top_Card return Card; procedure Flip; procedure Flip; procedure Place (C : Card; Col : Column); procedure Place (C : Card; Col : Column); procedure Move_Col (From, To : Column); procedure Move_Col (From, To : Column); Illegal_Move exception; Illegal_Move exception; Win, Lose : exception; Win, Lose : exception; … …end Klondike;end Klondike;

Page 27: Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

A main programA main program with Klondike;with Klondike;

with Cards;with Cards;with Text_IO; use Text_IO;with Text_IO; use Text_IO;

procedure Main isprocedure Main is procedure Play; procedure Play; procedure Play is separate; procedure Play is separate;beginbegin Klondike.Deal_Tableau; Klondike.Deal_Tableau; Play; Play;exceptionexception when Win => Put_Line (“won!!!”); when Win => Put_Line (“won!!!”); when Loose => Put_Line (“lost!”); when Loose => Put_Line (“lost!”);end Main;end Main;

Page 28: Structure of Ada Programs Building a program from components Programming Languages Spring 2004.

The actual playing algorithmThe actual playing algorithm

The actual playing algorithm is a The actual playing algorithm is a subprogram subunit:subprogram subunit:

separate (Main)separate (Main)procedure Play isprocedure Play is … …end;end;

Note that this Play routine has full access Note that this Play routine has full access to the facilities of Klondike and Cardsto the facilities of Klondike and Cards