forked from FLAME-HPC/xparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xparser.c
299 lines (271 loc) · 8.74 KB
/
xparser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
* \file xparser.c
* \code
* Author: Simon Coakley (USFD)
* Added to and Modified: David Worth (STFC)
* Lee Shawn Chin (STFC)
* Chris Greenough (STFC)
* Copyright (c) 2006 Simon Coakley
* License: n/a
* \endcode
* \brief Architecture for agent-based modelling based on X-machines.
*/
/** \mainpage xparser - parser tool for the FLAME framework
*
* \section intro_sec Introduction
*
* This program is the FLAME XMML parser. It accepts a model description
* in X-agent XML and produces source code for the model in either serial
* (default) or in parallel (via MPI libraries).
*
* The command line is:
*
* xparser [-p | -s] [XMML file]
*
* xparser without any arguments will produce:
*
* xparser: Version \<version number\>
*
* Usage: xparser [XMML file] [-s | -p]
*
* By default xparser will generate a serial program.
*
* The code xparser generates, although not pretty, should compile on most
* systems that have a compiler conforming to the C99 standard.
*
* \section install_sec Installation
*
* Compile using a C compiler. A Make file is provided. xparser has been tested
* on a large number of UNIX based systems.
*/
#include "header.h"
/** \fn int main(int argc, char * argv[])
* \brief Main program function.
* \param argc Argument count.
* \param argv Pointer Pointer to Argument vector
* \return system int
*/
int main(int argc, char * argv[])
{
/* Variables for parsing directories */
int lastd;
int i;
/* Error value */
int rc;
/* Variable to read in command line input */
char inputfile[1000];
char directory[1000];
char filename[1000];
char templatename[1000];
char templatedirectory[1000];
/* For reading input files */
input_file * current_input_file;
/* Structure to hold model data */
model_data * modeldata;
/* Hold modeldata data */
xmachine * xmachines;
xmachine_message * xmessage;
variable * envvar;
env_func * envfunc;
variable * envdefine;
variable * allvars;
variable * constant_filter_vars;
f_code * it_end_code;
layer * layers;
flame_communication * communications;
model_datatype * datatypes;
time_data * time_units;
input_file * temp_input_file;
/* Allocate memory for modeldata */
modeldata = (model_data *)malloc(sizeof(model_data));
/* 0=serial(default) 1=parallel 2=grid */
modeldata->code_type = 0;
/* 0=dgraph.dot 1=stategraph.dot */
modeldata->depends_style = 0;
modeldata->debug_mode = 1;
inputfile[1]='\0';
/* Initialise pointers */
modeldata->name = NULL;
modeldata->p_xmachines = &xmachines;
xmachines = NULL;
modeldata->p_xmessages = &xmessage;
xmessage = NULL;
modeldata->p_envvars = &envvar;
envvar = NULL;
modeldata->p_envfuncs = &envfunc;
envfunc = NULL;
modeldata->p_envdefines = &envdefine;
envdefine = NULL;
modeldata->p_allvars = &allvars;
allvars = NULL;
modeldata->p_it_end_code = &it_end_code;
it_end_code = NULL;
modeldata->p_layers = &layers;
layers = NULL;
modeldata->p_communications = &communications;
communications = NULL;
modeldata->p_datatypes = &datatypes;
datatypes = NULL;
modeldata->p_time_units = &time_units;
time_units = NULL;
modeldata->p_files = &temp_input_file;
temp_input_file = NULL;
modeldata->p_constant_filter_vars = &constant_filter_vars;
constant_filter_vars = NULL;
printf("xparser (Version %d.%d.%d)\n", VERSIONMAJOR, VERSIONMINOR, VERSIONMICRO);
/* Must be at least the input file name */
if(argc < 2)
{
printf("Usage: xparser [XMML file] [-s | -p] [-f]\n");
printf("\t-s\tSerial mode\n");
printf("\t-p\tParallel mode\n");
printf("\t-f\tFinal production mode\n");
free_modeldata(modeldata);
return 0;
}
/* Copy location of xparser */
strcpy(templatedirectory,argv[0]);
/* parser command line */
while(argc >1){
if(argv[1][0] == '-') {
switch (argv[1][1]){
case 's': modeldata->code_type = 0;
break;
case 'p': modeldata->code_type = 1;
break;
case 'f' : modeldata->debug_mode = 0;
break;
default: printf("xparser: Error - unknown option %s\n",argv[1]);
free_modeldata(modeldata);
return 0;
}
}
else
{
strcpy(inputfile,argv[1]);
/* printf("XMML input file : %s\n",argv[1]); */
}
argc--;
argv++;
}
if(inputfile[1] == '\0') {
printf("xparser: Error - XMML must be specified\n");
free_modeldata(modeldata);
return 0;
}
/* Print what type of code writing */
printf("\n");
printf("Code type : ");
if(modeldata->code_type == 0) printf("Serial");
if(modeldata->code_type == 1) printf("Parallel");
if(modeldata->debug_mode == 1) printf(" (DEBUG)");
printf("\n");
/* Calculate directory to write files to */
i = 0;
lastd = 0;
while(inputfile[i] != '\0')
{
/* For windows directories */
if(inputfile[i] == '\\') lastd=i;
/* For unix directories */
if(inputfile[i] == '/') lastd=i;
i++;
}
/* If a directory is in the path */
if(lastd != 0)
{
strcpy(directory, inputfile);
directory[lastd+1] = '\0';
}
else directory[0] = '\0';
/* Calculate directory where xparser and template files are */
i = 0;
lastd = 0;
while(templatedirectory[i] != '\0')
{
/* For windows directories */
if(templatedirectory[i] == '\\') lastd=i;
/* For unix directories */
if(templatedirectory[i] == '/') lastd=i;
i++;
}
/* If a directory is in the path */
if(lastd != 0)
{
templatedirectory[lastd+1] = '\0';
}
else templatedirectory[0] = '\0';
printf("Input XMML file : %s\n", inputfile);
printf("Model root dir : %s\n", directory);
printf("Template dir : %s\n", templatedirectory);
printf("\n");
current_input_file = add_input_file(modeldata->p_files);
current_input_file->fullfilepath = copystr(inputfile);
current_input_file->fulldirectory = copystr(directory);
current_input_file->localdirectory = copystr("");
/* Read model from model xml file */
current_input_file = * modeldata->p_files;
while(current_input_file)
{
if(current_input_file->enabled == 1)
readModel(current_input_file, directory, modeldata);
current_input_file = current_input_file->next;
}
rc = checkmodel(modeldata);
if(rc == -1)
{
free_modeldata(modeldata);
return 0;
}
/* Calculate dependency graph for model functions */
rc = create_dependency_graph(directory, modeldata);
if(rc == -1)
{
free_modeldata(modeldata);
return 0;
}
strcpy(filename, directory); strcat(filename, "Makefile");
strcpy(templatename, templatedirectory); strcat(templatename, "Makefile.tmpl");
parseTemplate(filename, templatename, modeldata);
strcpy(filename, directory); strcat(filename, "xml.c");
strcpy(templatename, templatedirectory); strcat(templatename, "xml.tmpl");
parseTemplate(filename, templatename, modeldata);
strcpy(filename, directory); strcat(filename, "main.c");
strcpy(templatename, templatedirectory); strcat(templatename, "main.tmpl");
parseTemplate(filename, templatename, modeldata);
strcpy(filename, directory); strcat(filename, "header.h");
strcpy(templatename, templatedirectory); strcat(templatename, "header.tmpl");
parseTemplate(filename, templatename, modeldata);
strcpy(filename, directory); strcat(filename, "memory.c");
strcpy(templatename, templatedirectory); strcat(templatename, "memory.tmpl");
parseTemplate(filename, templatename, modeldata);
strcpy(filename, directory); strcat(filename, "low_primes.h");
strcpy(templatename, templatedirectory); strcat(templatename, "low_primes.tmpl");
parseTemplate(filename, templatename, modeldata);
strcpy(filename, directory); strcat(filename, "messageboards.c");
strcpy(templatename, templatedirectory); strcat(templatename, "messageboards.tmpl");
parseTemplate(filename, templatename, modeldata);
strcpy(filename, directory); strcat(filename, "partitioning.c");
strcpy(templatename, templatedirectory); strcat(templatename, "partitioning.tmpl");
parseTemplate(filename, templatename, modeldata);
strcpy(filename, directory); strcat(filename, "timing.c");
strcpy(templatename, templatedirectory); strcat(templatename, "timing.tmpl");
parseTemplate(filename, templatename, modeldata);
strcpy(filename, directory); strcat(filename, "Doxyfile");
strcpy(templatename, templatedirectory); strcat(templatename, "Doxyfile.tmpl");
parseTemplate(filename, templatename, modeldata);
strcpy(filename, directory); strcat(filename, "rules.c");
strcpy(templatename, templatedirectory); strcat(templatename, "rules.tmpl");
parseTemplate(filename, templatename, modeldata);
printf("\n");
parseAgentHeaderTemplate(directory, modeldata);
/*parseUnittest(directory, modeldata);*/
/*parser0dtd(directory, modeldata);*/
/*parser0xsd(directory, modeldata);*/
free_modeldata(modeldata);
printf("\n--- xparser finished ---\n\n");
printf("To compile and run the generated code, you will need:\n");
printf(" * libmboard (version %s or newer)\n", LIBMBOARD_MINVER_STR);
/* Exit successfully by returning zero to Operating System */
return 0;
}