aboutsummaryrefslogtreecommitdiff
path: root/shell/math.c
diff options
context:
space:
mode:
Diffstat (limited to 'shell/math.c')
-rw-r--r--shell/math.c27
1 files changed, 15 insertions, 12 deletions
diff --git a/shell/math.c b/shell/math.c
index fa22bc400..077aba848 100644
--- a/shell/math.c
+++ b/shell/math.c
@@ -340,7 +340,20 @@ arith_apply(arith_state_t *math_state, operator op, var_or_num_t *numstack, var_
340 if (op == TOK_CONDITIONAL) /* Example: $((a ? b)) */ 340 if (op == TOK_CONDITIONAL) /* Example: $((a ? b)) */
341 return "malformed ?: operator"; 341 return "malformed ?: operator";
342 342
343 /* Resolve name to value, if needed */ 343 if (PREC(op) < UNARYPREC) {
344 /* In binops a ~ b, variables are resolved left-to-right,
345 * resolve top_of_stack[-1] _before_ resolving top_of_stack[0]
346 */
347 if (top_of_stack == numstack) /* need two arguments */
348 goto syntax_err;
349 /* Unless it is =, resolve top_of_stack[-1] name to value */
350 if (op != TOK_ASSIGN) {
351 err = arith_lookup_val(math_state, top_of_stack - 1);
352 if (err)
353 return err;
354 }
355 }
356 /* Resolve top_of_stack[0] name to value */
344 err = arith_lookup_val(math_state, top_of_stack); 357 err = arith_lookup_val(math_state, top_of_stack);
345 if (err) 358 if (err)
346 return err; 359 return err;
@@ -360,20 +373,10 @@ arith_apply(arith_state_t *math_state, operator op, var_or_num_t *numstack, var_
360 /* Binary operators */ 373 /* Binary operators */
361 arith_t right_side_val; 374 arith_t right_side_val;
362 375
363 /* Binary operators need two arguments */ 376 /* Pop numstack */
364 if (top_of_stack == numstack)
365 goto syntax_err;
366 /* ...and they pop one */
367 NUMPTR = top_of_stack; /* this decrements NUMPTR */ 377 NUMPTR = top_of_stack; /* this decrements NUMPTR */
368 top_of_stack--; /* now points to left side */ 378 top_of_stack--; /* now points to left side */
369 379
370 if (op != TOK_ASSIGN) {
371 /* Resolve left side value (unless the op is '=') */
372 err = arith_lookup_val(math_state, top_of_stack);
373 if (err)
374 return err;
375 }
376
377 right_side_val = rez; 380 right_side_val = rez;
378 rez = top_of_stack->val; 381 rez = top_of_stack->val;
379 if (op == TOK_BOR || op == TOK_OR_ASSIGN) 382 if (op == TOK_BOR || op == TOK_OR_ASSIGN)