------------------------------------------------------------------------ -- -- -- NAME: Stack Land -- -- AUTHOR: G. Pyle -- -- DATE: 1983 -- -- -- ------------------------------------------------------------------------ -- C*T Ada features with TEXT_IO; use TEXT_IO; -- Procedure STACKS is a procedure to demonstrate the use of stacks. procedure STACKS is -- We will use this type in the enumeration example. type VEGETABLES is (CARROT, LETTUCE, PEA, PRUNE); -- enumeraction types -- Define a generic stack. generic -- generics type ITEM is private; -- Type to be stacked. MAX : integer; -- Size of the stack. package STACK is -- packages -- procedure to add an item to the stack. procedure PUSH(X:ITEM); -- procedure to remove an item and return its value. function POP return ITEM; end STACK; package body STACK is S:array(1..MAX) of ITEM; TOP:INTEGER range 0 .. MAX; procedure PUSH(X:ITEM) is begin TOP:=TOP+1; S(TOP):=X; end PUSH; function POP return ITEM is begin TOP:=TOP-1; return S(TOP+1); end POP; begin TOP:=0; -- Initialize the stack top. end STACK; -- We will need integer and float I/O for our examples. -- We will also need enumeration I/O for our vegetables. package INT_IO is new INTEGER_IO(INTEGER); -- generic I/O package FLO_IO is new FLOAT_IO(FLOAT); package VEG_IO is new ENUMERATION_IO(VEGETABLES); -- Instantiate 3 stacks for different types, and with different -- sizes. package INTEGER_STACK is new STACK(INTEGER,5); package VEG_STACK is new STACK(VEGETABLES,7); package FLOAT_STACK is new STACK(FLOAT,6); -- A Simple task to demonstrate the INTEGER STACK -- Tasks task INTEGER_EXAMPLE; task body INTEGER_EXAMPLE is use INTEGER_STACK,INT_IO; begin PUSH(123); PUSH(456); PUT(INTEGER'(POP),10); -- qualifiers PUT(INTEGER'(POP),10); NEW_LINE; end INTEGER_EXAMPLE; -- A Simple task to demonstrate the FLOAT STACK task FLOAT_EXAMPLE; task body FLOAT_EXAMPLE is use FLOAT_STACK,FLO_IO; begin PUSH(123.0); PUSH(FLOAT(456)); -- conversion PUT(FLOAT'(POP),10); PUT(FLOAT'(POP),10); NEW_LINE; end; -- And a task for our enumeration example using vegetables. task ENUM_EXAMPLE; task body ENUM_EXAMPLE is use VEG_STACK,VEG_IO; begin PUSH(CARROT); PUSH(PRUNE); PUT(POP); PUT(POP); NEW_LINE; end; -- Print welcome messages from time to time. begin for WELCOME in 1 .. 5 loop PUT_LINE("Welcome to the wonderful world of stacks"); end loop; end STACKS; ------------------------------------------------------------------------