Subversion Repositories tendra.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
7 7u83 2
 * Copyright (c) 2002-2005 The TenDRA Project <http://www.tendra.org/>.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
11
 *    this list of conditions and the following disclaimer in the documentation
12
 *    and/or other materials provided with the distribution.
13
 * 3. Neither the name of The TenDRA Project nor the names of its contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific, prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
18
 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
 * EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 *
29
 * $Id$
30
 */
31
/*
2 7u83 32
    		 Crown Copyright (c) 1997
7 7u83 33
 
2 7u83 34
    This TenDRA(r) Computer Program is subject to Copyright
35
    owned by the United Kingdom Secretary of State for Defence
36
    acting through the Defence Evaluation and Research Agency
37
    (DERA).  It is made available to Recipients with a
38
    royalty-free licence for its use, reproduction, transfer
39
    to other parties and amendment for any purpose not excluding
40
    product development provided that any such use et cetera
41
    shall be deemed to be acceptance of the following conditions:-
7 7u83 42
 
2 7u83 43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
7 7u83 45
 
2 7u83 46
        (2) Any amended version of it shall be clearly marked to
47
        show both the nature of and the organisation responsible
48
        for the relevant amendment or amendments;
7 7u83 49
 
2 7u83 50
        (3) Its onward transfer from a recipient to another
51
        party shall be deemed to be that party's acceptance of
52
        these conditions;
7 7u83 53
 
2 7u83 54
        (4) DERA gives no warranty or assurance as to its
55
        quality or suitability for any purpose and DERA accepts
56
        no liability whatsoever in relation to any use to which
57
        it may be put.
58
*/
59
 
60
 
61
/*** alt.c --- Alternative ADT.
62
 *
63
 ** Author: Steve Folkes <smf@hermes.mod.uk>
64
 *
65
 *** Commentary:
66
 *
67
 * This file implements the alternative manipulation routines.  They are
68
 * specified in the file "rule.h".
69
 *
70
 *** Change Log:
71
 * $Log: alt.c,v $
72
 * Revision 1.1.1.1  1998/01/17  15:57:42  release
73
 * First version to be checked into rolling release.
74
 *
75
 * Revision 1.2  1994/12/15  09:57:59  smf
76
 * Brought into line with OSSG C Coding Standards Document, as per
77
 * "CR94_178.sid+tld-update".
78
 *
79
 * Revision 1.1.1.1  1994/07/25  16:04:31  smf
80
 * Initial import of SID 1.8 non shared files.
81
 *
82
**/
83
 
84
/****************************************************************************/
85
 
86
#include "rule.h"
87
#include "action.h"
88
#include "basic.h"
89
#include "name.h"
90
#include "type.h"
91
 
92
/*--------------------------------------------------------------------------*/
93
 
94
AltP
7 7u83 95
alt_create(void)
2 7u83 96
{
7 7u83 97
    AltP alt = ALLOCATE(AltT);
2 7u83 98
 
7 7u83 99
    alt->next      = NIL(AltP);
100
    types_init(alt_names(alt));
101
    bitvec_init(alt_first_set(alt));
102
    alt->item_head = NIL(ItemP);
2 7u83 103
    alt->item_tail = &(alt->item_head);
7 7u83 104
    return(alt);
2 7u83 105
}
106
 
107
AltP
7 7u83 108
alt_create_merge(ItemP initial_item, ItemP trailing_item,
109
		 TypeTransP translator, TableP table)
2 7u83 110
{
7 7u83 111
    AltP alt = alt_create();
2 7u83 112
 
7 7u83 113
    for (; initial_item; initial_item = item_next(initial_item)) {
114
	ItemP new_item = item_duplicate_and_translate(initial_item, translator,
115
						      table);
2 7u83 116
 
7 7u83 117
	alt_add_item(alt, new_item);
2 7u83 118
    }
7 7u83 119
    for (; trailing_item; trailing_item = item_next(trailing_item)) {
120
	ItemP new_item = item_duplicate(trailing_item);
2 7u83 121
 
7 7u83 122
	alt_add_item(alt, new_item);
2 7u83 123
    }
7 7u83 124
    return(alt);
2 7u83 125
}
126
 
127
AltP
7 7u83 128
alt_duplicate(AltP alt)
2 7u83 129
{
7 7u83 130
    AltP  new_alt = alt_create();
2 7u83 131
    ItemP item;
132
 
7 7u83 133
    for (item = alt_item_head(alt); item; item = item_next(item)) {
134
	ItemP new_item = item_duplicate(item);
2 7u83 135
 
7 7u83 136
	alt_add_item(new_alt, new_item);
2 7u83 137
    }
7 7u83 138
    return(new_alt);
2 7u83 139
}
140
 
141
BoolT
7 7u83 142
alt_less_than(AltP alt1, AltP alt2)
2 7u83 143
{
7 7u83 144
    ItemP      item1 = alt_item_head(alt1);
145
    ItemP      item2 = alt_item_head(alt2);
2 7u83 146
    KeyP       key1;
147
    KeyP       key2;
148
    TypeTupleP type1;
149
    TypeTupleP type2;
150
 
7 7u83 151
    if (item_type(item1) < item_type(item2)) {
152
	return(TRUE);
153
    } else if (item_type(item1) > item_type(item2)) {
154
	return(FALSE);
2 7u83 155
    }
7 7u83 156
    key1 = entry_key(item_entry(item1));
157
    key2 = entry_key(item_entry(item2));
158
    switch (key_compare(key1, key2)) EXHAUSTIVE {
2 7u83 159
      case CMP_LT:
7 7u83 160
	return(TRUE);
2 7u83 161
      case CMP_GT:
7 7u83 162
	return(FALSE);
2 7u83 163
      case CMP_EQ:
164
	break;
165
    }
7 7u83 166
    type1 = item_param(item1);
167
    type2 = item_param(item2);
168
    switch (types_compare(type1, type2)) EXHAUSTIVE {
2 7u83 169
      case CMP_LT:
7 7u83 170
	return(TRUE);
2 7u83 171
      case CMP_GT:
7 7u83 172
	return(FALSE);
2 7u83 173
      case CMP_EQ:
174
	break;
175
    }
7 7u83 176
    type1 = item_result(item1);
177
    type2 = item_result(item2);
178
    switch (types_compare(type1, type2)) EXHAUSTIVE {
2 7u83 179
      case CMP_LT:
7 7u83 180
	return(TRUE);
2 7u83 181
      case CMP_GT:
7 7u83 182
	return(FALSE);
2 7u83 183
      case CMP_EQ:
184
	break;
185
    }
186
    UNREACHED;
187
}
188
 
189
BoolT
7 7u83 190
alt_equal(AltP alt1, AltP alt2)
2 7u83 191
{
192
    ItemP item1;
193
    ItemP item2;
194
 
7 7u83 195
    if ((alt1 == NIL(AltP)) && (alt2 == NIL(AltP))) {
196
	return(TRUE);
197
    } else if ((alt1 == NIL(AltP)) || (alt2 == NIL(AltP))) {
198
	return(FALSE);
2 7u83 199
    }
7 7u83 200
    item1 = alt_item_head(alt1);
201
    item2 = alt_item_head(alt2);
2 7u83 202
    while (item1 && item2) {
7 7u83 203
	if ((item_entry(item1) == item_entry(item2)) &&
204
	    (types_equal_numbers(item_param(item1), item_param(item2))) &&
205
	    (types_equal_numbers(item_result(item1), item_result(item2)))) {
206
	    item1 = item_next(item1);
207
	    item2 = item_next(item2);
2 7u83 208
	} else {
7 7u83 209
	    return(FALSE);
2 7u83 210
	}
211
    }
7 7u83 212
    return(item1 == item2);
2 7u83 213
}
214
 
215
#ifdef FS_FAST
216
#undef alt_next
217
#endif /* defined (FS_FAST) */
218
AltP
7 7u83 219
alt_next(AltP alt)
2 7u83 220
{
7 7u83 221
    return(alt->next);
2 7u83 222
}
223
#ifdef FS_FAST
7 7u83 224
#define alt_next(a)	((a)->next)
2 7u83 225
#endif /* defined (FS_FAST) */
226
 
227
#ifdef FS_FAST
228
#undef alt_next_ref
229
#endif /* defined (FS_FAST) */
230
AltP *
7 7u83 231
alt_next_ref(AltP alt)
2 7u83 232
{
7 7u83 233
    return(&(alt->next));
2 7u83 234
}
235
#ifdef FS_FAST
7 7u83 236
#define alt_next_ref(a)	(&((a)->next))
2 7u83 237
#endif /* defined (FS_FAST) */
238
 
239
#ifdef FS_FAST
240
#undef alt_set_next
241
#endif /* defined (FS_FAST) */
242
void
7 7u83 243
alt_set_next(AltP alt1, AltP alt2)
2 7u83 244
{
245
    alt1->next = alt2;
246
}
247
#ifdef FS_FAST
7 7u83 248
#define alt_set_next(a1, a2)	((a1)->next = (a2))
2 7u83 249
#endif /* defined (FS_FAST) */
250
 
251
#ifdef FS_FAST
252
#undef alt_names
253
#endif /* defined (FS_FAST) */
254
TypeTupleP
7 7u83 255
alt_names(AltP alt)
2 7u83 256
{
7 7u83 257
    return(&(alt->names));
2 7u83 258
}
259
#ifdef FS_FAST
7 7u83 260
#define alt_names(a)	(&((a)->names))
2 7u83 261
#endif /* defined (FS_FAST) */
262
 
263
#ifdef FS_FAST
264
#undef alt_first_set
265
#endif /* defined (FS_FAST) */
266
BitVecP
7 7u83 267
alt_first_set(AltP alt)
2 7u83 268
{
7 7u83 269
    return(&(alt->first_set));
2 7u83 270
}
271
#ifdef FS_FAST
7 7u83 272
#define alt_first_set(a)	(&((a)->first_set))
2 7u83 273
#endif /* defined (FS_FAST) */
274
 
275
#ifdef FS_FAST
276
#undef alt_item_head
277
#endif /* defined (FS_FAST) */
278
ItemP
7 7u83 279
alt_item_head(AltP alt)
2 7u83 280
{
7 7u83 281
    return(alt->item_head);
2 7u83 282
}
283
#ifdef FS_FAST
7 7u83 284
#define alt_item_head(a)	((a)->item_head)
2 7u83 285
#endif /* defined (FS_FAST) */
286
 
287
ItemP
7 7u83 288
alt_unlink_item_head(AltP alt)
2 7u83 289
{
7 7u83 290
    ItemP item = alt_item_head(alt);
2 7u83 291
 
7 7u83 292
    alt->item_head = item_next(item);
293
    item_set_next(item, NIL(ItemP));
294
    if (alt->item_tail == item_next_ref(item)) {
2 7u83 295
	alt->item_tail = &(alt->item_head);
296
    }
7 7u83 297
    return(item);
2 7u83 298
}
299
 
300
void
7 7u83 301
alt_add_item(AltP alt, ItemP item)
2 7u83 302
{
303
    *(alt->item_tail) = item;
7 7u83 304
    alt->item_tail    = item_next_ref(item);
2 7u83 305
}
306
 
307
AltP
7 7u83 308
alt_deallocate(AltP alt)
2 7u83 309
{
7 7u83 310
    AltP  next = alt_next(alt);
2 7u83 311
    ItemP item;
312
 
7 7u83 313
    for (item = alt_item_head(alt); item; item = item_deallocate(item)) {
2 7u83 314
	/*NOTHING*/
315
    }
7 7u83 316
    types_destroy(alt_names(alt));
317
    bitvec_destroy(alt_first_set(alt));
318
    DEALLOCATE(alt);
319
    return(next);
2 7u83 320
}
321
 
322
void
7 7u83 323
write_alt(OStreamP ostream, AltP alt)
2 7u83 324
{
325
    ItemP item;
326
 
7 7u83 327
    for (item = alt_item_head(alt); item; item = item_next(item)) {
328
	write_tab(ostream);
329
	write_item(ostream, item);
330
	write_newline(ostream);
2 7u83 331
    }
332
}
333
 
334
void
7 7u83 335
write_alt_highlighting(OStreamP ostream, AltP alt, ItemP highlight)
2 7u83 336
{
337
    ItemP item;
338
 
7 7u83 339
    for (item = alt_item_head(alt); item; item = item_next(item)) {
2 7u83 340
	if (item == highlight) {
7 7u83 341
	    write_cstring(ostream, "==>>");
2 7u83 342
	}
7 7u83 343
	write_tab(ostream);
344
	write_item(ostream, item);
345
	write_newline(ostream);
2 7u83 346
    }
347
}
348
 
349
/*
350
 * Local variables(smf):
351
 * eval: (include::add-path-entry "../os-interface" "../library")
352
 * eval: (include::add-path-entry "../generated")
353
 * end:
354
**/