diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2018-12-16 19:21:57 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-12-16 19:21:57 +0100 |
commit | a50576a415a9b5d384a28c9bd4b55a4df2974248 (patch) | |
tree | 12db52aea474921a6fb5bc935405309b25e73c88 | |
parent | 6d29879c676a636656c7b29ab5be76518346b19c (diff) | |
download | busybox-w32-a50576a415a9b5d384a28c9bd4b55a4df2974248.tar.gz busybox-w32-a50576a415a9b5d384a28c9bd4b55a4df2974248.tar.bz2 busybox-w32-a50576a415a9b5d384a28c9bd4b55a4df2974248.zip |
bc: fold zbc_parse_else() into its only caller
While at it, allow newline between "else" and its body
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | miscutils/bc.c | 59 | ||||
-rwxr-xr-x | testsuite/bc.tests | 5 |
2 files changed, 26 insertions, 38 deletions
diff --git a/miscutils/bc.c b/miscutils/bc.c index cca64eaf5..9ce6ab667 100644 --- a/miscutils/bc.c +++ b/miscutils/bc.c | |||
@@ -4112,41 +4112,6 @@ static BC_STATUS zbc_parse_return(BcParse *p) | |||
4112 | # define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__), BC_STATUS_SUCCESS) | 4112 | # define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__), BC_STATUS_SUCCESS) |
4113 | #endif | 4113 | #endif |
4114 | 4114 | ||
4115 | static BC_STATUS zbc_parse_else(BcParse *p) | ||
4116 | { | ||
4117 | BcStatus s; | ||
4118 | BcInstPtr ip; | ||
4119 | BcInstPtr *ipp; | ||
4120 | size_t *label; | ||
4121 | |||
4122 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); | ||
4123 | |||
4124 | ip.idx = p->func->labels.len; | ||
4125 | ip.func = ip.len = 0; | ||
4126 | |||
4127 | dbg_lex("%s:%d after if() body: BC_INST_JUMP to %d", __func__, __LINE__, ip.idx); | ||
4128 | bc_parse_push(p, BC_INST_JUMP); | ||
4129 | bc_parse_pushIndex(p, ip.idx); | ||
4130 | |||
4131 | ipp = bc_vec_top(&p->exits); | ||
4132 | label = bc_vec_item(&p->func->labels, ipp->idx); | ||
4133 | dbg_lex("%s:%d rewriting label: %d -> %d", __func__, __LINE__, *label, p->func->code.len); | ||
4134 | *label = p->func->code.len; | ||
4135 | bc_vec_pop(&p->exits); | ||
4136 | |||
4137 | bc_vec_push(&p->exits, &ip); | ||
4138 | bc_vec_push(&p->func->labels, &ip.idx); | ||
4139 | |||
4140 | s = zbc_parse_stmt(p); | ||
4141 | if (s) RETURN_STATUS(s); | ||
4142 | |||
4143 | dbg_lex_done("%s:%d done", __func__, __LINE__); | ||
4144 | RETURN_STATUS(s); | ||
4145 | } | ||
4146 | #if ERRORS_ARE_FATAL | ||
4147 | # define zbc_parse_else(...) (zbc_parse_else(__VA_ARGS__), BC_STATUS_SUCCESS) | ||
4148 | #endif | ||
4149 | |||
4150 | static BC_STATUS zbc_parse_if(BcParse *p) | 4115 | static BC_STATUS zbc_parse_if(BcParse *p) |
4151 | { | 4116 | { |
4152 | BcStatus s; | 4117 | BcStatus s; |
@@ -4179,12 +4144,30 @@ static BC_STATUS zbc_parse_if(BcParse *p) | |||
4179 | 4144 | ||
4180 | s = zbc_parse_stmt(p); | 4145 | s = zbc_parse_stmt(p); |
4181 | if (s) RETURN_STATUS(s); | 4146 | if (s) RETURN_STATUS(s); |
4147 | |||
4182 | dbg_lex("%s:%d in if after stmt: p->l.t.t:%d", __func__, __LINE__, p->l.t.t); | 4148 | dbg_lex("%s:%d in if after stmt: p->l.t.t:%d", __func__, __LINE__, p->l.t.t); |
4183 | if (p->l.t.t == BC_LEX_KEY_ELSE) { | 4149 | if (p->l.t.t == BC_LEX_KEY_ELSE) { |
4184 | s = zbc_lex_next(&p->l); | 4150 | s = zbc_lex_next_and_skip_NLINE(&p->l); |
4151 | if (s) RETURN_STATUS(s); | ||
4152 | |||
4153 | ip.idx = p->func->labels.len; | ||
4154 | ip.func = ip.len = 0; | ||
4155 | |||
4156 | dbg_lex("%s:%d after if() body: BC_INST_JUMP to %d", __func__, __LINE__, ip.idx); | ||
4157 | bc_parse_push(p, BC_INST_JUMP); | ||
4158 | bc_parse_pushIndex(p, ip.idx); | ||
4159 | |||
4160 | ipp = bc_vec_top(&p->exits); | ||
4161 | label = bc_vec_item(&p->func->labels, ipp->idx); | ||
4162 | dbg_lex("%s:%d rewriting label: %d -> %d", __func__, __LINE__, *label, p->func->code.len); | ||
4163 | *label = p->func->code.len; | ||
4164 | bc_vec_pop(&p->exits); | ||
4165 | |||
4166 | bc_vec_push(&p->exits, &ip); | ||
4167 | bc_vec_push(&p->func->labels, &ip.idx); | ||
4168 | |||
4169 | s = zbc_parse_stmt(p); | ||
4185 | if (s) RETURN_STATUS(s); | 4170 | if (s) RETURN_STATUS(s); |
4186 | dbg_lex("%s:%d calling zbc_parse_else(), p->l.t.t:%d", __func__, __LINE__, p->l.t.t); | ||
4187 | s = zbc_parse_else(p); | ||
4188 | } | 4171 | } |
4189 | 4172 | ||
4190 | ipp = bc_vec_top(&p->exits); | 4173 | ipp = bc_vec_top(&p->exits); |
diff --git a/testsuite/bc.tests b/testsuite/bc.tests index 95cc28dad..36baeea89 100755 --- a/testsuite/bc.tests +++ b/testsuite/bc.tests | |||
@@ -61,6 +61,11 @@ testing "bc if(cond)<NL>" \ | |||
61 | "9\n" \ | 61 | "9\n" \ |
62 | "" "if(0)\n3\n9" | 62 | "" "if(0)\n3\n9" |
63 | 63 | ||
64 | testing "bc if(cond) stmt else<NL>" \ | ||
65 | "bc" \ | ||
66 | "4\n9\n" \ | ||
67 | "" "if(0)3 else\n4\n9" | ||
68 | |||
64 | testing "bc while(cond)<NL>" \ | 69 | testing "bc while(cond)<NL>" \ |
65 | "bc" \ | 70 | "bc" \ |
66 | "8\n7\n6\n5\n4\n3\n2\n1\n9\n" \ | 71 | "8\n7\n6\n5\n4\n3\n2\n1\n9\n" \ |