Warning: Attempt to read property "date" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247
WebSVN – tendra.SVN – Blame – /branches/tendra5/src/tools/disp/sort.c – Rev 2

Subversion Repositories tendra.SVN

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
#include "config.h"
32
#include "types.h"
33
#include "ascii.h"
34
#include "basic.h"
35
#include "binding.h"
36
#include "capsule.h"
37
#include "file.h"
38
#include "sort.h"
39
#include "tree.h"
40
#include "tdf.h"
41
#include "unit.h"
42
#include "utility.h"
43
 
44
 
45
/*
46
    WARN ABOUT UNDECLARED TOKENS
47
*/
48
 
49
int warn_undeclared = 0 ;
50
 
51
 
52
/*
53
    DECODING TOKENS
54
 
55
    Simple TOKENs are represented by TDF integers.  They may also be
56
    tokenised themselves.
57
*/
58
 
59
object *de_token_aux
60
    PROTO_N ( ( s, nm ) )
61
    PROTO_T ( sortname s X char *nm )
62
{
63
    word *w ;
64
    long bits, t ;
65
    object *obj = null ;
66
    int ap = 1, simple = 1 ;
67
    int just_tok = ( s == sort_unknown ? 1 : 0 ) ;
68
 
69
    /* Find the token number */
70
    long n = de_token () ;
71
    if ( n == token_make_tok ) {
72
	t = tdf_int () ;
73
    } else {
74
	simple = 0 ;
75
    }
76
 
77
    /* Look up simple tokens */
78
    if ( simple ) {
79
	SET ( t ) ;
80
	obj = find_binding ( crt_binding, var_token, t ) ;
81
	if ( obj == null ) {
82
	    obj = new_object ( var_token ) ;
83
	    set_binding ( crt_binding, var_token, t, obj ) ;
84
	}
85
 
86
	/* Check token sort */
87
	if ( res_sort ( obj ) == sort_unknown ) {
88
	    sortname is = implicit_sort ( obj ) ;
89
	    if ( is == sort_unknown && warn_undeclared ) {
90
		int old_recover = recover ;
91
		int old_exit_status = exit_status ;
92
		recover = 1 ;
93
		input_error ( "Warning : token %s used before it is declared",
94
			      object_name ( var_token, t ) ) ;
95
		recover = old_recover ;
96
		exit_status = old_exit_status ;
97
	    }
98
	    if ( is != sort_unknown && is != s ) {
99
		sortid es ;
100
		out ( "<error>" ) ;
101
		es = find_sort ( s ) ;
102
		input_error ( "Implicit sort error, token %s, %s expected",
103
			      object_name ( var_token, t ), es.name ) ;
104
	    }
105
	    implicit_sort ( obj ) = s ;
106
	} else if ( res_sort ( obj ) != s && !just_tok ) {
107
	    sortid es ;
108
	    out ( "<error>" ) ;
109
	    es = find_sort ( s ) ;
110
	    input_error ( "Sort error, token %s, %s expected",
111
			  object_name ( var_token, t ), es.name ) ;
112
	}
113
 
114
	/* Output token name if appropriate */
115
	if ( !dumb_mode ) {
116
	     if ( obj->named ) {
117
		if ( obj->name.simple ) {
118
		    out_string ( obj->name.val.str ) ;
119
		    ap = 0 ;
120
		}
121
	    } else {
122
		char buff [50] ;
123
		IGNORE sprintf ( buff, "~token_%ld", obj->id ) ;
124
		out_string ( buff ) ;
125
		ap = 0 ;
126
	    }
127
	}
128
    }
129
 
130
    /* Output "apply_token" if appropriate */
131
    if ( ap ) {
132
	if ( just_tok ) {
133
	    out_string ( "make_token" ) ;
134
	} else {
135
	    out_string ( "apply_" ) ;
136
	    out_string ( nm ) ;
137
	    out_string ( "_token" ) ;
138
	}
139
	w = new_word ( VERT_BRACKETS ) ;
140
	if ( simple ) {
141
	    SET ( t ) ;
142
	    out_object ( t, obj, var_token ) ;
143
	} else {
144
	    if ( n == token_token_apply_token ) {
145
		object *subobj = de_token_aux ( sort_token, "token" ) ;
146
		if ( subobj ) obj = subobj->aux ;
147
	    } else {
148
		/* use_tokdef */
149
		long len = tdf_int () ;
150
		skip_bits ( len ) ;
151
		out_string ( "use_tokdef(....)" ) ;
152
		IGNORE new_word ( SIMPLE ) ;
153
	    }
154
	}
155
    } else {
156
	/* Applications of named tokens are indicated by "*" */
157
	out_string ( "*" ) ;
158
    }
159
 
160
    /* Quit here if just reading token */
161
    if ( just_tok ) {
162
	if ( ap ) {
163
	    SET ( w ) ;
164
	    end_word ( w ) ;
165
	} else {
166
	    IGNORE new_word ( SIMPLE ) ;
167
	}
168
	return ( obj ) ;
169
    }
170
 
171
    /* Read length of token arguments */
172
    bits = tdf_int () ;
173
 
174
    /* Deal with tokens without arguments */
175
    if ( bits == 0 ) {
176
	if ( obj && res_sort ( obj ) != sort_unknown ) {
177
	    char *ps = arg_sorts ( obj ) ;
178
	    if ( ps && *ps ) {
179
		if ( simple ) {
180
		    SET ( t ) ;
181
		    input_error ( "Token arguments missing, token %s",
182
				  object_name ( var_token, t ) ) ;
183
		} else {
184
		    input_error ( "Token arguments missing" ) ;
185
		}
186
	    }
187
	}
188
	if ( ap ) {
189
	    SET ( w ) ;
190
	    end_word ( w ) ;
191
	} else {
192
	    IGNORE new_word ( SIMPLE ) ;
193
	}
194
	return ( obj ) ;
195
    }
196
 
197
    /* Deal with tokens with arguments */
198
    if ( obj && res_sort ( obj ) != sort_unknown && !is_foreign ( obj ) ) {
199
	/* Known token - decode arguments */
200
	if ( arg_sorts ( obj ) ) {
201
	    long p = posn ( here ) ;
202
	    if ( !ap ) w = new_word ( VERT_BRACKETS ) ;
203
	    decode ( arg_sorts ( obj ) ) ;
204
	    if ( p + bits != posn ( here ) ) {
205
		if ( simple ) {
206
		    SET ( t ) ;
207
		    input_error ( "Token arguments length wrong, token %s",
208
				  object_name ( var_token, t ) ) ;
209
		} else {
210
		    input_error ( "Token arguments length wrong" ) ;
211
		}
212
	    }
213
	} else {
214
	    if ( ap ) {
215
		SET ( w ) ;
216
		end_word ( w ) ;
217
	    } else {
218
		IGNORE new_word ( SIMPLE ) ;
219
	    }
220
	    return ( obj ) ;
221
	}
222
    } else {
223
	/* Unknown token - step over arguments */
224
	if ( !ap ) w = new_word ( VERT_BRACKETS ) ;
225
	out ( "...." ) ;
226
	skip_bits ( bits ) ;
227
    }
228
    SET ( w ) ;
229
    end_word ( w ) ;
230
    return ( obj ) ;
231
}
232
 
233
 
234
/*
235
    DECODING SIMPLE LABELS
236
*/
237
 
238
void de_make_label
239
    PROTO_N ( ( lab_no ) )
240
    PROTO_T ( long lab_no )
241
{
242
    if ( dumb_mode ) {
243
	word *w ;
244
	out_string ( "label" ) ;
245
	w = new_word ( HORIZ_BRACKETS ) ;
246
	out_int ( lab_no ) ;
247
	end_word ( w ) ;
248
    } else {
249
	out_string ( "~label_" ) ;
250
	out_int ( lab_no ) ;
251
    }
252
    if ( lab_no < 0 || lab_no >= max_lab_no ) {
253
	input_error ( "Label number %ld out of range", lab_no ) ;
254
    }
255
    return ;
256
}
257
 
258
 
259
/*
260
    FORMATTING SIZE FOR TDF STRINGS
261
 
262
    A string will be split by de_format_string into sections of length
263
    at most STRING_WIDTH.
264
*/
265
 
266
#define STRING_WIDTH		40
267
 
268
 
269
/*
270
    DECODING FORMATTED STRINGS
271
 
272
    A TDF string is read and output in a formatted form.
273
*/
274
 
275
void de_tdfstring_format
276
    PROTO_Z ()
277
{
278
    string s ;
279
    word *ptr1 ;
280
    long sz = tdf_int () ;
281
    long n = tdf_int () ;
282
    if ( sz != 8 ) {
283
	char sbuff [100] ;
284
	IGNORE sprintf ( sbuff, "make_string_%ld", sz ) ;
285
	out_string ( sbuff ) ;
286
	ptr1 = new_word ( HORIZ_BRACKETS ) ;
287
    }
288
    if ( sz > 8 ) {
289
	long i ;
290
	for ( i = 0 ; i < n ; i++ ) {
291
	    long v = fetch ( ( int ) sz ) ;
292
	    out_int ( v ) ;
293
	}
294
    } else {
295
	s = get_string ( n, sz ) ;
296
	n = ( long ) strlen ( s ) ;
297
	if ( n == 0 ) {
298
	    out ( "\"\"" ) ;
299
	    return ;
300
	}
301
	while ( n ) {
302
	    long m = ( n < STRING_WIDTH ? n : STRING_WIDTH ) ;
303
	    char *w = alloc_nof ( char, m + 3 ) ;
304
	    IGNORE memcpy ( w + 1, s, ( size_t ) m ) ;
305
	    w [0] = QUOTE ;
306
	    w [ m + 1 ] = QUOTE ;
307
	    w [ m + 2 ] = 0 ;
308
	    out ( w ) ;
309
	    n -= m ;
310
	    s += m ;
311
	}
312
    }
313
    if ( sz != 8 ) {
314
	SET ( ptr1 ) ;
315
	end_word ( ptr1 ) ;
316
    }
317
    return ;
318
}
319
 
320
 
321
/*
322
    DECODING THE EXP "solve" (OR "labelled")
323
 
324
    This is tricky because it is encoded as :
325
 
326
		    A1, ..., An, B, C1, ..., Cn
327
 
328
    where n is a TDF integer, Ai is given by the decode string str1,
329
    B is given by str2, and Ci is given by str3, but we want to print
330
    it in the order :
331
 
332
		      B, A1, C1, ..., An, Cn
333
 
334
    so there is a certain amount of to-ing and fro-ing.
335
*/
336
 
337
void de_solve_fn
338
    PROTO_N ( ( nm, str1, str2, str3, ntwice ) )
339
    PROTO_T ( char *nm X char *str1 X char *str2 X char *str3 X int ntwice )
340
{
341
    long i, n ;
342
    word *ptr1, *ptr2 ;
343
    place posn1, posn2 ;
344
 
345
    int tempflag = printflag ;
346
 
347
    out_string ( nm ) ;
348
    ptr1 = new_word ( VERT_BRACKETS ) ;
349
 
350
    /* Read the number of statements A1, ..., An */
351
    check_list () ;
352
    n = tdf_int () ;
353
 
354
    /* Record the position of A1 */
355
    posn1.byte = here.byte ;
356
    posn1.bit = here.bit ;
357
 
358
    /* Step over A1, ..., An */
359
    printflag = 0 ;
360
    for ( i = 0 ; i < n ; i++ ) decode ( str1 ) ;
361
    printflag = tempflag ;
362
 
363
    /* Decode B */
364
    decode ( str2 ) ;
365
 
366
    if ( ntwice ) {
367
	/* Read and check the number of statements C1, ..., Cn */
368
	long m ;
369
	check_list () ;
370
	m = tdf_int () ;
371
	if ( m != n ) input_error ( "Illegal %s construct", nm ) ;
372
    }
373
 
374
    for ( i = 0 ; i < n ; i++ ) {
375
	ptr2 = new_word ( VERT_BRACKETS ) ;
376
 
377
	/* Record the position of Ci */
378
	posn2.byte = here.byte ;
379
	posn2.bit = here.bit ;
380
 
381
	/* Go back and read Ai */
382
	set_place ( &posn1 ) ;
383
	decode ( str1 ) ;
384
 
385
	/* Record the position of A(i+1) */
386
	posn1.byte = here.byte ;
387
	posn1.bit = here.bit ;
388
 
389
	/* Go forward and read Ci */
390
	set_place ( &posn2 ) ;
391
	decode ( str3 ) ;
392
 
393
	end_word ( ptr2 ) ;
394
    }
395
    end_word ( ptr1 ) ;
396
    return ;
397
}
398
 
399
 
400
/*
401
    DECODING THE EXP "case"
402
 
403
    Only the layout makes this a special case.  The general form is :
404
 
405
		      A, L1, B1, ..., Ln, Bn
406
 
407
    where A is given by the decode string str1, Li is a label and Bi
408
    is given by str2.
409
*/
410
 
411
void de_case_fn
412
    PROTO_N ( ( nm, str1, str2 ) )
413
    PROTO_T ( char *nm X char *str1 X char *str2 )
414
{
415
    long i, n ;
416
    word *ptr1, *ptr2, *ptr3 ;
417
 
418
    out_string ( nm ) ;
419
    ptr1 = new_word ( VERT_BRACKETS ) ;
420
    decode ( str1 ) ;
421
    ptr2 = new_word ( VERT_BRACKETS ) ;
422
    check_list () ;
423
    n = tdf_int () ;
424
    for ( i = 0 ; i < n ; i++ ) {
425
	ptr3 = new_word ( HORIZ_NONE ) ;
426
	IGNORE de_label () ;
427
	out ( ":" ) ;
428
	format ( HORIZ_BRACKETS, "", str2 ) ;
429
	end_word ( ptr3 ) ;
430
    }
431
    end_word ( ptr2 ) ;
432
    end_word ( ptr1 ) ;
433
    return ;
434
}
435
 
436
 
437
/*
438
    DECODING THE EXP "make_proc"
439
 
440
    The general form is :
441
 
442
			A, B1, ..., Bn, C
443
 
444
    where A is given by the decode string str1, B by str2 and C by str3.
445
    However each Bi is grouped as a "make_proc_arg".
446
*/
447
 
448
void de_mk_proc_fn
449
    PROTO_N ( ( nm, str1, str2, str3 ) )
450
    PROTO_T ( char *nm X char *str1 X char *str2 X char *str3 )
451
{
452
    long i, n ;
453
    word *ptr ;
454
    out_string ( nm ) ;
455
    ptr = new_word ( VERT_BRACKETS ) ;
456
    decode ( str1 ) ;
457
    check_list () ;
458
    n = tdf_int () ;
459
    if ( n == 0 ) {
460
	out ( "empty" ) ;
461
    } else {
462
	for ( i = 0 ; i < n ; i++ ) {
463
	    out_string ( nm ) ;
464
	    format ( VERT_BRACKETS, "_arg", str2 ) ;
465
	}
466
    }
467
    decode ( str3 ) ;
468
    end_word ( ptr ) ;
469
    return ;
470
}