decs.c 6.39 KB
Newer Older
dcw's avatar
dcw committed
1
2
3
4
5
6
#include <dcw.h>
#include "struct.h"
#include "decs.h"


#ifdef HASPROTOS
dcw's avatar
dcw committed
7
8
9
10
11
12
13
14
15
16
static void init( FILE * , FILE * , char * );
static void data_decls( FILE * , declnlist );
static void one_proto( FILE * , char * , shapelist , BOOL );
static void protos( FILE * , declnlist , BOOL );
static void cons_fn_hdr( FILE * , char * , shapelist );
static void cons_fn_body( FILE * , char * , shapelist );
static void cons_fns( FILE * , declnlist );
static void print_fns( FILE * , declnlist );
static void print_fn_shape( FILE * , char * , shapelist );
static void print_param( FILE *, char * , char * , char * );
dcw's avatar
dcw committed
17
#else
dcw's avatar
dcw committed
18
19
20
21
22
23
24
25
26
27
static void init();
static void data_decls();
static void one_proto();
static void protos();
static void cons_fn_hdr();
static void cons_fn_body();
static void cons_fns();
static void print_fns();
static void print_fn_shape();
static void print_param();
dcw's avatar
dcw committed
28
29
30
#endif


dcw's avatar
dcw committed
31
static void init( h, c, basename ) FILE *c, *h; char *basename;
dcw's avatar
dcw committed
32
33
34
35
{
	fprintf( h, "/*\n" );
	fprintf( h, "\tAutomatically Generated by DataDec\n" );
	fprintf( h, " */\n\n");
dcw's avatar
dcw committed
36
37
38
39
	fprintf( h, "typedef char *string;\n\n" );
	fprintf( h, "typedef char BOOL;\n" );
	fprintf( h, "#define TRUE 0\n" );
	fprintf( h, "#define FALSE 1\n\n" );
dcw's avatar
dcw committed
40
41
42
43
44

	fprintf( c, "/*\n" );
	fprintf( c, "\tAutomatically Generated by DataDec\n" );
	fprintf( c, " */\n\n");

dcw's avatar
dcw committed
45
46
47
48
49
	fprintf( c, "#include <stdio.h>\n" );
	fprintf( c, "#include \"%s.h\"\n\n\n", basename );

	fprintf( c, "#define NEW(t) ((t *)malloc(sizeof(t)))\n\n" );
	fprintf( c, "#define ABORT(m) {printf m; exit(1);}\n\n\n" );
dcw's avatar
dcw committed
50
51
52
}


dcw's avatar
dcw committed
53
static void data_decls( f, dl ) FILE *f; declnlist dl;
dcw's avatar
dcw committed
54
55
56
57
58
59
60
61
{
	declnlist d;
	char *name;
	shapelist s;
	int n;
	char *tag;
	paramlist p;

dcw's avatar
dcw committed
62
63
	for( d=dl; d; d=d->next )
	{
dcw's avatar
dcw committed
64
65
66
		name = d->name;
		fprintf( f, "typedef struct %s_str *%s;\n", name, name );
	}
dcw's avatar
dcw committed
67
68
69
	fprintf( f, "\n\n" );
	for( d=dl; d; d=d->next )
	{
dcw's avatar
dcw committed
70
71
72
		name = d->name;
		fprintf( f, "/* ---- Type %s ---- */\n\n", name );
		n = 0;
dcw's avatar
dcw committed
73
74
		for( s = d->shapes; s; s=s->next )
		{
dcw's avatar
dcw committed
75
76
77
78
79
			fprintf( f, "#define %s_%s_tag %d\n",
				name, s->tagname, n++ );
		}
		fprintf( f, "\nstruct %s_str {\n", name );
		fprintf( f, "\tint\ttag;\n" );
dcw's avatar
dcw committed
80
81
		for( s = d->shapes; s; s=s->next )
		{
dcw's avatar
dcw committed
82
			tag = s->tagname;
dcw's avatar
dcw committed
83
84
85
86
			for( p = s->params; p; p=p->next )
			{
				fprintf( f, "\t%s\t%s_%s;\n",
					p->type, tag, p->name );
dcw's avatar
dcw committed
87
88
89
90
91
92
93
			}
		}
		fprintf( f, "};\n\n" );
	}
}


dcw's avatar
dcw committed
94
static void one_proto( f, name, s, protos ) FILE *f; char *name; shapelist s; BOOL protos;
dcw's avatar
dcw committed
95
96
97
98
99
100
101
{
	char *tag;
	paramlist p;
	BOOL first;

	tag = s->tagname;
	fprintf( f, "extern %s %s_%s( ", name, name, tag );
dcw's avatar
dcw committed
102
103
104
105
106
107
108
	if( protos )
	{
		if( s->params == NULL )
		{
			fprintf( f, "void " );
		} else
		{
dcw's avatar
dcw committed
109
			first = TRUE;
dcw's avatar
dcw committed
110
111
			for( p = s->params; p ; p=p->next, first=FALSE )
			{
dcw's avatar
dcw committed
112
				if( !first ) fprintf( f, ", " );
dcw's avatar
dcw committed
113
				fprintf( f, "%s ", p->type );
dcw's avatar
dcw committed
114
115
116
			}
		}
	}
dcw's avatar
dcw committed
117
	fprintf( f, ");\n" );
dcw's avatar
dcw committed
118
119
120
}


dcw's avatar
dcw committed
121
static void protos( f, d, protos ) FILE *f; declnlist d; BOOL protos;
dcw's avatar
dcw committed
122
123
{
	char *name;
dcw's avatar
dcw committed
124
	declnlist dl;
dcw's avatar
dcw committed
125
126
	shapelist s;

dcw's avatar
dcw committed
127
128
129
130
131
132
	for( dl=d; dl; dl = dl->next )
	{
		name = dl->name;
		for( s = dl->shapes; s; s=s->next )
		{
			one_proto( f, name, s, protos );
dcw's avatar
dcw committed
133
134
		}
	}
dcw's avatar
dcw committed
135
136
137
138
139
140
	for( dl=d; dl; dl = dl->next )
	{
		fprintf( f, "extern void print_%s( ", dl->name );
		if( protos ) fprintf( f, "%s ", dl->name );
		fprintf( f, ");\n" );
	}
dcw's avatar
dcw committed
141
142
143
}


dcw's avatar
dcw committed
144
static void cons_fn_hdr( f, name, s ) FILE *f; char *name; shapelist s;
dcw's avatar
dcw committed
145
146
147
148
149
150
151
152
{
	char *tag;
	paramlist p;
	BOOL first;

	tag = s->tagname;
	fprintf( f, "%s %s_%s( ", name, name, tag );
	first = TRUE;
dcw's avatar
dcw committed
153
154
	for( p=s->params; p; p=p->next, first=FALSE )
	{
dcw's avatar
dcw committed
155
156
157
158
		if( !first ) fprintf( f, ", " );
		fprintf( f, "%s", p->name );
	}
	fprintf( f, " ) " );
dcw's avatar
dcw committed
159
160
161
	for( p=s->params; p; p=p->next )
	{
		fprintf( f, "%s %s; ", p->type, p->name );
dcw's avatar
dcw committed
162
163
164
165
166
	}
	fputc( '\n', f );
}


dcw's avatar
dcw committed
167
static void cons_fn_body( f, name, s ) FILE *f; char *name; shapelist s;
dcw's avatar
dcw committed
168
169
170
171
172
173
174
175
{
	char *tag;
	paramlist p;

	tag = s->tagname;
	fprintf( f, "{\n\t%s new;\n\n", name );
	fprintf( f, "\tnew = NEW( struct %s_str );\n", name );
	fprintf( f, "\tnew->tag = %s_%s_tag;\n", name, tag );
dcw's avatar
dcw committed
176
177
	for( p=s->params; p; p=p->next )
	{
dcw's avatar
dcw committed
178
179
180
181
182
183
		fprintf( f, "\tnew->%s_%s = %s;\n", tag, p->name, p->name );
	}
	fprintf( f, "\treturn new;\n}\n" );
}


dcw's avatar
dcw committed
184
static void cons_fns( f, d ) FILE *f; declnlist d;
dcw's avatar
dcw committed
185
186
187
188
{
	char *name;
	shapelist s;

dcw's avatar
dcw committed
189
190
	for( ; d; d=d->next )
	{
dcw's avatar
dcw committed
191
		name = d->name;
dcw's avatar
dcw committed
192
193
194
195
		for( s = d->shapes; s; s=s->next )
		{
			cons_fn_hdr( f, name, s );
			cons_fn_body( f, name, s );
dcw's avatar
dcw committed
196
197
198
199
200
201
			fprintf( f, "\n\n" );
		}
	}
}


dcw's avatar
dcw committed
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
static void print_param( f, shape, ftype, fname ) FILE *f; char *shape, *ftype, *fname;
{
	char pname[200];

	sprintf( pname, "p->%s_%s", shape, fname );
	if( streq( ftype, "int" ) )
	{
		fprintf( f, "\t\tfprintf( f, \"%%d\", %s );\n", pname );
	} else if( streq( ftype, "char" ) )
	{
		fprintf( f, "\t\tfprintf( f, \"%%c\", %s );\n", pname );
	} else if( streq( ftype, "BOOL" ) )
	{
		fprintf( f, "\t\tfprintf( f, \"%%s\", %s?\"true\":\"false\" );\n",
			pname );
	} else if( streq( ftype, "string" ) )
	{
		fprintf( f, "\t\tfprintf( f, \"%%s\", %s );\n", pname );
	} else
	{
		fprintf( f, "\t\tprint_%s( f, %s );\n", ftype, pname );
	}
}


static void print_fn_shape( f, typename, s ) FILE *f; char *typename; shapelist s;
{
	printlist pl;
	paramlist p;
	int n;

	fprintf( f, "\tcase %s_%s_tag:\n", typename, s->tagname );

	if( s->pl == NULL )
	{
		for( p = s->params; p; p = p->next )
		{
			print_param( f, s->tagname, p->type, p->name );
		}
	} else
	{
		for( pl = s->pl; pl; pl = pl->next )
		{
			if( pl->item->tag == printitem_is_str )
			{
				fprintf( f, "\t\tfprintf( f, \"%s\" );\n",
					pl->item->str );
			} else
			{
				n = pl->item->num;
				for( p = s->params; --n; p = p->next )
				{
					ASSERT( p != NULL,
("datadec: bad printitem in shape %s of type %s\n",s->tagname,typename));
				}
				print_param( f, s->tagname, p->type, p->name );
			}
		}
	}
	fprintf( f, "\t\tbreak;\n" );
}


static void print_fns( f, d ) FILE *f ; declnlist d;
{
	shapelist s;

	for( ; d; d = d->next )
	{
		fprintf( f, "void print_%s( f, p ) FILE *f; %s p;\n",
			d->name, d->name );
		fprintf( f, "{\n\tswitch( p->tag )\n\t{\n" );
		for( s = d->shapes; s; s = s->next )
		{
			print_fn_shape( f, d->name, s );
		}
		fprintf( f, "\tdefault:\n" );
		fprintf( f,
		"\tABORT((\"print_%s: impossible tag %%d\\n\",p->tag));\n",
			d->name );
		fprintf( f, "\t}\n}\n\n" );
	}
}

dcw's avatar
dcw committed
286
287
288
289
void make_declns( d, c, h, basename ) declnlist d; FILE *c, *h; char *basename;
{
	printf( "Making data declarations\n" );

dcw's avatar
dcw committed
290
	init( h, c, basename );
dcw's avatar
dcw committed
291

dcw's avatar
dcw committed
292
	data_decls( h, d );
dcw's avatar
dcw committed
293
294

	fprintf( h, "#ifdef HASPROTOS\n" );
dcw's avatar
dcw committed
295
	protos( h, d, TRUE );
dcw's avatar
dcw committed
296
	fprintf( h, "#else\n" );
dcw's avatar
dcw committed
297
	protos( h, d, FALSE );
dcw's avatar
dcw committed
298
299
	fprintf( h, "#endif\n" );

dcw's avatar
dcw committed
300
301
	cons_fns( c, d );
	print_fns( c, d );
dcw's avatar
dcw committed
302
}