shell/math: code shrink
function old new delta evaluate_string 1489 1486 -3 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
parent
c72c5552ed
commit
2d06c83b87
1 changed files with 12 additions and 11 deletions
23
shell/math.c
23
shell/math.c
|
@ -264,10 +264,9 @@ static ALWAYS_INLINE int isalnum_(int c)
|
||||||
static arith_t
|
static arith_t
|
||||||
evaluate_string(arith_state_t *math_state, const char *expr);
|
evaluate_string(arith_state_t *math_state, const char *expr);
|
||||||
|
|
||||||
static const char*
|
static arith_t
|
||||||
arith_lookup_val(arith_state_t *math_state, var_or_num_t *t)
|
arith_lookup_val(arith_state_t *math_state, const char *name)
|
||||||
{
|
{
|
||||||
const char *name = t->var_name;
|
|
||||||
char c;
|
char c;
|
||||||
const char *p;
|
const char *p;
|
||||||
char *e = (char*)endofname(name);
|
char *e = (char*)endofname(name);
|
||||||
|
@ -277,6 +276,7 @@ arith_lookup_val(arith_state_t *math_state, var_or_num_t *t)
|
||||||
p = math_state->lookupvar(name);
|
p = math_state->lookupvar(name);
|
||||||
*e = c;
|
*e = c;
|
||||||
if (p) {
|
if (p) {
|
||||||
|
arith_t val;
|
||||||
size_t len = e - name;
|
size_t len = e - name;
|
||||||
remembered_name *cur;
|
remembered_name *cur;
|
||||||
remembered_name remember;
|
remembered_name remember;
|
||||||
|
@ -289,7 +289,8 @@ arith_lookup_val(arith_state_t *math_state, var_or_num_t *t)
|
||||||
&& !isalnum_(cur->var_name[len])
|
&& !isalnum_(cur->var_name[len])
|
||||||
) {
|
) {
|
||||||
/* yes */
|
/* yes */
|
||||||
return "expression recursion loop detected";
|
math_state->errmsg = "expression recursion loop detected";
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -299,16 +300,16 @@ arith_lookup_val(arith_state_t *math_state, var_or_num_t *t)
|
||||||
math_state->list_of_recursed_names = &remember;
|
math_state->list_of_recursed_names = &remember;
|
||||||
|
|
||||||
/* recursively evaluate p as expression */
|
/* recursively evaluate p as expression */
|
||||||
t->val = evaluate_string(math_state, p);
|
/* this sets math_state->errmsg on error */
|
||||||
|
val = evaluate_string(math_state, p);
|
||||||
|
|
||||||
/* pop current var name */
|
/* pop current var name */
|
||||||
math_state->list_of_recursed_names = remember.next;
|
math_state->list_of_recursed_names = remember.next;
|
||||||
|
|
||||||
return math_state->errmsg;
|
return val;
|
||||||
}
|
}
|
||||||
/* treat undefined var as 0 */
|
/* treat undefined var as 0 */
|
||||||
t->val = 0;
|
return 0;
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* "Applying" a token means performing it on the top elements on the integer
|
/* "Applying" a token means performing it on the top elements on the integer
|
||||||
|
@ -684,9 +685,9 @@ evaluate_string(arith_state_t *math_state, const char *expr)
|
||||||
|| expr[1] == '=' /* or "==..." */
|
|| expr[1] == '=' /* or "==..." */
|
||||||
) {
|
) {
|
||||||
/* Evaluate variable to value */
|
/* Evaluate variable to value */
|
||||||
errmsg = arith_lookup_val(math_state, numstackptr);
|
numstackptr->val = arith_lookup_val(math_state, numstackptr->var_name);
|
||||||
if (errmsg)
|
if (math_state->errmsg)
|
||||||
goto err_with_custom_msg;
|
return numstackptr->val; /* -1 */
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
dbg("[%d] var:IGNORED", (int)(numstackptr - numstack));
|
dbg("[%d] var:IGNORED", (int)(numstackptr - numstack));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue