Fix another crash and do some style fixes in bc parsing
diff --git a/include/bc.h b/include/bc.h
index c08d7b4..785f59b 100644
--- a/include/bc.h
+++ b/include/bc.h
@@ -122,7 +122,9 @@
((t) == BC_INST_VAR || (t) == BC_INST_ARRAY_ELEM || (t) == BC_INST_LAST || \
(t) == BC_INST_SCALE || (t) == BC_INST_IBASE || (t) == BC_INST_OBASE)
-#define BC_PARSE_PREV_PRE(p) ((p) >= BC_INST_INC_PRE && (p) <= BC_INST_BOOL_NOT)
+#define BC_PARSE_PREV_PREFIX(p) \
+ ((p) >= BC_INST_INC_PRE && (p) <= BC_INST_BOOL_NOT)
+#define BC_PARSE_OP_PREFIX(t) ((t) == BC_LEX_OP_BOOL_NOT || (t) == BC_LEX_NEG)
// We can calculate the conversion between tokens and exprs by subtracting the
// position of the first operator in the lex enum and adding the position of
diff --git a/src/bc/parse.c b/src/bc/parse.c
index 3f0148d..ff057a9 100644
--- a/src/bc/parse.c
+++ b/src/bc/parse.c
@@ -71,7 +71,7 @@
bc_parse_push(p, BC_PARSE_TOKEN_INST(t));
bc_vec_pop(&p->ops);
- *nexprs -= t != BC_LEX_OP_BOOL_NOT && t != BC_LEX_NEG;
+ *nexprs -= !BC_PARSE_OP_PREFIX(t);
}
bc_vec_push(&p->ops, &type);
@@ -88,7 +88,7 @@
bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
bc_vec_pop(&p->ops);
- *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
+ *nexs -= !BC_PARSE_OP_PREFIX(top);
if (p->ops.len <= ops_bgn) return bc_parse_err(p, BC_ERROR_PARSE_EXPR);
}
@@ -1295,19 +1295,19 @@
case BC_LEX_OP_BOOL_OR:
case BC_LEX_OP_BOOL_AND:
{
- if (t == BC_LEX_OP_BOOL_NOT) {
- if (!bin_last && p->l.last != BC_LEX_OP_BOOL_NOT)
+ if (BC_PARSE_OP_PREFIX(t)) {
+ if (!bin_last && !BC_PARSE_OP_PREFIX(p->l.last))
return bc_parse_err(p, BC_ERROR_PARSE_EXPR);
}
- else if (BC_PARSE_PREV_PRE(prev) || bin_last)
+ else if (BC_PARSE_PREV_PREFIX(prev) || bin_last)
return bc_parse_err(p, BC_ERROR_PARSE_EXPR);
- nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT;
+ nrelops += (t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT);
prev = BC_PARSE_TOKEN_INST(t);
bc_parse_operator(p, t, ops_bgn, &nexprs);
rprn = incdec = false;
get_token = true;
- bin_last = t != BC_LEX_OP_BOOL_NOT;
+ bin_last = !BC_PARSE_OP_PREFIX(t);
break;
}
@@ -1331,7 +1331,7 @@
// is handled in bc_parse_expr_status().
if (p->l.last == BC_LEX_LPAREN) return BC_STATUS_EMPTY_EXPR;
- if (bin_last || prev == BC_INST_BOOL_NOT)
+ if (bin_last || BC_PARSE_PREV_PREFIX(prev))
return bc_parse_err(p, BC_ERROR_PARSE_EXPR);
if (!nparens) {
@@ -1457,12 +1457,11 @@
bc_parse_push(p, BC_PARSE_TOKEN_INST(top));
- nexprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG;
+ nexprs -= !BC_PARSE_OP_PREFIX(top);
bc_vec_pop(&p->ops);
}
- if (prev == BC_INST_BOOL_NOT || nexprs != 1)
- return bc_parse_err(p, BC_ERROR_PARSE_EXPR);
+ if (nexprs != 1) return bc_parse_err(p, BC_ERROR_PARSE_EXPR);
for (i = 0; i < next.len && t != next.tokens[i]; ++i);
if (i == next.len && !BC_PARSE_VALID_END_TOKEN(t))