61 #pragma warning(disable : 4996)
74 #define DO(val) if (!(val)) return NULL
75 #define CALLFUNC(ggg,fff) (*((ggg)->funcs.fff))
76 #define SKIPSPACE(ccc) while (isspace(*ccc)) ccc++
77 #define isvarfirstletter(ccc) (isalpha(ccc) || (ccc) == '_')
80 static const char *parse_variable(
IfParser *g,
const char *cp,
86 return CALLFUNC(g, handle_error) (g, cp,
"variable name");
90 for (cp++; isalnum(*cp) || *cp ==
'_'; cp++) ;
95 static const char *parse_number(
IfParser *g,
const char *cp,
int *valp)
100 return CALLFUNC(g, handle_error) (g, cp,
"number");
104 *valp = strtol(cp, &hold_result, 0);
109 for (cp++; isdigit(*cp); cp++) ;
115 static const char *parse_value(
IfParser *g,
const char *cp,
int *valp)
130 return CALLFUNC(g, handle_error) (g, cp,
")");
135 DO (cp = parse_value (g, cp + 1, valp));
140 DO (cp = parse_value (g, cp + 1, valp));
145 DO (cp = parse_variable (g, cp + 1, &var));
148 return CALLFUNC(g, handle_error) (g, cp,
"(");
150 DO (cp = parse_variable (g, cp + 1, &var));
152 }
while (*cp && *cp !=
')');
154 return CALLFUNC(g, handle_error) (g, cp,
")");
159 if (strncmp (cp,
"defined", 7) == 0 && !isalnum(cp[7])) {
167 DO (cp = parse_variable (g, cp, &var));
169 if (paren && *cp !=
')')
170 return CALLFUNC(g, handle_error) (g, cp,
")");
179 DO (cp = parse_number (g, cp, valp));
181 return CALLFUNC(g, handle_error) (g, cp,
"variable or number");
183 DO (cp = parse_variable (g, cp, &var));
190 static const char *parse_product(
IfParser *g,
const char *cp,
int *valp)
194 DO (cp = parse_value (g, cp, valp));
199 DO (cp = parse_product (g, cp + 1, &rightval));
200 *valp = (*valp * rightval);
204 DO (cp = parse_product (g, cp + 1, &rightval));
205 *valp = (*valp / rightval);
209 DO (cp = parse_product (g, cp + 1, &rightval));
210 *valp = (*valp % rightval);
216 static const char *parse_sum(
IfParser *g,
const char *cp,
int *valp)
220 DO (cp = parse_product (g, cp, valp));
225 DO (cp = parse_sum (g, cp + 1, &rightval));
226 *valp = (*valp + rightval);
230 DO (cp = parse_sum (g, cp + 1, &rightval));
231 *valp = (*valp - rightval);
238 static const char *parse_shift(
IfParser *g,
const char *cp,
int *valp)
242 DO (cp = parse_sum (g, cp, valp));
248 DO (cp = parse_shift (g, cp + 2, &rightval));
249 *valp = (*valp << rightval);
255 DO (cp = parse_shift (g, cp + 2, &rightval));
256 *valp = (*valp >> rightval);
263 static const char *parse_inequality(
IfParser *g,
const char *cp,
int *valp)
267 DO (cp = parse_shift (g, cp, valp));
273 DO (cp = parse_inequality (g, cp + 2, &rightval));
274 *valp = (*valp <= rightval);
276 DO (cp = parse_inequality (g, cp + 1, &rightval));
277 *valp = (*valp < rightval);
283 DO (cp = parse_inequality (g, cp + 2, &rightval));
284 *valp = (*valp >= rightval);
286 DO (cp = parse_inequality (g, cp + 1, &rightval));
287 *valp = (*valp > rightval);
294 static const char *parse_equality(
IfParser *g,
const char *cp,
int *valp)
298 DO (cp = parse_inequality (g, cp, valp));
305 DO (cp = parse_equality (g, cp + 1, &rightval));
306 *valp = (*valp == rightval);
312 DO (cp = parse_equality (g, cp + 2, &rightval));
313 *valp = (*valp != rightval);
319 static const char *parse_band(
IfParser *g,
const char *cp,
int *valp)
323 DO (cp = parse_equality (g, cp, valp));
329 DO (cp = parse_band (g, cp + 1, &rightval));
330 *valp = (*valp & rightval);
338 static const char *parse_bor(
IfParser *g,
const char *cp,
int *valp)
342 DO (cp = parse_band (g, cp, valp));
348 DO (cp = parse_bor (g, cp + 1, &rightval));
349 *valp = (*valp | rightval);
356 static const char *parse_land(
IfParser *g,
const char *cp,
int *valp)
360 DO (cp = parse_bor (g, cp, valp));
366 return CALLFUNC(g, handle_error) (g, cp,
"&&");
367 DO (cp = parse_land (g, cp + 2, &rightval));
368 *valp = (*valp && rightval);
374 static const char *parse_lor(
IfParser *g,
const char *cp,
int *valp)
378 DO (cp = parse_land (g, cp, valp));
384 return CALLFUNC(g, handle_error) (g, cp,
"||");
385 DO (cp = parse_lor (g, cp + 2, &rightval));
386 *valp = (*valp || rightval);
399 return parse_lor (g, cp, valp);
#define isvarfirstletter(ccc)
#define CALLFUNC(ggg, fff)
const char * ParseIfExpression(IfParser *g, const char *cp, int *valp)
int(* eval_variable)(IfParser *, const char *, int)
int(* eval_defined)(IfParser *, const char *, int)
struct IfParser::@0 funcs