bc: allow {break} and {continue} (allow RBRACE to terminate them)
authorDenys Vlasenko <vda.linux@googlemail.com>
Tue, 25 Dec 2018 16:07:51 +0000 (17:07 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Tue, 25 Dec 2018 16:07:51 +0000 (17:07 +0100)
function                                             old     new   delta
zbc_parse_stmt_possibly_auto                        1599    1560     -39

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

index ec2f8613351e99c87b21a72e452fee94cafc5bfc..7c8edcf330f7f292984272ec940ca67a55a8dd50 100644 (file)
@@ -3927,7 +3927,7 @@ static BC_STATUS zbc_parse_read(BcParse *p)
 
        bc_parse_push(p, XC_INST_READ);
 
-       RETURN_STATUS(zbc_lex_next(&p->l));
+       RETURN_STATUS(s);
 }
 #define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS)
 
@@ -3953,7 +3953,7 @@ static BC_STATUS zbc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags,
        *prev = (type == BC_LEX_KEY_LENGTH) ? XC_INST_LENGTH : XC_INST_SQRT;
        bc_parse_push(p, *prev);
 
-       RETURN_STATUS(zbc_lex_next(&p->l));
+       RETURN_STATUS(s);
 }
 #define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS)
 
@@ -4328,7 +4328,6 @@ static BC_STATUS zbc_parse_for(BcParse *p)
 
 static BC_STATUS zbc_parse_break_or_continue(BcParse *p, BcLexType type)
 {
-       BcStatus s;
        size_t i;
 
        if (type == BC_LEX_KEY_BREAK) {
@@ -4338,15 +4337,8 @@ static BC_STATUS zbc_parse_break_or_continue(BcParse *p, BcLexType type)
        } else {
                i = *(size_t*)bc_vec_top(&p->conds);
        }
-
        bc_parse_pushJUMP(p, i);
 
-       s = zbc_lex_next(&p->l);
-       if (s) RETURN_STATUS(s);
-
-       if (p->l.lex != BC_LEX_SCOLON && p->l.lex != XC_LEX_NLINE)
-               RETURN_STATUS(bc_error_bad_token());
-
        RETURN_STATUS(zbc_lex_next(&p->l));
 }
 #define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS)
@@ -4800,9 +4792,9 @@ static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags)
                                if (BC_PARSE_LEAF(prev, rprn))
                                        return bc_error_bad_expression();
                                s = zbc_parse_builtin(p, t, flags, &prev);
+                               get_token = true;
                                paren_expr = true;
                                rprn = bin_last = false;
-                               //get_token = false; - already is
                                nexprs++;
                                break;
                        case BC_LEX_KEY_READ:
@@ -4810,9 +4802,9 @@ static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags)
                                        return bc_error_bad_expression();
                                s = zbc_parse_read(p);
                                prev = XC_INST_READ;
+                               get_token = true;
                                paren_expr = true;
                                rprn = bin_last = false;
-                               //get_token = false; - already is
                                nexprs++;
                                break;
                        case BC_LEX_KEY_SCALE:
@@ -4820,9 +4812,9 @@ static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags)
                                        return bc_error_bad_expression();
                                s = zbc_parse_scale(p, &prev, flags);
                                prev = XC_INST_SCALE;
+                               //get_token = false; - already is
                                paren_expr = true;
                                rprn = bin_last = false;
-                               //get_token = false; - already is
                                nexprs++;
                                break;
                        default:
index c84a2987c26961a69ae882b34e0d6ec1baef08f7..42fe83013a471ad848244e7495dd3565f9cb919b 100755 (executable)
@@ -66,7 +66,7 @@ testing "bc if 0 else if 1" \
        "2\n9\n" \
        "" "if (0) 1 else if (1) 2; 9"
 
-testing "bc for(;;)" \
+testing "bc for (;;)" \
        "bc" \
        "2\n3\n2\n9\n" \
        "" "i=2; for (;;) { 2; if(--i==0) break; 3; }; 9"
@@ -86,6 +86,11 @@ testing "bc for (init;cond;upd)" \
        "1\n2\n3\n9\n" \
        "" "for(i=1;i<4;i++)i; 9"
 
+testing "bc for (;;) {break}" \
+       "bc" \
+       "2\n9\n" \
+       "" "for (;;) {2;break}; 9"
+
 testing "bc define auto" \
        "bc" \
        "8\n9\n" \