File: gnatcoll-tribooleans.adb

package info (click to toggle)
libgnatcoll 18-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 5,068 kB
  • sloc: ada: 40,393; python: 354; ansic: 310; makefile: 245; sh: 31
file content (269 lines) | stat: -rw-r--r-- 8,831 bytes parent folder | download | duplicates (2)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
------------------------------------------------------------------------------
--                             G N A T C O L L                              --
--                                                                          --
--                     Copyright (C) 2008-2017, AdaCore                     --
--                                                                          --
-- This library is free software;  you can redistribute it and/or modify it --
-- under terms of the  GNU General Public License  as published by the Free --
-- Software  Foundation;  either version 3,  or (at your  option) any later --
-- version. This library is distributed in the hope that it will be useful, --
-- but WITHOUT ANY WARRANTY;  without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE.                            --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
--                                                                          --
------------------------------------------------------------------------------

package body GNATCOLL.Tribooleans is

   And_Truth_Table1 : constant array (Triboolean, Triboolean) of Triboolean :=
     (True          =>
        (True          => True,
         False         => False,
         Indeterminate => Indeterminate),
      False         =>
        (True          => False,
         False         => False,
         Indeterminate => False),
      Indeterminate         =>
        (True          => Indeterminate,
         False         => False,
         Indeterminate => Indeterminate));

   And_Truth_Table2 : constant array (Triboolean, Boolean) of Triboolean :=
     (True          => (True => True,          False => False),
      False         => (True => False,         False => False),
      Indeterminate => (True => Indeterminate, False => False));

   Or_Truth_Table1 : constant array (Triboolean, Triboolean) of Triboolean :=
     (True          =>
        (True          => True,
         False         => True,
         Indeterminate => True),
      False         =>
        (True          => True,
         False         => False,
         Indeterminate => Indeterminate),
      Indeterminate         =>
        (True          => True,
         False         => Indeterminate,
         Indeterminate => Indeterminate));

   Or_Truth_Table2 : constant array (Triboolean, Boolean) of Triboolean :=
     (True          => (True => True,          False => True),
      False         => (True => True,          False => False),
      Indeterminate => (True => True,          False => Indeterminate));

   Xor_Truth_Table1 : constant array (Triboolean, Triboolean) of Triboolean :=
     (True          =>
        (True          => False,
         False         => True,
         Indeterminate => Indeterminate),
      False         =>
        (True          => True,
         False         => False,
         Indeterminate => Indeterminate),
      Indeterminate         =>
        (True          => Indeterminate,
         False         => Indeterminate,
         Indeterminate => Indeterminate));

   Xor_Truth_Table2 : constant array (Triboolean, Boolean) of Triboolean :=
     (True          => (True => False,         False => True),
      False         => (True => True,          False => False),
      Indeterminate => (True => Indeterminate, False => Indeterminate));

   Eq_Truth_Table1 : constant array (Triboolean, Triboolean) of Triboolean :=
     (True          =>
        (True          => True,
         False         => False,
         Indeterminate => Indeterminate),
      False         =>
        (True          => False,
         False         => True,
         Indeterminate => Indeterminate),
      Indeterminate         =>
        (True          => Indeterminate,
         False         => Indeterminate,
         Indeterminate => Indeterminate));

   Eq_Truth_Table2 : constant array (Triboolean, Boolean) of Triboolean :=
     (True          => (True => True,          False => False),
      False         => (True => False,         False => True),
      Indeterminate => (True => Indeterminate, False => Indeterminate));

   -------------------
   -- To_TriBoolean --
   -------------------

   function To_TriBoolean (Value : Boolean) return Triboolean is
   begin
      if Value then
         return Triboolean'(True);
      else
         return Triboolean'(False);
      end if;
   end To_TriBoolean;

   ----------------
   -- To_Boolean --
   ----------------

   function To_Boolean (Value : Triboolean) return Boolean is
   begin
      return Value = Triboolean'(True);
   end To_Boolean;

   -----------
   -- "not" --
   -----------

   function "not" (Value : Triboolean) return Triboolean is
   begin
      case Value is
         when Triboolean'(True)          => return Triboolean'(False);
         when Triboolean'(False)         => return Triboolean'(True);
         when Triboolean'(Indeterminate) => return Triboolean'(Indeterminate);
      end case;
   end "not";

   -----------
   -- "and" --
   -----------

   function "and" (Value1, Value2 : Triboolean) return Triboolean is
   begin
      return And_Truth_Table1 (Value1, Value2);
   end "and";

   function "and" (Value1 : Triboolean; Value2 : Boolean) return Triboolean is
   begin
      return And_Truth_Table2 (Value1, Value2);
   end "and";

   function "and" (Value1 : Boolean; Value2 : Triboolean) return Triboolean is
   begin
      return And_Truth_Table2 (Value2, Value1);
   end "and";

   ----------
   -- "or" --
   ----------

   function "or" (Value1, Value2 : Triboolean) return Triboolean is
   begin
      return Or_Truth_Table1 (Value1, Value2);
   end "or";

   function "or" (Value1 : Triboolean; Value2 : Boolean) return Triboolean is
   begin
      return Or_Truth_Table2 (Value1, Value2);
   end "or";

   function "or" (Value1 : Boolean; Value2 : Triboolean) return Triboolean is
   begin
      return Or_Truth_Table2 (Value2, Value1);
   end "or";

   -----------
   -- "xor" --
   -----------

   function "xor" (Value1, Value2 : Triboolean) return Triboolean is
   begin
      return Xor_Truth_Table1 (Value1, Value2);
   end "xor";

   function "xor" (Value1 : Triboolean; Value2 : Boolean) return Triboolean is
   begin
      return Xor_Truth_Table2 (Value1, Value2);
   end "xor";

   function "xor" (Value1 : Boolean; Value2 : Triboolean) return Triboolean is
   begin
      return Xor_Truth_Table2 (Value2, Value1);
   end "xor";

   ---------
   -- "=" --
   ---------

   function "=" (Value1 : Boolean; Value2 : Triboolean) return Boolean is
   begin
      if Value1 then
         return Value2 = Triboolean'(True);
      else
         return Value2 = Triboolean'(False);
      end if;
   end "=";

   function "=" (Value1 : Triboolean; Value2 : Boolean) return Boolean is
   begin
      if Value2 then
         return Value1 = Triboolean'(True);
      else
         return Value1 = Triboolean'(False);
      end if;
   end "=";

   -----------
   -- Equal --
   -----------

   function Equal (Value1 : Triboolean; Value2 : Boolean) return Triboolean is
   begin
      return Eq_Truth_Table2 (Value1, Value2);
   end Equal;

   -----------
   -- Equal --
   -----------

   function Equal (Value1 : Boolean; Value2 : Triboolean) return Triboolean is
   begin
      return Eq_Truth_Table2 (Value2, Value1);
   end Equal;

   function Equal
     (Value1 : Triboolean; Value2 : Triboolean) return Triboolean is
   begin
      return Eq_Truth_Table1 (Value1, Value2);
   end Equal;

   -----------
   -- Image --
   -----------

   function Image (Value : Triboolean) return String is
   begin
      case Value is
         when True          => return "TRUE";
         when False         => return "FALSE";
         when Indeterminate => return "INDETERMINATE";
      end case;
   end Image;

   -----------
   -- Value --
   -----------

   function Value (Str : String) return Triboolean is
   begin
      if Boolean'Value (Str) then
         return True;
      else
         return False;
      end if;
   exception
      when Constraint_Error =>
         return Indeterminate;
   end Value;

end GNATCOLL.Tribooleans;