summaryrefslogtreecommitdiff
path: root/example/calc.adb
blob: 0dd0e71eb36b3392c8f2545cb8a9a2098a35dbeb (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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342


--  This source is licensed under the Sunset License v1.0


with

    Ada.Text_IO,
    Ada.Strings.Unbounded,
    Ada.Strings.Maps,
    Ada.Strings.Fixed,
    Ada.Command_Line,
    Packrat.Standard,
    Packrat.Utilities;

use

    Ada.Text_IO;


procedure Calc is


    type Lexer_Labels is (Whitespace, Decimal, Number, Bracket, Operator);
    type Parser_Labels is (Sum, Difference, Product, Quotient, Power, Factor, Negative);

    package My_Rat is new Packrat.Standard
        (Lexer_Labels, Parser_Labels, Character, String);




    --  Lexer Grammar

    --  <number> ::= <digits> . <digits> | <digits>
    --  <digits> ::= <zerotonine> <digits> | <zerotonine>
    --  <zerotonine> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9


    function Is_Operator is new Packrat.Utilities.In_Set
        (Ada.Strings.Maps.To_Set ("+-*/^"));
    function Sat_Operator is new My_Rat.Lexers.Satisfy (Is_Operator);
    function Operator is new My_Rat.Lexers.Stamp (Operator, Sat_Operator);

    function Is_Parens is new Packrat.Utilities.In_Set
        (Ada.Strings.Maps.To_Set ("()"));
    function Sat_Parens is new My_Rat.Lexers.Satisfy (Is_Parens);
    function Bracket is new My_Rat.Lexers.Stamp (Bracket, Sat_Parens);

    function Sat_Digit is new My_Rat.Lexers.Satisfy (Packrat.Utilities.Is_Digit);
    function Some_Digits is new My_Rat.Lexers.Many (Sat_Digit, 1);
    function Number is new My_Rat.Lexers.Stamp (Number, Some_Digits);

    function Match_Point is new My_Rat.Lexers.Match ('.');
    function Decimal_Seq is new My_Rat.Lexers.Sequence
        ((Some_Digits'Access, Match_Point'Access, Some_Digits'Access));
    function Decimal is new My_Rat.Lexers.Stamp (Decimal, Decimal_Seq);

    function Sat_Blank is new My_Rat.Lexers.Satisfy (Packrat.Utilities.Is_Whitespace);
    function Many_Blank is new My_Rat.Lexers.Many (Sat_Blank, 1);
    function Whitespace is new My_Rat.Lexers.Ignore (Whitespace, Many_Blank);

    package Scanner is new My_Rat.Lexers.Scan_Once
      ((Whitespace'Access, Decimal'Access, Number'Access,
        Bracket'Access, Operator'Access));




    --  Parser Grammar

    --  <expr> ::= <sum> <endofinput>
    --  <sum> ::= <sum> + <product> | <sum> - <product> | <product>
    --  <product> ::= <product> * <power> | <product> / <power> | <power>
    --  <power> ::= <factor> ^ <factor> | <factor>
    --  <factor> ::= ( <sum> ) | <negative> | <number>
    --  <negative> ::= - <sum>

    --  Or at least the above is what I started with, anyway.
    --  The grammar became modified slightly to take advantage of Ignore/Stamp in
    --  order to ensure the finished parse tree doesn't contain extra chaff.


    package Addsub_Redir is new My_Rat.Parsers.Redirect;

    function Is_Minus is new My_Rat.Lexer_Tokens.Is_Value ("-");
    function Sat_Minus is new My_Rat.Parsers.Satisfy (Is_Minus);
    function Ignore_Minus is new My_Rat.Parsers.Ignore (Sat_Minus);
    function Neg_Seq is new My_Rat.Parsers.Sequence_2 (Ignore_Minus, Addsub_Redir.Call);
    function Negative is new My_Rat.Parsers.Stamp (Negative, Neg_Seq);

    function Is_Decimal is new My_Rat.Lexer_Tokens.Is_Label (Decimal);
    function Is_Number is new My_Rat.Lexer_Tokens.Is_Label (Number);
    function Is_Left_Parens is new My_Rat.Lexer_Tokens.Is_Value ("(");
    function Is_Right_Parens is new My_Rat.Lexer_Tokens.Is_Value (")");
    function Sat_Decimal is new My_Rat.Parsers.Satisfy (Is_Decimal);
    function Sat_Number is new My_Rat.Parsers.Satisfy (Is_Number);
    function Sat_Left_Parens is new My_Rat.Parsers.Satisfy (Is_Left_Parens);
    function Sat_Right_Parens is new My_Rat.Parsers.Satisfy (Is_Right_Parens);
    function Fac_Num is new My_Rat.Parsers.Choice_2 (Sat_Decimal, Sat_Number);
    function Factor is new My_Rat.Parsers.Stamp (Factor, Fac_Num);
    function Fac_Between is new My_Rat.Parsers.Between
        (Sat_Left_Parens, Addsub_Redir.Call, Sat_Right_Parens);
    function Fac_Choice is new My_Rat.Parsers.Choice
        ((Fac_Between'Access, Negative'Access, Factor'Access));

    function Is_Exp is new My_Rat.Lexer_Tokens.Is_Value ("^");
    function Sat_Exp is new My_Rat.Parsers.Satisfy (Is_Exp);
    function Ignore_Exp is new My_Rat.Parsers.Ignore (Sat_Exp);
    function Pow_Seq is new My_Rat.Parsers.Sequence
        ((Fac_Choice'Access, Ignore_Exp'Access, Fac_Choice'Access));
    function Power is new My_Rat.Parsers.Stamp (Power, Pow_Seq);
    function Pow_Choice is new My_Rat.Parsers.Choice_2 (Power, Fac_Choice);

    package Muldiv_Redir is new My_Rat.Parsers.Redirect;

    function Is_Mult is new My_Rat.Lexer_Tokens.Is_Value ("*");
    function Sat_Mult is new My_Rat.Parsers.Satisfy (Is_Mult);
    function Ignore_Mult is new My_Rat.Parsers.Ignore (Sat_Mult);
    function Product_Seq is new My_Rat.Parsers.Sequence
        ((Muldiv_Redir.Call'Access, Ignore_Mult'Access, Pow_Choice'Access));
    function Product is new My_Rat.Parsers.Stamp (Product, Product_Seq);

    function Is_Div is new My_Rat.Lexer_Tokens.Is_Value ("/");
    function Sat_Div is new My_Rat.Parsers.Satisfy (Is_Div);
    function Ignore_Div is new My_Rat.Parsers.Ignore (Sat_Div);
    function Quotient_Seq is new My_Rat.Parsers.Sequence
        ((Muldiv_Redir.Call'Access, Ignore_Div'Access, Pow_Choice'Access));
    function Quotient is new My_Rat.Parsers.Stamp (Quotient, Quotient_Seq);

    function Muldiv_Choice is new My_Rat.Parsers.Choice
        ((Product'Access, Quotient'Access, Pow_Choice'Access));

    function Difference_Seq is new My_Rat.Parsers.Sequence
        ((Addsub_Redir.Call'Access, Ignore_Minus'Access, Muldiv_Choice'Access));
    function Difference is new My_Rat.Parsers.Stamp (Difference, Difference_Seq);

    function Is_Plus is new My_Rat.Lexer_Tokens.Is_Value ("+");
    function Sat_Plus is new My_Rat.Parsers.Satisfy (Is_Plus);
    function Ignore_Plus is new My_Rat.Parsers.Ignore (Sat_Plus);
    function Sum_Seq is new My_Rat.Parsers.Sequence
        ((Addsub_Redir.Call'Access, Ignore_Plus'Access, Muldiv_Choice'Access));
    function Sum is new My_Rat.Parsers.Stamp (Sum, Sum_Seq);

    function Addsub_Choice is new My_Rat.Parsers.Choice
        ((Sum'Access, Difference'Access, Muldiv_Choice'Access));

    function Expr is new My_Rat.Parsers.Sequence_2 (Addsub_Choice, My_Rat.Parsers.End_Of_Input);

    package Parser is new My_Rat.Parsers.Parse_Once (Expr);




    type Calc_Result is delta 10.0**(-9) digits 18;


    function Value
           (Tok : in My_Rat.Parser_Tokens.Finished_Token_Type)
        return String
    is
        Lexed : My_Rat.Lexer_Tokens.Token_Array :=
            My_Rat.Parser_Tokens.Value (Tok.Token);
    begin
        if Lexed'Length = 0 then
            return "";
        else
            return My_Rat.Lexer_Tokens.Value (Lexed (Lexed'First));
        end if;
    end Value;


    function Element
           (Subs : in My_Rat.Parse_Graphs.Token_Group_Array;
            Ind  : in Positive)
        return My_Rat.Parser_Tokens.Finished_Token_Type is
    begin
        return My_Rat.Parse_Graphs.Element (Subs (Subs'First), Ind);
    end Element;


    function Is_Integer
           (Val : in Calc_Result)
        return Boolean is
    begin
        return Val = Calc_Result (Integer (Val));
    end Is_Integer;


    function To_Result
           (Str : in String)
        return Calc_Result
    is
        Point : Natural := Ada.Strings.Fixed.Index (Str, ".");
    begin
        if Point = 0 then
            return Calc_Result (Integer'Value (Str));
        else
            return Calc_Result (Integer'Value (Str (Str'First .. Point - 1))) +
                Calc_Result (Integer'Value (Str (Point + 1 .. Str'Last))) /
                Calc_Result (10 ** (Str'Last - Point));
        end if;
    end To_Result;


    function Evaluate
           (Graph    : in My_Rat.Parser_Result;
            Position : in My_Rat.Parser_Tokens.Finished_Token_Type)
        return Calc_Result
    is
        Subgroups : My_Rat.Parse_Graphs.Token_Group_Array := Graph.Subgroups (Position);
        Temp : Calc_Result;
    begin
        case My_Rat.Parser_Tokens.Label (Position.Token) is
            when Sum =>
                return Evaluate (Graph, Element (Subgroups, 1)) +
                    Evaluate (Graph, Element (Subgroups, 2));
            when Difference =>
                return Evaluate (Graph, Element (Subgroups, 1)) -
                    Evaluate (Graph, Element (Subgroups, 2));
            when Product =>
                return Evaluate (Graph, Element (Subgroups, 1)) *
                    Evaluate (Graph, Element (Subgroups, 2));
            when Quotient =>
                Temp := Evaluate (Graph, Element (Subgroups, 2));
                if Temp = 0.0 then
                    raise Constraint_Error with "Div by zero";
                end if;
                return Evaluate (Graph, Element (Subgroups, 1)) / Temp;
            when Power =>
                Temp := Evaluate (Graph, Element (Subgroups, 2));
                if not Is_Integer (Temp) then
                    raise Constraint_Error with "Non-integer exponent";
                end if;
                return Calc_Result
                    (Long_Float (Evaluate (Graph, Element (Subgroups, 1))) ** Integer (Temp));
            when Factor =>
                return To_Result (Value (Position));
            when Negative =>
                return Calc_Result'(0.0) - Evaluate (Graph, Element (Subgroups, 1));
        end case;
    end Evaluate;


    function Image
           (Val : in Calc_Result)
        return String is
    begin
        return Val'Image;
    end Image;




    package SU renames Ada.Strings.Unbounded;
    package Comlin renames Ada.Command_Line;

    Comlin_Input : SU.Unbounded_String;
    Silent_Running : Boolean := False;
    Real_Arg_Count : Natural := 0;


begin


    Addsub_Redir.Set (Addsub_Choice'Access);
    Muldiv_Redir.Set (Muldiv_Choice'Access);


    for I in 1 .. Comlin.Argument_Count loop
        if Comlin.Argument (I) /= "--silent" then
            Real_Arg_Count := Real_Arg_Count + 1;
        end if;
    end loop;


    if Real_Arg_Count = 0 then
        Put_Line ("Command Line Calculator");
        New_Line;
        Put_Line ("This program calculates elementary mathematical");
        Put_Line ("expressions it receives as command line arguments.");
        Put_Line ("Naturally, it needs you to supply arguments for that.");
        Put_Line ("You may have to use quotes to ensure the shell doesn't mess with input.");
        New_Line;
        Put_Line ("Accepted operators are + - * / ^ ()");
        Put_Line ("Please ensure exponents are always integers and divisors are never zero.");
        New_Line;
        Put_Line ("If you do not want to see any output beyond the answer,");
        Put_Line ("then use the --silent switch.");
        return;
    end if;


    for I in 1 .. Comlin.Argument_Count loop
        if Comlin.Argument (I) = "--silent" then
            Silent_Running := True;
        else
            SU.Append (Comlin_Input, Comlin.Argument (I) & " ");
        end if;
    end loop;
    SU.Delete (Comlin_Input, SU.Length (Comlin_Input), SU.Length (Comlin_Input));


    declare
        Input : String := SU.To_String (Comlin_Input);
        Lexed_Tokens : My_Rat.Lexer_Result := Scanner.Scan (Input);
        Result_Graph : My_Rat.Parser_Result := Parser.Parse (Lexed_Tokens);
        Calculated : Calc_Result := Evaluate (Result_Graph, Result_Graph.Root_Elements (1));
    begin
        if Silent_Running then
            Put_Line (Image (Calculated));
            return;
        end if;

        Put_Line ("Input:");
        Put_Line (Input);
        New_Line;

        Put_Line ("Lexer token output:");
        for T of Lexed_Tokens loop
            Put (My_Rat.Lexer_Tokens.Debug_String (T));
        end loop;
        New_Line;

        Put_Line ("Parser graph output:");
        Put_Line (My_Rat.Parse_Graphs.Debug_String (Result_Graph));
        New_Line;

        Put_Line ("Root tokens:");
        for T of Result_Graph.Root_Elements loop
            Put (My_Rat.Parser_Tokens.Debug_String (T));
        end loop;
        New_Line;

        Put_Line ("Calculated result:");
        Put_Line (Image (Calculated));
    end;


end Calc;