summaryrefslogtreecommitdiff
path: root/tests/examplefiles/test.mod
blob: ba972e3044a17bd05df666e5a83662353324c2eb (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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
(* LIFO Storage Library
 *
 *  @file LIFO.mod
 *  LIFO implementation
 *
 *  Universal Dynamic Stack
 *
 *  Author: Benjamin Kowarsch
 *
 *  Copyright (C) 2009 Benjamin Kowarsch. All rights reserved.
 *
 *  License:
 *
 *  Redistribution  and  use  in source  and  binary forms,  with  or  without
 *  modification, are permitted provided that the following conditions are met
 *
 *  1) NO FEES may be charged for the provision of the software.  The software
 *     may  NOT  be published  on websites  that contain  advertising,  unless
 *     specific  prior  written  permission has been obtained.
 *
 *  2) Redistributions  of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *
 *  3) Redistributions  in binary form  must  reproduce  the  above  copyright
 *     notice,  this list of conditions  and  the following disclaimer  in the
 *     documentation and other materials provided with the distribution.
 *
 *  4) Neither the author's name nor the names of any contributors may be used
 *     to endorse  or  promote  products  derived  from this software  without
 *     specific prior written permission.
 *
 *  5) Where this list of conditions  or  the following disclaimer, in part or
 *     as a whole is overruled  or  nullified by applicable law, no permission
 *     is granted to use the software.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,  BUT NOT LIMITED TO,  THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY  AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT  SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE  FOR  ANY  DIRECT,  INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR
 * CONSEQUENTIAL  DAMAGES  (INCLUDING,  BUT  NOT  LIMITED  TO,  PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES;  LOSS OF USE,  DATA,  OR PROFITS; OR BUSINESS
 * INTERRUPTION)  HOWEVER  CAUSED  AND ON ANY THEORY OF LIABILITY,  WHETHER IN
 * CONTRACT,  STRICT LIABILITY,  OR TORT  (INCLUDING NEGLIGENCE  OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *  
 *)


IMPLEMENTATION (* OF *) MODULE LIFO;

FROM SYSTEM IMPORT ADDRESS, ADR, TSIZE;
FROM Storage IMPORT ALLOCATE, DEALLOCATE;


(* ---------------------------------------------------------------------------
// Private type : ListEntry
// ---------------------------------------------------------------------------
*)
TYPE ListPtr = POINTER TO ListEntry;

TYPE ListEntry = RECORD
    value : DataPtr;
    next  : ListPtr
END; (* ListEntry *)


(* ---------------------------------------------------------------------------
// Opaque type : LIFO.Stack
// ---------------------------------------------------------------------------
// CAUTION:  Modula-2 does not support the use of variable length array fields
// in records.  VLAs can  only  be implemented  using pointer arithmetic which
// means  there is  no type checking  and  no boundary checking  on the array.
// It also means that  array notation cannot be used on the array  which makes
// the code  difficult to read  and maintain.  As a result,  Modula-2  is less
// safe and less readable than C when it comes to using VLAs.  Great care must
// be taken to make sure that the code accessing VLA fields is safe.  Boundary
// checks must be inserted manually.  Size checks must be inserted manually to
// compensate for the absence of type checks. *)

TYPE Stack = POINTER TO StackDescriptor;

TYPE StackDescriptor = RECORD
    overflow   : ListPtr;
    entryCount : StackSize;
    arraySize  : StackSize;
    array      : ADDRESS (* ARRAY OF DataPtr *)
END; (* StackDescriptor *)


(* ---------------------------------------------------------------------------
// function:  LIFO.new( initial_size, status )
// ---------------------------------------------------------------------------
//
// Creates  and  returns  a new LIFO stack object  with an initial capacity of
// <initialSize>.  If  zero  is passed in  for <initialSize>,  then  the stack
// will be created  with an  initial capacity  of  LIFO.defaultStackSize.  The
// function fails  if a value greater than   LIFO.maximumStackSize  is  passed
// in  for <initialSize> or if memory could not be allocated.
//
// The initial capacity of a stack is the number of entries that can be stored
// in the stack without enlargement.
//
// The status of the operation  is passed back in <status>. *)

PROCEDURE new ( initialSize : StackSize; VAR status : Status ) : Stack;

VAR
    newStack : Stack;
    
BEGIN

    (* zero size means default *)
    IF initialSize = 0 THEN
        initialSize := defaultStackSize;
    END; (* IF *)
    
    (* bail out if initial size is too high *)
    IF initialSize > maximumStackSize THEN
        status := invalidSize;
        RETURN NIL;
    END; (* IF *)
    
    (* allocate new stack object *)
    ALLOCATE(newStack, TSIZE(Stack) + TSIZE(DataPtr) * (initialSize - 1));
    
    (* bail out if allocation failed *)
    IF newStack = NIL THEN
        status := allocationFailed;
        RETURN NIL;
    END; (* IF *)
        
    (* initialise meta data *)
    newStack^.arraySize := initialSize;
    newStack^.entryCount := 0;
    newStack^.overflow := NIL;
    
    (* pass status and new stack to caller *)
    status := success;
    RETURN newStack
    
END new;


(* ---------------------------------------------------------------------------
// function:  LIFO.push( stack, value, status )
// ---------------------------------------------------------------------------
//
// Adds a  new entry <value>  to the top of stack <stack>.  The  new entry  is
// added by reference,  no data is copied.  However,  no entry is added if the
// the stack is full,  that is  when the number of entries stored in the stack
// has reached LIFO.maximumStackSize.  The function fails  if NIL is passed in
// for <stack> or <value>,  or if memory could not be allocated.
//
// New entries are allocated dynamically  if the number of entries exceeds the
// initial capacity of the stack.
//
// The status of the operation is passed back in <status>. *)

PROCEDURE push ( VAR stack : Stack; value : DataPtr; VAR status : Status );
VAR
    newEntry : ListPtr;
    valuePtr : POINTER TO DataPtr;

BEGIN

    (* bail out if stack is NIL *)
    IF stack = NIL THEN
        status := invalidStack;
        RETURN;
    END; (* IF *)
    
    (* bail out if value is NIL *)
    IF value = NIL THEN
        status := invalidData;
        RETURN;
    END; (* IF *)

    (* bail out if stack is full *)
    IF stack^.entryCount >= maximumStackSize THEN
        status := stackFull;
        RETURN;
    END; (* IF *)

    (* check if index falls within array segment *)
    IF stack^.entryCount < stack^.arraySize THEN
    
        (* store value in array segment *)
        
        (* stack^.array^[stack^.entryCount] := value; *)
        valuePtr := ADR(stack^.array) + TSIZE(DataPtr) * stack^.entryCount;
        valuePtr^ := value;
        
    ELSE (* index falls within overflow segment *)
    
        (* allocate new entry slot *)
        NEW(newEntry);
        
        (* bail out if allocation failed *)
        IF newEntry = NIL THEN
            status := allocationFailed;
            RETURN;
        END; (* IF *)
        
        (* initialise new entry *)
        newEntry^.value := value;
        
        (* link new entry into overflow list *)
        newEntry^.next := stack^.overflow;
        stack^.overflow := newEntry;
    
    END; (* IF *)
    
    (* update entry counter *)
    INC(stack^.entryCount);
    
    (* pass status to caller *)
    status := success;
    RETURN

END push;


(* ---------------------------------------------------------------------------
// function:  LIFO.pop( stack, status )
// ---------------------------------------------------------------------------
//
// Removes the top most value from stack <stack> and returns it.  If the stack
// is empty,  that  is  when the  number  of  entries  stored in the stack has
// reached zero,  then NIL is returned.
//
// Entries which were allocated dynamically (above the initial capacity) are
// deallocated when their values are popped.
//
// The status of the operation is passed back in <status>. *)

PROCEDURE pop ( VAR stack : Stack; VAR status : Status ) : DataPtr;

VAR
    thisValue : DataPtr;
    thisEntry : ListPtr;
    valuePtr : POINTER TO DataPtr;

BEGIN

    (* bail out if stack is NIL *)
    IF stack = NIL THEN
        status := invalidStack;
        RETURN NIL;
    END; (* IF *)
    
    (* bail out if stack is empty *)
    IF stack^.entryCount = 0 THEN
        status := stackEmpty;
        RETURN NIL;
    END; (* IF *)

    DEC(stack^.entryCount);
    
    (* check if index falls within array segment *)
    IF stack^.entryCount < stack^.arraySize THEN
        
        (* obtain value at index entryCount in array segment *)
        
        (* thisValue := stack^.array^[stack^.entryCount]; *)
        valuePtr := ADR(stack^.array) + TSIZE(DataPtr) * stack^.entryCount;
        thisValue := valuePtr^;
        
    ELSE (* index falls within overflow segment *)
        
        (* obtain value of first entry in overflow list *)
        thisValue := stack^.overflow^.value;
        
        (* isolate first entry in overflow list *)
        thisEntry := stack^.overflow;
        stack^.overflow := stack^.overflow^.next;
        
        (* remove the entry from overflow list *)
        DISPOSE(thisEntry);
                
    END; (* IF *)

    (* return value and status to caller *)
    status := success;
    RETURN thisValue

END pop;


(* ---------------------------------------------------------------------------
// function:  LIFO.stackSize( stack )
// ---------------------------------------------------------------------------
//
// Returns the current capacity of <stack>.  The current capacity is the total
// number of allocated entries. Returns zero if NIL is passed in for <stack>.
*)
PROCEDURE stackSize( VAR stack : Stack ) : StackSize;

BEGIN

    (* bail out if stack is NIL *)
    IF stack = NIL THEN
        RETURN 0;
    END; (* IF *)

    IF stack^.entryCount < stack^.arraySize THEN
        RETURN stack^.arraySize;
    ELSE
        RETURN stack^.entryCount;
    END; (* IF *)
    
END stackSize;


(* ---------------------------------------------------------------------------
// function:  LIFO.stackEntries( stack )
// ---------------------------------------------------------------------------
//
// Returns  the  number of entries  stored in stack <stack>,  returns  zero if
// NIL is passed in for <stack>. *)

PROCEDURE stackEntries( VAR stack : Stack ) : StackSize;

BEGIN

    (* bail out if stack is NIL *)
    IF stack = NIL THEN
        RETURN 0;
    END; (* IF *)

    RETURN stack^.entryCount
    
END stackEntries;


(* ---------------------------------------------------------------------------
// function:  LIFO.dispose( stack )
// ---------------------------------------------------------------------------
//
// Disposes of LIFO stack object <stack>.  Returns NIL. *)

PROCEDURE dispose ( VAR stack : Stack ) : Stack;

VAR
    thisEntry : ListPtr;

BEGIN

    (* bail out if stack is NIL *)
    IF stack = NIL THEN
        RETURN NIL;
    END; (* IF *)
    
    (* deallocate any entries in stack's overflow list *)
    WHILE stack^.overflow # NIL DO
        
        (* isolate first entry in overflow list *)
        thisEntry := stack^.overflow;
        stack^.overflow := stack^.overflow^.next;
        
        (* deallocate the entry *)
        DISPOSE(thisEntry);
        
    END; (* WHILE *)
    
    (* deallocate stack object and pass NIL to caller *)
    DEALLOCATE(stack, TSIZE(Stack) + TSIZE(DataPtr) * (stack^.arraySize - 1));
    RETURN NIL

END dispose;


END LIFO.