Concepts of Programming Languages

(Sean Pound) #1

484 Chapter 11 Abstract Data Types and Encapsulation Constructs


function Empty(Stk: in Stack_Type) return Boolean is
begin
return Stk.Topsub = 0;
end Empty;

procedure Push(Stk : in out Stack_Type;
Element : in Integer) is
begin
if Stk.Topsub >= Max_Size then
Put_Line("ERROR - Stack overflow");
else
Stk.Topsub := Stk.Topsub + 1;
Stk.List(Topsub) := Element;
end if;
end Push;

procedure Pop(Stk : in out Stack_Type) is
begin
if Empty(Stk)
then Put_Line("ERROR - Stack underflow");
else Stk.Topsub := Stk.Topsub - 1;
end if;
end Pop;

function Top(Stk : in Stack_Type) return Integer is
begin
if Empty(Stk)
then Put_Line("ERROR - Stack is empty");
else return Stk.List(Stk.Topsub);
end if;
end Top;
end Stack_Pack;

The first line of the code of this body package contains two clauses: a with
and a use. The with clause makes the names defined in external packages
visible; in this case Ada.Text_IO, which provides functions for input and
output of text. The use clause eliminates the need for explicit qualification
of the references to entities from the named package. The issues of access
to external encapsulations and name qualifications are further discussed in
Section 11.7.
The body package must have subprogram definitions with headings that
match the subprogram headings in the associated package specification. The
package specification promises that these subprograms will be defined in the
associated body package.
The following procedure, Use_Stacks, is a client of package Stack_Pack.
It illustrates how the package might be used.
Free download pdf