Ada95

GNAT Ada95 compiler at Charlie, Illinois Institute of Technology

Written By Chia-Tien Dan Lo, 1999

What is GNAT? 

GNAT is a complete Ada95 compilation system, maintained and distributed under the Gnu Public Licence by Ada Core Technologies. ACT and ACT-Europe offer commercial support for industrial and academic users of GNAT. For more info: web site http://www.gnat.com. 

What version of GNAT at IIT?

GNAT 3.0

Your first Ada program at Charlie.
Hello World: print out a string!
Purpose of practical
The purpose of this practical is to gain experience in the use of the gnu Ada compiler and familiarize yourself with basic syntax. 
Example 1: Source Code of Hello world program 

-- hello world program -- This is a comment
WITH ada.text_io;       -- ADA is not case sensitive!
USE ada.text_io; -- auto search the put_line function;
PROCEDURE hello_world IS
BEGIN
  put_line("Welcome to Ada"); -- if no USE, ada.text_io.put_line("..") 

                              -- see Example 2 

END hello_world; 

  1. Use emacs or other word processors to write the hello_world program and make sure to name the file hello_world.adb
  2. File names must be the same as the name of the procedure for the main program body and have an extension .adb . 
  3. Compile the program with the command gcc -c hello_world.adb. This produces two files hello_world.o and hello_world.ali. 
  4. Link the object module with the command gnatbl hello_world.ali. This produces an executable file hello_world which can be executed by typing its name.
  5. You can also use gnatmake hello_world.adb to compile and link it at once. Gnatmake is a tool similar to make.
Example 2: Hello world program without using USE
-- hello world program -- This is a comment
WITH ada.text_io;      
PROCEDURE hello_world1 IS
BEGIN
  ada.text_io.put_line("Welcome to Ada"); -- use the full package name  
END hello_world;

 

Example 3: Private procedure and exit from loop; source code

 

-- Demonstrate a trivial procedure, with another nested inside.
with Ada.Text_IO, Ada.Integer_Text_IO; -- basic declaration
use Ada.Text_IO, Ada.Integer_Text_IO;  -- you'd better use these

procedure Compute is -- procedure in Ada likes main() in C

procedure Double(Item : in out Integer) is    -- local procedure definition
begin -- procedure Double.                    -- in: can not be modified
  Item := Item * 2;                           -- out: can be modified  
end Double;                                   -- in out: both directions

X : Integer := 1;                        -- Local variable X of type Integer.

begin -- procedure Compute               -- entry point for the process
  loop                                   -- loop forever
    get(X);                              -- read an integer value from console 
    New_Line;                     -- print new line; like printf("\n") in C
    Double(X);                    -- call double(x);   
    Put(X);                       -- print x; like printf("%d", x) in C
    exit when x=0;                -- exit when x equals 0
  end loop;                                        
end Compute;
Example 4: Float point test, Ada is stong typed language. Source code
with ada.text_io, ada.integer_text_io, ada.float_text_io;
use ada.text_io, ada.integer_text_io, ada.float_text_io;
procedure float_test is
  i1, i2: integer:= 0;
  f1, f2: float:= 0.0;
begin
  -- Ada is strong typed!
  i1 := (i2+1)*5;
  f2 := 1.0*8.0;
  f1 := float(i1)+f2;
  put_line("Output result f1 = ");
  put(f1); new_line;
end float_test;

            What would be the output?

Example 5:  Loop test. for loop, for loop reverse, and while loop. Source code

with ada.text_io, ada.integer_text_io;
use ada.text_io, ada.integer_text_io;
procedure loop_test is
  i: integer:= 0;
begin
  -- for loop forward
  put_line("For loop forward"); new_line;
  for i in 1..10  -- i is valid inside the loop only
  loop
    put(i);
    new_line;
  end loop;


  -- for loop reverse
put_line("For loop reverse"); new_line;
for i in reverse 1..10  -- i goes from 10 to 1
loop
  put(i);
  new_line;
end loop;


-- while loop reverse
put_line("while loop"); new_line;
while i < 10 
loop
  put(i); 
  new_line;
  i := i +1;
end loop;
end loop_test;

 


Example 6: Creating Ada task. Source dode

with ada.text_io, ada.integer_text_io;
use ada.text_io, ada.integer_text_io;
procedure vending is
  task vending_machine is
    entry insert(coin: in integer);
    entry push(change, goods:out integer);
  end vending_machine;


  task body vending_machine is
    items: integer:=50;
    price: integer:= 25;
    paid: integer:=0;
  begin
  loop -- forever
    select -- one of insert or push
      accept insert(coin: in integer) do
        paid := paid + coin;
      end insert;
    or
      accept push(change, goods: out integer) do
        if items > 0 and paid >= price then
          change := paid - price;
          goods := 1; items := items - 1;
        else
          change := paid; goods:=0;
        end if;
      end push;
      paid := 0;
  end select;
end loop;
end vending_machine;


change, goods: integer:= 0;                   -- local variables
begin                                         -- main entry point
  vending_machine.insert(50);                 -- call insert routine
  vending_machine.push(change, goods);
  put(change); new_line;
  put(goods); new_line;
end vending;

 

Example 7: The example will test "terminate" function in selective accept structure. If all clients terminate, the server then terminates as well. It is worth pointing out that "or terminate" doesn't mean the server can terminate selectively. Here is the source code.   

 

-- part of the program; click source code for complete version

loop

  select

    when ready_p => accept pizza; -- release connection right away!

      put_line("This is in Pizza.");

      if counter = 9 then -- clean up every 10 services

        mass:= true;

        ready_p:= false;

        counter := 0;

      else

        counter := counter +1;

      end if;

  or

    when true => accept cola;

    put_line("This is in cola.");

    if counter = 9 then

      mass:= true;

      counter := 0;

    else

      counter := counter +1;

    end if;

  or

    terminate; -- terminate if all clients are terminated!

  end select;

  if mass then -- check if clean up is needed!

    put_line("This is in clean_up.");

    mass:= false;

    ready_p:= true;

    counter:= 0;

  end if;

end loop;

 

Example 8: The example shows that a client can terminate a server explicitly. When a client finishes its job, it sends a termination signal to its server. When the server receives the termination signal, it accepts it and terminates right away. Here is the source code.
Example 9: There is an issue: how can a server know all its clients are terminated? The answer is the run-time system will provide the clients' information to the sever. The example will examine the fact that user can decide to terminate a server at run-time. Here is the source code.
Example10: Selective accept construct in Ada allows "else" part to be executed while all the barriers are false. So the server will keep running even though there is no request for it. For the performance issue, we don't want this kind of busy-waiting structure because the CPU resource should be shared among other threads. Here is a version of program that uses "else" structure. After compiling the program, you will find it not working. This just gives you a sense that the task is occupying CPU all the time in doing nothing! The problem will be solved in next example. Here is the source code
Example 11: By adding a statement "delay 0.0", we can fix the bug occurred the previous example. The delay statement in Ada is similar to the sleep in C. It lets the running process go to sleep and yields CPU to other threads. In the example, we let the server process sleep for 0.0 second (more precisely say, sleep at least 0.0 second), so that the main process can get execution for CPU. Hence the rendezvous can be established. By contrast, in the previous example, the main process hasn't have a change to issue the rendezvous request after the server starts. Here is the source code.
Example 12: The example tries to improve system performance by adding a "or delay 1.0" in selective accept structure. While there is no request for clients, the server can go to sleep for 1.0 second. On the other hand, if there is some request, the server must not go to sleep and must serve the clients. It is worth noting that the syntax of the "or delay 1.0" statement in Ada conflicts its semantics. Here is the source code.  

Last modified by Dan Lo @ 09/17/02 10:51:04 AM

Hit Counter

Home Up