generic
SIZE : POSITIVE;
type
ITEM is private
;
package
STACK is
procedure
PUSH(E : in
ITEM);
procedure
POP (E : out
ITEM);
OVERFLOW, UNDERFLOW : exception
;
end
STACK;
package body
STACK is
type
TABLE is array
(POSITIVE range
<>) of
ITEM;
SPACE : TABLE(1 .. SIZE);
INDEX : NATURAL := 0;
procedure
PUSH(E : in
ITEM) is
begin
if
INDEX >= SIZE then
raise
OVERFLOW;
end if
;
INDEX := INDEX + 1;
SPACE(INDEX) := E;
end
PUSH; procedure
POP(E : out
ITEM) is
begin
if
INDEX = 0 then
raise
UNDERFLOW;
end if
;
E := SPACE(INDEX);
INDEX := INDEX - 1;
end
POP; end
STACK;
package
STACK_INT is new
STACK(SIZE => 200, ITEM => INTEGER);
package
STACK_BOOL is new
STACK(100, BOOLEAN);
STACK_INT.PUSH(N);
STACK_BOOL.PUSH(TRUE);
generic
type
ITEM is private
;
package
ON_STACKS is
type
STACK(SIZE : POSITIVE) is limited private
;
procedure
PUSH(S : in out
STACK; E : in
ITEM);
procedure
POP (S : in out
STACK; E : out
ITEM);
OVERFLOW, UNDERFLOW : exception
;
private
type
TABLE is array
(POSITIVE range
<>) of
ITEM;
type
STACK(SIZE : POSITIVE) is
record
SPACE : TABLE(1 .. SIZE);
INDEX : NATURAL := 0;
end record
;
end
;
declare
package
STACK_REAL is new
ON_STACKS(REAL); use
STACK_REAL;
S : STACK(100)
begin
...
PUSH(S, 2.54);
...
end
;