struct.c 3.35 KB
Newer Older
dcw's avatar
dcw committed
1
2
3
4
5
6
7
8
9
10
11
12
/*
 *                   DATA STRUCTURES
 *                   ==== ==========
 *
 *                       FOR THE
 *                       === ===
 *
 *               DATA DECLARATION BUILDER
 *               ==== =========== =======
 *
 *      This module provides the internal structures needed
 *      for the data declaration builder.  It is itself the
13
14
 *      sort of output generated by the program (although a
 *	bit optimized!)
dcw's avatar
dcw committed
15
16
17
 */


18
#include <stdio.h>
19
20
#include <stdlib.h>
#include <string.h>
21
#include <malloc.h>
22

dcw's avatar
dcw committed
23
24
#include "struct.h"

dcw's avatar
dcw committed
25
#define Puts(s)	fputs(s,stdout)
26
27
#define NEW(t)	(t) malloc( sizeof(struct t) )
#define COPYOF(new,old) {new=malloc(1+strlen(old));if(new)strcpy(new,old);}
dcw's avatar
dcw committed
28

dcw's avatar
dcw committed
29

dcw's avatar
dcw committed
30
declnlist build_declnlist(name,s,next) char *name; shapelist s; declnlist next;
dcw's avatar
dcw committed
31
{
dcw's avatar
dcw committed
32
        declnlist new = NEW(declnlist);
dcw's avatar
dcw committed
33

dcw's avatar
dcw committed
34
        COPYOF(new->name, name);
dcw's avatar
dcw committed
35
36
37
38
39
40
        new->shapes = s;
        new->next   = next;
        return new;
}


dcw's avatar
dcw committed
41
void print_declnlist(d) declnlist d;
dcw's avatar
dcw committed
42
{
dcw's avatar
dcw committed
43
44
45
46
47
48
        for ( ; d; d = d->next)
	{
                Puts(d->name);
		Puts(" = ");
                print_shapelist(d->shapes);
                putchar('\n');
dcw's avatar
dcw committed
49
50
51
52
        }
}


dcw's avatar
dcw committed
53
shapelist build_shapelist(id, p, pl, next) char *id; paramlist p; printlist pl; shapelist next;
dcw's avatar
dcw committed
54
{
dcw's avatar
dcw committed
55
        shapelist new = NEW(shapelist);
dcw's avatar
dcw committed
56

dcw's avatar
dcw committed
57
        COPYOF(new->name, id);
dcw's avatar
dcw committed
58
        new->params = p;
59
        new->pl     = pl;
dcw's avatar
dcw committed
60
61
62
63
64
        new->next   = next;
        return new;
}


dcw's avatar
dcw committed
65
void print_shape(s) shapelist s;
66
{
dcw's avatar
dcw committed
67
68
	Puts(s->name);
	if (s->params)
69
	{
dcw's avatar
dcw committed
70
71
72
		Puts("( ");
		print_paramlist(s->params);
		Puts(" )");
73
	}
dcw's avatar
dcw committed
74
75
	putchar(' ');
	print_printlist(s->pl);
76
77
78
}


dcw's avatar
dcw committed
79
void print_shapelist(s) shapelist s;
dcw's avatar
dcw committed
80
{
dcw's avatar
dcw committed
81
82
83
84
85
86
        if (s != NULL)
	{
		print_shape(s);
                for (s = s->next; s; s = s->next)
		{
                        Puts("or ");
87
			print_shape(s);
dcw's avatar
dcw committed
88
89
90
91
92
                }
        }
}


dcw's avatar
dcw committed
93
paramlist build_paramlist(type, id, next) char *type, *id; paramlist next;
dcw's avatar
dcw committed
94
{
dcw's avatar
dcw committed
95
        paramlist new = NEW(paramlist);
dcw's avatar
dcw committed
96

dcw's avatar
dcw committed
97
98
        COPYOF(new->type, type);
        COPYOF(new->name, id);
dcw's avatar
dcw committed
99
        new->next  = next;
dcw's avatar
dcw committed
100

dcw's avatar
dcw committed
101
102
103
104
        return new;
}


dcw's avatar
dcw committed
105
void print_paramlist(p) paramlist p;
dcw's avatar
dcw committed
106
{
dcw's avatar
dcw committed
107
108
109
110
111
112
        if (p != NULL)
	{
                printf("%s %s", p->type, p->name);
                for (p = p->next; p; p = p->next)
		{
			printf(", %s %s", p->type, p->name);
113
                }
dcw's avatar
dcw committed
114
115
116
117
        }
}


dcw's avatar
dcw committed
118
printitem build_printitem_str(s) char *s;
dcw's avatar
dcw committed
119
{
dcw's avatar
dcw committed
120
	printitem new = NEW( printitem );
dcw's avatar
dcw committed
121

122
123
124
125
126
127
128
	new->tag = printitem_is_str;
	new->str = s;

	return new;
}


dcw's avatar
dcw committed
129
printitem build_printitem_num(n) int n;
130
{
dcw's avatar
dcw committed
131
	printitem new = NEW(printitem);
132
133
134
135
136
137
138
139

	new->tag = printitem_is_num;
	new->num = n;

	return new;
}


dcw's avatar
dcw committed
140
void print_printitem(i) printitem i;
141
{
dcw's avatar
dcw committed
142
	switch(i->tag)
143
144
	{
	case printitem_is_num:
dcw's avatar
dcw committed
145
		printf("%d ", i->num);
146
147
		break;
	case printitem_is_str:
dcw's avatar
dcw committed
148
		printf("\"%s\" ", i->str);
149
150
		break;
	default:
151
152
153
154
		fprintf( stderr,
			"datadec: print_printitem called with tag = %d\n",
			i->tag );
		exit(1);
155
156
157
158
	}
}


dcw's avatar
dcw committed
159
printlist build_printlist(h, t) printitem h; printlist t;
160
{
dcw's avatar
dcw committed
161
        printlist new = NEW(printlist);
162
163
164
165
166
167
168

        new->item = h;
        new->next = t;
        return new;
}


dcw's avatar
dcw committed
169
void print_printlist(p) printlist p;
170
{
dcw's avatar
dcw committed
171
	for (; p; p = p->next)
172
	{
dcw's avatar
dcw committed
173
		print_printitem(p->item);
174
	}
dcw's avatar
dcw committed
175
}
dcw's avatar
dcw committed
176
177
178
179
180
181
182
183


param findnthparam( n, p, sname, dname ) int n; paramlist p; char *sname, *dname;
{
	int m = n;

	for( ; p != NULL && --m; p = p->next );

184
185
186
187
188
189
190
	if( p == NULL )
	{
		fprintf( stderr,
			"datadec: bad print number %d in shape %s of type %s\n",
			m, sname, dname );
		exit(1);
	}
dcw's avatar
dcw committed
191
192
193
194
195
196

	return p;
}