File: values-operations.adb

package info (click to toggle)
gnat-gps 18-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 45,716 kB
  • sloc: ada: 362,679; python: 31,031; xml: 9,597; makefile: 1,030; ansic: 917; sh: 264; java: 17
file content (71 lines) | stat: -rw-r--r-- 1,316 bytes parent folder | download | duplicates (8)
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
with Except;
with Screen_Output;
with Stack;

package body Values.Operations is

   ----------
   -- Read --
   ----------

   function Read (Op : String) return Operation is
   begin
      if Op = "+" then
         return Add;

      elsif Op = "-" then
         return Sub;

      elsif Op = "*" then
         return Mul;

      elsif Op = "/" then
         return Div;

      else
         raise Except.User_Error;
      end if;
   end Read;

   -------------
   -- Process --
   -------------

   procedure Process (Op : Operation) is
      V2 : Value := Stack.Pop;
      V1 : Value := Stack.Pop;

      Result : Integer;

   begin
      case Op is
         when Add =>
            Result := V1.E + V2.E;

         when Div =>
            Result := V1.E / V2.E;

         when Mul =>
            Result := V1.E * V2.E;

         when Sub =>
            Result := V1.E - V2.E;
      end case;

      --  Create an integer Value by setting the field "E" of the record
      --  to Result.

      Stack.Push (new Value_Info'(E => Result));

   exception
      --  If we get a Constraint_Error exception, then we had a computation
      --  overflow or a divide by zero.

      when Constraint_Error =>
         Screen_Output.Error_Msg ("Operation error. Values popped.");

   end Process;

end Values.Operations;