shell/math: tweka comments

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
master
Denys Vlasenko 2023-06-16 20:47:43 +02:00
parent e127985839
commit 550696d492
1 changed files with 8 additions and 16 deletions

View File

@ -46,7 +46,6 @@
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
/* This is my infix parser/evaluator. It is optimized for size, intended /* This is my infix parser/evaluator. It is optimized for size, intended
* as a replacement for yacc-based parsers. However, it may well be faster * as a replacement for yacc-based parsers. However, it may well be faster
* than a comparable parser written in yacc. The supported operators are * than a comparable parser written in yacc. The supported operators are
@ -61,7 +60,6 @@
* to the stack instead of adding them to a queue to end up with an * to the stack instead of adding them to a queue to end up with an
* expression). * expression).
*/ */
/* /*
* Aug 24, 2001 Manuel Novoa III * Aug 24, 2001 Manuel Novoa III
* *
@ -245,7 +243,6 @@ is_right_associative(operator prec)
|| prec == PREC(TOK_CONDITIONAL); || prec == PREC(TOK_CONDITIONAL);
} }
typedef struct { typedef struct {
arith_t val; arith_t val;
char *var_name; char *var_name;
@ -254,13 +251,11 @@ typedef struct {
#define VALID_NAME(name) (name) #define VALID_NAME(name) (name)
#define NOT_NAME(name) (!(name)) #define NOT_NAME(name) (!(name))
typedef struct remembered_name { typedef struct remembered_name {
struct remembered_name *next; struct remembered_name *next;
const char *var_name; const char *var_name;
} remembered_name; } remembered_name;
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);
@ -278,7 +273,7 @@ arith_lookup_val(arith_state_t *math_state, var_or_num_t *t)
*/ */
for (cur = math_state->list_of_recursed_names; cur; cur = cur->next) { for (cur = math_state->list_of_recursed_names; cur; cur = cur->next) {
if (strcmp(cur->var_name, t->var_name) == 0) { if (strcmp(cur->var_name, t->var_name) == 0) {
/* Yes */ /* yes */
return "expression recursion loop detected"; return "expression recursion loop detected";
} }
} }
@ -500,7 +495,6 @@ static const char op_tokens[] ALIGN1 = {
'+', 0, TOK_ADD, '+', 0, TOK_ADD,
'-', 0, TOK_SUB, '-', 0, TOK_SUB,
'^', 0, TOK_BXOR, '^', 0, TOK_BXOR,
/* uniq */
'~', 0, TOK_BNOT, '~', 0, TOK_BNOT,
',', 0, TOK_COMMA, ',', 0, TOK_COMMA,
'?', 0, TOK_CONDITIONAL, '?', 0, TOK_CONDITIONAL,
@ -869,14 +863,9 @@ evaluate_string(arith_state_t *math_state, const char *expr)
if (errmsg) if (errmsg)
goto err_with_custom_msg; goto err_with_custom_msg;
dbg(" numstack:%d val:%lld '%s'", (int)(numstackptr - numstack), numstackptr[-1].val, numstackptr[-1].var_name); dbg(" numstack:%d val:%lld '%s'", (int)(numstackptr - numstack), numstackptr[-1].val, numstackptr[-1].var_name);
/* For ternary ?: we need to remove ? from opstack too, not just : */
if (prev_op == TOK_CONDITIONAL_SEP) { if (prev_op == TOK_CONDITIONAL_SEP) {
// This is caught in arith_apply() /* We just executed ":" */
//if (opstackptr == opstack) { /* Remove "?" from opstack too, not just ":" */
// /* Example: $((2:3)) */
// errmsg = "where is your ? in ?:";
// goto err_with_custom_msg;
//}
opstackptr--; opstackptr--;
if (*opstackptr != TOK_CONDITIONAL) { if (*opstackptr != TOK_CONDITIONAL) {
/* Example: $((1,2:3)) */ /* Example: $((1,2:3)) */
@ -890,12 +879,14 @@ dbg(" numstack:%d val:%lld '%s'", (int)(numstackptr - numstack), numstackptr[
dbg("':' executed: evaluation_disabled=%llx (restored)", EVAL_DISABLED); dbg("':' executed: evaluation_disabled=%llx (restored)", EVAL_DISABLED);
} }
} /* while (opstack not empty) */ } /* while (opstack not empty) */
if (op == TOK_RPAREN) /* unpaired RPAREN? */ if (op == TOK_RPAREN) /* unpaired RPAREN? */
goto err; goto err;
check_cond: check_cond:
if (op == TOK_CONDITIONAL) { if (op == TOK_CONDITIONAL) {
/* We know the value of EXPR in "EXPR ? ..." /* We just now evaluated EXPR before "?".
* Should we stop evaluating now? */ * Should we disable evaluation now?
*/
if (math_state->evaluation_disabled & TOP_BIT_ULL) if (math_state->evaluation_disabled & TOP_BIT_ULL)
goto err; /* >63 levels of ?: nesting not supported */ goto err; /* >63 levels of ?: nesting not supported */
math_state->evaluation_disabled <<= 1; math_state->evaluation_disabled <<= 1;
@ -915,6 +906,7 @@ dbg(" numstack:%d val:%lld '%s'", (int)(numstackptr - numstack), numstackptr[
insert_op = 0xff; insert_op = 0xff;
dbg("inserting %02x", op); dbg("inserting %02x", op);
if (op == TOK_CONDITIONAL_SEP) { if (op == TOK_CONDITIONAL_SEP) {
/* The next token is ":". Toggle "do not evaluate" bit */
math_state->evaluation_disabled ^= 1; math_state->evaluation_disabled ^= 1;
dbg("':' entered: evaluation_disabled=%llx (negated)", EVAL_DISABLED); dbg("':' entered: evaluation_disabled=%llx (negated)", EVAL_DISABLED);
} }