bc: fold zbc_parse_else() into its only caller
authorDenys Vlasenko <vda.linux@googlemail.com>
Sun, 16 Dec 2018 18:21:57 +0000 (19:21 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sun, 16 Dec 2018 18:21:57 +0000 (19:21 +0100)
While at it, allow newline between "else" and its body

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
miscutils/bc.c
testsuite/bc.tests

index cca64eaf5b677932821ba2ca74a39b4c6da79c0b..9ce6ab66741361c0638a358c423879656f80c142 100644 (file)
@@ -4112,41 +4112,6 @@ static BC_STATUS zbc_parse_return(BcParse *p)
 # define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__), BC_STATUS_SUCCESS)
 #endif
 
-static BC_STATUS zbc_parse_else(BcParse *p)
-{
-       BcStatus s;
-       BcInstPtr ip;
-       BcInstPtr *ipp;
-       size_t *label;
-
-       dbg_lex_enter("%s:%d entered", __func__, __LINE__);
-
-       ip.idx = p->func->labels.len;
-       ip.func = ip.len = 0;
-
-       dbg_lex("%s:%d after if() body: BC_INST_JUMP to %d", __func__, __LINE__, ip.idx);
-       bc_parse_push(p, BC_INST_JUMP);
-       bc_parse_pushIndex(p, ip.idx);
-
-       ipp = bc_vec_top(&p->exits);
-       label = bc_vec_item(&p->func->labels, ipp->idx);
-       dbg_lex("%s:%d rewriting label: %d -> %d", __func__, __LINE__, *label, p->func->code.len);
-       *label = p->func->code.len;
-       bc_vec_pop(&p->exits);
-
-       bc_vec_push(&p->exits, &ip);
-       bc_vec_push(&p->func->labels, &ip.idx);
-
-       s = zbc_parse_stmt(p);
-       if (s) RETURN_STATUS(s);
-
-       dbg_lex_done("%s:%d done", __func__, __LINE__);
-       RETURN_STATUS(s);
-}
-#if ERRORS_ARE_FATAL
-# define zbc_parse_else(...) (zbc_parse_else(__VA_ARGS__), BC_STATUS_SUCCESS)
-#endif
-
 static BC_STATUS zbc_parse_if(BcParse *p)
 {
        BcStatus s;
@@ -4179,12 +4144,30 @@ static BC_STATUS zbc_parse_if(BcParse *p)
 
        s = zbc_parse_stmt(p);
        if (s) RETURN_STATUS(s);
+
        dbg_lex("%s:%d in if after stmt: p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
        if (p->l.t.t == BC_LEX_KEY_ELSE) {
-               s = zbc_lex_next(&p->l);
+               s = zbc_lex_next_and_skip_NLINE(&p->l);
+               if (s) RETURN_STATUS(s);
+
+               ip.idx = p->func->labels.len;
+               ip.func = ip.len = 0;
+
+               dbg_lex("%s:%d after if() body: BC_INST_JUMP to %d", __func__, __LINE__, ip.idx);
+               bc_parse_push(p, BC_INST_JUMP);
+               bc_parse_pushIndex(p, ip.idx);
+
+               ipp = bc_vec_top(&p->exits);
+               label = bc_vec_item(&p->func->labels, ipp->idx);
+               dbg_lex("%s:%d rewriting label: %d -> %d", __func__, __LINE__, *label, p->func->code.len);
+               *label = p->func->code.len;
+               bc_vec_pop(&p->exits);
+
+               bc_vec_push(&p->exits, &ip);
+               bc_vec_push(&p->func->labels, &ip.idx);
+
+               s = zbc_parse_stmt(p);
                if (s) RETURN_STATUS(s);
-               dbg_lex("%s:%d calling zbc_parse_else(), p->l.t.t:%d", __func__, __LINE__, p->l.t.t);
-               s = zbc_parse_else(p);
        }
 
        ipp = bc_vec_top(&p->exits);
index 95cc28dad147d6277efcbf5104d9ec6b423d8c01..36baeea89bb4c3647cab29ff958b852aa3105d1b 100755 (executable)
@@ -61,6 +61,11 @@ testing "bc if(cond)<NL>" \
        "9\n" \
        "" "if(0)\n3\n9"
 
+testing "bc if(cond) stmt else<NL>" \
+       "bc" \
+       "4\n9\n" \
+       "" "if(0)3 else\n4\n9"
+
 testing "bc while(cond)<NL>" \
        "bc" \
        "8\n7\n6\n5\n4\n3\n2\n1\n9\n" \