summaryrefslogtreecommitdiff
path: root/src/kompsos-pretty_print.adb
blob: d1d89b8521c7926e52718ba28dd3459f58db8ce6 (plain)
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


--  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;


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 ID_Number)
        return String is
    begin
        return Str.Fixed.Trim (ID_Number'Image (Item), Str.Left);
    end Image;




    function Image
           (Item : in Variable)
        return String is
    begin
        return "Var#" & Image (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.Actual.Kind is
            when Atom_Term =>
                return Element_Image (Item.Actual.Value);
            when Var_Term =>
                return Image (Item.Actual.Refer);
            when Pair_Term =>
                if Item.Actual.Right.Actual = null then
                    return Image (Item.Actual.Left);
                else
                    return Image (Item.Actual.Left) & " " & Bare (Item.Actual.Right);
                end if;
            end case;
        end Bare;
    begin
        if Item.Actual = null then
            return "()";
        elsif Item.Actual.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;
        My_Var : Variable;
    begin
        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 Iter in Item.LVars.Iterate loop
                My_Var := (Ident => Name_Maps.Key (Iter), Name => Name_Maps.Element (Iter));
                SU.Append (Result, Latin.HT & Latin.HT & Image (My_Var) & 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 (Binding_Maps.Key (Iter)) & " => " &
                    Image (Binding_Maps.Element (Iter)) & Latin.LF);
            end loop;
        end if;
        return -Result;
    end Image;




    function Image
           (Item : in World)
        return String
    is
        Result : SU.Unbounded_String;
    begin
        if Item.Possibles.Is_Empty then
            return "States: N/A" & Latin.LF;
        end if;
        for Index in Integer range Item.Possibles.First_Index .. Item.Possibles.Last_Index loop
            SU.Append (Result, "State#" & Image (Index) & ":" & Latin.LF);
            SU.Append (Result, Image (Item.Possibles.Constant_Reference (Index)));
        end loop;
        return SU.Slice (Result, 1, SU.Length (Result) - 1);
    end Image;


end Kompsos.Pretty_Print;