diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2023-06-16 20:47:43 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2023-06-16 20:47:43 +0200 |
commit | 550696d492c7389927d3f335bed11aa4decbcae6 (patch) | |
tree | acb452585977dc68b72c5fc92760864e5532e115 | |
parent | e1279858394a6079be6816cbedaa3f10e74057cc (diff) | |
download | busybox-w32-550696d492c7389927d3f335bed11aa4decbcae6.tar.gz busybox-w32-550696d492c7389927d3f335bed11aa4decbcae6.tar.bz2 busybox-w32-550696d492c7389927d3f335bed11aa4decbcae6.zip |
shell/math: tweka comments
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | shell/math.c | 24 |
1 files changed, 8 insertions, 16 deletions
diff --git a/shell/math.c b/shell/math.c index b1aabef9d..2959e57ea 100644 --- a/shell/math.c +++ b/shell/math.c | |||
@@ -46,7 +46,6 @@ | |||
46 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | 46 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
47 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | 47 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
48 | */ | 48 | */ |
49 | |||
50 | /* This is my infix parser/evaluator. It is optimized for size, intended | 49 | /* This is my infix parser/evaluator. It is optimized for size, intended |
51 | * as a replacement for yacc-based parsers. However, it may well be faster | 50 | * as a replacement for yacc-based parsers. However, it may well be faster |
52 | * than a comparable parser written in yacc. The supported operators are | 51 | * than a comparable parser written in yacc. The supported operators are |
@@ -61,7 +60,6 @@ | |||
61 | * to the stack instead of adding them to a queue to end up with an | 60 | * to the stack instead of adding them to a queue to end up with an |
62 | * expression). | 61 | * expression). |
63 | */ | 62 | */ |
64 | |||
65 | /* | 63 | /* |
66 | * Aug 24, 2001 Manuel Novoa III | 64 | * Aug 24, 2001 Manuel Novoa III |
67 | * | 65 | * |
@@ -245,7 +243,6 @@ is_right_associative(operator prec) | |||
245 | || prec == PREC(TOK_CONDITIONAL); | 243 | || prec == PREC(TOK_CONDITIONAL); |
246 | } | 244 | } |
247 | 245 | ||
248 | |||
249 | typedef struct { | 246 | typedef struct { |
250 | arith_t val; | 247 | arith_t val; |
251 | char *var_name; | 248 | char *var_name; |
@@ -254,13 +251,11 @@ typedef struct { | |||
254 | #define VALID_NAME(name) (name) | 251 | #define VALID_NAME(name) (name) |
255 | #define NOT_NAME(name) (!(name)) | 252 | #define NOT_NAME(name) (!(name)) |
256 | 253 | ||
257 | |||
258 | typedef struct remembered_name { | 254 | typedef struct remembered_name { |
259 | struct remembered_name *next; | 255 | struct remembered_name *next; |
260 | const char *var_name; | 256 | const char *var_name; |
261 | } remembered_name; | 257 | } remembered_name; |
262 | 258 | ||
263 | |||
264 | static arith_t | 259 | static arith_t |
265 | evaluate_string(arith_state_t *math_state, const char *expr); | 260 | evaluate_string(arith_state_t *math_state, const char *expr); |
266 | 261 | ||
@@ -278,7 +273,7 @@ arith_lookup_val(arith_state_t *math_state, var_or_num_t *t) | |||
278 | */ | 273 | */ |
279 | for (cur = math_state->list_of_recursed_names; cur; cur = cur->next) { | 274 | for (cur = math_state->list_of_recursed_names; cur; cur = cur->next) { |
280 | if (strcmp(cur->var_name, t->var_name) == 0) { | 275 | if (strcmp(cur->var_name, t->var_name) == 0) { |
281 | /* Yes */ | 276 | /* yes */ |
282 | return "expression recursion loop detected"; | 277 | return "expression recursion loop detected"; |
283 | } | 278 | } |
284 | } | 279 | } |
@@ -500,7 +495,6 @@ static const char op_tokens[] ALIGN1 = { | |||
500 | '+', 0, TOK_ADD, | 495 | '+', 0, TOK_ADD, |
501 | '-', 0, TOK_SUB, | 496 | '-', 0, TOK_SUB, |
502 | '^', 0, TOK_BXOR, | 497 | '^', 0, TOK_BXOR, |
503 | /* uniq */ | ||
504 | '~', 0, TOK_BNOT, | 498 | '~', 0, TOK_BNOT, |
505 | ',', 0, TOK_COMMA, | 499 | ',', 0, TOK_COMMA, |
506 | '?', 0, TOK_CONDITIONAL, | 500 | '?', 0, TOK_CONDITIONAL, |
@@ -869,14 +863,9 @@ evaluate_string(arith_state_t *math_state, const char *expr) | |||
869 | if (errmsg) | 863 | if (errmsg) |
870 | goto err_with_custom_msg; | 864 | goto err_with_custom_msg; |
871 | dbg(" numstack:%d val:%lld '%s'", (int)(numstackptr - numstack), numstackptr[-1].val, numstackptr[-1].var_name); | 865 | dbg(" numstack:%d val:%lld '%s'", (int)(numstackptr - numstack), numstackptr[-1].val, numstackptr[-1].var_name); |
872 | /* For ternary ?: we need to remove ? from opstack too, not just : */ | ||
873 | if (prev_op == TOK_CONDITIONAL_SEP) { | 866 | if (prev_op == TOK_CONDITIONAL_SEP) { |
874 | // This is caught in arith_apply() | 867 | /* We just executed ":" */ |
875 | //if (opstackptr == opstack) { | 868 | /* Remove "?" from opstack too, not just ":" */ |
876 | // /* Example: $((2:3)) */ | ||
877 | // errmsg = "where is your ? in ?:"; | ||
878 | // goto err_with_custom_msg; | ||
879 | //} | ||
880 | opstackptr--; | 869 | opstackptr--; |
881 | if (*opstackptr != TOK_CONDITIONAL) { | 870 | if (*opstackptr != TOK_CONDITIONAL) { |
882 | /* Example: $((1,2:3)) */ | 871 | /* Example: $((1,2:3)) */ |
@@ -890,12 +879,14 @@ dbg(" numstack:%d val:%lld '%s'", (int)(numstackptr - numstack), numstackptr[ | |||
890 | dbg("':' executed: evaluation_disabled=%llx (restored)", EVAL_DISABLED); | 879 | dbg("':' executed: evaluation_disabled=%llx (restored)", EVAL_DISABLED); |
891 | } | 880 | } |
892 | } /* while (opstack not empty) */ | 881 | } /* while (opstack not empty) */ |
882 | |||
893 | if (op == TOK_RPAREN) /* unpaired RPAREN? */ | 883 | if (op == TOK_RPAREN) /* unpaired RPAREN? */ |
894 | goto err; | 884 | goto err; |
895 | check_cond: | 885 | check_cond: |
896 | if (op == TOK_CONDITIONAL) { | 886 | if (op == TOK_CONDITIONAL) { |
897 | /* We know the value of EXPR in "EXPR ? ..." | 887 | /* We just now evaluated EXPR before "?". |
898 | * Should we stop evaluating now? */ | 888 | * Should we disable evaluation now? |
889 | */ | ||
899 | if (math_state->evaluation_disabled & TOP_BIT_ULL) | 890 | if (math_state->evaluation_disabled & TOP_BIT_ULL) |
900 | goto err; /* >63 levels of ?: nesting not supported */ | 891 | goto err; /* >63 levels of ?: nesting not supported */ |
901 | math_state->evaluation_disabled <<= 1; | 892 | math_state->evaluation_disabled <<= 1; |
@@ -915,6 +906,7 @@ dbg(" numstack:%d val:%lld '%s'", (int)(numstackptr - numstack), numstackptr[ | |||
915 | insert_op = 0xff; | 906 | insert_op = 0xff; |
916 | dbg("inserting %02x", op); | 907 | dbg("inserting %02x", op); |
917 | if (op == TOK_CONDITIONAL_SEP) { | 908 | if (op == TOK_CONDITIONAL_SEP) { |
909 | /* The next token is ":". Toggle "do not evaluate" bit */ | ||
918 | math_state->evaluation_disabled ^= 1; | 910 | math_state->evaluation_disabled ^= 1; |
919 | dbg("':' entered: evaluation_disabled=%llx (negated)", EVAL_DISABLED); | 911 | dbg("':' entered: evaluation_disabled=%llx (negated)", EVAL_DISABLED); |
920 | } | 912 | } |