datadec.c 2.43 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
#include <stdio.h>
21
#include <stdlib.h>
22
#include <string.h>
23

dcw's avatar
dcw committed
24
25
26
#include "struct.h"
#include "lexer.h"
#include "parser.h"
dcw's avatar
dcw committed
27
#include "decs.h"
28
#include "optimize.h"
dcw's avatar
dcw committed
29
30


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

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


38
39
#define CHUNKSIZE 10000

dcw's avatar
dcw committed
40
41
typedef char bigstr[ CHUNKSIZE ];

42

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

dcw's avatar
dcw committed
52
53
54
	arg = 1;
	NEED_ANOTHER_ARG;

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

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

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

	REQUIRE_NO_MORE_ARGS;

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