Home

Introduction to Ada

 

Ada Fundamentals

 

Introduction

Ada (named after Ada Lovelace, a computer programmer (1815-1852)) is a computer language that can be used to write structured instructions that tell the computer what to do and when to do it.

Installing GNAT

To program in Ada, you need an Ada compiler. If you are involved in academics (student, teacher, etc), you can download it free from the GNAT programming studio that will provide you with an Ada compiler (you may have to create an account on the web site to be registered and activate the software). After downloading GNAT, you can install it. The default installation would be C:\GNAT\2009. The installation would create an icon for the GNAT programming studio. The icon is named GPS:

GPS icon

Creating a Project

After installing GNAT, you can start creating applications (you should first use a file utility, such as Windows Explorer, to create a folder that will contain the project; otherwise, the GPS can create the folder for you. For this project, we will use a folder named Exercise1 created in the C:\ drive).

To launch the GNAT programming studio, you can double-click the gps.exe icon. This would start with a splash screen:

GPS icon

After the GPS splash screen, the Welcome to GPS dialog box would display. To start a project, click the Create A New Project With Wizard radio button:

Welcome to GPS

Click OK. This would display the first page of the Create New Project wizard.

Create the Single Project radio button:

Create New Project

Click Forward.

In the second page of the Create New Project wizard, you must enter a name for the project. After entering the name of the project, you must specify the folder where the project will be stored:

  • You can use one of the folders created by the installation of the GNAT programming language. Normally, a default folder will be suggested and entered in the second text box. Otherwise, you can type it, or you can click the Browse button to locate and select an existing folder under the Drive:\GNAT directory
  • If you had already created a folder, enter its path in the Enter the Directory Where the Project File Will Be Created
  • If you had already created the folder but can't remember its path, you can click the Browse button to locate and select it
  • If you didn't create a folder yet, you can just enter its path in the Enter the Directory Where the Project File Will Be Created. If the folder exists and can be found, it will be recognized and used. If the folder doesn't exist, later on, the wizard will let you know and offer to create it

Create New Project

When you are ready, click Forward.

In the third page of the wizard, make sure (only) the Ada check box is checked:

Create New Project

Click Forward.

Create New Project

In the fourth page of the wizard, click Forward.

Create New Project

In the fifth page of the wizard, click Forward.

In the sixth page of the wizard, you must specify a folder for the build. You can use the same path you specified in the second page of the wizard. Otherwise, you can enter another. the scenario would the the same we reviewed in the second page:

Create New Project

Once you are ready, click Forward.

In the seventh page of the wizard, if you had already created an Ada source file, you can click Add to locate, select, and add it. If not, you don't have to do anything: you will add the files later on:

Create New Project

Once you are ready, click Forward.

Create New Project

In the eighth page of the wizard, click Forward.

Create New Project

In the ninth page of the wizard, accept the defaults and click Apply. If the path you specified in the second (and the sixth) page of the wizard exists already, the wizard would end and the project would be created. If the path is wrong, the wizard would display a message and ask if you want it to create the directory:

Create New Project

In this case, you can click Yes. The wizard would complete the project creation. When the project has been created, the GPS - GNAT Programming Studio would come up, divided in various windows. The lower-right window would indicate that the project doesn't have a source file:

GPS icon

 

 
 
 

Creating an Ada Source File

To make an Ada program useful, you must add a source file to it. To create a source file:

  • On the main menu, click File -> New
  • On the toolbar, click the Create a New File button Create a New File

This would create an empty file. In it, type the following:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Welcome is
begin
    Put_Line("Welcome to Ada Programming");
end Welcome;

GPS icon

After typing the code, you must save the document. To do this:

  • On the main menu, click File -> Save
  • On the toolbar, click the Save Current File button Save Current File
  • Press Ctrl + S

If the file had not been saved before, this action would display the Save File As dialog box. Locate and select the folder that contains the project. Normally, a name will be suggested for the file and it should have the .adb extension (otherwise, make sure you provide this information):

GPS icon

Once you are ready, click Save.

After creating a source file, you must add it to the project. To do this, on the main menu, click Project -> Edit Project Properties:

GPS icon

On the left list of buttons, click Main Files.

In the Main Files section, click Add. In the Select Files dialog box, click the check box of the file you had saved:

Select Files

Click OK:

Select Files

Click OK.

Building a Project

To build the project, on the main menu, you can click Build, position the mouse on Project, and click either Build <current file> or the name of the file

GPS - GNAT Programming Studio

This would display the Build Main dialog box:

Build Main

Click Execute. The compiler would build the executable and display the result in the lower-right window:

GPS - GNAT Programming Studio

Executing a Project

Once a project has been built, to see its result, you must execute it. To execute the application, on the main menu, click Build, position the mouse on Run, and click the name of the file:

GPS - GNAT Programming Studio

This would display the Arguments Selection dialog box:

Build Main

Click OK. The result would appear in the lower-right window:

GPS - GNAT Programming Studio

 

Accessories for Code

 

Comments

A comment is text that will not be considered when the project is built. Therefore, you can write anything you want as part of a comment.

A comment starts with --. Anything on the right side of that, up to the end of the line, is considered a comment.

Pragmas

A pragma is something that asks the compiler to take a certain action, such as to import something from an external file. This is referred to as a directive.

To create a pragma directive, start with the pragma keyword followed by the instruction.

  
 
 
   
 

Home Copyright © 2010 FunctionX, Inc. Next