1 (* M2StackWord.mod provides a generic stack for WORD sized objects.
3 Copyright (C) 2001-2023 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 This file is part of GNU Modula-2.
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Modula-2; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. *)
22 IMPLEMENTATION MODULE M2StackWord ;
24 FROM Storage IMPORT ALLOCATE, DEALLOCATE ;
25 FROM M2Error IMPORT InternalError ;
26 FROM M2Debug IMPORT Assert ;
32 StackBucketWord = POINTER TO BucketWord ;
34 bucket: ARRAY [0..MaxBucket-1] OF WORD ;
36 last : StackBucketWord ;
39 StackOfWord = POINTER TO StackDescriptor ;
40 StackDescriptor = RECORD
41 tail: StackBucketWord ;
46 InitStackWord - creates and returns a new stack.
49 PROCEDURE InitStackWord () : StackOfWord ;
62 KillBucket - destroys a StackBucketWord and returns, NIL.
65 PROCEDURE KillBucket (b: StackBucketWord) : StackBucketWord ;
69 b := KillBucket(b^.last) ;
77 KillStackWord - destroys a stack, returning NIL.
80 PROCEDURE KillStackWord (s: StackOfWord) : StackOfWord ;
84 s^.tail := KillBucket(s^.tail) ;
92 InitBucket - returns an empty StackBucketWord.
95 PROCEDURE InitBucket (l: StackBucketWord) : StackBucketWord ;
109 PushWord - pushes a word, w, onto, s.
112 PROCEDURE PushWord (s: StackOfWord; w: WORD) ;
116 InternalError ('stack has not been initialized')
119 IF (tail=NIL) OR (tail^.items=MaxBucket)
121 tail := InitBucket(tail)
136 PopWord - pops an element from stack, s.
139 PROCEDURE PopWord (s: StackOfWord) : WORD ;
145 InternalError ('stack has not been initialized')
149 InternalError ('stack underflow')
156 InternalError ('stack underflow')
164 RETURN( bucket[items] )
172 IsEmptyWord - returns TRUE if stack, s, is empty.
175 PROCEDURE IsEmptyWord (s: StackOfWord) : BOOLEAN ;
177 RETURN( (s=NIL) OR (s^.tail=NIL) )
182 PeepWord - returns the element at, n, items below in the stack.
183 Top of stack can be seen via Peep(s, 1)
186 PROCEDURE PeepWord (s: StackOfWord; n: CARDINAL) : WORD ;
192 InternalError ('stack underflow')
199 InternalError ('stack underflow')
209 InternalError ('stack underflow')
212 RETURN( b^.bucket[b^.items-n] )
219 InternalError ('stack underflow')
225 ReduceWord - reduce the stack by n elements.
228 PROCEDURE ReduceWord (s: StackOfWord; n: CARDINAL) ;
234 InternalError ('stack underflow')
241 InternalError ('stack underflow')
250 InternalError ('stack underflow')
251 ELSIF s^.tail^.items>=n
253 DEC( s^.tail^.items, n) ;
254 RETURN (* all done exit *)
258 s^.tail := s^.tail^.last ;
267 RemoveTop - throw away the top element of the stack.
270 PROCEDURE RemoveTop (s: StackOfWord) ;
277 NoOfItemsInStackWord - returns the number of items held in the stack, s.
280 PROCEDURE NoOfItemsInStackWord (s: StackOfWord) : CARDINAL ;
297 END NoOfItemsInStackWord ;