gnatprove_13.2.1_28fc3583/share/examples/spark/spark_book/Chapter-02/enum_example.adb

 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
with Ada.Text_IO;          use Ada.Text_IO;
with Ada.Integer_Text_IO;  use Ada.Integer_Text_IO;
procedure Enum_Example with
  Annotate => (GNATprove, Might_Not_Return)
is
   --Declaration of three enumeration types
   type Day_Type is (Monday, Tuesday, Wednesday, Thursday,
                     Friday, Saturday, Sunday);
   type Traffic_Light_Color is (Red, Green, Yellow);
   type Pixel_Color         is (Red, Green, Blue, Cyan,
                                Magenta, Yellow, Black, White);

   package Day_IO is new Ada.Text_IO.Enumeration_IO (Day_Type);

   function Next_Day (Day : in Day_Type) return Day_Type is
   begin
      if Day = Day_Type'Last then
         return Day_Type'First;
      else
         return Day_Type'Succ (Day);
      end if;
   end Next_Day;

   Today    : Day_Type;
   Tomorrow : Day_Type;
   Count    : Integer;
begin
   Put_Line ("What day is today?");
   Day_IO.Get (Today);
   Tomorrow := Next_Day (Today);
   Put ("Tomorrow is ");
   Day_IO.Put (Item  => Tomorrow,
               Width => 1,
               Set   => Ada.Text_IO.Lower_Case);
   New_Line;

   if Today > Tomorrow then
      Put_Line ("Today must be Sunday");
   end if;
   New_Line;

   Put_Line ("The week days are ");
   for Day in Day_Type range Monday .. Friday loop
      Day_IO.Put (Day);
      New_Line;
   end loop;
   New_Line (2);

   for Color in Traffic_Light_Color loop
      Put_Line (Traffic_Light_Color'Image (Color));
   end loop;
   New_Line (2);

   Count := 0;
   for Color in Pixel_Color range Red .. Yellow loop
      Count := Count + 1;
   end loop;
   Put (Item => Count, Width => 1);
end Enum_Example;