Skip to content

Commit 81caed4

Browse files
committed
Clean up preprocessor expression evaluation code
Remove redundant code and add/improve comments.
1 parent 204a5f7 commit 81caed4

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

Source/Preprocessor/expr.c

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,31 @@
1818
static Scanner *scan = 0;
1919

2020
typedef struct {
21+
/* One of the EXPR_xxx values defined below. */
2122
int op;
23+
/* op == EXPR_OP: value is the token specifying which operator.
24+
*
25+
* op == EXPR_VALUE && svalue == NULL: Numeric expression value.
26+
*
27+
* Otherwise unused.
28+
*/
2229
long value;
30+
/* op == EXPR_VALUE: If non-NULL, string expression value; if NULL see value.
31+
*
32+
* Otherwise unused.
33+
*/
2334
String *svalue;
2435
} exprval;
2536

2637
#define EXPR_TOP 1
2738
#define EXPR_VALUE 2
2839
#define EXPR_OP 3
2940
#define EXPR_GROUP 4
30-
#define EXPR_UMINUS 100
41+
42+
/* Special token value used here to distinguish from SWIG_TOKEN_MINUS
43+
* (which we use here for a two argument minus).
44+
*/
45+
#define OP_UMINUS 100
3146

3247
static exprval stack[256]; /* Parsing stack */
3348
static int sp = 0; /* Stack pointer */
@@ -38,7 +53,7 @@ static const char *errmsg = 0; /* Parsing error */
3853
/* Initialize the precedence table for various operators. Low values have higher precedence */
3954
static void init_precedence() {
4055
prec[SWIG_TOKEN_NOT] = 10;
41-
prec[EXPR_UMINUS] = 10;
56+
prec[OP_UMINUS] = 10;
4257
prec[SWIG_TOKEN_STAR] = 20;
4358
prec[SWIG_TOKEN_SLASH] = 20;
4459
prec[SWIG_TOKEN_PERCENT] = 20;
@@ -63,7 +78,7 @@ static void init_precedence() {
6378

6479
#define UNARY_OP(token) (((token) == SWIG_TOKEN_NOT) || \
6580
((token) == SWIG_TOKEN_LNOT) || \
66-
((token) == EXPR_UMINUS))
81+
((token) == OP_UMINUS))
6782

6883
/* Reduce a single operator on the stack */
6984
/* return 0 on failure, 1 on success */
@@ -183,7 +198,7 @@ static int reduce_op() {
183198
stack[sp - 1].value = !stack[sp].value;
184199
sp--;
185200
break;
186-
case EXPR_UMINUS:
201+
case OP_UMINUS:
187202
stack[sp - 1].value = -stack[sp].value;
188203
sp--;
189204
break;
@@ -278,13 +293,15 @@ int Preprocessor_expr(DOH *s, int *error) {
278293

279294
/* Put initial state onto the stack */
280295
stack[sp].op = EXPR_TOP;
281-
stack[sp].value = 0;
282296

283297
while (1) {
284298
/* Look at the top of the stack */
285299
switch (stack[sp].op) {
286300
case EXPR_TOP:
287-
/* An expression. Can be a number or another expression enclosed in parens */
301+
/* EXPR_TOP is a place-holder which can only appear on the top of the
302+
* stack. We can reduce it to any expression - a number, a string, an
303+
* unary operator, or another expression enclosed in parentheses.
304+
*/
288305
token = expr_token(scan);
289306
if (!token) {
290307
errmsg = "Expected an expression";
@@ -296,26 +313,27 @@ int Preprocessor_expr(DOH *s, int *error) {
296313
char *c = Char(Scanner_text(scan));
297314
stack[sp].value = (long) strtol(c, 0, 0);
298315
stack[sp].svalue = 0;
299-
/* stack[sp].value = (long) atol(Char(Scanner_text(scan))); */
300316
stack[sp].op = EXPR_VALUE;
301317
} else if (token == SWIG_TOKEN_PLUS) {
302318
} else if ((token == SWIG_TOKEN_MINUS) || (token == SWIG_TOKEN_LNOT) || (token == SWIG_TOKEN_NOT)) {
303319
if (token == SWIG_TOKEN_MINUS)
304-
token = EXPR_UMINUS;
320+
token = OP_UMINUS;
305321
stack[sp].value = token;
306-
stack[sp++].op = EXPR_OP;
322+
stack[sp].op = EXPR_OP;
323+
sp++;
307324
stack[sp].op = EXPR_TOP;
308-
stack[sp].svalue = 0;
309325
} else if (token == SWIG_TOKEN_LPAREN) {
310-
stack[sp++].op = EXPR_GROUP;
326+
stack[sp].op = EXPR_GROUP;
327+
sp++;
311328
stack[sp].op = EXPR_TOP;
312-
stack[sp].value = 0;
313-
stack[sp].svalue = 0;
314329
} else if (token == SWIG_TOKEN_ENDLINE) {
315330
} else if (token == SWIG_TOKEN_STRING) {
316331
stack[sp].svalue = NewString(Scanner_text(scan));
317332
stack[sp].op = EXPR_VALUE;
318333
} else if (token == SWIG_TOKEN_ID) {
334+
/* Defined macros have been expanded already so this is an unknown
335+
* macro, which gets treated as zero.
336+
*/
319337
stack[sp].value = 0;
320338
stack[sp].svalue = 0;
321339
stack[sp].op = EXPR_VALUE;
@@ -327,7 +345,9 @@ int Preprocessor_expr(DOH *s, int *error) {
327345
goto syntax_error;
328346
break;
329347
case EXPR_VALUE:
330-
/* A value is on the stack. We may reduce or evaluate depending on what the next token is */
348+
/* A value is on top of the stack. We may reduce or evaluate depending
349+
* on what the next token is.
350+
*/
331351
token = expr_token(scan);
332352
if (!token) {
333353
/* End of input. Might have to reduce if an operator is on stack */
@@ -371,7 +391,6 @@ int Preprocessor_expr(DOH *s, int *error) {
371391
stack[sp].value = token;
372392
sp++;
373393
stack[sp].op = EXPR_TOP;
374-
stack[sp].value = 0;
375394
} else {
376395
if (stack[sp - 1].op != EXPR_OP)
377396
goto syntax_error_expected_operator;
@@ -390,7 +409,6 @@ int Preprocessor_expr(DOH *s, int *error) {
390409
stack[sp].value = token;
391410
sp++;
392411
stack[sp].op = EXPR_TOP;
393-
stack[sp].value = 0;
394412
}
395413
break;
396414
case SWIG_TOKEN_RPAREN:

0 commit comments

Comments
 (0)