File: values-operations.adb

package info (click to toggle)
gnat-gps 4.3-5
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 49,096 kB
  • ctags: 20,461
  • sloc: ada: 274,120; ansic: 154,849; python: 9,890; tcl: 9,812; sh: 8,192; xml: 7,970; cpp: 4,737; yacc: 3,520; makefile: 2,136; lex: 2,043; java: 1,638; perl: 302; awk: 265; sed: 161; asm: 14; fortran: 2; lisp: 1
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;