2 |
7u83 |
1 |
/*
|
|
|
2 |
Crown Copyright (c) 1997
|
|
|
3 |
|
|
|
4 |
This TenDRA(r) Computer Program is subject to Copyright
|
|
|
5 |
owned by the United Kingdom Secretary of State for Defence
|
|
|
6 |
acting through the Defence Evaluation and Research Agency
|
|
|
7 |
(DERA). It is made available to Recipients with a
|
|
|
8 |
royalty-free licence for its use, reproduction, transfer
|
|
|
9 |
to other parties and amendment for any purpose not excluding
|
|
|
10 |
product development provided that any such use et cetera
|
|
|
11 |
shall be deemed to be acceptance of the following conditions:-
|
|
|
12 |
|
|
|
13 |
(1) Its Recipients shall ensure that this Notice is
|
|
|
14 |
reproduced upon any copies or amended versions of it;
|
|
|
15 |
|
|
|
16 |
(2) Any amended version of it shall be clearly marked to
|
|
|
17 |
show both the nature of and the organisation responsible
|
|
|
18 |
for the relevant amendment or amendments;
|
|
|
19 |
|
|
|
20 |
(3) Its onward transfer from a recipient to another
|
|
|
21 |
party shall be deemed to be that party's acceptance of
|
|
|
22 |
these conditions;
|
|
|
23 |
|
|
|
24 |
(4) DERA gives no warranty or assurance as to its
|
|
|
25 |
quality or suitability for any purpose and DERA accepts
|
|
|
26 |
no liability whatsoever in relation to any use to which
|
|
|
27 |
it may be put.
|
|
|
28 |
*/
|
|
|
29 |
|
|
|
30 |
|
|
|
31 |
/**********************************************************************
|
|
|
32 |
$Author: release $
|
|
|
33 |
$Date: 1998/01/17 15:55:45 $
|
|
|
34 |
$Revision: 1.1.1.1 $
|
|
|
35 |
$Log: table_fns.c,v $
|
|
|
36 |
* Revision 1.1.1.1 1998/01/17 15:55:45 release
|
|
|
37 |
* First version to be checked into rolling release.
|
|
|
38 |
*
|
|
|
39 |
* Revision 1.2 1997/08/23 13:31:07 pwe
|
|
|
40 |
* no invert order, and initial ANDF-DE
|
|
|
41 |
*
|
|
|
42 |
* Revision 1.1 1995/04/06 10:43:34 currie
|
|
|
43 |
* Initial revision
|
|
|
44 |
*
|
|
|
45 |
***********************************************************************/
|
|
|
46 |
|
|
|
47 |
|
|
|
48 |
#include "config.h"
|
|
|
49 |
#include "common_types.h"
|
|
|
50 |
#include "readglob.h"
|
|
|
51 |
#include "xalloc.h"
|
|
|
52 |
#include "basicread.h"
|
|
|
53 |
#include "sortmacs.h"
|
|
|
54 |
#include "exp.h"
|
|
|
55 |
#include "expmacs.h"
|
|
|
56 |
#include "main_reads.h"
|
|
|
57 |
#include "externs.h"
|
|
|
58 |
#include "spec_tok.h"
|
|
|
59 |
#include "read_sort.h"
|
|
|
60 |
#include "messages_r.h"
|
|
|
61 |
#include "installglob.h"
|
|
|
62 |
#include "install_fns.h"
|
|
|
63 |
#include "diagglob.h"
|
|
|
64 |
#include "dg_fns.h"
|
|
|
65 |
#include "check.h"
|
|
|
66 |
|
|
|
67 |
#include "table_fns.h"
|
|
|
68 |
|
|
|
69 |
|
|
|
70 |
|
|
|
71 |
|
|
|
72 |
/* VARIABLES */
|
|
|
73 |
/* The initial values are only to give something to push, jmf */
|
|
|
74 |
|
|
|
75 |
|
|
|
76 |
|
|
|
77 |
|
|
|
78 |
context * crt_context = (context*)0;
|
|
|
79 |
|
|
|
80 |
/* IDENTITY */
|
|
|
81 |
|
|
|
82 |
static tokformals_list nil_params;
|
|
|
83 |
|
|
|
84 |
|
|
|
85 |
/* PROCEDURES */
|
|
|
86 |
|
|
|
87 |
|
|
|
88 |
exp get_lab
|
|
|
89 |
PROTO_N ( (l) )
|
|
|
90 |
PROTO_T ( label l )
|
|
|
91 |
{
|
|
|
92 |
/* find the exp which is labelled by l */
|
|
|
93 |
context * con = crt_context;
|
|
|
94 |
while (con != (context*)0) {
|
|
|
95 |
lab_con * lc = con->labs;
|
|
|
96 |
while (lc != (lab_con*)0) {
|
|
|
97 |
if (lc->namel == l) return lc->e;
|
|
|
98 |
lc = lc->rest;
|
|
|
99 |
}
|
|
|
100 |
con = con->outer;
|
|
|
101 |
}
|
|
|
102 |
return *l;
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
void set_lab
|
|
|
106 |
PROTO_N ( (l, e) )
|
|
|
107 |
PROTO_T ( label l X exp e )
|
|
|
108 |
{
|
|
|
109 |
/* set the exp which is labelled by l */
|
|
|
110 |
if (crt_context == (context*)0 || crt_context->recursive == 0) {
|
|
|
111 |
*l = e;
|
|
|
112 |
}
|
|
|
113 |
else {
|
|
|
114 |
lab_con * lc = (lab_con*)xmalloc(sizeof(lab_con));
|
|
|
115 |
lc->namel = l; lc->e = e; lc->rest = crt_context->labs;
|
|
|
116 |
crt_context->labs = lc;
|
|
|
117 |
}
|
|
|
118 |
return;
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
|
|
|
122 |
dec * get_dec
|
|
|
123 |
PROTO_N ( (tg) )
|
|
|
124 |
PROTO_T ( int tg )
|
|
|
125 |
{
|
|
|
126 |
/* find the tag declaration indexed by tg */
|
|
|
127 |
dec * res = unit_ind_tags[tg];
|
|
|
128 |
res->dec_u.dec_val.index = tg;
|
|
|
129 |
return res;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
aldef * get_aldef
|
|
|
133 |
PROTO_N ( (tg) )
|
|
|
134 |
PROTO_T ( int tg )
|
|
|
135 |
{
|
|
|
136 |
/* find the alignment tag value indexed by tg */
|
|
|
137 |
return unit_ind_als[tg];
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
tok_define * get_tok
|
|
|
141 |
PROTO_N ( (tk) )
|
|
|
142 |
PROTO_T ( int tk )
|
|
|
143 |
{
|
|
|
144 |
/* find the token declaration indexed by tg */
|
|
|
145 |
context * con = crt_context;
|
|
|
146 |
while (con != (context *)0) {
|
|
|
147 |
int n = con->no_toks;
|
|
|
148 |
int nl = (n > LOCAL_TOKS)?LOCAL_TOKS:n;
|
|
|
149 |
tok_define * cbind = &con->loctoks[0];
|
|
|
150 |
int i;
|
|
|
151 |
for(i=0; i<nl; i++) {
|
|
|
152 |
if (tk == cbind[i].tdtoken) {
|
|
|
153 |
return & cbind[i];
|
|
|
154 |
}
|
|
|
155 |
}
|
|
|
156 |
cbind = con->othertoks;
|
|
|
157 |
for(i=LOCAL_TOKS; i<n; i++) {
|
|
|
158 |
if (tk == cbind[i-LOCAL_TOKS].tdtoken) {
|
|
|
159 |
return & cbind[i-LOCAL_TOKS];
|
|
|
160 |
}
|
|
|
161 |
}
|
|
|
162 |
con = con->outer;
|
|
|
163 |
}
|
|
|
164 |
return unit_ind_tokens[tk];
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
|
|
|
168 |
exp get_tag
|
|
|
169 |
PROTO_N ( (tg) )
|
|
|
170 |
PROTO_T ( tag tg )
|
|
|
171 |
{
|
|
|
172 |
/* find the exp known as tg */
|
|
|
173 |
context * con = crt_context;
|
|
|
174 |
while (con != (context*)0) {
|
|
|
175 |
tag_con * tc = con->tags;
|
|
|
176 |
while (tc != (tag_con*)0) {
|
|
|
177 |
if (tc->namet == tg) return tc->e;
|
|
|
178 |
tc = tc->rest;
|
|
|
179 |
}
|
|
|
180 |
con = con->outer;
|
|
|
181 |
}
|
|
|
182 |
return tg -> dec_u.dec_val.dec_exp;
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
|
|
|
186 |
|
|
|
187 |
void set_tag
|
|
|
188 |
PROTO_N ( (tg, e) )
|
|
|
189 |
PROTO_T ( tag tg X exp e )
|
|
|
190 |
{
|
|
|
191 |
/* set the exp known as tg */
|
|
|
192 |
if (crt_context == (context*)0 || crt_context->recursive == 0) {
|
|
|
193 |
tg -> dec_u.dec_val.dec_exp = e;
|
|
|
194 |
}
|
|
|
195 |
else {
|
|
|
196 |
tag_con * tc = (tag_con*)xmalloc(sizeof(tag_con));
|
|
|
197 |
tc->namet = tg; tc->e = e; tc->rest = crt_context->tags;
|
|
|
198 |
crt_context->tags = tc;
|
|
|
199 |
}
|
|
|
200 |
return;
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
|
|
|
204 |
/* apply tk to its parameters in pars, and return the result */
|
|
|
205 |
tokval apply_tok
|
|
|
206 |
PROTO_N ( (td, pars, sortcode, actual_pars) )
|
|
|
207 |
PROTO_T ( token td X bitstream pars X int sortcode X tokval * actual_pars )
|
|
|
208 |
{
|
|
|
209 |
if (td -> tok_special || td -> defined == 0)
|
|
|
210 |
/* handle the special tokens */
|
|
|
211 |
{ tokval tkv;
|
|
|
212 |
int done = 0;
|
|
|
213 |
tkv = special_token(td, pars, sortcode, &done);
|
|
|
214 |
if (done)
|
|
|
215 |
return tkv;
|
|
|
216 |
};
|
|
|
217 |
|
|
|
218 |
if (td -> defined == 0)
|
|
|
219 |
{
|
|
|
220 |
/* detect various errors and give helpful information */
|
|
|
221 |
if (td -> defined == 0)
|
|
|
222 |
failer(UNDEFINED_TOK);
|
|
|
223 |
|
|
|
224 |
if (td -> is_capsule_token &&
|
|
|
225 |
td -> tok_index < capsule_no_of_tokens &&
|
|
|
226 |
td -> tok_index >= 0 &&
|
|
|
227 |
td -> tok_name != (char*)0)
|
|
|
228 |
IGNORE fprintf(stderr, "token is: %s\n", td -> tok_name);
|
|
|
229 |
else
|
|
|
230 |
{
|
|
|
231 |
if (td -> is_capsule_token &&
|
|
|
232 |
td -> tok_index < capsule_no_of_tokens &&
|
|
|
233 |
td -> tok_index >= 0)
|
|
|
234 |
IGNORE fprintf(stderr, "capsule token number: %d\n", td -> tok_index);
|
|
|
235 |
else
|
|
|
236 |
{
|
|
|
237 |
if (td -> tok_index >= 0 &&
|
|
|
238 |
td -> tok_index < unit_no_of_tokens)
|
|
|
239 |
IGNORE fprintf(stderr, "local unit token number: %d\n",
|
|
|
240 |
td -> tok_index);
|
|
|
241 |
else
|
|
|
242 |
IGNORE fprintf(stderr, "token number out of bounds\n");
|
|
|
243 |
};
|
|
|
244 |
};
|
|
|
245 |
exit(EXIT_FAILURE);
|
|
|
246 |
};
|
|
|
247 |
|
|
|
248 |
{
|
|
|
249 |
int npars = td -> params.number; /* number of parameters */
|
|
|
250 |
context new_context; /* to construct the bindings for this expansion */
|
|
|
251 |
context * old_context = crt_context;
|
|
|
252 |
|
|
|
253 |
tokval val;
|
|
|
254 |
place old_place; /* to record the current place in the input stream */
|
|
|
255 |
tok_define * new_bindings;
|
|
|
256 |
int i,j;
|
|
|
257 |
dec * * old_tagtab;
|
|
|
258 |
/* to remember the current tag table */
|
|
|
259 |
exp* old_labtab;
|
|
|
260 |
/* to remember the current label table */
|
|
|
261 |
tok_define * * old_toktab;
|
|
|
262 |
/* to remember the current token table */
|
|
|
263 |
aldef * * old_altab;
|
|
|
264 |
/* to remember the current alignment tag table */
|
|
|
265 |
diag_tagdef * * old_diagtab; /* OLD DIAGS */
|
|
|
266 |
dgtag_struct * * old_dgtab; /* NEW DIAGS */
|
|
|
267 |
|
|
|
268 |
/* now remember them */
|
|
|
269 |
old_tagtab = unit_ind_tags;
|
|
|
270 |
old_labtab = unit_labtab;
|
|
|
271 |
old_toktab = unit_ind_tokens;
|
|
|
272 |
old_altab = unit_ind_als;
|
|
|
273 |
old_diagtab = unit_ind_diagtags; /* OLD DIAGS */
|
|
|
274 |
old_dgtab = unit_ind_dgtags; /* NEW DIAGS */
|
|
|
275 |
|
|
|
276 |
new_context.no_toks = (short) npars;
|
|
|
277 |
nil_params.number = 0;
|
|
|
278 |
|
|
|
279 |
if (td -> valpresent &&
|
|
|
280 |
(td -> unit_number == crt_tagdef_unit_no ))
|
|
|
281 |
{
|
|
|
282 |
/* if a value has already been computed
|
|
|
283 |
(there will be no parameters) */
|
|
|
284 |
|
|
|
285 |
if (sortcode == f_exp.code)
|
|
|
286 |
{tokval v;
|
|
|
287 |
v.tk_exp = copy(td -> tdvalue.tk_exp);
|
|
|
288 |
/* copy it if the result is an expression since
|
|
|
289 |
we may be going to alter it */
|
|
|
290 |
return v;
|
|
|
291 |
}
|
|
|
292 |
else {
|
|
|
293 |
return td -> tdvalue;
|
|
|
294 |
};
|
|
|
295 |
};
|
|
|
296 |
|
|
|
297 |
old_place = keep_place(); /* remember the current input stream */
|
|
|
298 |
set_place(pars);
|
|
|
299 |
|
|
|
300 |
/* now set up the new parameter bindings */
|
|
|
301 |
if (npars > LOCAL_TOKS) {
|
|
|
302 |
new_context.othertoks = (tok_define *) xcalloc (npars-LOCAL_TOKS,
|
|
|
303 |
sizeof (tok_define));
|
|
|
304 |
}
|
|
|
305 |
|
|
|
306 |
new_bindings = &new_context.loctoks[0];
|
|
|
307 |
|
|
|
308 |
|
|
|
309 |
for (j=0; j<npars; ++j)
|
|
|
310 |
{
|
|
|
311 |
/* read in the parameter values and bind them to the formals */
|
|
|
312 |
sortname sn;
|
|
|
313 |
exp old_crt_repeat; /* XX008 */
|
|
|
314 |
if (j>=LOCAL_TOKS) {
|
|
|
315 |
i = j-LOCAL_TOKS; new_bindings = new_context.othertoks;
|
|
|
316 |
}
|
|
|
317 |
else i = j;
|
|
|
318 |
sn = (td -> params.par_sorts)[j];
|
|
|
319 |
new_bindings[i].tdsort = sn;
|
|
|
320 |
/* parameter sort */
|
|
|
321 |
new_bindings[i].tdtoken = (td -> params.par_names)[j];
|
|
|
322 |
/* formal */
|
|
|
323 |
new_bindings[i].valpresent = 1;
|
|
|
324 |
/* the value is known */
|
|
|
325 |
new_bindings[i].unit_number = crt_tagdef_unit_no;
|
|
|
326 |
new_bindings[i].re_evaluate = 0;
|
|
|
327 |
/* and does not need re-evaluating */
|
|
|
328 |
new_bindings[i].params = nil_params;
|
|
|
329 |
/* it has no parameters itself */
|
|
|
330 |
|
|
|
331 |
if (sn.code == f_exp.code) { /* XX008 */
|
|
|
332 |
old_crt_repeat = crt_repeat;
|
|
|
333 |
crt_repeat = nilexp;
|
|
|
334 |
}
|
|
|
335 |
else {
|
|
|
336 |
SET(old_crt_repeat);
|
|
|
337 |
};
|
|
|
338 |
if (actual_pars != (tokval*)0) {
|
|
|
339 |
new_bindings[i].tdvalue = actual_pars[j]; /* this is not used at present*/
|
|
|
340 |
}
|
|
|
341 |
else {
|
|
|
342 |
new_bindings[i].tdvalue = read_sort(sn.code);
|
|
|
343 |
}
|
|
|
344 |
/* read a parameter */
|
|
|
345 |
if (sn.code == f_exp.code) { /* XX008 */
|
|
|
346 |
crt_repeat = old_crt_repeat;
|
|
|
347 |
};
|
|
|
348 |
|
|
|
349 |
new_bindings[i].defined = 1;
|
|
|
350 |
/* and say it is defined */
|
|
|
351 |
new_bindings[i].tok_special = 0;
|
|
|
352 |
/* and say it is not special */
|
|
|
353 |
new_bindings[i].recursive = (sn.code == TOKEN);
|
|
|
354 |
/* and say it is not recursive for simple sorts */
|
|
|
355 |
new_bindings[i].tok_context = crt_context;
|
|
|
356 |
};
|
|
|
357 |
|
|
|
358 |
set_place(td -> tdplace); /* set up the place to read the definition */
|
|
|
359 |
|
|
|
360 |
new_context.recursive = td -> recursive;
|
|
|
361 |
new_context.outer = td -> tok_context;
|
|
|
362 |
new_context.tags = (tag_con *)0;
|
|
|
363 |
new_context.labs = (lab_con *)0;
|
|
|
364 |
|
|
|
365 |
crt_context = &new_context;
|
|
|
366 |
|
|
|
367 |
/* now set up the tables which belong to the place where the
|
|
|
368 |
token was defined */
|
|
|
369 |
unit_ind_tags = td -> my_tagtab;
|
|
|
370 |
unit_labtab = td -> my_labtab;
|
|
|
371 |
unit_ind_tokens = td -> my_toktab;
|
|
|
372 |
unit_ind_als = td -> my_altab;
|
|
|
373 |
unit_ind_diagtags = td -> my_diagtab; /* OLD DIAGS */
|
|
|
374 |
unit_ind_dgtags = td -> my_dgtab; /* NEW DIAGS */
|
|
|
375 |
|
|
|
376 |
/* read the body of the definition */
|
|
|
377 |
td -> recursive = 1; /* set up to detect recursion */
|
|
|
378 |
val = read_sort(sortcode);
|
|
|
379 |
td -> recursive = new_context.recursive;
|
|
|
380 |
|
|
|
381 |
set_place(old_place); /* restore the place in the input stream */
|
|
|
382 |
|
|
|
383 |
new_bindings = &new_context.loctoks[0];
|
|
|
384 |
|
|
|
385 |
for (j = 0; j < npars; ++j) {/* kill off exps (they were copied) */
|
|
|
386 |
tok_define * q;
|
|
|
387 |
if (j>=LOCAL_TOKS) {
|
|
|
388 |
i = j-LOCAL_TOKS; new_bindings = new_context.othertoks;
|
|
|
389 |
}
|
|
|
390 |
else i = j;
|
|
|
391 |
q = &new_bindings[i];
|
|
|
392 |
if (q -> tdsort.code == f_exp.code) {
|
|
|
393 |
exp ek = q -> tdvalue.tk_exp;
|
|
|
394 |
kill_exp (ek, ek);
|
|
|
395 |
};
|
|
|
396 |
};
|
|
|
397 |
|
|
|
398 |
/* restore the old environment of tables */
|
|
|
399 |
crt_context = old_context;
|
|
|
400 |
unit_ind_tags = old_tagtab;
|
|
|
401 |
unit_labtab = old_labtab;
|
|
|
402 |
unit_ind_tokens = old_toktab;
|
|
|
403 |
unit_ind_als = old_altab;
|
|
|
404 |
unit_ind_diagtags = old_diagtab; /* OLD DIAGS */
|
|
|
405 |
unit_ind_dgtags = old_dgtab; /* NEW DIAGS */
|
|
|
406 |
|
|
|
407 |
if (!doing_aldefs && npars == 0 && new_context.recursive == 0)
|
|
|
408 |
{
|
|
|
409 |
/* if there were no parameters, record the value for the
|
|
|
410 |
next application of the token */
|
|
|
411 |
td -> valpresent = 1;
|
|
|
412 |
td -> tdvalue = val;
|
|
|
413 |
if (sortcode == f_exp.code)
|
|
|
414 |
{tokval v;
|
|
|
415 |
v.tk_exp = copy(val.tk_exp);
|
|
|
416 |
/* if we are remembering it we must copy, because the
|
|
|
417 |
returned value might be altered */
|
|
|
418 |
IGNORE hold(val.tk_exp);
|
|
|
419 |
return v;
|
|
|
420 |
};
|
|
|
421 |
}
|
|
|
422 |
else
|
|
|
423 |
if (npars > LOCAL_TOKS)
|
|
|
424 |
xfree((void *)new_context.othertoks);
|
|
|
425 |
/* free the space used for parameter binding */
|
|
|
426 |
while(new_context.tags != (tag_con*)0) {
|
|
|
427 |
tag_con * r = new_context.tags;
|
|
|
428 |
new_context.tags = new_context.tags->rest;
|
|
|
429 |
xfree((void *)r);
|
|
|
430 |
}
|
|
|
431 |
while(new_context.labs != (lab_con*)0) {
|
|
|
432 |
lab_con * r = new_context.labs;
|
|
|
433 |
new_context.labs = new_context.labs->rest;
|
|
|
434 |
xfree((void *)r);
|
|
|
435 |
}
|
|
|
436 |
return val;
|
|
|
437 |
};
|
|
|
438 |
}
|
|
|
439 |
|
|
|
440 |
|
|
|
441 |
|