decs.c 4.17 KB
Newer Older
dcw's avatar
dcw committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <dcw.h>
#include "struct.h"
#include "decs.h"


#ifdef HASPROTOS
static void p_type( FILE * , char * , char , int );
static void make_init( FILE * , FILE * , char * );
static void make_data_decls( FILE * , declnlist );
static void make_1_proto( FILE * , char * , shapelist , BOOL );
static void make_protos( FILE * , declnlist , BOOL );
static void make_fn_hdr( FILE * , char * , shapelist );
static void make_fn_body( FILE * , char * , shapelist );
static void make_fns( FILE * , declnlist );
#else
static void p_type();
static void make_init();
static void make_data_decls();
static void make_1_proto();
static void make_protos();
static void make_fn_hdr();
static void make_fn_body();
static void make_fns();
#endif


static void p_type( f, type, sep, stars ) FILE *f; char sep, *type; int stars;
{
	fprintf( f, "%s%c", type, sep );
	for( ; stars; stars-- ) {
		fputc( '*', f );
	}
}


static void make_init( h, c, basename ) FILE *c, *h; char *basename;
{
	fprintf( h, "/*\n" );
	fprintf( h, "\tAutomatically Generated by DataDec\n" );
	fprintf( h, " */\n\n");

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

	fprintf( c, "#include <dcw.h>\n" );
	fprintf( c, "#include <%s.h>\n\n\n", basename );
}


static void make_data_decls( f, dl ) FILE *f; declnlist dl;
{
	declnlist d;
	char *name;
	shapelist s;
	int n;
	char *tag;
	paramlist p;

	for( d=dl; d; d=d->next ) {
		name = d->name;
		fprintf( f, "typedef struct %s_str *%s;\n", name, name );
	}
	for( d=dl; d; d=d->next ) {
		name = d->name;
		fprintf( f, "/* ---- Type %s ---- */\n\n", name );
		n = 0;
		for( s = d->shapes; s; s=s->next ) {
			fprintf( f, "#define %s_%s_tag %d\n",
				name, s->tagname, n++ );
		}
		fprintf( f, "\nstruct %s_str {\n", name );
		fprintf( f, "\tint\ttag;\n" );
		for( s = d->shapes; s; s=s->next ) {
			tag = s->tagname;
			for( p = s->params; p; p=p->next ) {
				fputc( '\t', f );
				p_type( f, p->type, '\t', p->stars );
				fprintf( f, "%s_%s;\n", tag, p->name );
			}
		}
		fprintf( f, "};\n\n" );
	}
}


static void make_1_proto( f, name, s, protos ) FILE *f; char *name; shapelist s; BOOL protos;
{
	char *tag;
	paramlist p;
	BOOL first;

	tag = s->tagname;
	fprintf( f, "extern %s %s_%s( ", name, name, tag );
	if( protos ) {
		if( s->params == NULL ) {
			fprintf( f, "void" );
		} else {
			first = TRUE;
			for( p = s->params; p ; p=p->next, first=FALSE ) {
				if( !first ) fprintf( f, ", " );
				p_type( f, p->type, ' ', p->stars );
			}
		}
	}
	fprintf( f, " );\n" );
}


static void make_protos( f, d, protos ) FILE *f; declnlist d; BOOL protos;
{
	char *name;
	shapelist s;

	for( ; d; d = d->next ) {
		name = d->name;
		for( s = d->shapes; s; s=s->next ) {
			make_1_proto( f, name, s );
		}
	}
}


static void make_fn_hdr( f, name, s ) FILE *f; char *name; shapelist s;
{
	char *tag;
	paramlist p;
	BOOL first;

	tag = s->tagname;
	fprintf( f, "%s %s_%s( ", name, name, tag );
	first = TRUE;
	for( p=s->params; p; p=p->next, first=FALSE ) {
		if( !first ) fprintf( f, ", " );
		fprintf( f, "%s", p->name );
	}
	fprintf( f, " ) " );
	for( p=s->params; p; p=p->next ) {
		p_type( f, p->type, ' ', p->stars );
		fprintf( f, "%s; ", p->name );
	}
	fputc( '\n', f );
}


static void make_fn_body( f, name, s ) FILE *f; char *name; shapelist s;
{
	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 );
	for( p=s->params; p; p=p->next ) {
		fprintf( f, "\tnew->%s_%s = %s;\n", tag, p->name, p->name );
	}
	fprintf( f, "\treturn new;\n}\n" );
}


static void make_fns( f, d ) FILE *f; declnlist d;
{
	char *name;
	shapelist s;

	for( ; d; d=d->next ) {
		name = d->name;
		for( s = d->shapes; s; s=s->next ) {
			make_fn_hdr( f, name, s );
			make_fn_body( f, name, s );
			fprintf( f, "\n\n" );
		}
	}
}


void make_declns( d, c, h, basename ) declnlist d; FILE *c, *h; char *basename;
{
	printf( "Making data declarations\n" );

	make_init( h, c, basename );

	make_data_decls( h, d );

	fprintf( h, "#ifdef HASPROTOS\n" );
	make_protos( h, d, TRUE );
	fprintf( h, "#else\n" );
	make_protos( h, d, FALSE );
	fprintf( h, "#endif\n" );

	make_fns( c, d );
}