-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
401 lines (334 loc) · 11.9 KB
/
parser.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// --- Parsing the execution plan, preparing the score function
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <math.h>
#include "astrotest.h"
#include "lcg.h"
// Set initial parameters
void initializations( int argc, char* argv[], _hordat* *hordat ) {
FILE *plan;
char source_file[255], plan_file[255];
// Read in the (few) command line options
get_cmdline_options(argc,argv,source_file,plan_file);
if (plan_file[0]!='\0') {
if (!(plan = fopen( plan_file, "r"))) {
fprintf(stderr,"Can't open file '%s' for reading\n",plan_file);
exit(EXIT.FILE_ACCESS_ERROR);
}
}
else
plan = stdin;
read_plan(plan,¶ms,&scoreFunction);
if (plan != stdin) fclose(plan);
// Command line is stronger (if given)
if (source_file[0]!='\0') strcpy( params.source_file,source_file);
// More checks
if (scoreFunction.size == 0) {
fprintf(stderr,"No score function given");
exit(EXIT.MISSING_PARAMETERS);
}
// time/location tuples for horoscopes
*hordat = malloc(params.ref_size*sizeof(_hordat));
if (*hordat == NULL) {
fprintf(stderr,"Could not allocate %d bytes in heap space\n",params.ref_size*sizeof(_hordat));
exit(EXIT.ALLOC_ERROR);
}
// Attach debug score function if in debug mode
if (debug_level > 0) {
getScoreFn = get_score_debug;
}
}
// Read execution plan
void read_plan(FILE* plan, _params* params, _scoreFunction* scoreFunction) {
char line[255];
char parsing_block[255];
*parsing_block = '\0';
while(fgets(line,255,plan) != NULL) {
parse_single_line(line,parsing_block,params,scoreFunction);
}
}
void parse_single_line( char* line, char* parsing_block, _params* params, _scoreFunction* scoreFunction) {
char *s;
if (line[0]=='#') return; // Skip comments
// Detect (block)-begin
if ( (s = strstr(line,"-begin")) &&
((*(s+6)=='\0')||(*(s+6)=='\n'))) {
strncpy(parsing_block,line,s-line);
parsing_block[s-line] = '\0';
if (strcmp(parsing_block,"score")==0) {
scoreFunction->size = 0;
scoreFunction->terms = malloc( SCORE_FUNCTION.INITIAL_SIZE * sizeof( _scoreFunctionTerm ) );
scoreFunction->max_size = SCORE_FUNCTION.INITIAL_SIZE;
}
return;
}
// Detect (block)-end
if ((s = strstr(line,"-end")) &&
((*(s+4)=='\0')||(*(s+4)=='\n'))) {
*parsing_block = '\0';
return;
}
if (*parsing_block) {
// Handle line in a block
if (strcmp(parsing_block,"score")==0) {
parse_score_function(line,scoreFunction);
}
}
else {
// Line outside of a block
parse_plan_parameter(line,params);
}
}
void parse_plan_parameter(char* line, _params* params) {
char name[50],value[255];
auto void do_version(),do_hsys(),do_cont_with_mosh(),do_jd(),
do_rand_date_fn(),do_test_runs(),do_seed(),do_source_file(),
do_quantiles(),do_dbg_level(),do_ephepath( );
auto bool check_single_param(char*,void(*)());
if (sscanf(line," %[^:] : %s",name,value)==2) {
if (check_single_param("version",do_version)) return;
if (check_single_param("source-file",do_source_file)) return;
if (check_single_param("test-runs",do_test_runs)) return;
if (check_single_param("hsys",do_hsys)) return;
if (check_single_param("cont-with-mosh",do_cont_with_mosh)) return;
if (check_single_param("jd",do_jd)) return;
if (check_single_param("quantiles",do_quantiles)) return;
if (check_single_param("rand-date-fn",do_rand_date_fn)) return;
if (check_single_param("seed",do_seed)) return;
if (check_single_param("dbg-level",do_dbg_level)) return;
if (check_single_param("ephepath",do_ephepath)) return;
printf("Warning: Unknown parameter '%s'\n",name);
}
bool check_single_param( char* exp_name, void(do_param)()) {
if (strcmp(name,exp_name)==0) {
do_param();
return true;
}
return false;
}
void do_version( ) {
if (sscanf(value,"%lf",&(params->version))==0) {
fprintf(stderr,"Couldn't parse version number '%s'\n",value);
exit(EXIT.SYNTAX_ERROR);
}
if (params->version > VERSION) {
fprintf(stderr,"Version %f not supported\n",params->version);
exit(EXIT.VERSION_ERROR);
}
}
void do_hsys( ) {
if (strchr("PKORCAEVXHTBG",value[0])==NULL) {
fprintf(stderr,"House system '%c' not supported\n",value[0]);
exit(EXIT.UNKNOWN_HOUSE_SYSTEM);
}
params->hsys = value[0];
}
void do_cont_with_mosh( ) {
int i;
params->continue_with_moshier = (sscanf(value,"%d",&i)>0 && i>0);
}
void do_jd() {
if (sscanf(value," %lf , %lf , %lf",
&(params->jd1),
&(params->jd2),
&(params->jd3) ) <3 ) {
fprintf(stderr,"Couldn't read three Julian Date values from '%s'\n",value);
exit(EXIT.MISSING_PARAMETERS);
}
}
void do_quantiles() {
if (sscanf(value," %d ",&quantiles.size)<1) {
fprintf(stderr,"Couldn't read an integer value for number of quantiles from '%s'\n",value);
exit(EXIT.MISSING_PARAMETERS);
}
if (quantiles.size==0) {
fprintf(stderr,"0 is an illegal value for number of quantiles");
exit(EXIT.PARAMETER_ERROR);
}
}
void do_rand_date_fn() {
params->rand_date_fn = getRandomDateFunction(value[0]);
}
void do_test_runs() {
if (sscanf(value,"%d",¶ms->test_runs)!=1) {
fprintf(stderr,"Can't read integer value in '%s'\n",value);
exit(EXIT.MISSING_PARAMETERS);
}
}
void do_seed() {
if (sscanf(value,"%ld , %ld",&s1,&s2)!=2) {
fprintf(stderr,"Can't read two integer values for seed in '%s'\n",value);
exit(EXIT.MISSING_PARAMETERS);
}
}
void do_source_file() {
if (sscanf(value," %s ",params->source_file)!=1) {
fprintf(stderr,"Can't read one string for source file parameter in '%s'\n",value);
exit(EXIT.MISSING_PARAMETERS);
}
}
void do_dbg_level() {
if (sscanf(value," %d ",&debug_level)!=1) {
fprintf(stderr,"Debug level must be an integer '%s'\n",value);
exit(EXIT.PARAMETER_ERROR);
}
if (debug_level < 0) {
fprintf(stderr,"Debug level must be a positive integer '%s'\n",value);
exit(EXIT.PARAMETER_ERROR);
}
}
void do_ephepath( ) {
if (sscanf(value," %[^\n] ",params->ephepath)!=1) {
fprintf(stderr,"Can't read one string for ephepath parameter in '%s'\n",value);
exit(EXIT.PARAMETER_ERROR);
}
}
}
randomDateFunction getRandomDateFunction(char key) {
switch (key) {
// Select the desired random date function
case 'r':
printf("Random date function: Choose random time\n");
return random_time;
case 'R':
printf("Random date function: Choose random date and time\n");
return random_date;
case 'Q':
printf("Random date function: Random date by quantile\n");
return random_date_by_quantile;
case 'f':
printf("Random date function: Choose random time\n");
return random_shuffle;
case 'F':
printf("Random date function: Choose random shuffle\n");
return random_shuffle_lt;
default:
fprintf(stderr,"Unknown code '%c' for random date function\n",key);
exit(EXIT.MISSING_PARAMETERS);
}
}
void parse_score_function( char* line, _scoreFunction* scoreFunction) {
_scoreFunctionTerm term = {};
int ipl1, ipl2;
char fname[50],args[150],endc;
if (sscanf(line," %lf * %[^(] ( %[^)] %c",&(term.coeff),fname,args,&endc)!=4
|| endc != ')') {
fprintf(stderr,"Incorrect syntax in line '%s'\n", line);
exit(EXIT.SYNTAX_ERROR);
}
if (fabs(term.coeff) < 1.e-8) { // Wrongly scanned decimal often gives a value near 0
printf("Term with zero coefficient will be ignored: '%s'\n",line);
}
if (strcmp(fname,"zl")==0) {
term.f = zodiacalLengthInRange;
if (sscanf(args," %d , %lf , %lf",
&ipl1,
&term.args.start,
&term.args.end)!=3) {
fprintf(stderr,"Wrong number of arguments for zl function in '%s'\n",line);
exit(EXIT.SYNTAX_ERROR);
}
term.args.pl1 = get_pl( ipl1 );
}
if (strcmp(fname,"mp")==0) {
term.f = mundanePositionInRange;
if (sscanf(args," %d , %lf , %lf",
&ipl1,
&term.args.start,
&term.args.end)!=3) {
fprintf(stderr,"Wrong number of arguments for mp function in '%s'\n",line);
exit(EXIT.SYNTAX_ERROR);
}
term.args.pl1 = get_pl( ipl1 );
}
if (strcmp(fname,"zll")==0) {
term.f = zodiacalAspect;
if (sscanf(args," %d , %d , %lf , %lf",
&ipl1,
&ipl2,
&term.args.start,
&term.args.end)!=4) {
fprintf(stderr,"Wrong number of arguments for zll function in '%s'\n",line);
exit(EXIT.SYNTAX_ERROR);
}
term.args.pl1 = get_pl( ipl1 );
term.args.pl2 = get_pl( ipl2 );
}
if (strcmp(fname,"mpp")==0) {
term.f = mundaneAspect;
if (sscanf(args," %d , %d , %lf , %lf",
&ipl1,
&ipl2,
&term.args.start,
&term.args.end)!=4) {
fprintf(stderr,"Wrong number of arguments for mpp function in '%s'\n",line);
exit(EXIT.SYNTAX_ERROR);
}
term.args.pl1 = get_pl( ipl1 );
term.args.pl2 = get_pl( ipl2 );
}
if (!term.f) {
fprintf(stderr,"Unknown funcion '%s' in '%s'\n",fname,line);
exit(EXIT.SYNTAX_ERROR);
}
addTermToScoreFunction(&term,scoreFunction);
}
void addTermToScoreFunction(_scoreFunctionTerm* term,_scoreFunction* scoreFunction) {
int index = scoreFunction->size;
if (++scoreFunction->size >= scoreFunction->max_size) {
scoreFunction->max_size += SCORE_FUNCTION.DELTA_SIZE;
_scoreFunctionTerm* newTerms = (_scoreFunctionTerm*)
realloc( scoreFunction->terms, scoreFunction->max_size * sizeof( _scoreFunctionTerm ) );
if (newTerms == NULL) {
fprintf(stderr,"Couldn't allocate enough memory for score function terms, requested %d bytes",
scoreFunction->max_size * sizeof( _scoreFunctionTerm ));
exit(EXIT.ALLOC_ERROR);
}
scoreFunction->terms = newTerms;
}
scoreFunction->terms[index] = *term;
}
//--------------------------------------------------------------------
// Read command line parameters (if specified)
//--------------------------------------------------------------------
void get_cmdline_options(int argc, char *argv[],
char *source_file,
char *plan_file) {
// Default values, if no command line values given
strcpy(source_file,""); // source file name
strcpy(plan_file,"");
if (argc == 2) {
// One argument given: Assume plan file name
strcpy(plan_file,argv[1]);
}
if (argc == 3) {
// Two arguments given: plan file, then source file name
strcpy(plan_file,argv[1]);
strcpy(source_file,argv[2]);
}
if (argc > 3) {
fprintf(stderr,"Bad number of arguments (%d)\n",argc-1);
exit(EXIT.PARAMETER_ERROR);
}
}
int get_pl(int ipl) {
int pl;
// Search existing entry
for (int i=0;i<scoreFunction.cdata.pl2ipl_size;i++) {
if (scoreFunction.cdata.pl2ipl[i] == ipl) return i;
}
// Check size
if ( scoreFunction.cdata.pl2ipl_size >=
scoreFunction.cdata.pl2ipl_max_size) {
scoreFunction.cdata.pl2ipl_max_size += PL2IPL.DELTA_SIZE;
scoreFunction.cdata.pl2ipl = realloc( scoreFunction.cdata.pl2ipl,
scoreFunction.cdata.pl2ipl_max_size * sizeof( int ) );
}
// Add new entry
pl = scoreFunction.cdata.pl2ipl_size;
scoreFunction.cdata.pl2ipl[pl] = ipl;
scoreFunction.cdata.pl2ipl_size++;
return pl;
}