Subversion Repositories tendra.SVN

Rev

Rev 5 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5 Rev 6
Line -... Line 1...
-
 
1
/*
-
 
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
 */
1
/*
31
/*
2
    		 Crown Copyright (c) 1997
32
    		 Crown Copyright (c) 1997
3
    
33
 
4
    This TenDRA(r) Computer Program is subject to Copyright
34
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
35
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
36
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
37
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
38
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
39
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
40
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
41
    shall be deemed to be acceptance of the following conditions:-
12
    
42
 
13
        (1) Its Recipients shall ensure that this Notice is
43
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
44
        reproduced upon any copies or amended versions of it;
15
    
45
 
16
        (2) Any amended version of it shall be clearly marked to
46
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
47
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
48
        for the relevant amendment or amendments;
19
    
49
 
20
        (3) Its onward transfer from a recipient to another
50
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
51
        party shall be deemed to be that party's acceptance of
22
        these conditions;
52
        these conditions;
23
    
53
 
24
        (4) DERA gives no warranty or assurance as to its
54
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
55
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
56
        no liability whatsoever in relation to any use to which
27
        it may be put.
57
        it may be put.
28
*/
58
*/
Line 41... Line 71...
41
    CREATE A NEW OBJECT
71
    CREATE A NEW OBJECT
42
 
72
 
43
    This routine allocates a new object of type v.
73
    This routine allocates a new object of type v.
44
*/
74
*/
45
 
75
 
46
object *new_object
76
object *
47
    PROTO_N ( ( v ) )
-
 
48
    PROTO_T ( long v )
77
new_object(long v)
49
{
78
{
50
    static object *free_objs = null ;
79
    static object *free_objs = null;
51
    static int objs_left = 0 ;
80
    static int objs_left = 0;
52
 
81
 
53
    object *p ;
82
    object *p;
54
    if ( objs_left == 0 ) {
83
    if (objs_left == 0) {
55
	objs_left = 200 ;
84
	objs_left = 200;
56
	free_objs = alloc_nof ( object, objs_left ) ;
85
	free_objs = alloc_nof(object, objs_left);
57
    }
86
    }
58
    objs_left-- ;
87
    objs_left--;
59
    p = free_objs + objs_left ;
88
    p = free_objs + objs_left;
60
    p->named = 0 ;
89
    p->named = 0;
61
    p->id = ( var_count [v] )++ ;
90
    p->id = (var_count[v]) ++;
62
    p->order = -1 ;
91
    p->order = -1;
63
    p->aux = null ;
92
    p->aux = null;
64
    if ( v == var_tag ) {
93
    if (v == var_tag) {
65
	var ( p ) = 3 ;
94
	var(p) = 3;
66
    } else if ( v == var_token ) {
95
    } else if (v == var_token) {
67
	is_foreign ( p ) = 0 ;
96
	is_foreign(p) = 0;
68
	res_sort ( p ) = sort_unknown ;
97
	res_sort(p) = sort_unknown;
69
	implicit_sort ( p ) = sort_unknown ;
98
	implicit_sort(p) = sort_unknown;
70
	arg_sorts ( p ) = null ;
99
	arg_sorts(p) = null;
71
    }
100
    }
72
    return ( p ) ;
101
    return(p);
73
}
102
}
74
 
103
 
75
 
104
 
76
/*
105
/*
77
    SPARE BINDING TABLE
106
    SPARE BINDING TABLE
78
 
107
 
79
    In fact only two binding tables are ever needed.  The one not in use
108
    In fact only two binding tables are ever needed.  The one not in use
80
    is stored in spare_bt.
109
    is stored in spare_bt.
81
*/
110
*/
82
 
111
 
83
static binding *spare_bt = null ;
112
static binding *spare_bt = null;
84
 
113
 
85
 
114
 
86
/*
115
/*
87
    CREATE A NEW BINDING TABLE
116
    CREATE A NEW BINDING TABLE
88
 
117
 
89
    This routine allocates a new binding table and initializes its entries.
118
    This routine allocates a new binding table and initializes its entries.
90
*/
119
*/
91
 
120
 
92
binding *new_binding_table
121
binding *
93
    PROTO_Z ()
122
new_binding_table(void)
94
{
123
{
95
    binding *bt ;
124
    binding *bt;
96
    long i, n = no_variables ;
125
    long i, n = no_variables;
97
    if ( n == 0 ) return ( null ) ;
126
    if (n == 0) return(null);
98
    if ( spare_bt ) {
127
    if (spare_bt) {
99
	bt = spare_bt ;
128
	bt = spare_bt;
100
	spare_bt = null ;
129
	spare_bt = null;
101
	for ( i = 0 ; i < n ; i++ ) {
130
	for (i = 0; i < n; i++) {
102
	    bt [i].max_no = 0 ;
131
	    bt[i].max_no = 0;
103
	}
132
	}
104
    } else {
133
    } else {
105
	bt = alloc_nof ( binding, n ) ;
134
	bt = alloc_nof(binding, n);
106
	for ( i = 0 ; i < n ; i++ ) {
135
	for (i = 0; i < n; i++) {
107
	    bt [i].max_no = 0 ;
136
	    bt[i].max_no = 0;
108
	    bt [i].sz = 0 ;
137
	    bt[i].sz = 0;
109
	    bt [i].table = null ;
138
	    bt[i].table = null;
110
	}
139
	}
111
    }
140
    }
112
    return ( bt ) ;
141
    return(bt);
113
}
142
}
114
 
143
 
115
 
144
 
116
/*
145
/*
117
    FREE A BINDING
146
    FREE A BINDING
118
 
147
 
119
    The binding bt is returned to free.
148
    The binding bt is returned to free.
120
*/
149
*/
121
 
150
 
122
void free_binding_table
151
void
123
    PROTO_N ( ( bt ) )
-
 
124
    PROTO_T ( binding *bt )
152
free_binding_table(binding *bt)
125
{
153
{
126
    spare_bt = bt ;
154
    spare_bt = bt;
127
    return ;
155
    return;
128
}
156
}
129
 
157
 
130
 
158
 
131
/*
159
/*
132
    SET THE SIZE OF A BINDING
160
    SET THE SIZE OF A BINDING
133
 
161
 
134
    The vth entry of bt is set to size n.
162
    The vth entry of bt is set to size n.
135
*/
163
*/
136
 
164
 
137
void set_binding_size
165
void
138
    PROTO_N ( ( bt, v, n ) )
-
 
139
    PROTO_T ( binding *bt X long v X long n )
166
set_binding_size(binding *bt, long v, long n)
140
{
167
{
141
    object **p ;
168
    object **p;
142
    binding *b ;
169
    binding *b;
143
    long i, m = n + 10 ;
170
    long i, m = n + 10;
144
    if ( v < 0 || v >= no_variables ) {
171
    if (v < 0 || v >= no_variables) {
145
	input_error ( "Illegal binding sort" ) ;
172
	input_error("Illegal binding sort");
146
	return ;
173
	return;
147
    }
174
    }
148
    b = bt + v ;
175
    b = bt + v;
149
    b->max_no = n ;
176
    b->max_no = n;
150
    if ( b->sz < m ) {
177
    if (b->sz < m) {
151
	p = realloc_nof ( b->table, object *, m ) ;
178
	p = realloc_nof(b->table, object *, m);
152
	b->sz = m ;
179
	b->sz = m;
153
	b->table = p ;
180
	b->table = p;
154
    } else {
181
    } else {
155
	p = b->table ;
182
	p = b->table;
156
    }
183
    }
157
    for ( i = 0 ; i < b->sz ; i++ ) p [i] = null ;
184
    for (i = 0; i < b->sz; i++)p[i] = null;
158
    return ;
185
    return;
159
}
186
}
160
 
187
 
161
 
188
 
162
/*
189
/*
163
    SET AN ENTRY IN A BINDING
190
    SET AN ENTRY IN A BINDING
164
 
191
 
165
    The nth entry of the vth entry of the binding bt is set to p.
192
    The nth entry of the vth entry of the binding bt is set to p.
166
*/
193
*/
167
 
194
 
168
void set_binding
195
void
169
    PROTO_N ( ( bt, v, n, p ) )
-
 
170
    PROTO_T ( binding *bt X long v X long n X object *p )
196
set_binding(binding *bt, long v, long n, object *p)
171
{
197
{
172
    binding *b ;
198
    binding *b;
173
    if ( v < 0 || v >= no_variables ) {
199
    if (v < 0 || v >= no_variables) {
174
	input_error ( "Illegal binding sort" ) ;
200
	input_error("Illegal binding sort");
175
	return ;
201
	return;
176
    }
202
    }
177
    b = bt + v ;
203
    b = bt + v;
178
    if ( n >= b->max_no || n < 0 ) {
204
    if (n >= b->max_no || n < 0) {
179
	out ( "<error>" ) ;
205
	out("<error>");
180
	input_error ( "Object number %ld (%s) too big", n, var_types [v] ) ;
206
	input_error("Object number %ld (%s) too big", n, var_types[v]);
181
	while ( n >= b->sz ) {
207
	while (n >= b->sz) {
182
	    /* Table is extended (errors only) */
208
	    /* Table is extended (errors only) */
183
	    long i, m = b->sz + 100 ;
209
	    long i, m = b->sz + 100;
184
	    b->sz = m ;
210
	    b->sz = m;
185
	    b->table = realloc_nof ( b->table, object *, m ) ;
211
	    b->table = realloc_nof(b->table, object *, m);
186
	    for ( i = 1 ; i <= 100 ; i++ ) b->table [ m - i ] = null ;
212
	    for (i = 1; i <= 100; i++)b->table[m - i] = null;
187
	}
213
	}
188
    }
214
    }
189
    if ( b->table [n] ) {
215
    if (b->table[n]) {
190
	input_error ( "Object %s (%s) already bound", object_name ( v, n ),
216
	input_error("Object %s (%s) already bound", object_name(v, n),
191
		      var_types [v] ) ;
217
		      var_types[v]);
192
    }
218
    }
193
    b->table [n] = p ;
219
    b->table[n] = p;
194
    return ;
220
    return;
195
}
221
}
196
 
222
 
197
 
223
 
198
/*
224
/*
199
    FILL IN BLANK ENTRIES IN A BINDING
225
    FILL IN BLANK ENTRIES IN A BINDING
200
 
226
 
201
    Objects are allocated for all the entries in the binding bt which
227
    Objects are allocated for all the entries in the binding bt which
202
    are not associated with an existing object.
228
    are not associated with an existing object.
203
*/
229
*/
204
 
230
 
205
void complete_binding
231
void
206
    PROTO_N ( ( bt ) )
-
 
207
    PROTO_T ( binding *bt )
232
complete_binding(binding *bt)
208
{
233
{
209
    long v ;
234
    long v;
210
    for ( v = 0 ; v < no_variables ; v++ ) {
235
    for (v = 0; v < no_variables; v++) {
211
	long i ;
236
	long i;
212
	binding *b = bt + v ;
237
	binding *b = bt + v;
213
	for ( i = 0 ; i < b->max_no ; i++ ) {
238
	for (i = 0; i < b->max_no; i++) {
214
	    if ( b->table [i] == null ) {
239
	    if (b->table[i] == null) {
215
		b->table [i] = new_object ( v ) ;
240
		b->table[i] = new_object(v);
216
	    }
241
	    }
217
	}
242
	}
218
    }
243
    }
219
    return ;
244
    return;
220
}
245
}
221
 
246
 
222
 
247
 
223
/*
248
/*
224
    FIND AN ENTRY IN A BINDING
249
    FIND AN ENTRY IN A BINDING
225
 
250
 
226
    The nth entry of the vth entry of binding bt is returned.
251
    The nth entry of the vth entry of binding bt is returned.
227
*/
252
*/
228
 
253
 
229
object *find_binding
254
object *
230
    PROTO_N ( ( bt, v, n ) )
-
 
231
    PROTO_T ( binding *bt X long v X long n )
255
find_binding(binding *bt, long v, long n)
232
{
256
{
233
    binding *b ;
257
    binding *b;
234
    if ( v < 0 || v >= no_variables ) {
258
    if (v < 0 || v >= no_variables) {
235
	input_error ( "Illegal binding sort" ) ;
259
	input_error("Illegal binding sort");
236
	return ( null ) ;
260
	return(null);
237
    }
261
    }
238
    b = bt + v ;
262
    b = bt + v;
239
    if ( n >= b->max_no || n < 0 ) {
263
    if (n >= b->max_no || n < 0) {
240
	out ( "<error>" ) ;
264
	out("<error>");
241
	input_error ( "Object number %ld (%s) too big", n, var_types [v] ) ;
265
	input_error("Object number %ld (%s) too big", n, var_types[v]);
242
    }
266
    }
243
    if ( n >= b->sz ) return ( null ) ;
267
    if (n >= b->sz) return(null);
244
    return ( b->table [n] ) ;
268
    return(b->table[n]);
245
}
269
}
246
 
270
 
247
 
271
 
248
/*
272
/*
249
    OUTPUT AN OBJECT
273
    OUTPUT AN OBJECT
250
 
274
 
251
    The object p of type v and number n is output.
275
    The object p of type v and number n is output.
252
*/
276
*/
253
 
277
 
254
void out_object
278
void
255
    PROTO_N ( ( n, p, v ) )
-
 
256
    PROTO_T ( long n X object *p X long v )
279
out_object(long n, object *p, long v)
257
{
280
{
258
    if ( v < 0 || v >= no_variables ) {
281
    if (v < 0 || v >= no_variables) {
259
	out ( "<error>" ) ;
282
	out("<error>");
260
	input_error ( "Illegal binding sort" ) ;
283
	input_error("Illegal binding sort");
261
	return ;
284
	return;
262
    }
285
    }
263
    if ( dumb_mode ) {
286
    if (dumb_mode) {
264
	word *w ;
287
	word *w;
265
	out_string ( var_types [v] ) ;
288
	out_string(var_types[v]);
266
	w = new_word ( HORIZ_BRACKETS ) ;
289
	w = new_word(HORIZ_BRACKETS);
267
	out_int ( n ) ;
290
	out_int(n);
268
	end_word ( w ) ;
291
	end_word(w);
269
	return ;
292
	return;
270
    }
293
    }
271
    if ( p == null ) {
294
    if (p == null) {
272
	p = find_binding ( crt_binding, v, n ) ;
295
	p = find_binding(crt_binding, v, n);
273
	if ( p == null ) {
296
	if (p == null) {
274
	    p = new_object ( v ) ;
297
	    p = new_object(v);
275
	    set_binding ( crt_binding, v, n, p ) ;
298
	    set_binding(crt_binding, v, n, p);
276
	}
299
	}
277
    }
300
    }
278
    if ( p->named ) {
301
    if (p->named) {
279
	if ( p->name.simple ) {
302
	if (p->name.simple) {
280
	    out ( p->name.val.str ) ;
303
	    out(p->name.val.str);
281
	} else {
304
	} else {
282
	    out_unique ( p->name.val.uniq ) ;
305
	    out_unique(p->name.val.uniq);
283
	}
306
	}
284
	return ;
307
	return;
285
    }
308
    }
286
    out_char ( '~' ) ;
309
    out_char('~');
287
    out_string ( var_types [v] ) ;
310
    out_string(var_types[v]);
288
    out_char ( '_' ) ;
311
    out_char('_');
289
    out_int ( p->id ) ;
312
    out_int(p->id);
290
    return ;
313
    return;
291
}
314
}
292
 
315
 
293
 
316
 
294
/*
317
/*
295
    RETURN AN OBJECT NAME (FOR ERROR REPORTING)
318
    RETURN AN OBJECT NAME (FOR ERROR REPORTING)
296
 
319
 
297
    The name of object type v and number n is returned.
320
    The name of object type v and number n is returned.
298
*/
321
*/
299
 
322
 
300
char *object_name
323
char *
301
    PROTO_N ( ( v, n ) )
-
 
302
    PROTO_T ( long v X long n )
324
object_name(long v, long n)
303
{
325
{
304
    object *p ;
326
    object *p;
305
    char *buff = alloc_nof ( char, 1000 ) ;
327
    char *buff = alloc_nof(char, 1000);
306
    if ( dumb_mode ) {
328
    if (dumb_mode) {
307
	IGNORE sprintf ( buff, "%ld", n ) ;
329
	IGNORE sprintf(buff, "%ld", n);
308
	return ( buff ) ;
330
	return(buff);
309
    }
331
    }
310
    p = find_binding ( crt_binding, v, n ) ;
332
    p = find_binding(crt_binding, v, n);
311
    if ( p->named ) {
333
    if (p->named) {
312
	if ( p->name.simple ) {
334
	if (p->name.simple) {
313
	    IGNORE sprintf ( buff, "%s", p->name.val.str ) ;
335
	    IGNORE sprintf(buff, "%s", p->name.val.str);
314
	} else {
336
	} else {
315
	    IGNORE sprintf ( buff, "unique(%ld)", p->id ) ;
337
	    IGNORE sprintf(buff, "unique(%ld)", p->id);
316
	}
338
	}
317
    } else {
339
    } else {
318
	IGNORE sprintf ( buff, "%ld", p->id ) ;
340
	IGNORE sprintf(buff, "%ld", p->id);
319
    }
341
    }
320
    return ( buff ) ;
342
    return(buff);
321
}
343
}