Tuesday 2 July 2013

Learning Ada

I saw an article today about Ada which I have not looked at before, so I decided to try and write a couple of simple programs. It was not that difficult to write some very basic programs having seen Pascal and Oberon before.
The source code is stored in files matching the procedure name and ending in .adb "Ada Body" and to compile them I used gnatmake <filename>.
gnatmake is part of GNAT compiler tools and appears to be a front end to gcc.

Compulsory Hello World program

with Ada.Text_IO;
use Ada.Text_IO;

procedure Hello is
begin
  Put_Line("Hello World");
end Hello;



Program with Basic I/O (plus comments so I don't forget)

with Ada.Text_IO;
use Ada.Text_IO;
--following lines are needed for unbound string
with Ada.Strings.Unbounded;
use Ada.Strings.Unbounded;
with Ada.Strings.Unbounded.Text_IO;
use Ada.Strings.Unbounded.Text_IO;

procedure input is
   Buffer : Unbounded_String;
   --fixed length string
   --S: String(1..10) := (others => ' ');
   --Last: Natural;
begin
   Put("enter some text: ");
   --read in a fixed length string
   --Get_Line(S, Last);
   --read in entire line, careful what is done with it
   Buffer := get_line;
   --output our fixed length string, input truncated
   --Put_Line("text entered: " & S);
   --output unbound string
   Put_Line("text entered: " & Buffer);


end input;

 Note: Lines that start "--" are comments.