gnatprove_11.2.3_f7ece6d3/share/examples/spark/spark_book/Chapter-02/control_structures.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
 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
with ADC;
with DAC;
with Ada.Text_IO;
with Ada.Integer_Text_IO;
with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions;

procedure Control_Structures is

   subtype Uppercase is Character range 'A' .. 'Z';

   A, B, C       : Integer;
   D, E, F, G, H : Integer;
   Ch            : Character;
   Temperature   : Integer;
   Valve_Setting : Integer;
   Value         : Integer;
   Sum           : Integer;
   Success       : Boolean;
   Approx, X, Y  : Float;
   Tolerance     : constant Float := 0.0001;
   Letter        : Uppercase := 'Q';

   procedure Calculate_Valve (Current_Temp : in  Integer;
                              New_Setting  : out Integer) is
   begin
      New_Setting := 2 * Current_Temp;
   end Calculate_Valve;

   function Double (First  : in Integer;
                    Second : in Integer) return Boolean is
      (abs Second = 2 * abs First);


begin
   A := 1;
   B := 2;
   C := 3;
   D := 3;
   E := 4;
   Ch := 'D';
   X := 123.4546;


   if A < 0 then
      A := -A;
      D := 1;
   end if;

   if A in 1 .. 12 then
      B := 17;
   end if;

   if A > B then
      E := 1;
      F := A;
   else
      E := 2;
      F := B;
   end if;


   if A = B then
      F := 3;
   elsif A > B then
      F := 4;
   else
      F := 5;
   end if;


   if A > B and A > C then
      G := 6;
   elsif B > A and B > C then
      G := 7;
   elsif C > A and C > B then
      G := 8;
   end if;

   Success := True;
   case Ch is
      when 'a' .. 'z' =>
         H := 1;
      when 'A' .. 'Z' =>
         H := 2;
      when '0' .. '9' =>
         H := 3;
      when '.' | '!' | '?' =>
         H := 4;
      when others =>
         H := 5;
         Success := False;
   end case;

   -- If  expressions
   if A > B then
      C := D + 5;
   else
      C := F / 2;
   end if;

   C := (if A > B then D + 5 else F / 2);

   if A > B then
      C := D + 5;
   elsif A = B then
      C := 2 * A;
   else
      C := F / 2;
   end if;

   C := (if A > B then D + 5 elsif A = B then 2 * A else F / 2);

   if X >= 0.0 then
      Y := Sqrt (X);
   else
      Y := Sqrt (-2.0 * X);
   end if;

   Y := Sqrt (if X >= 0.0 then X else -2.0 * X);

  -- Check syntax for Boolean if expression examples

   if (if C - D = 0 then E > 2 else True) then
      null;
   end if;

   if (if C - D = 0 then E > 2) then
      null;
   end if;

   if (if A < 0 then B < 0) then
      null;
   end if;


   -- case expression example
   Value := (case Letter is
                when 'A' | 'E' | 'I' | 'L' | 'U' |
                     'N' | 'O' | 'R' | 'S' | 'T'     => 1,
                when 'D' | 'G'                       => 2,
                when 'B' | 'C' | 'M' | 'P'           => 3,
                when 'F' | 'H' | 'V' | 'W' | 'Y'     => 4,
                when 'K'                             => 5,
                when 'J' | 'X'                       => 8,
                when 'Q' | 'Z'                       => 10);


   -- loop examples

   Sum := 0;
   loop
      Ada.Integer_Text_IO.Get (Value);
      exit when Value < 0;
      Sum := Sum + Value;
   end loop;


   Approx := X / 2.0;
   while abs (X - Approx ** 2) > Tolerance loop
      Approx := 0.5 * (Approx + X / Approx);
   end loop;

   for Count in Integer range 5 .. 8 loop
      Ada.Integer_Text_IO.Put (Count);
   end loop;

   for Count in reverse Integer range 5 .. 8 loop
      Ada.Integer_Text_IO.Put (Count);
   end loop;

   A := 9;
   B := 2;

   for Count in Integer range A .. B loop -- With a null range, this
      Ada.Integer_Text_IO.Put (Count);    -- loop iterates zero times
   end loop;

   for Count in reverse Integer range A .. B loop -- With a null range, this
      Ada.Integer_Text_IO.Put (Count);            -- loop iterates zero times
   end loop;

   loop
      ADC.Read (Temperature);          -- Read the temperature from the ADC
      Calculate_Valve (Current_Temp => Temperature,    -- Calculate the new
                       New_Setting  => Valve_Setting); -- gas valve setting
      DAC.Write (Valve_Setting);       -- Change the gas valve setting
   end loop;


end Control_Structures;