Properly parse unary '+' operator. It's ignored, essentially.

This commit is contained in:
Dianne Skoll
2025-05-17 11:33:27 -04:00
parent 098cf4707a
commit e4a6a5cf01

View File

@@ -2144,8 +2144,7 @@ static int make_atom(expr_node *atom, Var *locals)
/* */
/* Parse an atom. */
/* */
/* ATOM: '+' ATOM | */
/* '(' EXPR ')' | */
/* ATOM: '(' EXPR ')' | */
/* CONSTANT | */
/* VAR | */
/* FUNCTION_CALL */
@@ -2159,18 +2158,6 @@ static expr_node *parse_atom(char const **e, int *r, Var *locals, int level)
*r = PEEK_TOKEN();
if (*r != OK) return NULL;
/* Ignore unary-plus operators */
while (TOKEN_IS("+")) {
*r = GET_TOKEN();
if (*r != OK) {
return NULL;
}
*r = PEEK_TOKEN();
if (*r != OK) {
return NULL;
}
}
if (TOKEN_IS("(")) {
/* Parenthesiszed expession: '(' EXPR ')' */
@@ -2238,6 +2225,7 @@ static expr_node *parse_atom(char const **e, int *r, Var *locals, int level)
/* */
/* FACTOR_EXP: '-' FACTOR_EXP | */
/* '!' FACTOR_EXP | */
/* '+' FACTOR_EXP */
/* ATOM */
/* */
/***************************************************************/
@@ -2251,11 +2239,13 @@ static expr_node *parse_factor(char const **e, int *r, Var *locals, int level)
if (*r != OK) {
return NULL;
}
if (TOKEN_IS("!") || TOKEN_IS("-")) {
if (TOKEN_IS("!") || TOKEN_IS("-") || TOKEN_IS("+")) {
if (TOKEN_IS("!")) {
op = '!';
} else {
} else if (TOKEN_IS("-")) {
op = '-';
} else {
op = '+';
}
/* Pull off the peeked token */
GET_TOKEN();
@@ -2264,6 +2254,11 @@ static expr_node *parse_factor(char const **e, int *r, Var *locals, int level)
return NULL;
}
/* Ignore unary plus operator */
if (op == '+') {
return node;
}
/* Optimize '-' or '!' followed by integer constant */
if (node->type == N_CONSTANT &&node->u.value.type == INT_TYPE) {
if (op == '-') {