m2decs.c 16.9 KB
Newer Older
dcw's avatar
dcw committed
1
2
3
4
/* Generate data declarations in Modula-2 */


#include <dcw.h>
dcw's avatar
dcw committed
5
#include <string.h>
dcw's avatar
dcw committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "struct.h"
#include "decs.h"


/*
/^#ifdef HASPROTOS
!/endif$
stat %
*/

#ifdef HASPROTOS
static void line( char * , long , long , long , long );
static void defn_declns( char * , char * , declnlist );
static void defn_onetype( decln );
dcw's avatar
dcw committed
20
static void impln_declns( char * , char * , char * , declnlist );
dcw's avatar
dcw committed
21
22
23
24
25
26
27
28
29
30
static void impln_onetype( decln );
static void variantfields( decln );
static void normalfields( decln );
static void consproc_header( char * , shape );
static void consproc_body( decln , shape );
static void deconskind_header( char * );
static void deconskind_body( decln );
static void deconskind_inner( decln );
static void deconsproc_header( char * , shape );
static void deconsproc_body( decln , shape );
31
32
33
34
static void writeproc_header( char * );
static void writeproc_body( decln );
static void write_using_case( char * , char * , shapelist );
static void write_all_params( char * , shape );
dcw's avatar
dcw committed
35
36
37
static void write_default_printlist( shape );
static void write_printlist( shape , char * );
static void write_param( char * , param );
38
static void outstring( char * );
dcw's avatar
dcw committed
39
40
static void write_bool( void );
static char * lookup_type( char * );
41
static char * lookup_write_proc( char * );
dcw's avatar
dcw committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
static int predefined_type( char * );
#else
static void line();
static void defn_declns();
static void defn_onetype();
static void impln_declns();
static void impln_onetype();
static void variantfields();
static void normalfields();
static void consproc_header();
static void consproc_body();
static void deconskind_header();
static void deconskind_body();
static void deconskind_inner();
static void deconsproc_header();
static void deconsproc_body();
58
59
60
61
static void writeproc_header();
static void writeproc_body();
static void write_using_case();
static void write_all_params();
dcw's avatar
dcw committed
62
63
64
static void write_default_printlist();
static void write_printlist();
static void write_param();
65
static void outstring();
dcw's avatar
dcw committed
66
67
static void write_bool();
static char * lookup_type();
68
static char * lookup_write_proc();
dcw's avatar
dcw committed
69
70
71
72
73
74
75
static int predefined_type();
#endif


/* ------------------------- PUBLIC PROCEDURES --------------------------- */


dcw's avatar
dcw committed
76
void make_declns( exports, globals, begin, d, base ) declnlist d; char *exports, *globals, *begin, *base;
dcw's avatar
dcw committed
77
{
dcw's avatar
dcw committed
78
	printf( "m2datadec: Making data declarations in %s.{def,mod}\n", base );
dcw's avatar
dcw committed
79

dcw's avatar
dcw committed
80
	defn_declns( exports, base, d );
dcw's avatar
dcw committed
81

dcw's avatar
dcw committed
82
	impln_declns( globals, begin, base, d );
dcw's avatar
dcw committed
83
84
85
86
87
88
}


/* ------------------------- PRIVATE PROCEDURES -------------------------- */


dcw's avatar
dcw committed
89
90
91
92
static int numtabs = 0;
static FILE *outfile;


dcw's avatar
dcw committed
93
94
95
#define indent() numtabs++
#define outdent() numtabs--

96
#define nl()		fputc( '\n', outfile )
97
#define outchar(c)	fputc( c, outfile )
98
#define usefile(f)	outfile = f, numtabs = 0
dcw's avatar
dcw committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119


/*VARARGS*/
static void line( fmt, a, b, c, d ) char *fmt; long a, b, c, d;
{
	int i;

	for( i=numtabs; i; i-- ) fputc( '\t', outfile );
	fprintf( outfile, fmt, a, b, c, d );
	nl();
}


/* ------------------------- Definition module --------------------------- */


static void defn_declns( exports, modulename, decs ) char *exports, *modulename; declnlist decs;
{
	FILE		*defnfile;
        char		tempname[256];
	declnlist	d;
120
	char		*exportptr;
dcw's avatar
dcw committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

        sprintf( tempname, "%s.def", modulename );
        defnfile = fopen( tempname, "w" );
        ASSERT( defnfile != NULL, ("m2datadec: can't create '%s'\n",tempname) );

	usefile( defnfile );

	line( "DEFINITION MODULE %s ;", modulename );
	nl();
	line( "(*" );
	line( " * Automatically Generated by M2DataDec" );
	line( " *)" );
	nl();
	nl();
	line( "FROM FIO IMPORT File;" );
	nl();
	nl();

139
	exportptr=exports;
dcw's avatar
dcw committed
140
141
	if( *exports != '\0' )
	{
142
143
		int i;

dcw's avatar
dcw committed
144
		line( "(* Contents of EXPORT section *)" );
145
		for( ; *exportptr; exportptr++ )
146
147
148
149
150
151
152
153
154
155
156
		{
			if( *exportptr == '@' && exportptr[1] == '@'
			&&   exportptr[2] == '\n' )
			{
				exportptr += 2;
				break;
			}
			outchar( *exportptr );
		}

		nl();
dcw's avatar
dcw committed
157
158
159
160
161
162
163
164
165
166
167
		nl();
	}

	line( "(* Types - declared forward *)" );
	nl();
	line( "TYPE" );
	indent();
	for( d = decs; d != NULL; d=d->next )
	{
		if( d->Struct )
		{
168
			line( "%s;", d->name );
dcw's avatar
dcw committed
169
170
171
172
173
174
175
176
177
178
179
180
		} else
		{
			line( "%s\t= INTEGER;", d->name );
		}
	}
	outdent();
	nl();

	for( d = decs; d != NULL; d=d->next )
	{
		defn_onetype( d );
	}
181

182
183
184
185
186
187
188
189
	if( *exportptr != '\0' )
	{
		nl();
		line( "(* Remaining contents of EXPORT section *)" );
		line( exportptr );
		nl();
	}

dcw's avatar
dcw committed
190
191
192
193
194
195
196
197
198
	line( "END %s.", modulename );
	nl();
	fclose( defnfile );
}


static void defn_onetype( d ) decln d;
{
	shapelist	s;
199
	BOOL		first;
dcw's avatar
dcw committed
200
201
202
203
204
205
206
207
208
209
210

	line( "(* ----------- Type %s ----------- *)", d->name );
	nl();

	line( "(* Constructor functions for %s *)", d->name );
	for( s = d->shapes; s != NULL; s = s->next )
	{
		consproc_header( d->name, s );
	}
	nl();

211
	if( d->ManyShapes )
dcw's avatar
dcw committed
212
	{
213
214
215
216
		line( "(* Kind of %s *)", d->name );
		line( "TYPE" );
		fprintf( outfile, "\tKindOf%s = ( ", d->name );
		first = TRUE;
dcw's avatar
dcw committed
217
218
		for( s = d->shapes; s; s=s->next )
		{
219
220
221
			if( !first ) fputs( ", ", outfile );
			fprintf( outfile, "%sIs%s", d->name, s->name );
			first = FALSE;
dcw's avatar
dcw committed
222
		}
223
		fprintf( outfile, " );\n" );
dcw's avatar
dcw committed
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
		nl();
		line( "(* Deconstructor kind function for %s *)",
			d->name );
		deconskind_header( d->name );
		nl();
	}

	if( d->Struct )
	{
		line( "(* Deconstructor functions for %s *)", d->name );

		for( s = d->shapes; s != NULL; s = s->next )
		{
			if( s->params != NULL )
			{
				deconsproc_header( d->name, s );
			}
		}
		nl();
	}

245
246
	line( "(* write function for %s *)", d->name );
	writeproc_header( d->name );
247
	nl();
dcw's avatar
dcw committed
248
249
250
251
252
253
254
	nl();
}


/* ----------------------- Implementation module ------------------------- */


dcw's avatar
dcw committed
255
static void impln_declns( globals, begin, modulename, decs ) char *globals, *begin, *modulename; declnlist decs;
dcw's avatar
dcw committed
256
257
258
{
	FILE		*implnfile;
        char		tempname[256];
259
	declnlist	d;
260
	char		*globalptr;
dcw's avatar
dcw committed
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278

        sprintf( tempname, "%s.mod", modulename );
        implnfile = fopen( tempname, "w" );
        ASSERT( implnfile!=NULL, ("m2datadec: can't create '%s'\n",tempname) );

	usefile( implnfile );

	line( "IMPLEMENTATION MODULE %s ;", modulename );
	nl();
	line( "(*" );
	line( " * Automatically Generated by M2DataDec" );
	line( " *)" );
	nl();
	nl();
	line( "FROM Assertions IMPORT Assert, Abort;" );
	nl();
	line( "FROM FIO IMPORT" );
	indent();
279
	line( "File, WriteString, WriteChar, WriteInteger," );
280
	line( "WriteCardinal, WriteReal, WriteLine, WriteLn;" );
dcw's avatar
dcw committed
281
282
283
284
285
286
	nl();
	outdent();
	line( "FROM Storage IMPORT ALLOCATE;" );
	nl();
	nl();

287
	globalptr = globals;
dcw's avatar
dcw committed
288
289
290
	if( *globals != '\0' )
	{
		line( "(* Contents of GLOBAL section *)" );
291
		for( ; *globalptr; globalptr++ )
292
293
294
295
296
297
298
299
300
301
		{
			if( *globalptr == '@' && globalptr[1] == '@'
			&&   globalptr[2] == '\n' )
			{
				globalptr += 2;
				break;
			}
			outchar( *globalptr );
		}

dcw's avatar
dcw committed
302
303
304
		nl();
	}

305
306
307
308
	line( "(* Pointer type declarations *)" );
	line( "TYPE" );
	indent();
	for( d = decs; d != NULL; d=d->next )
dcw's avatar
dcw committed
309
	{
310
311
312
313
314
		if( d->Struct )
		{
			line( "%s\t= POINTER TO %sRec;", d->name, d->name );
			nl();
		}
dcw's avatar
dcw committed
315
	}
316
317
318
	outdent();

	for( d = decs; d != NULL; d=d->next )
dcw's avatar
dcw committed
319
	{
320
		impln_onetype( d );
dcw's avatar
dcw committed
321
	}
322
	write_bool();
323

324
325
326
327
328
329
330
331
	if( *globalptr != '\0' )
	{
		nl();
		nl();
		line( "(* Remaining contents of GLOBAL section *)" );
		line( globalptr );
		nl();
	}
dcw's avatar
dcw committed
332
333

	line( "BEGIN" );
dcw's avatar
dcw committed
334
335
336
337
338
	if( *begin != '\0' )
	{
		line( "\t(* Contents of BEGIN section *)" );
		line( begin );
	}
dcw's avatar
dcw committed
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
	line( "END %s.", modulename );
	fclose( implnfile );
}


static void impln_onetype( d ) decln d;
{
	shapelist s;

	nl();
	line( "(* ---- Type %s ---- *)", d->name );
	nl();

	if( d->Struct )
	{
		line( "TYPE" );
		indent();
356
		line( "%sRec = RECORD", d->name );
dcw's avatar
dcw committed
357
358
359
360
361
362
363
		indent();

		if( d->Union )
		{
			variantfields( d );
		} else if( d->TagField )
		{
364
			line( "tag\t: KindOf%s;", d->name );
dcw's avatar
dcw committed
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
			normalfields( d );
		} else
		{
			normalfields( d );
		}
		outdent();
		line( "END;" );
		nl();

		outdent();
	}
	nl();

	for( s = d->shapes; s != NULL; s = s->next )
	{
		consproc_body( d, s );
	}

383
	if( d->ManyShapes )
dcw's avatar
dcw committed
384
385
386
387
388
389
390
391
392
393
394
395
	{
		deconskind_body( d );
	}

	for( s = d->shapes; s != NULL; s = s->next )
	{
		if( s->params != NULL )
		{
			deconsproc_body( d, s );
		}
	}

396
	writeproc_body( d );
dcw's avatar
dcw committed
397
398
399
400
401
402
403
404
405
406
407
}


/* ------------------------- Fields of record ---------------------------- */


static void variantfields( d ) decln d;
{
	shapelist	s;
	paramlist	p;

408
	line( "CASE tag : KindOf%s OF", d->name );
dcw's avatar
dcw committed
409
410
411

	for( s = d->shapes; s != NULL; s=s->next )
	{
412
		line( "|%sIs%s:", d->name, s->name );
dcw's avatar
dcw committed
413
414
415
		indent();
		for( p = s->params; p != NULL; p=p->next )
		{
416
417
			line( "%s%s\t: %s;", s->name, p->name,
			      lookup_type(p->type) );
dcw's avatar
dcw committed
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
		}
		outdent();
	}

	line( "END;" );
}


static void normalfields( d ) decln d;
{
	shapelist	s;
	paramlist	p;

	for( s = d->shapes; s != NULL; s=s->next )
	{
		for( p = s->params; p != NULL; p=p->next )
		{
435
436
			line( "%s%s\t: %s;", s->name, p->name,
			      lookup_type(p->type) );
dcw's avatar
dcw committed
437
438
439
440
441
442
443
444
445
446
447
448
449
		}
	}
}


/* ----------------------- Construction procedures ----------------------- */


static void consproc_header( name, s ) char *name; shape s;
{
	paramlist	p;
	BOOL		first;

450
	fprintf( outfile, "PROCEDURE %s%s( ", name, s->name );
dcw's avatar
dcw committed
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
	first = TRUE;
	for( p = s->params; p != NULL ; p = p->next )
	{
		if( !first ) fputs( "; ", outfile );
		fprintf( outfile, "%s : %s ",
			 p->name, lookup_type(p->type) );
		first = FALSE;
	}
	fprintf( outfile, ") : %s;\n", name );
}


static void consproc_body( d, s ) decln d; shape s;
{
	paramlist p;

	consproc_header( d->name, s );
	nl();

	if( d->Struct && ! ( d->UseNull && d->shapes == s ) )
	{
		line( "VAR\tnew : %s;", d->name );
		nl();
	}
	line( "BEGIN" );
	indent();

	if( d->UseNull && d->shapes == s )
	{
		line( "RETURN NIL;" );
	} else if( ! d->Struct )
	{
483
484
485
486
487
488
489
490
		if( d->ManyShapes )
		{
			line( "RETURN %s( ORD(%sIs%s) );",
				d->name, d->name, s->name );
		} else
		{
			line( "RETURN %s( 0 );", d->name );
		}
dcw's avatar
dcw committed
491
492
493
494
495
	} else
	{
		line( "NEW( new );", d->name );
		if( d->TagField )
		{
496
			line( "new^.tag\t:= %sIs%s;", d->name, s->name );
dcw's avatar
dcw committed
497
498
499
		}
		for( p=s->params; p != NULL; p=p->next )
		{
500
			line( "new^.%s%s\t:= %s;", s->name, p->name, p->name );
dcw's avatar
dcw committed
501
502
503
504
505
		}
		line( "RETURN new;" );
	}

	outdent();
506
	line( "END %s%s;", d->name, s->name );
dcw's avatar
dcw committed
507
508
509
510
511
512
513
514
515
516
	nl();
	nl();
}


/* -------------------- Deconstruction Kind procedure -------------------- */


static void deconskind_header( name ) char *name;
{
517
	line( "PROCEDURE %sKind( this : %s ) : KindOf%s;", name, name, name );
dcw's avatar
dcw committed
518
519
520
521
522
523
524
525
526
527
528
}


static void deconskind_body( d ) decln d;
{
	deconskind_header( d->name );
	nl();
	line( "BEGIN" );
	indent();
	deconskind_inner( d );
	outdent();
529
	line( "END %sKind;", d->name );
dcw's avatar
dcw committed
530
531
532
533
534
535
536
537
538
539
540
	nl();
	nl();
}


static void deconskind_inner( d ) decln d;
{
	shapelist s = d->shapes;

	if( ! d->Struct )		/* enumerated type */
	{
541
		line( "RETURN VAL(KindOf%s, this);", d->name );
dcw's avatar
dcw committed
542
543
544
545
546
547
548
		return;
	}
	if( d->UseNull )
	{
		line( "IF this = NIL" );
		line( "THEN" );
		indent();
549
		line( "RETURN %sIs%s;", d->name, s->name );
dcw's avatar
dcw committed
550
551
552
553
554
555
556
557
558
		outdent();
		line( "END; (* IF *)" );
		s = s->next;
	}
	if( d->TagField )
	{
		line( "RETURN this^.tag;" );
	} else
	{
559
		line( "RETURN %sIs%s;", d->name, s->name );
dcw's avatar
dcw committed
560
561
562
563
564
565
566
567
568
569
570
	}
}


/* ---------------------- Deconstruction procedures ---------------------- */


static void deconsproc_header( name, s ) char *name; shape s;
{
	paramlist	p;

571
	fprintf( outfile, "PROCEDURE Get%s%s( this : %s ", name, s->name,
dcw's avatar
dcw committed
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
		 	  name );

	for( p = s->params; p != NULL ; p=p->next )
	{
		fprintf( outfile, "; VAR %s : %s ", p->name,
			 lookup_type(p->type) );
	}
	fprintf( outfile, ");\n" );
}


static void deconsproc_body( d, s ) decln d; shape s;
{
	paramlist p;

	deconsproc_header( d->name, s );
	nl();
	line( "BEGIN" );
	indent();

	for( p=s->params; p != NULL; p=p->next )
	{
594
		line( "%s\t:= this^.%s%s;", p->name, s->name, p->name );
dcw's avatar
dcw committed
595
596
597
	}

	outdent();
598
	line( "END Get%s%s;", d->name, s->name );
dcw's avatar
dcw committed
599
600
601
602
603
	nl();
	nl();
}


604
/* --------------------------- Write procedure --------------------------- */
dcw's avatar
dcw committed
605
606


607
static void writeproc_header( name ) char *name;
dcw's avatar
dcw committed
608
{
609
	line( "PROCEDURE Write%s( f : File; this : %s );", name, name );
dcw's avatar
dcw committed
610
611
612
}


613
static void writeproc_body( d ) decln d;
dcw's avatar
dcw committed
614
615
616
{
	shapelist shapes;

617
	writeproc_header( d->name );
dcw's avatar
dcw committed
618
	nl();
dcw's avatar
dcw committed
619
620
621
622
623
624
625

	if( d->PutLoop )
	{
		line( "VAR over : BOOLEAN;" );
		nl();
	}

dcw's avatar
dcw committed
626
627
	line( "BEGIN" );
	indent();
dcw's avatar
dcw committed
628
629
630
631
632
633
634
635
636

	if( d->PutLoop )
	{
		line( "REPEAT" );
		indent();
		line( "over := TRUE;" );
		nl();
	}

dcw's avatar
dcw committed
637
638
639
640
641
642
	shapes = d->shapes;
	if( d->UseNull )
	{
		line( "IF this = NIL" );
		line( "THEN" );
		indent();
dcw's avatar
dcw committed
643
		write_all_params( d->name, /* first */ shapes );
dcw's avatar
dcw committed
644
		outdent();
dcw's avatar
dcw committed
645
646
		line( "ELSE" );
		indent();
dcw's avatar
dcw committed
647
648
		shapes = shapes->next;
	}
dcw's avatar
dcw committed
649

dcw's avatar
dcw committed
650
651
	if( d->TagField )
	{
652
		write_using_case( "this^.tag", d->name, shapes );
dcw's avatar
dcw committed
653
654
	} else if( d->Struct )	/* only one shape left */
	{
655
		write_all_params( d->name, shapes );
dcw's avatar
dcw committed
656
657
	} else			/* enumerated type */
	{
658
659
		char tag[256];

660
661
662
663
664
665
666
667
		if( d->ManyShapes )
		{
			sprintf( tag, "VAL(KindOf%s, this)", d->name );
			write_using_case( tag, d->name, shapes );
		} else
		{
			line( "WriteString( f, \"%s\" );", shapes->name );
		}
dcw's avatar
dcw committed
668
	}
dcw's avatar
dcw committed
669
670
671
672
673
674
675
676
677
678
679
	if( d->UseNull )
	{
		outdent();
		line( "END; (* IF *)" );
	}

	if( d->PutLoop )
	{
		outdent();
		line( "UNTIL over;" );
	}
dcw's avatar
dcw committed
680
	outdent();
681
	line( "END Write%s;", d->name );
dcw's avatar
dcw committed
682
683
684
685
	nl();
}


686
static void write_using_case( tag, dname, s ) char *tag, *dname; shapelist s;
dcw's avatar
dcw committed
687
688
689
690
{
	line( "CASE %s OF", tag );
	for( ; s != NULL; s = s->next )
	{
691
		line( "|%sIs%s:", dname, s->name );
dcw's avatar
dcw committed
692
		indent();
693
		write_all_params( dname, s );
dcw's avatar
dcw committed
694
695
696
697
		outdent();
	}
	line( "ELSE" );
	indent();
698
	line( "Abort( \"Write%s: impossible tag\" );", dname );
dcw's avatar
dcw committed
699
700
701
702
703
	outdent();
	line( "END; (* CASE *)" );
}


dcw's avatar
dcw committed
704
static void write_all_params( dname, s ) char *dname; shape s;
dcw's avatar
dcw committed
705
{
dcw's avatar
dcw committed
706
707
708
709
710
711
712
	if( s->pl == NULL )
	{
		write_default_printlist( s );
	} else
	{
		write_printlist( s, dname );
	}
dcw's avatar
dcw committed
713
714
715
}


dcw's avatar
dcw committed
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
static void write_default_printlist( s ) shape s;
{
	BOOL		first;
	paramlist	p = s->params;

	/* No print items given - use defaults */

	if( p )
	{
		line( "WriteString( f, \"%s(\" );", s->name );
		for( first = TRUE; p != NULL; p = p->next )
		{
			if( ! first ) line( "WriteChar( f, \",\" );" );
			first = FALSE;
			write_param( s->name, p );
		}
		line( "WriteChar( f, ')' );" );
	} else
	{
		line( "WriteString( f, \"%s\" );", s->name );
	}
}


static void write_printlist( s, dname ) shape s; char *dname;
dcw's avatar
dcw committed
741
{
dcw's avatar
dcw committed
742
743
	/* Some print items given - use them */

dcw's avatar
dcw committed
744
	printlist	pl;
dcw's avatar
dcw committed
745
746
	param		p;
	int		n;
dcw's avatar
dcw committed
747

dcw's avatar
dcw committed
748
749
750
	/* All but the last item */

	for( pl = s->pl; pl->next != NULL; pl = pl->next )
dcw's avatar
dcw committed
751
	{
dcw's avatar
dcw committed
752
		if( pl->item->tag == printitem_is_str )
dcw's avatar
dcw committed
753
		{
dcw's avatar
dcw committed
754
			outstring( pl->item->str );
755
756
		} else
		{
dcw's avatar
dcw committed
757
758
759
			p = findnthparam( pl->item->num, s->params,
					  s->name, dname );
			write_param( s->name, p );
dcw's avatar
dcw committed
760
		}
dcw's avatar
dcw committed
761
762
763
764
765
766
767
	}

	/* Last item */

	if( pl->item->tag == printitem_is_str )
	{
		outstring( pl->item->str );
dcw's avatar
dcw committed
768
769
	} else
	{
dcw's avatar
dcw committed
770
771
		p = findnthparam( pl->item->num, s->params, s->name, dname );
		if( streq( p->type, dname ) )
dcw's avatar
dcw committed
772
		{
dcw's avatar
dcw committed
773
774
775
776
777
			line( "this := this^.%s%s;", s->name, p->name );
			line( "over := FALSE;" );
		} else
		{
			write_param( s->name, p );
dcw's avatar
dcw committed
778
779
780
781
782
		}
	}
}


dcw's avatar
dcw committed
783
784
785
786
787
788
789
static void write_param( sname, p ) char *sname; param p;
{
	line( "%s( f, this^.%s%s );",
	      lookup_write_proc(p->type), sname, p->name );
}


790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
static void outstring( str ) char *str;
{
	char *p;
	BOOL over = FALSE;

	while( ! over )
	{
		p = strstr( str, "\\n" );
		if( p == NULL )
		{
			line( "WriteString( f, \"%s\" );", str );
			over = TRUE;
		} else
		{
			if( p == str )
			{
				line( "WriteLine( f );" );
			} else
			{
				line( "WriteLn( f, \"%.*s\" );", (p-str), str );
			}
			if( *(p+2) != '\0' )
			{
				str = p + 2;
			} else
			{
				over = TRUE;
			}
		}
	}
}


dcw's avatar
dcw committed
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
static void write_bool()
{
	line( "PROCEDURE WriteBool( f : File; bool : BOOLEAN );" );
	nl();
	line( "BEGIN" );
	indent();
	line( "IF bool" );
	line( "THEN" );
	indent();
	line( "WriteString( f, \"TRUE\" );" );
	outdent();
	line( "ELSE" );
	indent();
	line( "WriteString( f, \"FALSE\" );" );
	outdent();
	line( "END;" );
	outdent();
	line( "END WriteBool;" );
}


/* --------------------- Predefined type lookup system  ------------------ */


struct typelookup
{
	char	*name;
	char	*type_name;
851
	char	*write_name;
dcw's avatar
dcw committed
852
853
854
855
856
};


static struct typelookup lookup[] = {
	"int",		"INTEGER",	"WriteInteger",	
857
	"Int",		"INTEGER",	"WriteInteger",	
dcw's avatar
dcw committed
858
859
	"INT",		"INTEGER",	"WriteInteger",	
	"INTEGER",	"INTEGER",	"WriteInteger",	
860
861
862
863
	"card",		"CARDINAL",	"WriteCardinal",	
	"Card",		"CARDINAL",	"WriteCardinal",	
	"CARD",		"CARDINAL",	"WriteCardinal",	
	"CARDINAL",	"CARDINAL",	"WriteCardinal",	
dcw's avatar
dcw committed
864
	"string",	"string",	"WriteString",	
865
	"String",	"String",	"WriteString",	
dcw's avatar
dcw committed
866
	"real",		"REAL",		"WriteReal",	
867
	"Real",		"REAL",		"WriteReal",	
dcw's avatar
dcw committed
868
	"REAL",		"REAL",		"WriteReal",	
869
870
	"float",	"REAL",		"WriteReal",	
	"Float",	"REAL",		"WriteReal",	
dcw's avatar
dcw committed
871
	"char",		"CHAR",		"WriteChar",	
872
	"Char",		"CHAR",		"WriteChar",	
dcw's avatar
dcw committed
873
874
	"CHAR",		"CHAR",		"WriteChar",	
	"bool",		"BOOLEAN",	"WriteBool",	
875
	"Bool",		"BOOLEAN",	"WriteBool",	
dcw's avatar
dcw committed
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
	"BOOL",		"BOOLEAN",	"WriteBool",	
	"BOOLEAN",	"BOOLEAN",	"WriteBool",
};

#define NUM_LOOKUP_TYPES	(sizeof(lookup)/sizeof(lookup[0]))


static char *lookup_type( typename ) char *typename;
{
	int	predef;

	predef = predefined_type( typename );
	return (predef == -1) ? typename : lookup[predef].type_name;
}


892
static char *lookup_write_proc( typename ) char *typename;
dcw's avatar
dcw committed
893
894
895
896
897
898
899
{
	int	predef;
	static  char tmp[256];

	predef = predefined_type( typename );
	if( predef == -1 )
	{
900
		sprintf( tmp, "Write%s", typename );
dcw's avatar
dcw committed
901
902
903
		return tmp;
	} else
	{
904
		return lookup[predef].write_name;
dcw's avatar
dcw committed
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
	}
}


static int predefined_type( typename ) char *typename;
{
	int i;

	for( i=0; i<NUM_LOOKUP_TYPES; i++ )
	{
		if( streq( lookup[i].name, typename ) )
		{
			return i;
		}
	}
	return -1;
}