home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog1 / chap01.txt < prev    next >
Encoding:
Text File  |  1991-07-01  |  11.7 KB  |  293 lines

  1.  
  2.  
  3.  
  4.  
  5.                                                         Chapter 1
  6.                                                   GETTING STARTED
  7.  
  8.  
  9. WHAT IS ADA?
  10. _________________________________________________________________
  11.  
  12. Ada is a relatively new programming language developed by the
  13. United States Department of Defense in an attempt to solve the
  14. software muddle as it existed in the mid 1970's.  It was felt that
  15. the 2000 or so programming languages in use at that time could be
  16. replaced in large part by one well planned language for use in
  17. embedded Real-Time systems.  Following a major effort on the part
  18. of the DOD, which is well documented in many other places, Ada was
  19. developed as a solution to the software problem.
  20.  
  21. Ada is a very well planned and precisely defined language that can
  22. be used throughout a wide area of software applications.  The
  23. language has existed long enough that a relatively large number of
  24. capable compilers exist for use on mainframe computers, as well as
  25. minicomputers, and even microcomputers.  An Ada compiler has a big
  26. job to do which you will see as we progress through our study of
  27. the language.  It is therefore not a trivial effort to bring a
  28. validated Ada compiler to market.  In spite of this, at least five
  29. companies have developed fully validated Ada compilers that run
  30. under MS-DOS on a PC.  Although some of these will run on a minimal
  31. PC, nothing less than a PC with an 80386 is recommended for use
  32. with any Ada compiler due to the time required for compilation.
  33.  
  34. The Ada programming language was designed in such a way that many
  35. of the trivial errors, which we humans are very capable of
  36. generating, are detected and reported at compile time rather than
  37. after execution of the program is begun.  It is at this point that
  38. errors are most easily repaired since a good compiler can give the
  39. programmer a very good hint at just what the error is.
  40.  
  41. This chapter will give you some definitions so we can begin
  42. discussing the use of Ada in chapter 2.  The definitions will be
  43. very broad in nature because they are used in many places in an Ada
  44. program, but they are extremely important.
  45.  
  46.  
  47.  
  48. WHAT IS AN IDENTIFIER?
  49. _________________________________________________________________
  50.  
  51. An identifier is a name we use to refer to any object in Ada and
  52. it must be formed by following some fairly rigid rules.  We will
  53. list the rules for forming a valid identifier, then make up a few
  54. for illustrative purposes.
  55.  
  56. 1.   An identifier must start with a letter of the alphabet.
  57.  
  58.                                                          Page 1-1
  59.  
  60.                                       Chapter 1 - Getting Started
  61.  
  62.  
  63. 2.   Following the initial letter, the identifier can be made up
  64.      of as many letters, numbers, and underlines as desired
  65.      provided that the underlines occur only singly, and an
  66.      underline is not the last character.
  67.  
  68. 3.   Case of letters is not significant.
  69.  
  70. 4.   There is no limit to the length of an identifier but each
  71.      identifier must fit on one line of text and the writer of the
  72.      compiler may impose a line length limit.
  73.  
  74. 5.   No blanks or special characters can be used as part of an
  75.      identifier.
  76.  
  77. With these rules in mind, lets make up a few good identifiers and
  78. a few invalid identifiers.
  79.  
  80.      Ada                -- A perfectly valid identifier
  81.      ADA                -- The same one, case doesn't matter
  82.      Ada_Compiler       -- A very descriptive identifier
  83.      The_Year_1776      -- Another descriptive identifier
  84.      a1b2c3d4e5f6       -- Very nondescript, but valid
  85.      12_times_each      -- Can't start with a number
  86.      This__is__neat     -- Multiple underlines illegal
  87.      This is neat       -- blanks illegal
  88.      Ada_"tutorial"     -- special characters illegal
  89.  
  90. By this time you should get the idea of what a valid Ada identifier
  91. is.  It may seem like a lot of effort to define just what an
  92. identifier is, but you will be very busy naming everything you use
  93. in Ada, so you must know how to name things before you can do
  94. anything meaningful with the language.
  95.  
  96.  
  97. IDENTIFIER SELECTION
  98. _________________________________________________________________
  99.  
  100. In addition to an identifier being correct, it should also be
  101. usable and meaningful.  As an example, consider the following list
  102. of valid identifiers and see which convey to you some idea of what
  103. they refer to.
  104.  
  105.      Time_Of_Day
  106.      Final_Score
  107.      Get_the Present_Temperature
  108.      X12
  109.      Ztx
  110.      t
  111.  
  112. Ada was designed to be written once and read many times.  This is
  113. truly what happens with any non-trivial program designed and
  114. developed by a group of persons.  As such, little attention is paid
  115. to the fact that it may be a bit tedious to key in long identifiers
  116.  
  117.                                                          Page 1-2
  118.  
  119.                                       Chapter 1 - Getting Started
  120.  
  121. when the program is being written.  The extra effort pays off when
  122. it is read repeatedly, since it is so easy to follow the logic of
  123. the program.  The first three identifiers above are preferred
  124. because of the information they convey to the reader, and the last
  125. three are to be considered of little value in defining the program
  126. logic.  Of course, if you were using a mathematical relationship
  127. that used the variable named "t" in its calculations, that
  128. particular name for a variable might be a good choice.
  129.  
  130.  
  131. WHAT ARE RESERVED WORDS?
  132. _________________________________________________________________
  133.  
  134. Ada uses 63 identifiers which are called reserved words.  They are
  135. reserved for specific uses within an Ada program and cannot be used
  136. for any other purpose.  As you study the language, you will see
  137. very clearly how to use each of the reserved words and why these
  138. particular words were chosen.  Since Ada is such a large language
  139. containing many options and cross checks, writing an Ada compiler
  140. is an enormous job, but the use of reserved words simplifies the
  141. compiler writers job.  The reserved words also make the final
  142. program much easier to read and understand.
  143.  
  144. Don't worry about the reserved words at this point.  It was
  145. necessary to mention that they do exist and constitute an
  146. additional limitation to the naming of identifiers which we
  147. discussed in the previous section.  It might be a good idea to
  148. spend a few minutes looking through the list in section 2.9 of the
  149. LRM (Language Reference Manual).  Note that all reserved words will
  150. be listed in boldface type when used in the text portion of this
  151. tutorial.
  152.  
  153.  
  154. CASE CONVENTIONS USED IN THIS TUTORIAL
  155. _________________________________________________________________
  156.  
  157. Ada allows you to use either case for alphabetic characters in an
  158. identifier and you can freely mix them up in any way you desire.
  159. Good programming practice, however, would lead you to select a
  160. convention for where to use upper case and where to use lower case.
  161. A good selection of case could be an aid to understanding the
  162. program since it would convey some information about what the
  163. identifier is.
  164.  
  165. In order to write the example programs in a standard format, the
  166. author did a search of Ada programs to see if a standard exists
  167. which would dictate which case should be used for alphabetic
  168. characters.  The search was conducted by studying the code in the
  169. three books mentioned in the Introduction to this tutorial and
  170. about 12 other books.  No conformance to any standard was found,
  171. so the following will be adopted for all of the sample programs in
  172. this tutorial.  Since you are just beginning to study Ada, you may
  173. not understand what each of the categories are.  After you complete
  174.  
  175.  
  176.                                                          Page 1-3
  177.  
  178.                                       Chapter 1 - Getting Started
  179.  
  180. a few of the lessons, you can return here to review the alphabetic
  181. case rules listed for the example programs.
  182.  
  183. reserved words - All reserved words will be written in lower case.
  184.       This is the only consistency found in the search of the Ada
  185.       programs.
  186.  
  187. Variables - All variables will be written with the initial letter
  188.       capitalized, and all others in lower case.
  189.  
  190. TYPES - All types will be written in all capital letters.
  191.  
  192. CONSTANTS - All constants will be written in all capital letters.
  193.  
  194. ENUM VALUES - All enumerated values will be written in all capital
  195.       letters.
  196.  
  197. ATTRIBUTES - All attributes will be written in all capital letters.
  198.  
  199. Procedure Names - All procedure names will be written with the
  200.       initial letter capitalized and all others in lower case.
  201.  
  202. Function Names - Same as procedure names.
  203.  
  204. Package Names - Same as procedure names.
  205.  
  206. Note that all program identifiers will be listed in boldface type
  207. when they are referred to in the text portion of this tutorial.
  208.  
  209.  
  210. WHAT ABOUT PROGRAMMING STYLE?
  211. _________________________________________________________________
  212.  
  213. Programming style can go a long way to aiding in the understanding
  214. of a completed program and much discussion throughout this tutorial
  215. will be given to style.  You have the freedom to add indentation
  216. and blank lines to your program in your own way to improve
  217. readability and at the same time make the program look like your
  218. own work.  In the early lessons, however, it would be to your
  219. advantage to follow the style given in the example programs and
  220. adopt it as your own.  As you gain experience, you will develop
  221. your own style of Ada source code formatting.
  222.  
  223.  
  224. PRELIMINARY DEFINITIONS
  225. _________________________________________________________________
  226.  
  227. Several topics, which are unique to Ada, are used in many places
  228. throughout the language.  Since a full definition of these will be
  229. impossible until we cover some of the earlier topics, we must delay
  230. the full definition until later.  On the other hand, the use of
  231. them becomes necessary fairly soon, so we will give a brief
  232. definition of these here, and a complete definition later.  If you
  233.  
  234.  
  235.                                                          Page 1-4
  236.  
  237.                                       Chapter 1 - Getting Started
  238.  
  239. don't fully understand these early definitions, don't worry about
  240. it yet, because we will return for a fuller definition later.
  241.  
  242. Exceptions - When most languages find a fatal runtime error, they
  243.       simply abort the program.  This is unacceptable for a real
  244.       time language because it must continue running, correcting the
  245.       error if possible.  An exception is an exceptional, or error,
  246.       condition that arises during execution of the program.  An Ada
  247.       program, if it is properly written, has the ability to define
  248.       what to do for each of these error conditions, and continue
  249.       operation.  Think of an exception as an error flag until we
  250.       define it more properly.
  251.  
  252. Renaming - Ada gives you, the programmer, the ability to assign a
  253.       new name to various entities in a program for your own
  254.       convenience.  Ada permits the renaming of objects, exceptions,
  255.       task entries, and subprograms.  It is simply an alias which
  256.       can be used to refer to the entity which is renamed.
  257.  
  258. Overloading - Ada allows you to use the same name for several
  259.       different items.  The system is smart enough to know which
  260.       entity you are referring to, when you use the overloaded name,
  261.       by the immediate context of its use.  For example, if I say,
  262.       "Jack used a jack to change the flat tire.", you understand
  263.       that there are two uses of the word "Jack", and you understand
  264.       what each means by the way it is used in the statement.  Ada
  265.       can also use the same name to refer to different things, and
  266.       intelligently know what the various uses mean.
  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.                                                          Page 1-5