diff options
Diffstat (limited to 'miscutils')
-rw-r--r-- | miscutils/bc.c | 44 |
1 files changed, 27 insertions, 17 deletions
diff --git a/miscutils/bc.c b/miscutils/bc.c index 5cd80c1e5..ee3061621 100644 --- a/miscutils/bc.c +++ b/miscutils/bc.c | |||
@@ -3705,11 +3705,7 @@ static size_t bc_program_addFunc(char *name) | |||
3705 | return idx; | 3705 | return idx; |
3706 | } | 3706 | } |
3707 | 3707 | ||
3708 | #define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops))) | 3708 | #define BC_PARSE_TOP_OP(p) (*(BcLexType*)bc_vec_top(&(p)->ops)) |
3709 | #define BC_PARSE_LEAF(p, rparen) \ | ||
3710 | (((p) >= XC_INST_NUM && (p) <= XC_INST_SQRT) || (rparen) || \ | ||
3711 | (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST) | ||
3712 | |||
3713 | // We can calculate the conversion between tokens and exprs by subtracting the | 3709 | // We can calculate the conversion between tokens and exprs by subtracting the |
3714 | // position of the first operator in the lex enum and adding the position of the | 3710 | // position of the first operator in the lex enum and adding the position of the |
3715 | // first in the expr enum. Note: This only works for binary operators. | 3711 | // first in the expr enum. Note: This only works for binary operators. |
@@ -4047,6 +4043,24 @@ static BC_STATUS zbc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr, | |||
4047 | } | 4043 | } |
4048 | #define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS) | 4044 | #define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS) |
4049 | 4045 | ||
4046 | #if 0 | ||
4047 | #define BC_PARSE_LEAF(p, rparen) \ | ||
4048 | ((rparen) \ | ||
4049 | || ((p) >= XC_INST_NUM && (p) <= XC_INST_SQRT) \ | ||
4050 | || (p) == BC_INST_INC_POST \ | ||
4051 | || (p) == BC_INST_DEC_POST \ | ||
4052 | ) | ||
4053 | #else | ||
4054 | static int ok_in_expr(BcInst p) | ||
4055 | { | ||
4056 | return (p >= XC_INST_NUM && p <= XC_INST_SQRT) | ||
4057 | || p == BC_INST_INC_POST | ||
4058 | || p == BC_INST_DEC_POST | ||
4059 | ; | ||
4060 | } | ||
4061 | #define BC_PARSE_LEAF(p, rparen) ((rparen) || ok_in_expr(p)) | ||
4062 | #endif | ||
4063 | |||
4050 | static BC_STATUS zbc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn, | 4064 | static BC_STATUS zbc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn, |
4051 | bool rparen, size_t *nexprs) | 4065 | bool rparen, size_t *nexprs) |
4052 | { | 4066 | { |
@@ -4057,10 +4071,7 @@ static BC_STATUS zbc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn, | |||
4057 | s = zbc_lex_next(&p->l); | 4071 | s = zbc_lex_next(&p->l); |
4058 | if (s) RETURN_STATUS(s); | 4072 | if (s) RETURN_STATUS(s); |
4059 | 4073 | ||
4060 | type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST || | 4074 | type = BC_PARSE_LEAF(etype, rparen) ? XC_LEX_OP_MINUS : XC_LEX_NEG; |
4061 | (etype >= XC_INST_NUM && etype <= XC_INST_SQRT) ? | ||
4062 | XC_LEX_OP_MINUS : | ||
4063 | XC_LEX_NEG; | ||
4064 | *prev = BC_TOKEN_2_INST(type); | 4075 | *prev = BC_TOKEN_2_INST(type); |
4065 | 4076 | ||
4066 | // We can just push onto the op stack because this is the largest | 4077 | // We can just push onto the op stack because this is the largest |
@@ -4723,7 +4734,7 @@ static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags) | |||
4723 | case BC_LEX_LPAREN: | 4734 | case BC_LEX_LPAREN: |
4724 | if (BC_PARSE_LEAF(prev, rprn)) | 4735 | if (BC_PARSE_LEAF(prev, rprn)) |
4725 | return bc_error_bad_expression(); | 4736 | return bc_error_bad_expression(); |
4726 | ++nparens; | 4737 | nparens++; |
4727 | paren_expr = rprn = bin_last = false; | 4738 | paren_expr = rprn = bin_last = false; |
4728 | get_token = true; | 4739 | get_token = true; |
4729 | bc_vec_push(&p->ops, &t); | 4740 | bc_vec_push(&p->ops, &t); |
@@ -4741,7 +4752,7 @@ static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags) | |||
4741 | dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__); | 4752 | dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__); |
4742 | return BC_STATUS_PARSE_EMPTY_EXP; | 4753 | return BC_STATUS_PARSE_EMPTY_EXP; |
4743 | } | 4754 | } |
4744 | --nparens; | 4755 | nparens--; |
4745 | paren_expr = rprn = true; | 4756 | paren_expr = rprn = true; |
4746 | get_token = bin_last = false; | 4757 | get_token = bin_last = false; |
4747 | s = zbc_parse_rightParen(p, ops_bgn, &nexprs); | 4758 | s = zbc_parse_rightParen(p, ops_bgn, &nexprs); |
@@ -4772,7 +4783,7 @@ static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags) | |||
4772 | bc_parse_push(p, (char) prev); | 4783 | bc_parse_push(p, (char) prev); |
4773 | paren_expr = get_token = true; | 4784 | paren_expr = get_token = true; |
4774 | rprn = bin_last = false; | 4785 | rprn = bin_last = false; |
4775 | ++nexprs; | 4786 | nexprs++; |
4776 | break; | 4787 | break; |
4777 | case BC_LEX_KEY_LENGTH: | 4788 | case BC_LEX_KEY_LENGTH: |
4778 | case BC_LEX_KEY_SQRT: | 4789 | case BC_LEX_KEY_SQRT: |
@@ -4781,16 +4792,15 @@ static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags) | |||
4781 | s = zbc_parse_builtin(p, t, flags, &prev); | 4792 | s = zbc_parse_builtin(p, t, flags, &prev); |
4782 | paren_expr = true; | 4793 | paren_expr = true; |
4783 | rprn = get_token = bin_last = false; | 4794 | rprn = get_token = bin_last = false; |
4784 | ++nexprs; | 4795 | nexprs++; |
4785 | break; | 4796 | break; |
4786 | case BC_LEX_KEY_READ: | 4797 | case BC_LEX_KEY_READ: |
4787 | if (BC_PARSE_LEAF(prev, rprn)) | 4798 | if (BC_PARSE_LEAF(prev, rprn)) |
4788 | return bc_error_bad_expression(); | 4799 | return bc_error_bad_expression(); |
4789 | else | 4800 | s = zbc_parse_read(p); |
4790 | s = zbc_parse_read(p); | ||
4791 | paren_expr = true; | 4801 | paren_expr = true; |
4792 | rprn = get_token = bin_last = false; | 4802 | rprn = get_token = bin_last = false; |
4793 | ++nexprs; | 4803 | nexprs++; |
4794 | prev = XC_INST_READ; | 4804 | prev = XC_INST_READ; |
4795 | break; | 4805 | break; |
4796 | case BC_LEX_KEY_SCALE: | 4806 | case BC_LEX_KEY_SCALE: |
@@ -4799,7 +4809,7 @@ static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags) | |||
4799 | s = zbc_parse_scale(p, &prev, flags); | 4809 | s = zbc_parse_scale(p, &prev, flags); |
4800 | paren_expr = true; | 4810 | paren_expr = true; |
4801 | rprn = get_token = bin_last = false; | 4811 | rprn = get_token = bin_last = false; |
4802 | ++nexprs; | 4812 | nexprs++; |
4803 | prev = XC_INST_SCALE; | 4813 | prev = XC_INST_SCALE; |
4804 | break; | 4814 | break; |
4805 | default: | 4815 | default: |