Debian Codegraph @

gcc/m2/mc/mcStack.mod

raw
1 (* Copyright (C) 2015-2025 Free Software Foundation, Inc. *)
2 (* This file is part of GNU Modula-2.
3
4 GNU Modula-2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 3, or (at your option) any later
7 version.
8
9 GNU Modula-2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with GCC; see the file COPYING3. If not see
16 <http://www.gnu.org/licenses/>. *)
17
18 IMPLEMENTATION MODULE mcStack ;
19
20 FROM Storage IMPORT ALLOCATE, DEALLOCATE ;
21
22 FROM Indexing IMPORT Index, InitIndex, LowIndice, HighIndice, GetIndice, PutIndice,
23 DeleteIndice, KillIndex ;
24
25
26 TYPE
27 stack = POINTER TO RECORD
28 list : Index ;
29 count: CARDINAL ;
30 END ;
31
32 (*
33 init - create and return a stack.
34 *)
35
36 PROCEDURE init () : stack ;
37 VAR
38 s: stack ;
39 BEGIN
40 NEW (s) ;
41 WITH s^ DO
42 list := InitIndex (1) ;
43 count := 0
44 END ;
45 RETURN s
46 END init ;
47
48
49 (*
50 kill - deletes stack, s.
51 *)
52
53 PROCEDURE kill (VAR s: stack) ;
54 BEGIN
55 s^.list := KillIndex (s^.list) ;
56 DISPOSE (s) ;
57 s := NIL
58 END kill ;
59
60
61 (*
62 push - an address, a, onto the stack, s.
63 It returns, a.
64 *)
65
66 PROCEDURE push (s: stack; a: ADDRESS) : ADDRESS ;
67 BEGIN
68 WITH s^ DO
69 IF count=0
70 THEN
71 PutIndice (list, LowIndice (list), a)
72 ELSE
73 PutIndice (list, HighIndice (list)+1, a)
74 END ;
75 INC (count)
76 END ;
77 RETURN a
78 END push ;
79
80
81 (*
82 pop - and return the top element from stack, s.
83 *)
84
85 PROCEDURE pop (s: stack) : ADDRESS ;
86 VAR
87 a: ADDRESS ;
88 BEGIN
89 WITH s^ DO
90 IF count = 0
91 THEN
92 HALT
93 ELSE
94 DEC (count) ;
95 a := GetIndice (list, HighIndice (list)) ;
96 DeleteIndice (list, HighIndice (list)) ;
97 RETURN a
98 END
99 END
100 END pop ;
101
102
103 (*
104 replace - performs a pop; push (a); return a.
105 *)
106
107 PROCEDURE replace (s: stack; a: ADDRESS) : ADDRESS ;
108 VAR
109 b: ADDRESS ;
110 BEGIN
111 b := pop (s) ;
112 RETURN push (s, a)
113 END replace ;
114
115
116 (*
117 depth - returns the depth of the stack.
118 *)
119
120 PROCEDURE depth (s: stack) : CARDINAL ;
121 BEGIN
122 RETURN s^.count
123 END depth ;
124
125
126 (*
127 access - returns the, i, th stack element.
128 The top of stack is defined by:
129
130 access (s, depth (s)).
131 *)
132
133 PROCEDURE access (s: stack; i: CARDINAL) : ADDRESS ;
134 BEGIN
135 IF (i>s^.count) OR (i=0)
136 THEN
137 HALT
138 ELSE
139 RETURN GetIndice (s^.list, i)
140 END
141 END access ;
142
143
144 END mcStack.