dc: fit returning of string
authorDenys Vlasenko <vda.linux@googlemail.com>
Fri, 4 Jan 2019 05:18:00 +0000 (06:18 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Fri, 4 Jan 2019 05:18:00 +0000 (06:18 +0100)
function                                             old     new   delta
zxc_program_exec                                    4087    4098     +11

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

index 1e8056c019e5a4dc393a5a6e3db405a3b863fcef..e497b0d0268f5e6f419131818b64d78b948c133e 100644 (file)
@@ -6020,6 +6020,9 @@ static BC_STATUS zbc_program_return(char inst)
        BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
 
        if (inst == XC_INST_RET) {
+               // bc needs this for e.g. RESULT_CONSTANT ("return 5")
+               // because bc constants are per-function.
+               // TODO: maybe avoid if value is already RESULT_TEMP?
                BcStatus s;
                BcNum *num;
                BcResult *operand = bc_vec_top(&G.prog.results);
@@ -6458,7 +6461,17 @@ static BC_STATUS zxc_program_exec(void)
 
                dbg_exec("inst at %zd:%d results.len:%d", ip->inst_idx - 1, inst, G.prog.results.len);
                switch (inst) {
+               case XC_INST_RET:
+                       if (IS_DC) { // end of '?' reached
+                               bc_vec_pop(&G.prog.exestack);
+                               goto read_updated_ip;
+                       }
+                       // bc: fall through
 #if ENABLE_BC
+               case BC_INST_RET0:
+                       dbg_exec("BC_INST_RET[0]:");
+                       s = zbc_program_return(inst);
+                       goto read_updated_ip;
                case BC_INST_JUMP_ZERO: {
                        BcNum *num;
                        bool zero;
@@ -6495,11 +6508,6 @@ static BC_STATUS zxc_program_exec(void)
                        dbg_exec("BC_INST_HALT:");
                        QUIT_OR_RETURN_TO_MAIN;
                        break;
-               case XC_INST_RET:
-               case BC_INST_RET0:
-                       dbg_exec("BC_INST_RET[0]:");
-                       s = zbc_program_return(inst);
-                       goto read_updated_ip;
                case XC_INST_BOOL_OR:
                case XC_INST_BOOL_AND:
 #endif // ENABLE_BC
index 87b1e71c3084cded0bc6bd022679578d4c72360a..1708a483503e891b05e5e18fec9f0a3056420f50 100755 (executable)
@@ -46,6 +46,11 @@ testing "dc read" \
        "2\n9\n1\n" \
        "1?2\nf" "9\n"
 
+testing "dc read string" \
+       "dc -finput" \
+       "2\nstr\n1\n" \
+       "1?2\nf" "[str]\n"
+
 optional FEATURE_DC_BIG
 # All tests below depend on FEATURE_DC_BIG