decs.c 6.51 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
	fprintf( h, "typedef char *string;\n\n" );
	fprintf( h, "typedef char BOOL;\n" );
dcw's avatar
dcw committed
38 39
	fprintf( h, "#define TRUE 1\n" );
	fprintf( h, "#define FALSE 0\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
	fprintf( c, "#include <stdio.h>\n" );
	fprintf( c, "#include \"%s.h\"\n\n\n", basename );

dcw's avatar
dcw committed
48
	fprintf( c, "#define NEW(t) ((t *)malloc(sizeof(t)))\n\n\n" );
dcw's avatar
dcw committed
49 50 51
}


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

dcw's avatar
dcw committed
61 62
	for( d=dl; d; d=d->next )
	{
dcw's avatar
dcw committed
63 64 65
		name = d->name;
		fprintf( f, "typedef struct %s_str *%s;\n", name, name );
	}
dcw's avatar
dcw committed
66 67 68
	fprintf( f, "\n\n" );
	for( d=dl; d; d=d->next )
	{
dcw's avatar
dcw committed
69 70 71
		name = d->name;
		fprintf( f, "/* ---- Type %s ---- */\n\n", name );
		n = 0;
dcw's avatar
dcw committed
72 73
		for( s = d->shapes; s; s=s->next )
		{
dcw's avatar
dcw committed
74 75 76 77 78
			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
79
		fprintf( f, "\tunion {\n" );
dcw's avatar
dcw committed
80 81
		for( s = d->shapes; s; s=s->next )
		{
dcw's avatar
dcw committed
82
			if( s->params )
dcw's avatar
dcw committed
83
			{
dcw's avatar
dcw committed
84 85 86 87 88 89 90
				fprintf( f, "\t\tstruct {\n" );
				for( p = s->params; p; p=p->next )
				{
					fprintf( f, "\t\t\t%s\t%s;\n",
						p->type, p->name );
				}
				fprintf( f, "\t\t} %s;\n", s->tagname );
dcw's avatar
dcw committed
91 92
			}
		}
dcw's avatar
dcw committed
93
		fprintf( f, "\t} u;\n};\n\n" );
dcw's avatar
dcw committed
94 95 96 97
	}
}


dcw's avatar
dcw committed
98
static void one_proto( f, name, s, protos ) FILE *f; char *name; shapelist s; BOOL protos;
dcw's avatar
dcw committed
99 100 101 102 103 104 105
{
	char *tag;
	paramlist p;
	BOOL first;

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


dcw's avatar
dcw committed
125
static void protos( f, d, protos ) FILE *f; declnlist d; BOOL protos;
dcw's avatar
dcw committed
126 127
{
	char *name;
dcw's avatar
dcw committed
128
	declnlist dl;
dcw's avatar
dcw committed
129 130
	shapelist s;

dcw's avatar
dcw committed
131 132 133 134 135 136
	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
137 138
		}
	}
dcw's avatar
dcw committed
139 140 141 142 143 144
	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
145 146 147
}


dcw's avatar
dcw committed
148
static void cons_fn_hdr( f, name, s ) FILE *f; char *name; shapelist s;
dcw's avatar
dcw committed
149 150 151 152 153 154 155 156
{
	char *tag;
	paramlist p;
	BOOL first;

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


dcw's avatar
dcw committed
171
static void cons_fn_body( f, name, s ) FILE *f; char *name; shapelist s;
dcw's avatar
dcw committed
172 173 174 175 176 177 178 179
{
	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
180 181
	for( p=s->params; p; p=p->next )
	{
dcw's avatar
dcw committed
182
		fprintf( f, "\tnew->u.%s.%s = %s;\n", tag, p->name, p->name );
dcw's avatar
dcw committed
183 184 185 186 187
	}
	fprintf( f, "\treturn new;\n}\n" );
}


dcw's avatar
dcw committed
188
static void cons_fns( f, d ) FILE *f; declnlist d;
dcw's avatar
dcw committed
189 190 191 192
{
	char *name;
	shapelist s;

dcw's avatar
dcw committed
193 194
	for( ; d; d=d->next )
	{
dcw's avatar
dcw committed
195
		name = d->name;
dcw's avatar
dcw committed
196 197 198 199
		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
200 201 202 203 204 205
			fprintf( f, "\n\n" );
		}
	}
}


dcw's avatar
dcw committed
206 207 208 209
static void print_param( f, shape, ftype, fname ) FILE *f; char *shape, *ftype, *fname;
{
	char pname[200];

dcw's avatar
dcw committed
210
	sprintf( pname, "p->u.%s.%s", shape, fname );
dcw's avatar
dcw committed
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
	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" );
dcw's avatar
dcw committed
283
		fprintf( f, "\t\tfprintf( stderr," );
dcw's avatar
dcw committed
284
		fprintf( f,
dcw's avatar
dcw committed
285 286 287
		" \"print_%s: impossible tag %%d\\n\", p->tag );\n",
		d->name );
		fprintf( f, "\t\texit(1);\n\t}\n}\n\n" );
dcw's avatar
dcw committed
288 289 290
	}
}

dcw's avatar
dcw committed
291 292 293 294
void make_declns( d, c, h, basename ) declnlist d; FILE *c, *h; char *basename;
{
	printf( "Making data declarations\n" );

dcw's avatar
dcw committed
295
	init( h, c, basename );
dcw's avatar
dcw committed
296

dcw's avatar
dcw committed
297
	data_decls( h, d );
dcw's avatar
dcw committed
298 299

	fprintf( h, "#ifdef HASPROTOS\n" );
dcw's avatar
dcw committed
300
	protos( h, d, TRUE );
dcw's avatar
dcw committed
301
	fprintf( h, "#else\n" );
dcw's avatar
dcw committed
302
	protos( h, d, FALSE );
dcw's avatar
dcw committed
303 304
	fprintf( h, "#endif\n" );

dcw's avatar
dcw committed
305 306
	cons_fns( c, d );
	print_fns( c, d );
dcw's avatar
dcw committed
307
}