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
270
|
-- Programmed by Jedidiah Barber
-- Licensed under the Sunset License v1.0
-- See license.txt for further details
with
Ada.Characters.Latin_1,
Ada.Strings.Fixed,
Kompsos.Collector;
package body Kompsos.Pretty_Print is
package Latin renames Ada.Characters.Latin_1;
package Str renames Ada.Strings;
function Image
(Item : in Integer)
return String is
begin
return Str.Fixed.Trim (Integer'Image (Item), Str.Left);
end Image;
function Image
(Item : in Long_Natural)
return String is
begin
return Str.Fixed.Trim (Long_Natural'Image (Item), Str.Left);
end Image;
function Image
(Item : in Variable)
return String is
begin
return "Vargen#" & Image (Long_Natural (Item.Ident)) &
(if SU.Length (Item.Name) /= 0
then "/" & SU.To_String (Item.Name)
else "");
end Image;
function Image
(Item : in Term)
return String
is
function Bare
(Item : in Term)
return String is
begin
case Item.Kind is
when Null_Term =>
return "()";
when Atom_Term =>
return Element_Image (Item.Atom);
when Var_Term =>
return Image (Item.Var);
when Pair_Term =>
if Item.Right.Kind = Null_Term then
return Image (Item.Left);
elsif Item.Right.Kind /= Pair_Term then
return Image (Item.Left) & " . " & Bare (Item.Right);
else
return Image (Item.Left) & " " & Bare (Item.Right);
end if;
end case;
end Bare;
begin
if Item.Kind = Pair_Term then
return "(" & Bare (Item) & ")";
else
return Bare (Item);
end if;
end Image;
function Image
(Item : in State)
return String
is
Result : SU.Unbounded_String;
begin
SU.Append (Result, Latin.HT & "Generation:");
if Item.Ident.Is_Empty then
SU.Append (Result, " N/A" & Latin.LF);
else
SU.Append (Result, Latin.LF);
for Iter in Item.Ident.Iterate loop
SU.Append (Result, Latin.HT & Latin.HT & "Vargen#" &
Image (Long_Natural (ID_Number_Maps.Key (Iter))) & " => " &
Image (Long_Natural (ID_Number_Maps.Element (Iter))) & Latin.LF);
end loop;
end if;
SU.Append (Result, Latin.HT & "Variables:");
if Item.LVars.Is_Empty then
SU.Append (Result, " N/A" & Latin.LF);
else
SU.Append (Result, Latin.LF);
for Index in Item.LVars.First_Index .. Item.LVars.Last_Index loop
SU.Append (Result, Latin.HT & Latin.HT &
"Var#" & Image (Long_Natural (Index)) &
(if SU.Length (Item.LVars (Index)) /= 0
then "/" & SU.To_String (Item.LVars (Index))
else "") & Latin.LF);
end loop;
end if;
SU.Append (Result, Latin.HT & "Substitution:");
if Item.Subst.Is_Empty then
SU.Append (Result, " N/A" & Latin.LF);
else
SU.Append (Result, Latin.LF);
for Iter in Item.Subst.Iterate loop
SU.Append (Result, Latin.HT & Latin.HT &
Image (Long_Natural (Binding_Maps.Key (Iter))) & " => " &
Image (Binding_Maps.Element (Iter)) & Latin.LF);
end loop;
end if;
return -Result;
end Image;
function Image
(Item : in State_Array)
return String
is
Result : SU.Unbounded_String;
begin
if Item'Length = 0 then
return "States: N/A" & Latin.LF;
end if;
for Index in Item'Range loop
SU.Append (Result, "State#" & Image (Index) & ":" & Latin.LF);
SU.Append (Result, Image (Item (Index)));
end loop;
return SU.Slice (Result, 1, SU.Length (Result) - 1);
end Image;
function Image
(Item : in Goal)
return String
is
Result : SU.Unbounded_String;
Counter : Positive := 1;
package Collect is new Collector (Item, Empty_State);
begin
if not Collect.Has_Next then
return "States: N/A" & Latin.LF;
end if;
loop
SU.Append (Result, "State#" & Image (Counter) & ":" & Latin.LF);
SU.Append (Result, Image (Collect.Next));
exit when not Collect.Has_Next;
Counter := Counter + 1;
end loop;
return SU.Slice (Result, 1, SU.Length (Result) - 1);
end Image;
procedure Do_Structure_DOT
(This : in Goal;
Nodes : in out DOT_Node_Maps.Map;
Next : in out Long_Natural;
Result : in out SU.Unbounded_String) is
begin
if This.Actual = null or else Nodes.Contains (This.Actual) then
return;
end if;
Nodes.Insert (This.Actual, Next);
Next := Next + 1;
case This.Actual.Kind is
when Static_Node =>
SU.Append (Result, Latin.HT &
"n" & Image (Nodes.Element (This.Actual)) & " [label=""static""];" & Latin.LF);
when Fresh_Node =>
SU.Append (Result, Latin.HT &
"n" & Image (Nodes.Element (This.Actual)) & " [label=""fresh""];" & Latin.LF);
Do_Structure_DOT (This.Actual.Frs_Goal, Nodes, Next, Result);
if Nodes.Contains (This.Actual.Frs_Goal.Actual) then
SU.Append (Result, Latin.HT &
"n" & Image (Nodes.Element (This.Actual)) & " -> " &
"n" & Image (Nodes.Element (This.Actual.Frs_Goal.Actual)) & ";" & Latin.LF);
end if;
when Unify_Node =>
SU.Append (Result, Latin.HT &
"n" & Image (Nodes.Element (This.Actual)) & " [label=""unify""];" & Latin.LF);
Do_Structure_DOT (This.Actual.Uni_Goal, Nodes, Next, Result);
if Nodes.Contains (This.Actual.Uni_Goal.Actual) then
SU.Append (Result, Latin.HT &
"n" & Image (Nodes.Element (This.Actual)) & " -> " &
"n" & Image (Nodes.Element (This.Actual.Uni_Goal.Actual)) & ";" & Latin.LF);
end if;
when Disjunct_Node =>
SU.Append (Result, Latin.HT &
"n" & Image (Nodes.Element (This.Actual)) & " [label=""disjunct""];" & Latin.LF);
Do_Structure_DOT (This.Actual.Dis_Goal1, Nodes, Next, Result);
Do_Structure_DOT (This.Actual.Dis_Goal2, Nodes, Next, Result);
if Nodes.Contains (This.Actual.Dis_Goal1.Actual) then
SU.Append (Result, Latin.HT &
"n" & Image (Nodes.Element (This.Actual)) & " -> " &
"n" & Image (Nodes.Element (This.Actual.Dis_Goal1.Actual)) &
" [label=""1""];" & Latin.LF);
end if;
if Nodes.Contains (This.Actual.Dis_Goal2.Actual) then
SU.Append (Result, Latin.HT &
"n" & Image (Nodes.Element (This.Actual)) & " -> " &
"n" & Image (Nodes.Element (This.Actual.Dis_Goal2.Actual)) &
" [label=""2""];" & Latin.LF);
end if;
when Conjunct_Node =>
SU.Append (Result, Latin.HT &
"n" & Image (Nodes.Element (This.Actual)) & " [label=""conjunct""];" & Latin.LF);
Do_Structure_DOT (This.Actual.Con_Goal, Nodes, Next, Result);
if Nodes.Contains (This.Actual.Con_Goal.Actual) then
SU.Append (Result, Latin.HT &
"n" & Image (Nodes.Element (This.Actual)) & " -> " &
"n" & Image (Nodes.Element (This.Actual.Con_Goal.Actual)) & ";" & Latin.LF);
end if;
when Recurse_Node =>
SU.Append (Result, Latin.HT &
"n" & Image (Nodes.Element (This.Actual)) & " [label=""recurse""];" & Latin.LF);
Do_Structure_DOT (This.Actual.Rec_Goal, Nodes, Next, Result);
if Nodes.Contains (This.Actual.Rec_Goal.Actual) then
SU.Append (Result, Latin.HT &
"n" & Image (Nodes.Element (This.Actual)) & " -> " &
"n" & Image (Nodes.Element (This.Actual.Rec_Goal.Actual)) & ";" & Latin.LF);
end if;
end case;
end Do_Structure_DOT;
function Structure_DOT
(This : in Goal;
Name : in String := "")
return String
is
Result : SU.Unbounded_String;
Nodes : DOT_Node_Maps.Map;
Next_ID : Long_Natural := 0;
begin
SU.Append (Result, "digraph ");
if Name /= "" then
SU.Append (Result, Name & " ");
end if;
SU.Append (Result, "{" & Latin.LF);
Do_Structure_DOT (This, Nodes, Next_ID, Result);
SU.Append (Result, "}");
return SU.To_String (Result);
end Structure_DOT;
end Kompsos.Pretty_Print;
|