struct.c 3.31 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 19
#include <stdio.h>
#include <malloc.h>
dcw's avatar
dcw committed
20 21
#include "struct.h"

dcw's avatar
dcw committed
22
#define Puts(s)	fputs(s,stdout)
23 24
#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
25

dcw's avatar
dcw committed
26

dcw's avatar
dcw committed
27
declnlist build_declnlist(name,s,next) char *name; shapelist s; declnlist next;
dcw's avatar
dcw committed
28
{
dcw's avatar
dcw committed
29
        declnlist new = NEW(declnlist);
dcw's avatar
dcw committed
30

dcw's avatar
dcw committed
31
        COPYOF(new->name, name);
dcw's avatar
dcw committed
32 33 34 35 36 37
        new->shapes = s;
        new->next   = next;
        return new;
}


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


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

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


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


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


dcw's avatar
dcw committed
90
paramlist build_paramlist(type, id, next) char *type, *id; paramlist next;
dcw's avatar
dcw committed
91
{
dcw's avatar
dcw committed
92
        paramlist new = NEW(paramlist);
dcw's avatar
dcw committed
93

dcw's avatar
dcw committed
94 95
        COPYOF(new->type, type);
        COPYOF(new->name, id);
dcw's avatar
dcw committed
96
        new->next  = next;
dcw's avatar
dcw committed
97

dcw's avatar
dcw committed
98 99 100 101
        return new;
}


dcw's avatar
dcw committed
102
void print_paramlist(p) paramlist p;
dcw's avatar
dcw committed
103
{
dcw's avatar
dcw committed
104 105 106 107 108 109
        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);
110
                }
dcw's avatar
dcw committed
111 112 113 114
        }
}


dcw's avatar
dcw committed
115
printitem build_printitem_str(s) char *s;
dcw's avatar
dcw committed
116
{
dcw's avatar
dcw committed
117
	printitem new = NEW( printitem );
dcw's avatar
dcw committed
118

119 120 121 122 123 124 125
	new->tag = printitem_is_str;
	new->str = s;

	return new;
}


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

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

	return new;
}


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


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

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


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


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

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

181 182 183 184 185 186 187
	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
188 189 190 191 192 193

	return p;
}