datadec.c 2.41 KB
Newer Older
dcw's avatar
dcw committed
1
2
3
4
5
6
7
8
9
10
/*
 *                      DATA DECLARATION BUILDER
 *                      ==== =========== =======
 *
 ******* Author:
 *
 *      Duncan White, Imperial College, London, England.
 *
 ******* Description:
 *
dcw's avatar
dcw committed
11
12
13
14
 *      This program builds C or Modula-2 data declarations,
 *	constructor and deconstructor functions and write functions
 *	from a series of HOPE/Miranda style recursive data declarations.
 *	(with optional hints on printing)
dcw's avatar
dcw committed
15
 *
dcw's avatar
dcw committed
16
17
 *      The output produced is placed in pair of files (eg. x.c and x.h )
 *	which together form a module provided the relevent data types.
dcw's avatar
dcw committed
18
19
 */

20
21
#include <stdio.h>
#include <string.h>
dcw's avatar
dcw committed
22
23
24
#include "struct.h"
#include "lexer.h"
#include "parser.h"
dcw's avatar
dcw committed
25
#include "decs.h"
26
#include "optimize.h"
dcw's avatar
dcw committed
27
28


29
#define MUSTBE(b)	{if(!(b)){fprintf(stderr,"Usage: datadec [-vno] outfile [infile]\n");exit(1);}}
dcw's avatar
dcw committed
30
31
32
33
34
35

#define NEED_ANOTHER_ARG	MUSTBE( IS_ANOTHER_ARG )
#define REQUIRE_NO_MORE_ARGS	MUSTBE( argc == arg )
#define IS_ANOTHER_ARG		(argc > arg)


36
37
#define CHUNKSIZE 10000

dcw's avatar
dcw committed
38
39
typedef char bigstr[ CHUNKSIZE ];

40

dcw's avatar
dcw committed
41
int main( int argc, char **argv )
dcw's avatar
dcw committed
42
{
dcw's avatar
dcw committed
43
44
	char		*basename;
	char		*s;
dcw's avatar
dcw committed
45
46
        declnlist	declns;
	int		len;
dcw's avatar
dcw committed
47
	int		arg;
dcw's avatar
dcw committed
48
	bigstr		exports, globals, begin;
dcw's avatar
dcw committed
49

dcw's avatar
dcw committed
50
51
52
	arg = 1;
	NEED_ANOTHER_ARG;

dcw's avatar
dcw committed
53
	verbose = FALSE; opt = TRUE;
dcw's avatar
dcw committed
54
	while( *(s=argv[arg]) == '-' )
dcw's avatar
dcw committed
55
	{
dcw's avatar
dcw committed
56
57
58
59
60
61
62
		for( s++; *s; s++ )
		{
			switch( *s )
			{
			case 'v':
				verbose = TRUE;
				break;
dcw's avatar
dcw committed
63
64
			case 'n':
				opt = FALSE;
dcw's avatar
dcw committed
65
				break;
66
67
68
			case 'o':
				opt = TRUE;
				break;
dcw's avatar
dcw committed
69
			default:
70
71
72
				fprintf( stderr,
					"datadec: illegal option -%c\n", *s );
				exit(1);
dcw's avatar
dcw committed
73
74
			}
		}
dcw's avatar
dcw committed
75
		arg++;
dcw's avatar
dcw committed
76
		NEED_ANOTHER_ARG;
dcw's avatar
dcw committed
77
	}
dcw's avatar
dcw committed
78

dcw's avatar
dcw committed
79
	basename = argv[arg++];
dcw's avatar
dcw committed
80
	len = strlen( basename );
dcw's avatar
dcw committed
81
82
	if( !strcmp( basename+len-2, ".c" ) )
	{
dcw's avatar
dcw committed
83
84
85
		basename[len-2] = '\0';
	}

dcw's avatar
dcw committed
86
87
        if( IS_ANOTHER_ARG ) {
                lexfile = fopen( argv[arg], "r" );
88
89
90
91
92
93
                if( lexfile == NULL )
		{
			fprintf( stderr, "datadec: can't open '%s'\n",
				argv[arg] );
			exit(1);
		}
dcw's avatar
dcw committed
94
		arg++;
dcw's avatar
dcw committed
95
96
97
        } else {
                lexfile = stdin;
        }
dcw's avatar
dcw committed
98
99
100

	REQUIRE_NO_MORE_ARGS;

dcw's avatar
dcw committed
101
        if( parse_data( exports, globals, begin, &declns ) )
dcw's avatar
dcw committed
102
103
104
	{
		if( verbose )
		{
dcw's avatar
dcw committed
105
			printf( "datadec: declns are:\n\n" );
dcw's avatar
dcw committed
106
			print_declnlist( declns );
107
108
			printf( "exports = {%s}\n", exports );
			printf( "globals = {%s}\n", globals );
dcw's avatar
dcw committed
109
			printf( "begin = {%s}\n", begin );
dcw's avatar
dcw committed
110
		}
111
		optimize( declns );
dcw's avatar
dcw committed
112
		make_declns( exports, globals, begin, declns, basename );
dcw's avatar
dcw committed
113
114
115
116
	} else
	{
		fprintf( stderr, "datadec: can't parse input properly\n" );
	}
dcw's avatar
dcw committed
117
	exit(0);
118
	/*NOTREACHED*/
dcw's avatar
dcw committed
119
}