Ada Distilled by Richard Riehle
Total Page:16
File Type:pdf, Size:1020Kb
Ada Distilled by Richard Riehle An Introduction to Ada Programming for Experienced Computer Programmers by Richard Riehle AdaWorks Software Engineering http://www.adaworks.com Copyright 2002, AdaWorks Software Engineering Public Edition. Permission to copy if AdaWorks is acknowledged in copies Version: July 2003 Page 1 of 113 Ada Distilled by Richard Riehle Acknowledgments There are always a lot of people involved in the creation of any book, even one as small and modest as this one. Those who have contributed to the best features of this book include my students at Naval Postgraduate School, Mr. Michael Berenato of Computer Sciences Corporation, Mr. Ed Colbert of Absolute Software, and many students from Lockheed-Martin Corporation, Computer Sciences Corporation, British Aerospace, various branches of the uniformed services, to name a few. I also owe a special thanks to Dr. Ben Brosgol, Dr. Robert Dewar, Mr. Mark Gerhardt, and Dr. Mantak Shing for what I have learned from them. Also thanks to the contributors to comp.lang.ada Usenet forum and the Team_Ada Listserve. Phil Thornley deserves extra credit for his detailed reading of the manuscript and many corrections. Special thanks goes to Ed Colbert for his careful study of some of my program examples. He is one of those people who can spot a program error at fifty paces. Using this unique skill, Ed brought many errors, some big and some small, to my attention. Also thanks to more recent input from Phil Thornley and Adrian Hoe. Any other errors are strictly mine. Any mistakes in wording, spelling, or facts are mine and mine alone. I hope this book will be valuable to the intended audience. It is moderate in its intent: help the beginning Ada programmer get a good start with some useful examples of working code. More advanced books are listed in the bibliography. The serious student should also have one of those books at hand when starting in on a real project. Richard Riehle Intended Audience for this Book This book is aimed at experienced programmers who want to learn Ada at the programming level. It is not a "...for dummies" book, nor is it intended as a program design book. Instead, we highlight some key features of the Ada language, with coded examples, that are essential for getting started as an Ada programmer. Ada is a rich and flexibile language used for designing large-scale software systems. This book emphasizes syntax, control structures, subprogram rules, and how-to coding issues rather than design issues. There are other really fine books available that deal with design. Also, this is not a comprehensive treatment of the Ada language. The bibliography lists some books that can help you once you have finished the material in this book. Think of this a quick-start book, one that enables you, the experienced programmer, to get into the Ada language quickly and easily. The examples use text-oriented programs for simplicity of learning. If you prefer to do the exercises for a GUI environment, check out the sites listed for compilers and tools so you can download bindings for programming in a variety of windowing environments, including Wintel, Linux, and Unix. Of particular interest to those wanting to explore Window style programming are GtkAda, CLAW, GWindows, and JEWL. The last of these, JEWL, is especially useful if you have no prior experience writing Windows programs. Happy Coding, Richard Riehle Page 2 of 113 Ada Distilled by Richard Riehle Table of Contents TABLE OF CONTENTS............................................................................................................................. 3 1. WHAT IS ADA DISTILLED? ................................................................................................................ 4 2. SUMMARY OF LANGUAGE................................................................................................................ 5 3. TYPES AND THE TYPE MODEL ...................................................................................................... 16 4. CONTROL STRUCTURES FOR ALGORITHMS............................................................................ 26 5. ACCESS TYPES (POINTERS) ............................................................................................................ 33 6. SUBPROGRAMS................................................................................................................................... 40 7. PACKAGE DESIGN.............................................................................................................................. 47 8. CHILD LIBRARY UNITS .................................................................................................................... 52 9. OBJECT-ORIENTED PROGRAMMING WITH PACKAGES....................................................... 57 10. USING STANDARD LIBRARIES AND ANNEXES ....................................................................... 64 11. EXCEPTION MANAGEMENT........................................................................................................ 71 12. GENERIC COMPONENTS................................................................................................................ 74 13. NEW NAMES FROM OLD ONES .................................................................................................... 82 14. CONCURRENCY WITH TASKING................................................................................................. 87 A. ANNEXES, APPENDICES AND STANDARD LIBRARIES.......................................................... 92 ANNEX L PRAGMAS - LANGUAGE-DEFINED COMPILER DIRECTIVES............................... 104 WINDOWS 95/98/NT/XP/ME/2000 PROGRAMMING ...................................................................... 105 C. BIBLIOGRAPHY............................................................................................................................... 107 Author's Observation and Opinion The Ada language is designed to maximize the possibilities for error detection early in the development process. This reduces the overall cost of software development since it is known that errors corrected early, will cost less than those detected late. No other language, not C++, not Java, not Eiffel, provides the level of support for safety-critical software found in Ada. Developers can and do make mistakes, even using Ada, but the probability of such mistakes is substantially less when using Ada than it is when using other languages. Therefore, when failure is not acceptable, during development or during software execution, the wise developer will select Ada. Although Ada is not as popular as some of the more well-known languages, it has substantial benefits over most of them when software dependability is a primary objective. In addition, contemporary Ada is as appropriate for developing GUI-based applications, information systems applications, and computationally intensive systems as any existing language. It is truly a general-purpose language, well-suited to any kind of software problem you might encounter. Page 3 of 113 Ada Distilled by Richard Riehle 1. What is Ada Distilled? This book is for experienced programmers new to Ada. Heavily commented example programs help experienced programmer experiment with Ada. This is not a comprehensive book on the entire Ada language. In particular, we say very little about Ada.Finalization, Storage Pool Management, Representation Specifications, Concurrency, and other more advanced topics. Other books, listed in the bibliography, cover advanced topics. This book is an entry point to your study of Ada. The text is organized around example programs with line-by-line comments. Ada comments are the double-hyphen and continue to the end of a line. Comments might be explanatory notes and/or corresponding section of the Ada Language Reference Manual (ALRM) in the format of ALRM X.5.3/22. with Ada.Text_IO; -- 1 10.1.2, A.10 Context clause A hello program in Ada. This will compile procedure Hello is -- 2 6.3 Specification with "is" and execute with begin -- 3 6.3 Start algorithmic code any Ada compiler. Ada.Text_IO.Put_Line(“Hello Ada”); -- 4 A.10.6 Executable source code end Hello; -- 5 6.3 End of procedure scope where each line is numbered. The 10.1.2 and 6.3, etc. refer to ALRM Chapters 6.3, 10.1.2. A.10.6 refers to Annex A.10.6. There is occasional commentary by source code line number. The line numbers are not part of Ada, but provided on our examples for ease of commentary. Boxed notes are also included for some examples. The preceding example shows one of these as: 1.1 Ada Compilers and Tools Ada 95 compilers support a wide range of platforms. A free, open source, compiler, GNAT, based on GNU technology, can be downloaded from the Web. Commercial Ada compilers and tools are available from: Ada Core Technologies (GNAT), DDC-I, Rational (recently acquired by IBM), RR Software, Irvine Compiler Corporation, Green Hills, SofCheck, Aonix, and OC Systems. Free editors, including AdaGide, are useful for developing small Ada programs. More information on tools, including GUI development tools, can be accessed using one of the URL's mentioned in the introduction and in the bibliography. Development tools exist for many operating systems. These include CLAW for Microsoft Operating Systems, and GtkAda for other GUI environments including Microsoft operating systems, Linux, BSD, OS/2, Java Virtual Machine, and every variety of Unix. The AdaGide editor is available for MS Windows. 1.2 Ada Education The bibliography of this book