diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2018-12-25 01:16:37 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-12-25 01:16:37 +0100 |
commit | bb116031a038237ecc82896fb6b12b89b72f8d91 (patch) | |
tree | 24bad0f039c3fd421d1935a084f2798430ab26e1 | |
parent | d897c9aca6424b9a143746f73df1c132292bb091 (diff) | |
download | busybox-w32-bb116031a038237ecc82896fb6b12b89b72f8d91.tar.gz busybox-w32-bb116031a038237ecc82896fb6b12b89b72f8d91.tar.bz2 busybox-w32-bb116031a038237ecc82896fb6b12b89b72f8d91.zip |
bc: simplify bc_parse_expr_empty_ok()
function old new delta
bc_parse_expr_empty_ok 1846 1819 -27
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | miscutils/bc.c | 36 |
1 files changed, 20 insertions, 16 deletions
diff --git a/miscutils/bc.c b/miscutils/bc.c index 62bdd9a44..5d9bae47e 100644 --- a/miscutils/bc.c +++ b/miscutils/bc.c | |||
@@ -4661,20 +4661,21 @@ static BC_STATUS zbc_parse_stmt_or_funcdef(BcParse *p) | |||
4661 | // This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP | 4661 | // This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP |
4662 | static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags) | 4662 | static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags) |
4663 | { | 4663 | { |
4664 | BcStatus s = BC_STATUS_SUCCESS; | ||
4665 | BcInst prev = XC_INST_PRINT; | 4664 | BcInst prev = XC_INST_PRINT; |
4666 | BcLexType top, t = p->l.lex; | 4665 | BcLexType top, t = p->l.lex; |
4667 | size_t nexprs = 0, ops_bgn = p->ops.len; | 4666 | size_t nexprs = 0, ops_bgn = p->ops.len; |
4668 | unsigned nparens, nrelops; | 4667 | unsigned nparens, nrelops; |
4669 | bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last; | 4668 | bool paren_first, paren_expr, rprn, get_token, assign, bin_last; |
4670 | 4669 | ||
4671 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); | 4670 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
4672 | paren_first = (p->l.lex == BC_LEX_LPAREN); | 4671 | paren_first = (p->l.lex == BC_LEX_LPAREN); |
4673 | nparens = nrelops = 0; | 4672 | nparens = nrelops = 0; |
4674 | paren_expr = rprn = done = get_token = assign = false; | 4673 | paren_expr = rprn = get_token = assign = false; |
4675 | bin_last = true; | 4674 | bin_last = true; |
4676 | 4675 | ||
4677 | for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.lex) { | 4676 | for (; bc_parse_exprs(t); t = p->l.lex) { |
4677 | BcStatus s = BC_STATUS_SUCCESS; | ||
4678 | |||
4678 | dbg_lex("%s:%d t:%d", __func__, __LINE__, t); | 4679 | dbg_lex("%s:%d t:%d", __func__, __LINE__, t); |
4679 | switch (t) { | 4680 | switch (t) { |
4680 | case BC_LEX_OP_INC: | 4681 | case BC_LEX_OP_INC: |
@@ -4743,10 +4744,7 @@ static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags) | |||
4743 | if (bin_last || prev == XC_INST_BOOL_NOT) | 4744 | if (bin_last || prev == XC_INST_BOOL_NOT) |
4744 | return bc_error_bad_expression(); | 4745 | return bc_error_bad_expression(); |
4745 | if (nparens == 0) { | 4746 | if (nparens == 0) { |
4746 | s = BC_STATUS_SUCCESS; | 4747 | goto exit_loop; |
4747 | done = true; | ||
4748 | get_token = false; | ||
4749 | break; | ||
4750 | } | 4748 | } |
4751 | if (!paren_expr) { | 4749 | if (!paren_expr) { |
4752 | dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__); | 4750 | dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__); |
@@ -4763,7 +4761,7 @@ static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags) | |||
4763 | paren_expr = true; | 4761 | paren_expr = true; |
4764 | rprn = get_token = bin_last = false; | 4762 | rprn = get_token = bin_last = false; |
4765 | s = zbc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL); | 4763 | s = zbc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL); |
4766 | ++nexprs; | 4764 | nexprs++; |
4767 | break; | 4765 | break; |
4768 | case XC_LEX_NUMBER: | 4766 | case XC_LEX_NUMBER: |
4769 | if (BC_PARSE_LEAF(prev, rprn)) | 4767 | if (BC_PARSE_LEAF(prev, rprn)) |
@@ -4807,21 +4805,24 @@ static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags) | |||
4807 | if (BC_PARSE_LEAF(prev, rprn)) | 4805 | if (BC_PARSE_LEAF(prev, rprn)) |
4808 | return bc_error_bad_expression(); | 4806 | return bc_error_bad_expression(); |
4809 | s = zbc_parse_scale(p, &prev, flags); | 4807 | s = zbc_parse_scale(p, &prev, flags); |
4808 | prev = XC_INST_SCALE; | ||
4810 | paren_expr = true; | 4809 | paren_expr = true; |
4811 | rprn = get_token = bin_last = false; | 4810 | rprn = get_token = bin_last = false; |
4812 | nexprs++; | 4811 | nexprs++; |
4813 | prev = XC_INST_SCALE; | ||
4814 | break; | 4812 | break; |
4815 | default: | 4813 | default: |
4816 | s = bc_error_bad_token(); | 4814 | s = bc_error_bad_token(); |
4817 | break; | 4815 | break; |
4818 | } | 4816 | } |
4819 | 4817 | ||
4820 | if (!s && get_token) s = zbc_lex_next(&p->l); | 4818 | if (s || G_interrupt) // error, or ^C: stop parsing |
4819 | return BC_STATUS_FAILURE; | ||
4820 | if (get_token) { | ||
4821 | s = zbc_lex_next(&p->l); | ||
4822 | if (s) return s; | ||
4823 | } | ||
4821 | } | 4824 | } |
4822 | 4825 | exit_loop: | |
4823 | if (s) return s; | ||
4824 | if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing | ||
4825 | 4826 | ||
4826 | while (p->ops.len > ops_bgn) { | 4827 | while (p->ops.len > ops_bgn) { |
4827 | top = BC_PARSE_TOP_OP(p); | 4828 | top = BC_PARSE_TOP_OP(p); |
@@ -4840,20 +4841,23 @@ static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags) | |||
4840 | return bc_error_bad_expression(); | 4841 | return bc_error_bad_expression(); |
4841 | 4842 | ||
4842 | if (!(flags & BC_PARSE_REL) && nrelops) { | 4843 | if (!(flags & BC_PARSE_REL) && nrelops) { |
4844 | BcStatus s; | ||
4843 | s = zbc_POSIX_does_not_allow("comparison operators outside if or loops"); | 4845 | s = zbc_POSIX_does_not_allow("comparison operators outside if or loops"); |
4844 | if (s) return s; | 4846 | if (s) return s; |
4845 | } else if ((flags & BC_PARSE_REL) && nrelops > 1) { | 4847 | } else if ((flags & BC_PARSE_REL) && nrelops > 1) { |
4848 | BcStatus s; | ||
4846 | s = zbc_POSIX_requires("exactly one comparison operator per condition"); | 4849 | s = zbc_POSIX_requires("exactly one comparison operator per condition"); |
4847 | if (s) return s; | 4850 | if (s) return s; |
4848 | } | 4851 | } |
4849 | 4852 | ||
4850 | if (flags & BC_PARSE_PRINT) { | 4853 | if (flags & BC_PARSE_PRINT) { |
4851 | if (paren_first || !assign) bc_parse_push(p, XC_INST_PRINT); | 4854 | if (paren_first || !assign) |
4855 | bc_parse_push(p, XC_INST_PRINT); | ||
4852 | bc_parse_push(p, XC_INST_POP); | 4856 | bc_parse_push(p, XC_INST_POP); |
4853 | } | 4857 | } |
4854 | 4858 | ||
4855 | dbg_lex_done("%s:%d done", __func__, __LINE__); | 4859 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
4856 | return s; | 4860 | return BC_STATUS_SUCCESS; |
4857 | } | 4861 | } |
4858 | 4862 | ||
4859 | #endif // ENABLE_BC | 4863 | #endif // ENABLE_BC |