Lecture 13 – October 11, 2005

 

Here is a link to the adagide IDE if you are having trouble getting  jGrasp to work.

http://www.martincarlisle.com/adagide.html

 

Here is a link to a list of free Ada compilers. 

http://www.adahome.com/Resources/Compilers/Free.html

 

I would download the one at

http://www.adahome.com/articles/1997-10/pr_objectadafree.html

 

 

Homework due Tuesday,  October 18th : 

 

Ada comment operator is –

Everything following – on a line is ignored by the compiler

The code below is an example of an Ada package.  Any Ada program wanting access to the material in this package only has to have it in the same project (or in the same directory as the program) and say:    with usefulStuff;

 

  • Line 1 makes the procedures in the package ada.text_io available to this package.
  • Line 3 is the package header statement – notice that it is a package specification not a package body with executable code.   If it were, the header statement would be: package body usefulStuff is
  • Lines 5, 6 and 7 describe (declare) new user-defined types.  Note that the repetition of the word type is required (unlike in Pascal).
  • Lines 9-12 define an Ada record.   Notice the end record which marks the end of the record declaration and the use of the newly defined types Mysuit and Myrank  as fields in the record.
  • Line 14 shows the declaration for an ada array.  Note the use of the starting and ending values for array bounds and the use of the .. between the two values.
  • Lines 16, 17, 18 show instantiations of procedure enumeration_io found in the package ada.text_io.  These instantiations allow user defined types to be read and printed (unlike Pascal's).
  • Finally note the use of the package name in the end statement in line 20.

 

 

 1 ÏÏÏwith ada.text_io;
 2
 3
ÏØÓìpackage usefulStuff is
 4 Ïϧ
 5
ÏϧÏïÏtype Mysuit is (clubs, diamonds, hearts, spades);
 6 ÏϧÏïÏtype Myrank is (two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace);
 7 ÏϧÏïÏtype Player is (playerOne, playerTwo, tie);
 8 Ïϧ
 9
ÏϧÏïÏtype card is record
10 ÏϧÏÏÏÏÏÏÏÏÏsuit : Mysuit;
11 ÏϧÏÏÏÏÏÏÏÏÏrank:  Myrank;
12 ÏϧÏÏÏÏÏÏend record;
13 Ïϧ
14
ÏϧÏïÏtype deck is array (1..52) of card;
15 Ïϧ
16
ÏÏ§ÏØÓìpackage suit_io is new ada.text_io.enumeration_io (Mysuit);
17 ÏÏ§ÏØÓìpackage rank_io is new ada.text_io.enumeration_io (Myrank);
18 ÏÏ§ÏØÓìpackage player_io is new ada.text_io.enumeration_io (Player);
19 ÏÏ©end usefulStuff;  
20

 

The above package can be compiled although it has no executable code in it.  It is also available with out the indentations an line numbers here.

You should download it and compile it.

 

 

Below you will find an example of an Ada generic specification for a queue.   It specifies all of the types, procedures and functions that will be in the package body queuesgeneric which will contain the template for the executable code.  The template will have to be instantiated in a procedure that "with's" it.  That code is shown below the specification.  Again,  each of these can be separately compiled.   Note:  package specifications and package bodies can't be executed.  You can download it and compile it.

 

 1 generic   -- specification
 2
 3
type element is private;
 4
 5
package queuesgeneric is
 6
 7
-- type definition
 8  type queue (capacity: positive) is limited private;
 9
10
-- exported exceptions
11       QueueFull : exception;
12       QueueEmpty : exception;
13       
14 -- constructors
15     procedure MakeEmpty (Q : in out queue);
16     -- pre: Q is defined
17     -- post:  Q is empty
18     
19     procedure Enqueue (Q: in out  Queue; E : in element);
20     -- pre:  Q and E are defined
21     -- post:  Q is returned with E as the top element
22     -- raises: QueueFull if Q already contains Capacity Elements
23     
24     procedure dequeue (Q: in out Queue);
25     -- pre:  Q is defined
26     -- post:  Q is returned with the top Element discarded
27     -- raises QueueEmpty if Q contains no elemelnts
28     
29 -- selector
30    function first (Q : in Queue) return Element;
31    -- pre:  Q is defined
32    -- post:  The first element of Q is returned
33    -- raises:  QueueEmpty if Q contains no elements
34  
35 -- inquiry operations
36    Function isEmpty (Q : in Queue) return Boolean;
37    -- pre:  Q is defined
38    -- post:  returns True if Q is empty, False otherwise
39
40
   Function isFull (Q:  in Queue) return boolean;
41    -- pre:  Q is defined
42    -- post:  returns True if Q is full, False otherwise
43
44
Private
45     Type List is Array (positive Range <> ) of element;
46     Type Queue (Capacity : positive) is record
47                 tail : natural := 0;
48                 store : List (1..Capacity);
49     End record;       
50 End QueuesGeneric;
51    

 

  • Note the use of the word body in line 1.
  • Note that this package body contains two procedures  and three functions.
  • It utilizes two out of Ada's three parameter modes: in, and in out.  The third parameter mode is out
  • It shows in line 5 and elsewhere, that :=  is the assignment operator.
  • It illustrates that each Ada if statement is terminated by an end if.
  • It shows the use of the equality operator =
  • It shows the use of the return statement in the functions.
  • The reserved words in this package body are in lavender.

 

 1 package body QueuesGeneric is
 2
 3
  procedure makeEmpty (Q : in out queue ) is
 4      begin
 5        Q.tail := 0;
 6      end makeEmpty;
 7      
 8   procedure Enqueue (Q : in out Queue;  E : in Element) is
 9      begin
10        if Q.tail = q.capacity then
11           raise QueueFull;
12        else
13           Q.tail := q.tail + 1;
14           Q.store (Q.tail) := E;
15        end if;
16      end Enqueue;
17      
18    procedure Dequeue (Q : in out Queue) is
19       begin
20         if q.tail = 0 then
21            raise QueueEmpty;
22         else
23            Q.store(1..Q.tail-1) := Q.store (2..Q.tail); -- slice
24            Q.tail := Q.tail - 1;
25         end if;
26       end Dequeue;
27       
28     function first (Q : in Queue) return Element is
29        begin
30          if q.tail = 0  then
31             raise QueueEmpty;
32          else
33             return Q.Store(1);
34          end if;
35        end first;
36        
37      function isEmpty (Q: in Queue) return boolean is
38         begin
39           return Q.tail = 0;
40         end IsEmpty;
41         
42      function isFull (Q : in Queue) return Boolean is
43         begin
44           return Q.tail = Q.capacity;
45         end isFull;
46         
47      end QueuesGeneric;
48         

 

You can download and compile  the body for queuesgeneric