Skip to content
Snippets Groups Projects
struct.c 3.31 KiB
Newer Older
dcw's avatar
dcw committed
/*
 *                   DATA STRUCTURES
 *                   ==== ==========
 *
 *                       FOR THE
 *                       === ===
 *
 *               DATA DECLARATION BUILDER
 *               ==== =========== =======
 *
 *      This module provides the internal structures needed
 *      for the data declaration builder.  It is itself the
 *      sort of output generated by the program (although a
 *	bit optimized!)
dcw's avatar
dcw committed
 */


#include <stdio.h>
#include <malloc.h>
dcw's avatar
dcw committed
#include "struct.h"

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

dcw's avatar
dcw committed

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

dcw's avatar
dcw committed
        COPYOF(new->name, name);
dcw's avatar
dcw committed
        new->shapes = s;
        new->next   = next;
        return new;
}


dcw's avatar
dcw committed
void print_declnlist(d) declnlist d;
dcw's avatar
dcw committed
{
dcw's avatar
dcw committed
        for ( ; d; d = d->next)
	{
                Puts(d->name);
		Puts(" = ");
                print_shapelist(d->shapes);
                putchar('\n');
dcw's avatar
dcw committed
        }
}


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

dcw's avatar
dcw committed
        COPYOF(new->name, id);
dcw's avatar
dcw committed
        new->params = p;
dcw's avatar
dcw committed
        new->next   = next;
        return new;
}


dcw's avatar
dcw committed
void print_shape(s) shapelist s;
dcw's avatar
dcw committed
	Puts(s->name);
	if (s->params)
dcw's avatar
dcw committed
		Puts("( ");
		print_paramlist(s->params);
		Puts(" )");
dcw's avatar
dcw committed
	putchar(' ');
	print_printlist(s->pl);
dcw's avatar
dcw committed
void print_shapelist(s) shapelist s;
dcw's avatar
dcw committed
{
dcw's avatar
dcw committed
        if (s != NULL)
	{
		print_shape(s);
                for (s = s->next; s; s = s->next)
		{
                        Puts("or ");
			print_shape(s);
dcw's avatar
dcw committed
                }
        }
}


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

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

dcw's avatar
dcw committed
        return new;
}


dcw's avatar
dcw committed
void print_paramlist(p) paramlist p;
dcw's avatar
dcw committed
{
dcw's avatar
dcw committed
        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);
dcw's avatar
dcw committed
        }
}


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

	new->tag = printitem_is_str;
	new->str = s;

	return new;
}


dcw's avatar
dcw committed
printitem build_printitem_num(n) int n;
dcw's avatar
dcw committed
	printitem new = NEW(printitem);

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

	return new;
}


dcw's avatar
dcw committed
void print_printitem(i) printitem i;
dcw's avatar
dcw committed
	switch(i->tag)
	{
	case printitem_is_num:
dcw's avatar
dcw committed
		printf("%d ", i->num);
		break;
	case printitem_is_str:
dcw's avatar
dcw committed
		printf("\"%s\" ", i->str);
		fprintf( stderr,
			"datadec: print_printitem called with tag = %d\n",
			i->tag );
		exit(1);
dcw's avatar
dcw committed
printlist build_printlist(h, t) printitem h; printlist t;
dcw's avatar
dcw committed
        printlist new = NEW(printlist);

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


dcw's avatar
dcw committed
void print_printlist(p) printlist p;
dcw's avatar
dcw committed
	for (; p; p = p->next)
dcw's avatar
dcw committed
		print_printitem(p->item);
dcw's avatar
dcw committed
}
dcw's avatar
dcw committed


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

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

	if( p == NULL )
	{
		fprintf( stderr,
			"datadec: bad print number %d in shape %s of type %s\n",
			m, sname, dname );
		exit(1);
	}