1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
|
<!--startcut ==============================================-->
<!-- *** BEGIN HTML header *** -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML><HEAD>
<title>Introduction to Programming Ada LG #81</title>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#0000AF"
ALINK="#FF0000">
<!-- *** END HTML header *** -->
<CENTER>
<A HREF="http://www.linuxgazette.com/">
<IMG ALT="LINUX GAZETTE" SRC="../gx/lglogo.png"
WIDTH="600" HEIGHT="124" border="0"></A>
<BR>
<!-- *** BEGIN navbar *** -->
<IMG ALT="" SRC="../gx/navbar/left.jpg" WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="bottom"><A HREF="adam.html"><IMG ALT="[ Prev ]" SRC="../gx/navbar/prev.jpg" WIDTH="16" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="index.html"><IMG ALT="[ Table of Contents ]" SRC="../gx/navbar/toc.jpg" WIDTH="220" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../index.html"><IMG ALT="[ Front Page ]" SRC="../gx/navbar/frontpage.jpg" WIDTH="137" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="http://www.linuxgazette.com/cgi-bin/talkback/all.py?site=LG&article=http://www.linuxgazette.com/issue81/adam2.html"><IMG ALT="[ Talkback ]" SRC="../gx/navbar/talkback.jpg" WIDTH="121" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../lg_faq.html"><IMG ALT="[ FAQ ]" SRC="./../gx/navbar/faq.jpg"WIDTH="62" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="arndt.html"><IMG ALT="[ Next ]" SRC="../gx/navbar/next.jpg" WIDTH="15" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><IMG ALT="" SRC="../gx/navbar/right.jpg" WIDTH="15" HEIGHT="45" ALIGN="bottom">
<!-- *** END navbar *** -->
<P>
</CENTER>
<!--endcut ============================================================-->
<H4 ALIGN="center">
"Linux Gazette...<I>making Linux just a little more fun!</I>"
</H4>
<P> <HR> <P>
<!--===================================================================-->
<center>
<H1><font color="maroon">Introduction to Programming Ada</font></H1>
<H4>By <a href="mailto:thomas_adam16@yahoo.com">Thomas Adam</a></H4>
</center>
<P> <HR> <P>
<!-- END header -->
<H2>Introduction</H2>
<P>I'm quite old-fashioned when it comes to computers. I am one of these
people whom prefers working at a command-line driven interface rather than
a GUI. So it should not come as a shock to you that many of the
programming languages I have experimented in are also based on <I>textual
</I> input / output. <B>Ada</B>, along with <B>Perl, Bash, Sed, Awk,
C</B>, etc is no such exception.</P>
<P>Over the years, there have been quite a few programming languages
mentioned in the <I>Linux Gazette</I>. Ben Okopnik has done two
<B><I>very</I></B> good tutorials on both <B>Perl</B> and <B>Bash</B>,
and other people have contributed to describing other languages, such as:
<B>Smalltalk, C++, Python</B>. Over the next couple of months, I shall
be writing a series of articles to do with programming in Ada.</P>
<H2>What is Ada?</H2>
<P>Glad you asked :-) Originally <B>Ada</B> was a US governmental (DoD)
developed programming language. The standard was originally known as
Ada83, but this is now obsolete, as it was recently "overhauled"
and re-born as <B>Ada95</B>. This is now the preferred standard and
implementation of the Ada programming language.</P>
<P>In 1983, Ada was standardised by ANSI. Thus, it went through all the
official motions and in that year, that first edition was released. Then
four years later in 1987, ISO released an equivalent standard. At this
time though, the idea of so called OOP (Object-Orientated Programming) was
a concept that had not really been considered.</P>
<P>Ada however, was not designed by a committee. The original design was
implemented by Jean Ichbiah, who won a language design competition. Then
in 1995, Tucker Taft led a small group of developers and Ada95 was born.
Unlike the previous version (Ada83), the implementation of Ada95 (or
Ada9X, as it is sometimes known) underwent a public "benchmark"
test; whereby testers of the language gave their feedback, and suggestions
to make the syntactical and lexicographical layout more efficient.</P>
<P>The name <B>Ada</B> is attributed to a woman called <B>Ada Loveless</B>
(1815-1852) who is considered to be the world's first programmer. Ada
is used in all sorts of situations, and since it is a <B>concurrent</B>
programming language, it is most commonly used in <B>embedded systems</B>.
Ada has been used in some of the following:</P>
<UL>
<LI>Airplanes</LI>
<LI>Air Traffic Control systems</LI>
<LI>Financial Systems</LI>
<LI>Telecommunications Systems</LI>
<LI>Medical Devices</LI>
</UL>
<P>The list however is by no means exhaustive :-)</P>
<H2>Ada Compilers</H2>
<P>Unlike other scripting programming languages (Perl, Bash, Python, tcsh,
etc), Ada like C is compiled rather than interpreted. This means that
the person that is going to run the program does not need the
interpreter installed to use it. Ada programs are therefore standalone
from having any kind of Ada packages installed. <I>[Unless you have used
pragmas to interface with other languages, like C, in which case you might
have libraries, but more on that later -- TA]</I></P>
<P>The Ada compiler that I recommend to you is called <B>GNAT</B>, which
stands for: <B>GNU</B> <B>NYU</B> (New York University) <B>A</B>da
<B>T</B>ranslator. It is free (GNU license :-), and there is a wealth of
information on it. It is based on the <B>gcc</B> compiler, which has had
the Ada syntax bundled in with it.</P>
<P>It is available from the following website, which then has a link to
the GNAT compiler:</P>
<A HREF="http://www.gnuada.org/alt.html"
TARGET="_blank">www.gnuada.org/alt.html</A>
<P>A word of caution here. I recommend that you download a pre-compiled
binary version of GNAT, and use the package <B>alien</B> if need be to
convert to .DEB .RPM .TGZ, etc. The reason I say this, is because you will
need an Ada version of <B>gcc</B> (often called <B>gnatcc</B>) to bootstrap
the compiler. If this is the first time you are installing GNAT then the
compilation from source code will not be possible.</P>
<P>That said, you can then go ahead and install the package once you have
downloaded it. You'll find that with the RPM version of GNAT, that there
should be one single RPM: "GNAT-3.13p-7.rpm" which will contain
everything you need to start programming in this wonderful language
:-).</P>
<H2>Ada IDE's</H2>
<P>Before we start our first program in Ada, I thought it would be good to
make you aware of some of the IDE's (<B>I</B>ntegrated <B>D</B>evelopment
<B>E</B>nvironment). These are programs which help you to program in the
specified language by offering features such as: </P>
<UL>
<LI>Syntax Highlightling</LI>
<LI>Compiler Support</LI>
<LI>Keyword Definitions</LI>
<LI>Pre-defined Templates</LI>
</UL>
<P>The two that I would recommend to you are:-</P>
<P><B>TIA</B> (TIny Ada) -- a console based IDE written in Ada and is built
around the use of GNAT</P>
<P><B>GRASP</B> -- an X11 IDE which supports among other languages,
Ada!</P>
<P>For all you EMACS fans, there is an extension called <B>Glade</B> which
is installed as part of the main GNAT distribution. This is an EMACS
extension which supports the GNAT compiler, syntax highlighting, etc. More
information on that can be found at <A
HREF="http://www.gnuada.org">gnuada</A> website</P>
<P>You do not have to use and IDE at all to be able to use GNAT.
I actually don't bother, and instead use <B>jed</B> if I am at the
console (although this does not yet support Ada syntax highlighting) and
<A
HREF="http://www.linuxgazette.com/issue79/adam.html#prognedit"
TARGET="_blank"><B>Nedit</B></A> if I am in X11. This <I>does</I> support
Ada syntax highlighting :-)</P>
<H2>The Features of Ada</H2>
<P>Ada95 has been greately enhanced over its predecesor Ada83. The biggest
improvement has been <B>object orientation</B>, which many people will
find useful. Some of the features that Ada has are:</P>
<UL>
<LI>Packages (Modules), related types, objects can all be defined</LI>
<LI>Packages and data types can be made generic</LI>
<LI>Data representation gives a means to support systems
programming</LI>
<LI>Object-orientated programming is supported</LI>
<LI>Errors can be caught and delt with explicitly</LI>
<LI>Tasks (multiple paralell threads) can be created and used. This
ability is inherent to Ada95 and is uncommon is most other
langugaes</LI>
<LI>Interfaces to other languages (such as: C, Fortran, COLBOL) can be
included in the language</LI>
<LI>Ada supports hard <B>types</B> of data, both for textual and
mathematical Input/Output. As a result, Ada is well suited to writing
and maintaining complex systems</LI>
</UL>
<P>In addition to the above, there are also:</P>
<UL>
<LI>Packages</LI>
<LI>Functions</LI>
<LI>Procedures</LI>
<LI>Task Units</LI>
<LI>Records</LI>
<LI>Arrays</LI>
</UL>
<P>And many more....</P>
<H2>Hello World!</H2>
<P>Now it's time to write our first ada program. In time-honoured
tradition, we are going to start by writing a <I>Hello World</I>
example. Open up a text editor, and type in the following:</P>
<TABLE BORDER="1">
<TBODY>
<TR COLSPAN="12">
<TD>
<PRE>
<FONT COLOR="red">with</FONT> text_io;
<FONT COLOR="red">use</FONT> text_io;
<FONT COLOR="red">procedure</FONT> hello_world <FONT COLOR="red">is </FONT>
<FONT COLOR="red">begin</FONT>
<FONT COLOR="red"> put</FONT>("Hello World!");
<FONT COLOR="red">end</FONT> hello_world;
</PRE>
</TD>
</TR>
</TBODY>
</TABLE>
<P>Easy, isn't it :-). Before we can run the program, we have to save it.
But it has to be with the correct suffix. GNU/Linux doesn't require any
suffix (file extension) as a rule, but it is <B>essential</B> when
programming in <B>Ada</B> and using the GNAT compiler. A list of valid
extensions are:</P>
<UL>
<LI>.ads - Ada package specification</LI>
<LI>.adb - Ada package body or program</LI>
</UL>
<P>When writing anything other than a package (which we won't be doing for
<I>some</I> time yet -- I can assure you) :-) you should append a
".adb" extension to your filename. This is so that the compiler
knows that the file it is compiling is a program and not a package
specification!</P>
<P>So, save your file as <B>hello_world.adb</B>
<P>Now we are ready to start to compile / build the program. This has to
be done so that we can run it. You cannot run an Ada program until it has
been compiled and built.</P>
<P>Change to the directory that you have just saved the file, and issue
the command:</P>
<PRE>
gnatmake hello_world.adb
</PRE>
<P>This will compile -> link -> build your Ada code into a compiled
program.</P>
<P>Now if you type in:</P>
<PRE>
./hello_world
</PRE>
<P>the response:</P>
<PRE><B>hello world!</B></PRE>
<P>is output to the screen, and the program exits.</P>
<P>You should also have noticed that as you issued the command, the following
output was produced:</P>
<PRE>
gnatgcc -c hello_world.adb
gnatbind -x hello_world.ali
gnatlink hello_world.ali
</PRE>
<P>You could, if you wish, type each of the above commands in turn to both
<B>compile, bind and link</B> your program (respectively). Luckily
<B>gnatmake</B> provides a nice automation for this :-). If you now look
in your directory, along with the main program, you'll find that GNAT has
created other files too, namely:</P>
<UL>
<LI>hello_world.ali</LI>
<LI>hello_world.o</LI>
</UL>
<P><B>.ali</B> files are GNAT link files that contain information about
debugging and linking for the main program</P>
<P><B>.o</B> files are object files which can be used in conjunction with
the program-debugger: <B>gdb</B>.</P>
<P> In short, unless you plan to debug your program, you can delete these files.</P>
<H2>Explanation: Hello World</H2>
<P>In perl, you can issue a command such as: <B>print("Hello");</B> and
that can be the <B><I>only</I></B> line in your program (excluding the
she-bang line), and it will run.</P>
<P>Ada however, has to be told exactly which packages it is to use before
it can perform even the simplest of commands like echoing statements to
the VDU. A <B>package</B> is a collection of functions and procedures that
perform specific tasks. If you do not declare explicitly these at the
start of the program, GNAT, when it comes to compile your program, will
bomb out immediately.</P>
<P>Therefore, if we wish to read and write I/O (Input/Output) to a screen
terminal, this has to be stated. All I/O functions are found within the
package <B>text_io</B>, and the first two lines within our hello_world
example are crucial....</P>
<PRE>
<FONT COLOR="red">with</FONT> text_io;
<FONT COLOR="red">use</FONT> text_io;
</PRE>
<P>The <FONT COLOR="red">with</FONT> statement in Ada indicates that we
will be requiring the use of the named package, in this case
<B>text_io</B>. If more than one package is required then this can be
added, by separating each package name by a comma (,). When we have
finished, we must append a semi-colon (;) to the end of the line, similiar
to that of Perl. The <FONT COLOR="red">with</FONT> statement is a
mandatory command that must <B>ALWAYS</B> be present at the start of your
program in order for it to work.</P>
<P>The package <B>text_io</B>, as I have already stated allows I/O
functions / procedures. This involves printing messages to the screen,
allowing user input to be entered, etc. It is a package that is used in
virtually every program you will ever write in Ada.</P>
<P>The <FONT COLOR="red">use</FONT> statement MUST be used only after the <FONT
COLOR="red">with</FONT> statement has been made. It allows for unqualified
references to be made to procedures and functions from other packages.
Without the use of this statement, each procedure or function call must
have the name of the package that it belongs to, followed by a period
(full stop) preceeding it. For example, below is what the
<B>hello_world</B> program would look like <B>without</B> the <FONT
COLOR="red">use</FONT> statement.</P>
<TABLE BORDER="1">
<TBODY>
<TR COLSPAN="12">
<TD>
<PRE>
<FONT COLOR="red">with</FONT> text_io;
<FONT COLOR="red">procedure</FONT> hello_world <FONT COLOR="red">is </FONT>
<FONT COLOR="red">begin</FONT>
<FONT COLOR="red"> text_io.put</FONT>("Hello World!");
<FONT COLOR="red">end</FONT> hello_world;
</PRE>
</TD>
</TR>
</TBODY>
</TABLE>
<P>You can see how this has increased the amount of information that we
have to type in, without the use of the <FONT COLOR="red">use</FONT>
statement. When more than one package is used that might have the same
procedure or function names, the compiler can usually tell to which
package you are referring, based on the parameters passed to it.</P>
<P>The third line:</P>
<PRE>
<FONT COLOR="red">procedure</FONT> hello_world <FONT COLOR="red">is </FONT>
</PRE>
<P>declares that we are writing a new procedure with the name
<B>hello_world</B>. The statement word <FONT COLOR="red">is</FONT> tells
us that we are about to start the declarative section of the procedure,
more on that later.</P>
<P>The keyword <BR><BR>
<FONT COLOR="red">begin</FONT><BR><BR> then tells us that we are going to
start the executable part of the procedure -- i.e. where all the
statements will appear and be executed, which in this case is:</P>
<PRE>
<FONT COLOR="red">put</FONT>("Hello World!")</FONT>
</PRE>
<P>Which calls the procedure <FONT COLOR="red">put</FONT> from the package
<B>text_io</B> to print the message <B>Hello World!</B> on the screen.</P>
<P>The last line:</P>
<PRE>
<FONT COLOR="red">end</FONT> hello_world;
</PRE>
<P>simply just ends the named procedure.</P>
<P>In short, the basic structure for an Ada program looks like the
following:</P>
<TABLE BORDER="1">
<TBODY>
<TR COLSPAN="12">
<TD>
<PRE>
<FONT COLOR="red">with</FONT> text_io;
<FONT COLOR="red">use</FONT> text_io;
<FONT COLOR="red">procedure</FONT> program_name <FONT COLOR="red">is </FONT>
[ declaritive part here ]
<FONT COLOR="red">begin</FONT>
[ executable section here ]
<FONT COLOR="red">end</FONT> program_name;
</PRE>
</TD>
</TR>
</TBODY>
</TABLE>
<P>Also within the package <B>text_io</B> are commands such as:</P>
<PRE><FONT COLOR="red">
put
put_line
get
get_line
new_line
</FONT></PRE>
<P>Plus many others...</P>
<P>
<FONT COLOR="red">put</FONT> does what we have already seen.<BR>
<FONT COLOR="red">put_line</FONT> does the same as <FONT
COLOR="red">put</FONT>, except starts on a new line.<BR>
<FONT COLOR="red">new_line</FONT> is a command issued on its own, which
starts a new line. If you use it, make sure that you put a semicolon at the
end of it, like:<BR><BR>
<FONT COLOR="red">new_line;</FONT><BR><BR>
In fact, that statement about the
semicolon (;) goes for each command that you make in Ada.
</P>
<P>Next month, we will be looking at:</P>
<UL>
<LI>text_io (inputting data via the <FONT COLOR="red">get</FONT>
statement)</LI>
<LI>data types</LI>
<LI>simple arithmetic expressions</LI>
<LI>enumeration types</LI>
</UL>
<H2>Exercises</H2>
<P>Well, that is all for this month. I'm sorry if it seems like I'm not
explaining enough things all in one go, but trying to explain anything
more at this point, is I think overload. So, I am going to leave you with
a few exercises for you to try.....</P>
<P>1. Print your name on the screen</P>
<P>2. Print your address on the screen, using only <FONT
COLOR="red">put</FONT> and <FONT COLOR="red">new_line</FONT></P>
<P>3. Repeat exercise 2, this time with <FONT COLOR="red">put_line</FONT>
<P>If you submit them to me, I will print them in my next installment of
this article!</P>
<P>As with all of my articles, if you have any questions, suggestions,
rants or raves (hopefully not complaints :-) drop me a line!!</P>
<HR WIDTH=25%>
<PRE>
with text_io, ada.integer_text_io;
use text_io, ada.integer_text_io;
procedure happy_programming is
loop_number : integer :=0;
begin
while loop_number /= 10 loop
loop_number := loop_number + 1;
put("Happy Programming in Ada");
new_line;
end loop;
end happy_programming;
</PRE>
<!-- *** BEGIN bio *** -->
<SPACER TYPE="vertical" SIZE="30">
<P>
<H4><IMG ALIGN=BOTTOM ALT="" SRC="../gx/note.gif">Thomas Adam</H4>
<EM>My name is Thomas Adam. I am 18, and am currently studying for A-Levels
(=university entrance exam). I live
on a small farm, in the county of Dorset in England. I am a massive Linux
enthusiast, and help with linux proxy issues while I am at school. I have been
using Linux now for about six years. When not using Linux, I play the piano,
and enjoy walking and cycling.</EM>
<!-- *** END bio *** -->
<!-- *** BEGIN copyright *** -->
<P> <hr> <!-- P -->
<H5 ALIGN=center>
Copyright © 2002, Thomas Adam.<BR>
Copying license <A HREF="../copying.html">http://www.linuxgazette.com/copying.html</A><BR>
Published in Issue 81 of <i>Linux Gazette</i>, August 2002</H5>
<!-- *** END copyright *** -->
<!--startcut ==========================================================-->
<HR><P>
<CENTER>
<!-- *** BEGIN navbar *** -->
<IMG ALT="" SRC="../gx/navbar/left.jpg" WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="bottom"><A HREF="adam.html"><IMG ALT="[ Prev ]" SRC="../gx/navbar/prev.jpg" WIDTH="16" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="index.html"><IMG ALT="[ Table of Contents ]" SRC="../gx/navbar/toc.jpg" WIDTH="220" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../index.html"><IMG ALT="[ Front Page ]" SRC="../gx/navbar/frontpage.jpg" WIDTH="137" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="http://www.linuxgazette.com/cgi-bin/talkback/all.py?site=LG&article=http://www.linuxgazette.com/issue81/adam2.html"><IMG ALT="[ Talkback ]" SRC="../gx/navbar/talkback.jpg" WIDTH="121" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../lg_faq.html"><IMG ALT="[ FAQ ]" SRC="./../gx/navbar/faq.jpg"WIDTH="62" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="arndt.html"><IMG ALT="[ Next ]" SRC="../gx/navbar/next.jpg" WIDTH="15" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><IMG ALT="" SRC="../gx/navbar/right.jpg" WIDTH="15" HEIGHT="45" ALIGN="bottom">
<!-- *** END navbar *** -->
</CENTER>
</BODY></HTML>
<!--endcut ============================================================-->
|