aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--miscutils/bc.c36
1 files changed, 20 insertions, 16 deletions
diff --git a/miscutils/bc.c b/miscutils/bc.c
index 68e3668c4..4fac2c99a 100644
--- a/miscutils/bc.c
+++ b/miscutils/bc.c
@@ -5178,6 +5178,7 @@ static void bc_program_binOpRetire(BcResult *r)
5178 bc_result_pop_and_push(r); 5178 bc_result_pop_and_push(r);
5179} 5179}
5180 5180
5181// Note: *r and *n need not be initialized by caller
5181static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n) 5182static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n)
5182{ 5183{
5183 BcStatus s; 5184 BcStatus s;
@@ -5206,7 +5207,7 @@ static BC_STATUS zbc_program_op(char inst)
5206{ 5207{
5207 BcStatus s; 5208 BcStatus s;
5208 BcResult *opd1, *opd2, res; 5209 BcResult *opd1, *opd2, res;
5209 BcNum *n1, *n2 = NULL; 5210 BcNum *n1, *n2;
5210 5211
5211 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); 5212 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
5212 if (s) RETURN_STATUS(s); 5213 if (s) RETURN_STATUS(s);
@@ -5565,7 +5566,7 @@ static BC_STATUS zbc_program_print(char inst, size_t idx)
5565 BcStatus s; 5566 BcStatus s;
5566 BcResult *r; 5567 BcResult *r;
5567 BcNum *num; 5568 BcNum *num;
5568 bool pop = inst != XC_INST_PRINT; 5569 bool pop = (inst != XC_INST_PRINT);
5569 5570
5570 if (!STACK_HAS_MORE_THAN(&G.prog.results, idx)) 5571 if (!STACK_HAS_MORE_THAN(&G.prog.results, idx))
5571 RETURN_STATUS(bc_error_stack_has_too_few_elements()); 5572 RETURN_STATUS(bc_error_stack_has_too_few_elements());
@@ -5609,7 +5610,7 @@ static BC_STATUS zbc_program_negate(void)
5609{ 5610{
5610 BcStatus s; 5611 BcStatus s;
5611 BcResult res, *ptr; 5612 BcResult res, *ptr;
5612 BcNum *num = NULL; 5613 BcNum *num;
5613 5614
5614 s = zbc_program_prep(&ptr, &num); 5615 s = zbc_program_prep(&ptr, &num);
5615 if (s) RETURN_STATUS(s); 5616 if (s) RETURN_STATUS(s);
@@ -5745,8 +5746,9 @@ static BC_STATUS zbc_program_assign(char inst)
5745{ 5746{
5746 BcStatus s; 5747 BcStatus s;
5747 BcResult *left, *right, res; 5748 BcResult *left, *right, res;
5748 BcNum *l = NULL, *r = NULL; 5749 BcNum *l, *r;
5749 bool assign = inst == XC_INST_ASSIGN, ib, sc; 5750 bool assign = (inst == XC_INST_ASSIGN);
5751 bool ib, sc;
5750 5752
5751 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign); 5753 s = zbc_program_binOpPrep(&left, &l, &right, &r, assign);
5752 if (s) RETURN_STATUS(s); 5754 if (s) RETURN_STATUS(s);
@@ -5907,7 +5909,7 @@ static BC_STATUS zbc_program_incdec(char inst)
5907{ 5909{
5908 BcStatus s; 5910 BcStatus s;
5909 BcResult *ptr, res, copy; 5911 BcResult *ptr, res, copy;
5910 BcNum *num = NULL; 5912 BcNum *num;
5911 char inst2 = inst; 5913 char inst2 = inst;
5912 5914
5913 s = zbc_program_prep(&ptr, &num); 5915 s = zbc_program_prep(&ptr, &num);
@@ -5920,9 +5922,9 @@ static BC_STATUS zbc_program_incdec(char inst)
5920 } 5922 }
5921 5923
5922 res.t = BC_RESULT_ONE; 5924 res.t = BC_RESULT_ONE;
5923 inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ? 5925 inst = (inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST)
5924 BC_INST_ASSIGN_PLUS : 5926 ? BC_INST_ASSIGN_PLUS
5925 BC_INST_ASSIGN_MINUS; 5927 : BC_INST_ASSIGN_MINUS;
5926 5928
5927 bc_vec_push(&G.prog.results, &res); 5929 bc_vec_push(&G.prog.results, &res);
5928 s = zbc_program_assign(inst); 5930 s = zbc_program_assign(inst);
@@ -6059,9 +6061,9 @@ static BC_STATUS zbc_program_builtin(char inst)
6059{ 6061{
6060 BcStatus s; 6062 BcStatus s;
6061 BcResult *opnd; 6063 BcResult *opnd;
6062 BcNum *num = NULL; 6064 BcNum *num;
6063 BcResult res; 6065 BcResult res;
6064 bool len = inst == XC_INST_LENGTH; 6066 bool len = (inst == XC_INST_LENGTH);
6065 6067
6066 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) 6068 if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
6067 RETURN_STATUS(bc_error_stack_has_too_few_elements()); 6069 RETURN_STATUS(bc_error_stack_has_too_few_elements());
@@ -6108,7 +6110,7 @@ static BC_STATUS zdc_program_divmod(void)
6108{ 6110{
6109 BcStatus s; 6111 BcStatus s;
6110 BcResult *opd1, *opd2, res, res2; 6112 BcResult *opd1, *opd2, res, res2;
6111 BcNum *n1, *n2 = NULL; 6113 BcNum *n1, *n2;
6112 6114
6113 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); 6115 s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
6114 if (s) RETURN_STATUS(s); 6116 if (s) RETURN_STATUS(s);
@@ -6284,7 +6286,7 @@ static BC_STATUS zdc_program_nquit(void)
6284{ 6286{
6285 BcStatus s; 6287 BcStatus s;
6286 BcResult *opnd; 6288 BcResult *opnd;
6287 BcNum *num = NULL; 6289 BcNum *num;
6288 unsigned long val; 6290 unsigned long val;
6289 6291
6290 s = zbc_program_prep(&opnd, &num); 6292 s = zbc_program_prep(&opnd, &num);
@@ -6432,7 +6434,6 @@ static void bc_program_pushGlobal(char inst)
6432static BC_STATUS zbc_program_exec(void) 6434static BC_STATUS zbc_program_exec(void)
6433{ 6435{
6434 BcResult r, *ptr; 6436 BcResult r, *ptr;
6435 BcNum *num;
6436 BcInstPtr *ip = bc_vec_top(&G.prog.exestack); 6437 BcInstPtr *ip = bc_vec_top(&G.prog.exestack);
6437 BcFunc *func = bc_program_func(ip->func); 6438 BcFunc *func = bc_program_func(ip->func);
6438 char *code = func->code.v; 6439 char *code = func->code.v;
@@ -6447,6 +6448,7 @@ static BC_STATUS zbc_program_exec(void)
6447 switch (inst) { 6448 switch (inst) {
6448#if ENABLE_BC 6449#if ENABLE_BC
6449 case BC_INST_JUMP_ZERO: { 6450 case BC_INST_JUMP_ZERO: {
6451 BcNum *num;
6450 bool zero; 6452 bool zero;
6451 dbg_exec("BC_INST_JUMP_ZERO:"); 6453 dbg_exec("BC_INST_JUMP_ZERO:");
6452 s = zbc_program_prep(&ptr, &num); 6454 s = zbc_program_prep(&ptr, &num);
@@ -6564,16 +6566,18 @@ static BC_STATUS zbc_program_exec(void)
6564 dbg_exec("BC_INST_binaryop:"); 6566 dbg_exec("BC_INST_binaryop:");
6565 s = zbc_program_op(inst); 6567 s = zbc_program_op(inst);
6566 break; 6568 break;
6567 case XC_INST_BOOL_NOT: 6569 case XC_INST_BOOL_NOT: {
6570 BcNum *num;
6568 dbg_exec("XC_INST_BOOL_NOT:"); 6571 dbg_exec("XC_INST_BOOL_NOT:");
6569 s = zbc_program_prep(&ptr, &num); 6572 s = zbc_program_prep(&ptr, &num);
6570 if (s) RETURN_STATUS(s); 6573 if (s) RETURN_STATUS(s);
6571 bc_num_init_DEF_SIZE(&r.d.n); 6574 bc_num_init_DEF_SIZE(&r.d.n);
6572 if (!bc_num_cmp(num, &G.prog.zero)) 6575 if (bc_num_cmp(num, &G.prog.zero) == 0)
6573 bc_num_one(&r.d.n); 6576 bc_num_one(&r.d.n);
6574 //else bc_num_zero(&r.d.n); - already is 6577 //else bc_num_zero(&r.d.n); - already is
6575 bc_program_retire(&r, BC_RESULT_TEMP); 6578 bc_program_retire(&r, BC_RESULT_TEMP);
6576 break; 6579 break;
6580 }
6577 case XC_INST_NEG: 6581 case XC_INST_NEG:
6578 dbg_exec("XC_INST_NEG:"); 6582 dbg_exec("XC_INST_NEG:");
6579 s = zbc_program_negate(); 6583 s = zbc_program_negate();