Saturday, August 11, 2012

AdaTutor - The Format of an Ada Program (2)

Capitalization and Spacing

with Ada.Text_IO;
procedure Hello is
begin
  Ada.Text_IO.Put_Line("Hello!");
end Hello;
Except in quoted Strings like "Hello!", capitalization isn't important in Ada.  The Ada 95 RM shows all reserved words in lower case and all identifiers with initial capitals, so that's the style we're using in our examples.  (The Ada 83 RM, by contrast, used all capitals for identifiers.  That style is rarely used today.)

In Ada, unlike Fortran, statements may begin anywhere on the line; columns aren't significant.  Most people indent the local declarations and the executable statements two or three spaces, as shown above.  Spaces and tabs may not appear within reserved words and identifiers, but they may freely be used between elements of the program.

Since a semicolon marks the end of a statement, we can put several statements on one line, like this:
     I : Integer;   X, Y : Float;
We can also continue a statement over several lines, breaking anywhere a space would be permitted, like this:
         Ada.Text_IO.
       Put_Line(
       "Hello!");

Many people follow the capitalization and spacing style of the Ada 95 RM, so we'll do that in this course.

Comments

with Ada.Text_IO;
procedure Hello is
  -- This program displays "Hello!" on the screen.
  I    : Integer; -- These declarations aren't needed; they
  X, Y : Float;   -- were added only to provide examples.
begin
  Ada.Text_IO.Put_Line("Hello!");
end Hello;
Comments in Ada begin with a double hyphen (double minus sign) and continue through the end of the line.  No space is permitted inside the double hyphen. As shown, a comment can be an entire line, or it can follow code on a line.  Comments spanning several lines require -- on each line.

Ada programs generally have less need for comments than programs written in other languages.  The reason is that, as we'll see, the identifiers may be as long as we like (up to the length of a line) and can have very meaningful names.  Also, when we learn about named notation, we'll see that calls to subprograms can be made much more readable in Ada than in other languages. These features and others tend to make Ada programs self documenting.

Identifiers

Here, reserved words are in lower case, and all other identifiers have initial capitals.  (The Ada reserved words, which may not be used to identify anything in our programs, are listed on page 7 of your printed course notes, and in section 2.9 of the Ada 95 RM.)  Ada identifiers may contain letters, digits, and underlines, but must start with a letter.  They may not contain spaces or other characters.  Identifiers may be as long as you like, up to the length of a line.  (The maximum line length depends on the particular implementation of Ada.)  All characters are significant, so This_Is_a_Long_Identifier_1 and This_Is_a_Long_Identifier_2 are distinct.  Since underlines are significant, A_3 is different from A3.  Every underline must be followed by a letter or a digit.  Thus, you can't have two underlines together, and an identifier can't end in an underline.

Question

  1. 1553B
  2. Begin
  3. THIS_IS_A_VERY_LONG_NAME_FOR_AN_IDENTIFIER
  4. SYS$QIO
  5. Number_Of__Targets
  6. Max Speed
Only one of the above is a legal Ada identifier. Which is it?

Numbers

In numbers, unlike identifiers, underlines are not significant.  Thus 12345 and 12_345 are equivalent.  Spaces aren't permitted within numbers.  When an underline appears in a number, it must be surrounded by digits.  Thus, all of these are illegal: 123_ 123_.0 _123 12__3

Floating point numbers must have at least one digit before and one digit after the decimal point.  Unlike Basic and Fortran, Ada forces us to write 1.0 instead of 1., and 0.5 instead of .5.  Scientific notation may be used: 1.3E-3 is the same as 0.0013.  Non-negative exponents may be used even in integers: 12e3 means 12_000.

Numbers may be specified in any base from 2 to 16, as shown below.  These three numbers are all equal:
     16#7C03#         2#0111_1100_0000_0011#         8#076003#

Although it's rarely done, a number with a base may have an exponent.  The number after the E is still written in decimal, and gives the power by which the base is raised.  Thus 2#110#E5 is 6 times 2**5, or 192.

< prev   next >

No comments: