diff options
Diffstat (limited to 'miscutils/bc.c')
| -rw-r--r-- | miscutils/bc.c | 236 |
1 files changed, 57 insertions, 179 deletions
diff --git a/miscutils/bc.c b/miscutils/bc.c index 2da7c317c..7ecddf014 100644 --- a/miscutils/bc.c +++ b/miscutils/bc.c | |||
| @@ -236,7 +236,6 @@ typedef struct BcNum { | |||
| 236 | #define BC_NUM_KARATSUBA_LEN (32) | 236 | #define BC_NUM_KARATSUBA_LEN (32) |
| 237 | 237 | ||
| 238 | typedef enum BcInst { | 238 | typedef enum BcInst { |
| 239 | |||
| 240 | #if ENABLE_BC | 239 | #if ENABLE_BC |
| 241 | BC_INST_INC_PRE, | 240 | BC_INST_INC_PRE, |
| 242 | BC_INST_DEC_PRE, | 241 | BC_INST_DEC_PRE, |
| @@ -333,7 +332,6 @@ typedef enum BcInst { | |||
| 333 | 332 | ||
| 334 | BC_INST_INVALID = -1, | 333 | BC_INST_INVALID = -1, |
| 335 | #endif | 334 | #endif |
| 336 | |||
| 337 | } BcInst; | 335 | } BcInst; |
| 338 | 336 | ||
| 339 | typedef struct BcId { | 337 | typedef struct BcId { |
| @@ -349,7 +347,6 @@ typedef struct BcFunc { | |||
| 349 | } BcFunc; | 347 | } BcFunc; |
| 350 | 348 | ||
| 351 | typedef enum BcResultType { | 349 | typedef enum BcResultType { |
| 352 | |||
| 353 | BC_RESULT_TEMP, | 350 | BC_RESULT_TEMP, |
| 354 | 351 | ||
| 355 | BC_RESULT_VAR, | 352 | BC_RESULT_VAR, |
| @@ -367,7 +364,6 @@ typedef enum BcResultType { | |||
| 367 | BC_RESULT_ONE, | 364 | BC_RESULT_ONE, |
| 368 | 365 | ||
| 369 | BC_RESULT_OBASE, | 366 | BC_RESULT_OBASE, |
| 370 | |||
| 371 | } BcResultType; | 367 | } BcResultType; |
| 372 | 368 | ||
| 373 | typedef union BcResultData { | 369 | typedef union BcResultData { |
| @@ -675,7 +671,6 @@ typedef struct BcParse { | |||
| 675 | } BcParse; | 671 | } BcParse; |
| 676 | 672 | ||
| 677 | typedef struct BcProgram { | 673 | typedef struct BcProgram { |
| 678 | |||
| 679 | size_t len; | 674 | size_t len; |
| 680 | size_t scale; | 675 | size_t scale; |
| 681 | 676 | ||
| @@ -709,7 +704,6 @@ typedef struct BcProgram { | |||
| 709 | BcNum one; | 704 | BcNum one; |
| 710 | 705 | ||
| 711 | size_t nchars; | 706 | size_t nchars; |
| 712 | |||
| 713 | } BcProgram; | 707 | } BcProgram; |
| 714 | 708 | ||
| 715 | #define BC_PROG_STACK(s, n) ((s)->len >= ((size_t) n)) | 709 | #define BC_PROG_STACK(s, n) ((s)->len >= ((size_t) n)) |
| @@ -1124,7 +1118,6 @@ static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx) | |||
| 1124 | if (idx == v->len) | 1118 | if (idx == v->len) |
| 1125 | bc_vec_push(v, data); | 1119 | bc_vec_push(v, data); |
| 1126 | else { | 1120 | else { |
| 1127 | |||
| 1128 | char *ptr; | 1121 | char *ptr; |
| 1129 | 1122 | ||
| 1130 | if (v->len == v->cap) bc_vec_grow(v, 1); | 1123 | if (v->len == v->cap) bc_vec_grow(v, 1); |
| @@ -1210,7 +1203,6 @@ static size_t bc_map_find(const BcVec *v, const void *ptr) | |||
| 1210 | size_t low = 0, high = v->len; | 1203 | size_t low = 0, high = v->len; |
| 1211 | 1204 | ||
| 1212 | while (low < high) { | 1205 | while (low < high) { |
| 1213 | |||
| 1214 | size_t mid = (low + high) / 2; | 1206 | size_t mid = (low + high) / 2; |
| 1215 | BcId *id = bc_vec_item(v, mid); | 1207 | BcId *id = bc_vec_item(v, mid); |
| 1216 | int result = bc_id_cmp(ptr, id); | 1208 | int result = bc_id_cmp(ptr, id); |
| @@ -1421,7 +1413,6 @@ static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p) | |||
| 1421 | if (n->neg) RETURN_STATUS(bc_error("negative number")); | 1413 | if (n->neg) RETURN_STATUS(bc_error("negative number")); |
| 1422 | 1414 | ||
| 1423 | for (result = 0, pow = 1, i = n->rdx; i < n->len; ++i) { | 1415 | for (result = 0, pow = 1, i = n->rdx; i < n->len; ++i) { |
| 1424 | |||
| 1425 | unsigned long prev = result, powprev = pow; | 1416 | unsigned long prev = result, powprev = pow; |
| 1426 | 1417 | ||
| 1427 | result += ((unsigned long) n->num[i]) * pow; | 1418 | result += ((unsigned long) n->num[i]) * pow; |
| @@ -1461,8 +1452,7 @@ static void bc_num_ulong2num(BcNum *n, unsigned long val) | |||
| 1461 | } | 1452 | } |
| 1462 | } | 1453 | } |
| 1463 | 1454 | ||
| 1464 | static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, | 1455 | static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, size_t len) |
| 1465 | size_t len) | ||
| 1466 | { | 1456 | { |
| 1467 | size_t i, j; | 1457 | size_t i, j; |
| 1468 | for (i = 0; i < len; ++i) { | 1458 | for (i = 0; i < len; ++i) { |
| @@ -1556,7 +1546,6 @@ static void bc_num_extend(BcNum *n, size_t places) | |||
| 1556 | size_t len = n->len + places; | 1546 | size_t len = n->len + places; |
| 1557 | 1547 | ||
| 1558 | if (places != 0) { | 1548 | if (places != 0) { |
| 1559 | |||
| 1560 | if (n->cap < len) bc_num_expand(n, len); | 1549 | if (n->cap < len) bc_num_expand(n, len); |
| 1561 | 1550 | ||
| 1562 | memmove(n->num + places, n->num, sizeof(BcDig) * n->len); | 1551 | memmove(n->num + places, n->num, sizeof(BcDig) * n->len); |
| @@ -1591,15 +1580,13 @@ static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a, | |||
| 1591 | BcNum *restrict b) | 1580 | BcNum *restrict b) |
| 1592 | { | 1581 | { |
| 1593 | if (idx < n->len) { | 1582 | if (idx < n->len) { |
| 1594 | |||
| 1595 | b->len = n->len - idx; | 1583 | b->len = n->len - idx; |
| 1596 | a->len = idx; | 1584 | a->len = idx; |
| 1597 | a->rdx = b->rdx = 0; | 1585 | a->rdx = b->rdx = 0; |
| 1598 | 1586 | ||
| 1599 | memcpy(b->num, n->num + idx, b->len * sizeof(BcDig)); | 1587 | memcpy(b->num, n->num + idx, b->len * sizeof(BcDig)); |
| 1600 | memcpy(a->num, n->num, idx * sizeof(BcDig)); | 1588 | memcpy(a->num, n->num, idx * sizeof(BcDig)); |
| 1601 | } | 1589 | } else { |
| 1602 | else { | ||
| 1603 | bc_num_zero(b); | 1590 | bc_num_zero(b); |
| 1604 | bc_num_copy(a, n); | 1591 | bc_num_copy(a, n); |
| 1605 | } | 1592 | } |
| @@ -1869,8 +1856,7 @@ static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b, | |||
| 1869 | s = zbc_num_add(&z0, &z1, &temp, 0); | 1856 | s = zbc_num_add(&z0, &z1, &temp, 0); |
| 1870 | if (s) goto err; | 1857 | if (s) goto err; |
| 1871 | s = zbc_num_add(&temp, &z2, c, 0); | 1858 | s = zbc_num_add(&temp, &z2, c, 0); |
| 1872 | 1859 | err: | |
| 1873 | err: | ||
| 1874 | bc_num_free(&temp); | 1860 | bc_num_free(&temp); |
| 1875 | bc_num_free(&z2); | 1861 | bc_num_free(&z2); |
| 1876 | bc_num_free(&z1); | 1862 | bc_num_free(&z1); |
| @@ -1919,8 +1905,7 @@ static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size | |||
| 1919 | 1905 | ||
| 1920 | c->rdx = maxrdx; | 1906 | c->rdx = maxrdx; |
| 1921 | bc_num_retireMul(c, scale, a->neg, b->neg); | 1907 | bc_num_retireMul(c, scale, a->neg, b->neg); |
| 1922 | 1908 | err: | |
| 1923 | err: | ||
| 1924 | bc_num_free(&cpb); | 1909 | bc_num_free(&cpb); |
| 1925 | bc_num_free(&cpa); | 1910 | bc_num_free(&cpa); |
| 1926 | RETURN_STATUS(s); | 1911 | RETURN_STATUS(s); |
| @@ -2037,8 +2022,7 @@ static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c, | |||
| 2037 | neg = d->neg; | 2022 | neg = d->neg; |
| 2038 | bc_num_retireMul(d, ts, a->neg, b->neg); | 2023 | bc_num_retireMul(d, ts, a->neg, b->neg); |
| 2039 | d->neg = neg; | 2024 | d->neg = neg; |
| 2040 | 2025 | err: | |
| 2041 | err: | ||
| 2042 | bc_num_free(&temp); | 2026 | bc_num_free(&temp); |
| 2043 | RETURN_STATUS(s); | 2027 | RETURN_STATUS(s); |
| 2044 | } | 2028 | } |
| @@ -2311,13 +2295,13 @@ static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t) | |||
| 2311 | if (s) goto err; | 2295 | if (s) goto err; |
| 2312 | 2296 | ||
| 2313 | if (n->len != 0) { | 2297 | if (n->len != 0) { |
| 2314 | if (n->rdx < digits) bc_num_extend(n, digits - n->rdx); | 2298 | if (n->rdx < digits) |
| 2299 | bc_num_extend(n, digits - n->rdx); | ||
| 2315 | } else | 2300 | } else |
| 2316 | bc_num_zero(n); | 2301 | bc_num_zero(n); |
| 2317 | 2302 | err: | |
| 2318 | err: | ||
| 2319 | bc_num_free(&result); | 2303 | bc_num_free(&result); |
| 2320 | int_err: | 2304 | int_err: |
| 2321 | bc_num_free(&base); | 2305 | bc_num_free(&base); |
| 2322 | bc_num_free(&mult); | 2306 | bc_num_free(&mult); |
| 2323 | bc_num_free(&temp); | 2307 | bc_num_free(&temp); |
| @@ -2353,9 +2337,11 @@ static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale) | |||
| 2353 | if (a->len == 0) { | 2337 | if (a->len == 0) { |
| 2354 | bc_num_setToZero(b, scale); | 2338 | bc_num_setToZero(b, scale); |
| 2355 | RETURN_STATUS(BC_STATUS_SUCCESS); | 2339 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 2356 | } else if (a->neg) { | 2340 | } |
| 2341 | if (a->neg) { | ||
| 2357 | RETURN_STATUS(bc_error("negative number")); | 2342 | RETURN_STATUS(bc_error("negative number")); |
| 2358 | } else if (BC_NUM_ONE(a)) { | 2343 | } |
| 2344 | if (BC_NUM_ONE(a)) { | ||
| 2359 | bc_num_one(b); | 2345 | bc_num_one(b); |
| 2360 | bc_num_extend(b, scale); | 2346 | bc_num_extend(b, scale); |
| 2361 | RETURN_STATUS(BC_STATUS_SUCCESS); | 2347 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| @@ -3767,8 +3753,7 @@ static BC_STATUS zbc_parse_call(BcParse *p, char *name, uint8_t flags) | |||
| 3767 | bc_parse_pushIndex(p, entry_ptr->idx); | 3753 | bc_parse_pushIndex(p, entry_ptr->idx); |
| 3768 | 3754 | ||
| 3769 | RETURN_STATUS(zbc_lex_next(&p->l)); | 3755 | RETURN_STATUS(zbc_lex_next(&p->l)); |
| 3770 | 3756 | err: | |
| 3771 | err: | ||
| 3772 | free(name); | 3757 | free(name); |
| 3773 | RETURN_STATUS(s); | 3758 | RETURN_STATUS(s); |
| 3774 | } | 3759 | } |
| @@ -3804,8 +3789,7 @@ static BC_STATUS zbc_parse_name(BcParse *p, BcInst *type, uint8_t flags) | |||
| 3804 | bc_parse_push(p, *type); | 3789 | bc_parse_push(p, *type); |
| 3805 | bc_parse_pushName(p, name); | 3790 | bc_parse_pushName(p, name); |
| 3806 | free(name); | 3791 | free(name); |
| 3807 | } | 3792 | } else if (p->l.t.t == BC_LEX_LPAREN) { |
| 3808 | else if (p->l.t.t == BC_LEX_LPAREN) { | ||
| 3809 | if (flags & BC_PARSE_NOCALL) { | 3793 | if (flags & BC_PARSE_NOCALL) { |
| 3810 | s = bc_error_bad_token(); | 3794 | s = bc_error_bad_token(); |
| 3811 | goto err; | 3795 | goto err; |
| @@ -3820,8 +3804,7 @@ static BC_STATUS zbc_parse_name(BcParse *p, BcInst *type, uint8_t flags) | |||
| 3820 | } | 3804 | } |
| 3821 | 3805 | ||
| 3822 | RETURN_STATUS(s); | 3806 | RETURN_STATUS(s); |
| 3823 | 3807 | err: | |
| 3824 | err: | ||
| 3825 | free(name); | 3808 | free(name); |
| 3826 | RETURN_STATUS(s); | 3809 | RETURN_STATUS(s); |
| 3827 | } | 3810 | } |
| @@ -3908,15 +3891,14 @@ static BC_STATUS zbc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr, | |||
| 3908 | char inst; | 3891 | char inst; |
| 3909 | BcInst etype = *prev; | 3892 | BcInst etype = *prev; |
| 3910 | 3893 | ||
| 3911 | if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM || | 3894 | if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM |
| 3912 | etype == BC_INST_SCALE || etype == BC_INST_LAST || | 3895 | || etype == BC_INST_SCALE || etype == BC_INST_LAST |
| 3913 | etype == BC_INST_IBASE || etype == BC_INST_OBASE) | 3896 | || etype == BC_INST_IBASE || etype == BC_INST_OBASE |
| 3914 | { | 3897 | ) { |
| 3915 | *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC); | 3898 | *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC); |
| 3916 | bc_parse_push(p, inst); | 3899 | bc_parse_push(p, inst); |
| 3917 | s = zbc_lex_next(&p->l); | 3900 | s = zbc_lex_next(&p->l); |
| 3918 | } | 3901 | } else { |
| 3919 | else { | ||
| 3920 | *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC); | 3902 | *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC); |
| 3921 | *paren_expr = true; | 3903 | *paren_expr = true; |
| 3922 | 3904 | ||
| @@ -4339,8 +4321,7 @@ static BC_STATUS zbc_parse_funcdef(BcParse *p) | |||
| 4339 | 4321 | ||
| 4340 | dbg_lex_done("%s:%d done", __func__, __LINE__); | 4322 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
| 4341 | RETURN_STATUS(s); | 4323 | RETURN_STATUS(s); |
| 4342 | 4324 | err: | |
| 4343 | err: | ||
| 4344 | dbg_lex_done("%s:%d done (error)", __func__, __LINE__); | 4325 | dbg_lex_done("%s:%d done (error)", __func__, __LINE__); |
| 4345 | free(name); | 4326 | free(name); |
| 4346 | RETURN_STATUS(s); | 4327 | RETURN_STATUS(s); |
| @@ -4397,8 +4378,7 @@ static BC_STATUS zbc_parse_auto(BcParse *p) | |||
| 4397 | 4378 | ||
| 4398 | dbg_lex_done("%s:%d done", __func__, __LINE__); | 4379 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
| 4399 | RETURN_STATUS(zbc_lex_next(&p->l)); | 4380 | RETURN_STATUS(zbc_lex_next(&p->l)); |
| 4400 | 4381 | err: | |
| 4401 | err: | ||
| 4402 | free(name); | 4382 | free(name); |
| 4403 | dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__); | 4383 | dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__); |
| 4404 | RETURN_STATUS(s); | 4384 | RETURN_STATUS(s); |
| @@ -4561,23 +4541,16 @@ static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags) | |||
| 4561 | 4541 | ||
| 4562 | for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) { | 4542 | for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) { |
| 4563 | switch (t) { | 4543 | switch (t) { |
| 4564 | |||
| 4565 | case BC_LEX_OP_INC: | 4544 | case BC_LEX_OP_INC: |
| 4566 | case BC_LEX_OP_DEC: | 4545 | case BC_LEX_OP_DEC: |
| 4567 | { | ||
| 4568 | s = zbc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags); | 4546 | s = zbc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags); |
| 4569 | rprn = get_token = bin_last = false; | 4547 | rprn = get_token = bin_last = false; |
| 4570 | break; | 4548 | break; |
| 4571 | } | ||
| 4572 | |||
| 4573 | case BC_LEX_OP_MINUS: | 4549 | case BC_LEX_OP_MINUS: |
| 4574 | { | ||
| 4575 | s = zbc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs); | 4550 | s = zbc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs); |
| 4576 | rprn = get_token = false; | 4551 | rprn = get_token = false; |
| 4577 | bin_last = prev == BC_INST_MINUS; | 4552 | bin_last = prev == BC_INST_MINUS; |
| 4578 | break; | 4553 | break; |
| 4579 | } | ||
| 4580 | |||
| 4581 | case BC_LEX_OP_ASSIGN_POWER: | 4554 | case BC_LEX_OP_ASSIGN_POWER: |
| 4582 | case BC_LEX_OP_ASSIGN_MULTIPLY: | 4555 | case BC_LEX_OP_ASSIGN_MULTIPLY: |
| 4583 | case BC_LEX_OP_ASSIGN_DIVIDE: | 4556 | case BC_LEX_OP_ASSIGN_DIVIDE: |
| @@ -4585,18 +4558,16 @@ static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags) | |||
| 4585 | case BC_LEX_OP_ASSIGN_PLUS: | 4558 | case BC_LEX_OP_ASSIGN_PLUS: |
| 4586 | case BC_LEX_OP_ASSIGN_MINUS: | 4559 | case BC_LEX_OP_ASSIGN_MINUS: |
| 4587 | case BC_LEX_OP_ASSIGN: | 4560 | case BC_LEX_OP_ASSIGN: |
| 4588 | { | 4561 | if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM |
| 4589 | if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM && | 4562 | && prev != BC_INST_SCALE && prev != BC_INST_IBASE |
| 4590 | prev != BC_INST_SCALE && prev != BC_INST_IBASE && | 4563 | && prev != BC_INST_OBASE && prev != BC_INST_LAST |
| 4591 | prev != BC_INST_OBASE && prev != BC_INST_LAST) | 4564 | ) { |
| 4592 | { | ||
| 4593 | s = bc_error("bad assignment:" | 4565 | s = bc_error("bad assignment:" |
| 4594 | " left side must be variable" | 4566 | " left side must be variable" |
| 4595 | " or array element" | 4567 | " or array element" |
| 4596 | ); // note: shared string | 4568 | ); // note: shared string |
| 4597 | break; | 4569 | break; |
| 4598 | } | 4570 | } |
| 4599 | } | ||
| 4600 | // Fallthrough. | 4571 | // Fallthrough. |
| 4601 | case BC_LEX_OP_POWER: | 4572 | case BC_LEX_OP_POWER: |
| 4602 | case BC_LEX_OP_MULTIPLY: | 4573 | case BC_LEX_OP_MULTIPLY: |
| @@ -4612,74 +4583,53 @@ static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags) | |||
| 4612 | case BC_LEX_OP_BOOL_NOT: | 4583 | case BC_LEX_OP_BOOL_NOT: |
| 4613 | case BC_LEX_OP_BOOL_OR: | 4584 | case BC_LEX_OP_BOOL_OR: |
| 4614 | case BC_LEX_OP_BOOL_AND: | 4585 | case BC_LEX_OP_BOOL_AND: |
| 4615 | { | ||
| 4616 | if (((t == BC_LEX_OP_BOOL_NOT) != bin_last) | 4586 | if (((t == BC_LEX_OP_BOOL_NOT) != bin_last) |
| 4617 | || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT) | 4587 | || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT) |
| 4618 | ) { | 4588 | ) { |
| 4619 | return bc_error_bad_expression(); | 4589 | return bc_error_bad_expression(); |
| 4620 | } | 4590 | } |
| 4621 | |||
| 4622 | nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT; | 4591 | nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT; |
| 4623 | prev = BC_TOKEN_2_INST(t); | 4592 | prev = BC_TOKEN_2_INST(t); |
| 4624 | bc_parse_operator(p, t, ops_bgn, &nexprs); | 4593 | bc_parse_operator(p, t, ops_bgn, &nexprs); |
| 4625 | s = zbc_lex_next(&p->l); | 4594 | s = zbc_lex_next(&p->l); |
| 4626 | rprn = get_token = false; | 4595 | rprn = get_token = false; |
| 4627 | bin_last = t != BC_LEX_OP_BOOL_NOT; | 4596 | bin_last = t != BC_LEX_OP_BOOL_NOT; |
| 4628 | |||
| 4629 | break; | 4597 | break; |
| 4630 | } | ||
| 4631 | |||
| 4632 | case BC_LEX_LPAREN: | 4598 | case BC_LEX_LPAREN: |
| 4633 | { | ||
| 4634 | if (BC_PARSE_LEAF(prev, rprn)) | 4599 | if (BC_PARSE_LEAF(prev, rprn)) |
| 4635 | return bc_error_bad_expression(); | 4600 | return bc_error_bad_expression(); |
| 4636 | ++nparens; | 4601 | ++nparens; |
| 4637 | paren_expr = rprn = bin_last = false; | 4602 | paren_expr = rprn = bin_last = false; |
| 4638 | get_token = true; | 4603 | get_token = true; |
| 4639 | bc_vec_push(&p->ops, &t); | 4604 | bc_vec_push(&p->ops, &t); |
| 4640 | |||
| 4641 | break; | 4605 | break; |
| 4642 | } | ||
| 4643 | |||
| 4644 | case BC_LEX_RPAREN: | 4606 | case BC_LEX_RPAREN: |
| 4645 | { | ||
| 4646 | if (bin_last || prev == BC_INST_BOOL_NOT) | 4607 | if (bin_last || prev == BC_INST_BOOL_NOT) |
| 4647 | return bc_error_bad_expression(); | 4608 | return bc_error_bad_expression(); |
| 4648 | |||
| 4649 | if (nparens == 0) { | 4609 | if (nparens == 0) { |
| 4650 | s = BC_STATUS_SUCCESS; | 4610 | s = BC_STATUS_SUCCESS; |
| 4651 | done = true; | 4611 | done = true; |
| 4652 | get_token = false; | 4612 | get_token = false; |
| 4653 | break; | 4613 | break; |
| 4654 | } | 4614 | } |
| 4655 | else if (!paren_expr) { | 4615 | if (!paren_expr) { |
| 4656 | dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__); | 4616 | dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__); |
| 4657 | return BC_STATUS_PARSE_EMPTY_EXP; | 4617 | return BC_STATUS_PARSE_EMPTY_EXP; |
| 4658 | } | 4618 | } |
| 4659 | |||
| 4660 | --nparens; | 4619 | --nparens; |
| 4661 | paren_expr = rprn = true; | 4620 | paren_expr = rprn = true; |
| 4662 | get_token = bin_last = false; | 4621 | get_token = bin_last = false; |
| 4663 | |||
| 4664 | s = zbc_parse_rightParen(p, ops_bgn, &nexprs); | 4622 | s = zbc_parse_rightParen(p, ops_bgn, &nexprs); |
| 4665 | |||
| 4666 | break; | 4623 | break; |
| 4667 | } | ||
| 4668 | |||
| 4669 | case BC_LEX_NAME: | 4624 | case BC_LEX_NAME: |
| 4670 | { | ||
| 4671 | if (BC_PARSE_LEAF(prev, rprn)) | 4625 | if (BC_PARSE_LEAF(prev, rprn)) |
| 4672 | return bc_error_bad_expression(); | 4626 | return bc_error_bad_expression(); |
| 4673 | paren_expr = true; | 4627 | paren_expr = true; |
| 4674 | rprn = get_token = bin_last = false; | 4628 | rprn = get_token = bin_last = false; |
| 4675 | s = zbc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL); | 4629 | s = zbc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL); |
| 4676 | ++nexprs; | 4630 | ++nexprs; |
| 4677 | |||
| 4678 | break; | 4631 | break; |
| 4679 | } | ||
| 4680 | |||
| 4681 | case BC_LEX_NUMBER: | 4632 | case BC_LEX_NUMBER: |
| 4682 | { | ||
| 4683 | if (BC_PARSE_LEAF(prev, rprn)) | 4633 | if (BC_PARSE_LEAF(prev, rprn)) |
| 4684 | return bc_error_bad_expression(); | 4634 | return bc_error_bad_expression(); |
| 4685 | bc_parse_number(p); | 4635 | bc_parse_number(p); |
| @@ -4687,58 +4637,40 @@ static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags) | |||
| 4687 | prev = BC_INST_NUM; | 4637 | prev = BC_INST_NUM; |
| 4688 | paren_expr = get_token = true; | 4638 | paren_expr = get_token = true; |
| 4689 | rprn = bin_last = false; | 4639 | rprn = bin_last = false; |
| 4690 | |||
| 4691 | break; | 4640 | break; |
| 4692 | } | ||
| 4693 | |||
| 4694 | case BC_LEX_KEY_IBASE: | 4641 | case BC_LEX_KEY_IBASE: |
| 4695 | case BC_LEX_KEY_LAST: | 4642 | case BC_LEX_KEY_LAST: |
| 4696 | case BC_LEX_KEY_OBASE: | 4643 | case BC_LEX_KEY_OBASE: |
| 4697 | { | ||
| 4698 | if (BC_PARSE_LEAF(prev, rprn)) | 4644 | if (BC_PARSE_LEAF(prev, rprn)) |
| 4699 | return bc_error_bad_expression(); | 4645 | return bc_error_bad_expression(); |
| 4700 | prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE); | 4646 | prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE); |
| 4701 | bc_parse_push(p, (char) prev); | 4647 | bc_parse_push(p, (char) prev); |
| 4702 | |||
| 4703 | paren_expr = get_token = true; | 4648 | paren_expr = get_token = true; |
| 4704 | rprn = bin_last = false; | 4649 | rprn = bin_last = false; |
| 4705 | ++nexprs; | 4650 | ++nexprs; |
| 4706 | |||
| 4707 | break; | 4651 | break; |
| 4708 | } | ||
| 4709 | |||
| 4710 | case BC_LEX_KEY_LENGTH: | 4652 | case BC_LEX_KEY_LENGTH: |
| 4711 | case BC_LEX_KEY_SQRT: | 4653 | case BC_LEX_KEY_SQRT: |
| 4712 | { | ||
| 4713 | if (BC_PARSE_LEAF(prev, rprn)) | 4654 | if (BC_PARSE_LEAF(prev, rprn)) |
| 4714 | return bc_error_bad_expression(); | 4655 | return bc_error_bad_expression(); |
| 4715 | s = zbc_parse_builtin(p, t, flags, &prev); | 4656 | s = zbc_parse_builtin(p, t, flags, &prev); |
| 4716 | paren_expr = true; | 4657 | paren_expr = true; |
| 4717 | rprn = get_token = bin_last = false; | 4658 | rprn = get_token = bin_last = false; |
| 4718 | ++nexprs; | 4659 | ++nexprs; |
| 4719 | |||
| 4720 | break; | 4660 | break; |
| 4721 | } | ||
| 4722 | |||
| 4723 | case BC_LEX_KEY_READ: | 4661 | case BC_LEX_KEY_READ: |
| 4724 | { | ||
| 4725 | if (BC_PARSE_LEAF(prev, rprn)) | 4662 | if (BC_PARSE_LEAF(prev, rprn)) |
| 4726 | return bc_error_bad_expression(); | 4663 | return bc_error_bad_expression(); |
| 4727 | else if (flags & BC_PARSE_NOREAD) | 4664 | else if (flags & BC_PARSE_NOREAD) |
| 4728 | s = bc_error_nested_read_call(); | 4665 | s = bc_error_nested_read_call(); |
| 4729 | else | 4666 | else |
| 4730 | s = zbc_parse_read(p); | 4667 | s = zbc_parse_read(p); |
| 4731 | |||
| 4732 | paren_expr = true; | 4668 | paren_expr = true; |
| 4733 | rprn = get_token = bin_last = false; | 4669 | rprn = get_token = bin_last = false; |
| 4734 | ++nexprs; | 4670 | ++nexprs; |
| 4735 | prev = BC_INST_READ; | 4671 | prev = BC_INST_READ; |
| 4736 | |||
| 4737 | break; | 4672 | break; |
| 4738 | } | ||
| 4739 | |||
| 4740 | case BC_LEX_KEY_SCALE: | 4673 | case BC_LEX_KEY_SCALE: |
| 4741 | { | ||
| 4742 | if (BC_PARSE_LEAF(prev, rprn)) | 4674 | if (BC_PARSE_LEAF(prev, rprn)) |
| 4743 | return bc_error_bad_expression(); | 4675 | return bc_error_bad_expression(); |
| 4744 | s = zbc_parse_scale(p, &prev, flags); | 4676 | s = zbc_parse_scale(p, &prev, flags); |
| @@ -4746,15 +4678,10 @@ static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags) | |||
| 4746 | rprn = get_token = bin_last = false; | 4678 | rprn = get_token = bin_last = false; |
| 4747 | ++nexprs; | 4679 | ++nexprs; |
| 4748 | prev = BC_INST_SCALE; | 4680 | prev = BC_INST_SCALE; |
| 4749 | |||
| 4750 | break; | 4681 | break; |
| 4751 | } | ||
| 4752 | |||
| 4753 | default: | 4682 | default: |
| 4754 | { | ||
| 4755 | s = bc_error_bad_token(); | 4683 | s = bc_error_bad_token(); |
| 4756 | break; | 4684 | break; |
| 4757 | } | ||
| 4758 | } | 4685 | } |
| 4759 | 4686 | ||
| 4760 | if (!s && get_token) s = zbc_lex_next(&p->l); | 4687 | if (!s && get_token) s = zbc_lex_next(&p->l); |
| @@ -4764,7 +4691,6 @@ static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags) | |||
| 4764 | if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing | 4691 | if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing |
| 4765 | 4692 | ||
| 4766 | while (p->ops.len > ops_bgn) { | 4693 | while (p->ops.len > ops_bgn) { |
| 4767 | |||
| 4768 | top = BC_PARSE_TOP_OP(p); | 4694 | top = BC_PARSE_TOP_OP(p); |
| 4769 | assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN; | 4695 | assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN; |
| 4770 | 4696 | ||
| @@ -4783,8 +4709,7 @@ static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags) | |||
| 4783 | if (!(flags & BC_PARSE_REL) && nrelops) { | 4709 | if (!(flags & BC_PARSE_REL) && nrelops) { |
| 4784 | s = bc_POSIX_does_not_allow("comparison operators outside if or loops"); | 4710 | s = bc_POSIX_does_not_allow("comparison operators outside if or loops"); |
| 4785 | IF_ERROR_RETURN_POSSIBLE(if (s) return s); | 4711 | IF_ERROR_RETURN_POSSIBLE(if (s) return s); |
| 4786 | } | 4712 | } else if ((flags & BC_PARSE_REL) && nrelops > 1) { |
| 4787 | else if ((flags & BC_PARSE_REL) && nrelops > 1) { | ||
| 4788 | s = bc_POSIX_requires("exactly one comparison operator per condition"); | 4713 | s = bc_POSIX_requires("exactly one comparison operator per condition"); |
| 4789 | IF_ERROR_RETURN_POSSIBLE(if (s) return s); | 4714 | IF_ERROR_RETURN_POSSIBLE(if (s) return s); |
| 4790 | } | 4715 | } |
| @@ -4871,8 +4796,7 @@ static BC_STATUS zdc_parse_cond(BcParse *p, uint8_t inst) | |||
| 4871 | s = zdc_parse_register(p); | 4796 | s = zdc_parse_register(p); |
| 4872 | if (s) RETURN_STATUS(s); | 4797 | if (s) RETURN_STATUS(s); |
| 4873 | s = zbc_lex_next(&p->l); | 4798 | s = zbc_lex_next(&p->l); |
| 4874 | } | 4799 | } else |
| 4875 | else | ||
| 4876 | bc_parse_push(p, BC_PARSE_STREND); | 4800 | bc_parse_push(p, BC_PARSE_STREND); |
| 4877 | 4801 | ||
| 4878 | RETURN_STATUS(s); | 4802 | RETURN_STATUS(s); |
| @@ -5035,19 +4959,14 @@ static BcVec* bc_program_search(char *id, bool var) | |||
| 5035 | static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex) | 4959 | static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex) |
| 5036 | { | 4960 | { |
| 5037 | switch (r->t) { | 4961 | switch (r->t) { |
| 5038 | |||
| 5039 | case BC_RESULT_STR: | 4962 | case BC_RESULT_STR: |
| 5040 | case BC_RESULT_TEMP: | 4963 | case BC_RESULT_TEMP: |
| 5041 | case BC_RESULT_IBASE: | 4964 | case BC_RESULT_IBASE: |
| 5042 | case BC_RESULT_SCALE: | 4965 | case BC_RESULT_SCALE: |
| 5043 | case BC_RESULT_OBASE: | 4966 | case BC_RESULT_OBASE: |
| 5044 | { | ||
| 5045 | *num = &r->d.n; | 4967 | *num = &r->d.n; |
| 5046 | break; | 4968 | break; |
| 5047 | } | 4969 | case BC_RESULT_CONSTANT: { |
| 5048 | |||
| 5049 | case BC_RESULT_CONSTANT: | ||
| 5050 | { | ||
| 5051 | BcStatus s; | 4970 | BcStatus s; |
| 5052 | char **str = bc_vec_item(&G.prog.consts, r->d.id.idx); | 4971 | char **str = bc_vec_item(&G.prog.consts, r->d.id.idx); |
| 5053 | unsigned base_t; | 4972 | unsigned base_t; |
| @@ -5066,14 +4985,11 @@ static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex) | |||
| 5066 | 4985 | ||
| 5067 | *num = &r->d.n; | 4986 | *num = &r->d.n; |
| 5068 | r->t = BC_RESULT_TEMP; | 4987 | r->t = BC_RESULT_TEMP; |
| 5069 | |||
| 5070 | break; | 4988 | break; |
| 5071 | } | 4989 | } |
| 5072 | |||
| 5073 | case BC_RESULT_VAR: | 4990 | case BC_RESULT_VAR: |
| 5074 | case BC_RESULT_ARRAY: | 4991 | case BC_RESULT_ARRAY: |
| 5075 | case BC_RESULT_ARRAY_ELEM: | 4992 | case BC_RESULT_ARRAY_ELEM: { |
| 5076 | { | ||
| 5077 | BcVec *v; | 4993 | BcVec *v; |
| 5078 | 4994 | ||
| 5079 | v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR); | 4995 | v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR); |
| @@ -5082,24 +4998,16 @@ static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex) | |||
| 5082 | v = bc_vec_top(v); | 4998 | v = bc_vec_top(v); |
| 5083 | if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1); | 4999 | if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1); |
| 5084 | *num = bc_vec_item(v, r->d.id.idx); | 5000 | *num = bc_vec_item(v, r->d.id.idx); |
| 5085 | } | 5001 | } else |
| 5086 | else | ||
| 5087 | *num = bc_vec_top(v); | 5002 | *num = bc_vec_top(v); |
| 5088 | |||
| 5089 | break; | 5003 | break; |
| 5090 | } | 5004 | } |
| 5091 | |||
| 5092 | case BC_RESULT_LAST: | 5005 | case BC_RESULT_LAST: |
| 5093 | { | ||
| 5094 | *num = &G.prog.last; | 5006 | *num = &G.prog.last; |
| 5095 | break; | 5007 | break; |
| 5096 | } | ||
| 5097 | |||
| 5098 | case BC_RESULT_ONE: | 5008 | case BC_RESULT_ONE: |
| 5099 | { | ||
| 5100 | *num = &G.prog.one; | 5009 | *num = &G.prog.one; |
| 5101 | break; | 5010 | break; |
| 5102 | } | ||
| 5103 | } | 5011 | } |
| 5104 | 5012 | ||
| 5105 | RETURN_STATUS(BC_STATUS_SUCCESS); | 5013 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| @@ -5193,8 +5101,7 @@ static BC_STATUS zbc_program_op(char inst) | |||
| 5193 | bc_program_binOpRetire(&res); | 5101 | bc_program_binOpRetire(&res); |
| 5194 | 5102 | ||
| 5195 | RETURN_STATUS(s); | 5103 | RETURN_STATUS(s); |
| 5196 | 5104 | err: | |
| 5197 | err: | ||
| 5198 | bc_num_free(&res.d.n); | 5105 | bc_num_free(&res.d.n); |
| 5199 | RETURN_STATUS(s); | 5106 | RETURN_STATUS(s); |
| 5200 | } | 5107 | } |
| @@ -5244,8 +5151,7 @@ static BC_STATUS zbc_program_read(void) | |||
| 5244 | 5151 | ||
| 5245 | bc_vec_pushByte(&f->code, BC_INST_POP_EXEC); | 5152 | bc_vec_pushByte(&f->code, BC_INST_POP_EXEC); |
| 5246 | bc_vec_push(&G.prog.stack, &ip); | 5153 | bc_vec_push(&G.prog.stack, &ip); |
| 5247 | 5154 | exec_err: | |
| 5248 | exec_err: | ||
| 5249 | bc_parse_free(&parse); | 5155 | bc_parse_free(&parse); |
| 5250 | G.in_read = 0; | 5156 | G.in_read = 0; |
| 5251 | G.prog.file = sv_file; | 5157 | G.prog.file = sv_file; |
| @@ -5461,8 +5367,7 @@ static BC_STATUS zbc_num_printNum(BcNum *n, BcNum *base, size_t width, BcNumDigi | |||
| 5461 | s = zbc_num_mul(&frac_len, base, &frac_len, 0); | 5367 | s = zbc_num_mul(&frac_len, base, &frac_len, 0); |
| 5462 | if (s) goto err; | 5368 | if (s) goto err; |
| 5463 | } | 5369 | } |
| 5464 | 5370 | err: | |
| 5465 | err: | ||
| 5466 | bc_num_free(&frac_len); | 5371 | bc_num_free(&frac_len); |
| 5467 | bc_num_free(&digit); | 5372 | bc_num_free(&digit); |
| 5468 | bc_num_free(&fracp); | 5373 | bc_num_free(&fracp); |
| @@ -5549,8 +5454,7 @@ static BC_STATUS zbc_program_print(char inst, size_t idx) | |||
| 5549 | if (BC_PROG_NUM(r, num)) { | 5454 | if (BC_PROG_NUM(r, num)) { |
| 5550 | s = zbc_num_print(num, !pop); | 5455 | s = zbc_num_print(num, !pop); |
| 5551 | if (!s) bc_num_copy(&G.prog.last, num); | 5456 | if (!s) bc_num_copy(&G.prog.last, num); |
| 5552 | } | 5457 | } else { |
| 5553 | else { | ||
| 5554 | char *str; | 5458 | char *str; |
| 5555 | 5459 | ||
| 5556 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx; | 5460 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx; |
| @@ -5564,8 +5468,7 @@ static BC_STATUS zbc_program_print(char inst, size_t idx) | |||
| 5564 | ++G.prog.nchars; | 5468 | ++G.prog.nchars; |
| 5565 | if (c == '\n') G.prog.nchars = 0; | 5469 | if (c == '\n') G.prog.nchars = 0; |
| 5566 | } | 5470 | } |
| 5567 | } | 5471 | } else { |
| 5568 | else { | ||
| 5569 | bc_program_printString(str); | 5472 | bc_program_printString(str); |
| 5570 | if (inst == BC_INST_PRINT) bb_putchar('\n'); | 5473 | if (inst == BC_INST_PRINT) bb_putchar('\n'); |
| 5571 | } | 5474 | } |
| @@ -5646,8 +5549,7 @@ static BC_STATUS zbc_program_logical(char inst) | |||
| 5646 | #define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__) COMMA_SUCCESS) | 5549 | #define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__) COMMA_SUCCESS) |
| 5647 | 5550 | ||
| 5648 | #if ENABLE_DC | 5551 | #if ENABLE_DC |
| 5649 | static BC_STATUS zbc_program_assignStr(BcResult *r, BcVec *v, | 5552 | static BC_STATUS zbc_program_assignStr(BcResult *r, BcVec *v, bool push) |
| 5650 | bool push) | ||
| 5651 | { | 5553 | { |
| 5652 | BcNum n2; | 5554 | BcNum n2; |
| 5653 | BcResult res; | 5555 | BcResult res; |
| @@ -5704,8 +5606,7 @@ static BC_STATUS zbc_program_copyToVar(char *name, bool var) | |||
| 5704 | if (var) { | 5606 | if (var) { |
| 5705 | bc_num_init_DEF_SIZE(&r.d.n); | 5607 | bc_num_init_DEF_SIZE(&r.d.n); |
| 5706 | bc_num_copy(&r.d.n, n); | 5608 | bc_num_copy(&r.d.n, n); |
| 5707 | } | 5609 | } else { |
| 5708 | else { | ||
| 5709 | bc_array_init(&r.d.v, true); | 5610 | bc_array_init(&r.d.v, true); |
| 5710 | bc_array_copy(&r.d.v, (BcVec *) n); | 5611 | bc_array_copy(&r.d.v, (BcVec *) n); |
| 5711 | } | 5612 | } |
| @@ -5731,9 +5632,7 @@ static BC_STATUS zbc_program_assign(char inst) | |||
| 5731 | sc = left->t == BC_RESULT_SCALE; | 5632 | sc = left->t == BC_RESULT_SCALE; |
| 5732 | 5633 | ||
| 5733 | #if ENABLE_DC | 5634 | #if ENABLE_DC |
| 5734 | |||
| 5735 | if (right->t == BC_RESULT_STR) { | 5635 | if (right->t == BC_RESULT_STR) { |
| 5736 | |||
| 5737 | BcVec *v; | 5636 | BcVec *v; |
| 5738 | 5637 | ||
| 5739 | if (left->t != BC_RESULT_VAR) | 5638 | if (left->t != BC_RESULT_VAR) |
| @@ -5828,7 +5727,6 @@ static BC_STATUS bc_program_pushVar(char *code, size_t *bgn, | |||
| 5828 | BcNum *num = bc_vec_top(v); | 5727 | BcNum *num = bc_vec_top(v); |
| 5829 | 5728 | ||
| 5830 | if (pop || copy) { | 5729 | if (pop || copy) { |
| 5831 | |||
| 5832 | if (!BC_PROG_STACK(v, 2 - copy)) { | 5730 | if (!BC_PROG_STACK(v, 2 - copy)) { |
| 5833 | free(name); | 5731 | free(name); |
| 5834 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); | 5732 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
| @@ -5838,13 +5736,11 @@ static BC_STATUS bc_program_pushVar(char *code, size_t *bgn, | |||
| 5838 | name = NULL; | 5736 | name = NULL; |
| 5839 | 5737 | ||
| 5840 | if (!BC_PROG_STR(num)) { | 5738 | if (!BC_PROG_STR(num)) { |
| 5841 | |||
| 5842 | r.t = BC_RESULT_TEMP; | 5739 | r.t = BC_RESULT_TEMP; |
| 5843 | 5740 | ||
| 5844 | bc_num_init_DEF_SIZE(&r.d.n); | 5741 | bc_num_init_DEF_SIZE(&r.d.n); |
| 5845 | bc_num_copy(&r.d.n, num); | 5742 | bc_num_copy(&r.d.n, num); |
| 5846 | } | 5743 | } else { |
| 5847 | else { | ||
| 5848 | r.t = BC_RESULT_STR; | 5744 | r.t = BC_RESULT_STR; |
| 5849 | r.d.id.idx = num->rdx; | 5745 | r.d.id.idx = num->rdx; |
| 5850 | } | 5746 | } |
| @@ -5860,8 +5756,7 @@ static BC_STATUS bc_program_pushVar(char *code, size_t *bgn, | |||
| 5860 | } | 5756 | } |
| 5861 | #define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS) | 5757 | #define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS) |
| 5862 | 5758 | ||
| 5863 | static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, | 5759 | static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst) |
| 5864 | char inst) | ||
| 5865 | { | 5760 | { |
| 5866 | BcStatus s = BC_STATUS_SUCCESS; | 5761 | BcStatus s = BC_STATUS_SUCCESS; |
| 5867 | BcResult r; | 5762 | BcResult r; |
| @@ -5872,9 +5767,7 @@ static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, | |||
| 5872 | if (inst == BC_INST_ARRAY) { | 5767 | if (inst == BC_INST_ARRAY) { |
| 5873 | r.t = BC_RESULT_ARRAY; | 5768 | r.t = BC_RESULT_ARRAY; |
| 5874 | bc_vec_push(&G.prog.results, &r); | 5769 | bc_vec_push(&G.prog.results, &r); |
| 5875 | } | 5770 | } else { |
| 5876 | else { | ||
| 5877 | |||
| 5878 | BcResult *operand; | 5771 | BcResult *operand; |
| 5879 | unsigned long temp; | 5772 | unsigned long temp; |
| 5880 | 5773 | ||
| @@ -5891,8 +5784,7 @@ static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, | |||
| 5891 | r.d.id.idx = (size_t) temp; | 5784 | r.d.id.idx = (size_t) temp; |
| 5892 | bc_program_retire(&r, BC_RESULT_ARRAY_ELEM); | 5785 | bc_program_retire(&r, BC_RESULT_ARRAY_ELEM); |
| 5893 | } | 5786 | } |
| 5894 | 5787 | err: | |
| 5895 | err: | ||
| 5896 | if (s) free(r.d.id.name); | 5788 | if (s) free(r.d.id.name); |
| 5897 | RETURN_STATUS(s); | 5789 | RETURN_STATUS(s); |
| 5898 | } | 5790 | } |
| @@ -5976,8 +5868,7 @@ static BC_STATUS zbc_program_call(char *code, size_t *idx) | |||
| 5976 | if (a->idx) { | 5868 | if (a->idx) { |
| 5977 | bc_num_init_DEF_SIZE(¶m.n); | 5869 | bc_num_init_DEF_SIZE(¶m.n); |
| 5978 | bc_vec_push(v, ¶m.n); | 5870 | bc_vec_push(v, ¶m.n); |
| 5979 | } | 5871 | } else { |
| 5980 | else { | ||
| 5981 | bc_array_init(¶m.v, true); | 5872 | bc_array_init(¶m.v, true); |
| 5982 | bc_vec_push(v, ¶m.v); | 5873 | bc_vec_push(v, ¶m.v); |
| 5983 | } | 5874 | } |
| @@ -6011,8 +5902,7 @@ static BC_STATUS zbc_program_return(char inst) | |||
| 6011 | if (s) RETURN_STATUS(s); | 5902 | if (s) RETURN_STATUS(s); |
| 6012 | bc_num_init(&res.d.n, num->len); | 5903 | bc_num_init(&res.d.n, num->len); |
| 6013 | bc_num_copy(&res.d.n, num); | 5904 | bc_num_copy(&res.d.n, num); |
| 6014 | } | 5905 | } else { |
| 6015 | else { | ||
| 6016 | bc_num_init_DEF_SIZE(&res.d.n); | 5906 | bc_num_init_DEF_SIZE(&res.d.n); |
| 6017 | //bc_num_zero(&res.d.n); - already is | 5907 | //bc_num_zero(&res.d.n); - already is |
| 6018 | } | 5908 | } |
| @@ -6075,7 +5965,8 @@ static BC_STATUS zbc_program_builtin(char inst) | |||
| 6075 | 5965 | ||
| 6076 | bc_num_init_DEF_SIZE(&res.d.n); | 5966 | bc_num_init_DEF_SIZE(&res.d.n); |
| 6077 | 5967 | ||
| 6078 | if (inst == BC_INST_SQRT) s = zbc_num_sqrt(num, &res.d.n, G.prog.scale); | 5968 | if (inst == BC_INST_SQRT) |
| 5969 | s = zbc_num_sqrt(num, &res.d.n, G.prog.scale); | ||
| 6079 | #if ENABLE_BC | 5970 | #if ENABLE_BC |
| 6080 | else if (len != 0 && opnd->t == BC_RESULT_ARRAY) { | 5971 | else if (len != 0 && opnd->t == BC_RESULT_ARRAY) { |
| 6081 | bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len); | 5972 | bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len); |
| @@ -6121,8 +6012,7 @@ static BC_STATUS zbc_program_divmod(void) | |||
| 6121 | bc_vec_push(&G.prog.results, &res); | 6012 | bc_vec_push(&G.prog.results, &res); |
| 6122 | 6013 | ||
| 6123 | RETURN_STATUS(s); | 6014 | RETURN_STATUS(s); |
| 6124 | 6015 | err: | |
| 6125 | err: | ||
| 6126 | bc_num_free(&res2.d.n); | 6016 | bc_num_free(&res2.d.n); |
| 6127 | bc_num_free(&res.d.n); | 6017 | bc_num_free(&res.d.n); |
| 6128 | RETURN_STATUS(s); | 6018 | RETURN_STATUS(s); |
| @@ -6148,12 +6038,10 @@ static BC_STATUS zbc_program_modexp(void) | |||
| 6148 | 6038 | ||
| 6149 | // Make sure that the values have their pointers updated, if necessary. | 6039 | // Make sure that the values have their pointers updated, if necessary. |
| 6150 | if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) { | 6040 | if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) { |
| 6151 | |||
| 6152 | if (r1->t == r2->t) { | 6041 | if (r1->t == r2->t) { |
| 6153 | s = zbc_program_num(r2, &n2, false); | 6042 | s = zbc_program_num(r2, &n2, false); |
| 6154 | if (s) RETURN_STATUS(s); | 6043 | if (s) RETURN_STATUS(s); |
| 6155 | } | 6044 | } |
| 6156 | |||
| 6157 | if (r1->t == r3->t) { | 6045 | if (r1->t == r3->t) { |
| 6158 | s = zbc_program_num(r3, &n3, false); | 6046 | s = zbc_program_num(r3, &n3, false); |
| 6159 | if (s) RETURN_STATUS(s); | 6047 | if (s) RETURN_STATUS(s); |
| @@ -6168,8 +6056,7 @@ static BC_STATUS zbc_program_modexp(void) | |||
| 6168 | bc_program_binOpRetire(&res); | 6056 | bc_program_binOpRetire(&res); |
| 6169 | 6057 | ||
| 6170 | RETURN_STATUS(s); | 6058 | RETURN_STATUS(s); |
| 6171 | 6059 | err: | |
| 6172 | err: | ||
| 6173 | bc_num_free(&res.d.n); | 6060 | bc_num_free(&res.d.n); |
| 6174 | RETURN_STATUS(s); | 6061 | RETURN_STATUS(s); |
| 6175 | } | 6062 | } |
| @@ -6205,7 +6092,6 @@ static BC_STATUS zbc_program_asciify(void) | |||
| 6205 | if (s) RETURN_STATUS(s); | 6092 | if (s) RETURN_STATUS(s); |
| 6206 | 6093 | ||
| 6207 | if (BC_PROG_NUM(r, num)) { | 6094 | if (BC_PROG_NUM(r, num)) { |
| 6208 | |||
| 6209 | bc_num_init_DEF_SIZE(&n); | 6095 | bc_num_init_DEF_SIZE(&n); |
| 6210 | bc_num_copy(&n, num); | 6096 | bc_num_copy(&n, num); |
| 6211 | bc_num_truncate(&n, n.rdx); | 6097 | bc_num_truncate(&n, n.rdx); |
| @@ -6218,8 +6104,7 @@ static BC_STATUS zbc_program_asciify(void) | |||
| 6218 | c = (char) val; | 6104 | c = (char) val; |
| 6219 | 6105 | ||
| 6220 | bc_num_free(&n); | 6106 | bc_num_free(&n); |
| 6221 | } | 6107 | } else { |
| 6222 | else { | ||
| 6223 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx; | 6108 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx; |
| 6224 | str2 = *bc_program_str(idx); | 6109 | str2 = *bc_program_str(idx); |
| 6225 | c = str2[0]; | 6110 | c = str2[0]; |
| @@ -6239,10 +6124,8 @@ static BC_STATUS zbc_program_asciify(void) | |||
| 6239 | break; | 6124 | break; |
| 6240 | } | 6125 | } |
| 6241 | } | 6126 | } |
| 6242 | |||
| 6243 | free(str); | 6127 | free(str); |
| 6244 | } | 6128 | } else |
| 6245 | else | ||
| 6246 | bc_vec_push(&G.prog.strs, &str); | 6129 | bc_vec_push(&G.prog.strs, &str); |
| 6247 | 6130 | ||
| 6248 | res.t = BC_RESULT_STR; | 6131 | res.t = BC_RESULT_STR; |
| @@ -6251,8 +6134,7 @@ static BC_STATUS zbc_program_asciify(void) | |||
| 6251 | bc_vec_push(&G.prog.results, &res); | 6134 | bc_vec_push(&G.prog.results, &res); |
| 6252 | 6135 | ||
| 6253 | RETURN_STATUS(BC_STATUS_SUCCESS); | 6136 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 6254 | 6137 | num_err: | |
| 6255 | num_err: | ||
| 6256 | bc_num_free(&n); | 6138 | bc_num_free(&n); |
| 6257 | RETURN_STATUS(s); | 6139 | RETURN_STATUS(s); |
| 6258 | } | 6140 | } |
| @@ -6311,8 +6193,7 @@ static BC_STATUS zbc_program_nquit(void) | |||
| 6311 | } | 6193 | } |
| 6312 | #define zbc_program_nquit(...) (zbc_program_nquit(__VA_ARGS__) COMMA_SUCCESS) | 6194 | #define zbc_program_nquit(...) (zbc_program_nquit(__VA_ARGS__) COMMA_SUCCESS) |
| 6313 | 6195 | ||
| 6314 | static BC_STATUS zbc_program_execStr(char *code, size_t *bgn, | 6196 | static BC_STATUS zbc_program_execStr(char *code, size_t *bgn, bool cond) |
| 6315 | bool cond) | ||
| 6316 | { | 6197 | { |
| 6317 | BcStatus s = BC_STATUS_SUCCESS; | 6198 | BcStatus s = BC_STATUS_SUCCESS; |
| 6318 | BcResult *r; | 6199 | BcResult *r; |
| @@ -6402,12 +6283,11 @@ static BC_STATUS zbc_program_execStr(char *code, size_t *bgn, | |||
| 6402 | bc_vec_push(&G.prog.stack, &ip); | 6283 | bc_vec_push(&G.prog.stack, &ip); |
| 6403 | 6284 | ||
| 6404 | RETURN_STATUS(BC_STATUS_SUCCESS); | 6285 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 6405 | 6286 | err: | |
| 6406 | err: | ||
| 6407 | bc_parse_free(&prs); | 6287 | bc_parse_free(&prs); |
| 6408 | f = bc_program_func(fidx); | 6288 | f = bc_program_func(fidx); |
| 6409 | bc_vec_pop_all(&f->code); | 6289 | bc_vec_pop_all(&f->code); |
| 6410 | exit: | 6290 | exit: |
| 6411 | bc_vec_pop(&G.prog.results); | 6291 | bc_vec_pop(&G.prog.results); |
| 6412 | RETURN_STATUS(s); | 6292 | RETURN_STATUS(s); |
| 6413 | } | 6293 | } |
| @@ -6448,7 +6328,6 @@ static void bc_program_addFunc(char *name, size_t *idx) | |||
| 6448 | *idx = entry_ptr->idx; | 6328 | *idx = entry_ptr->idx; |
| 6449 | 6329 | ||
| 6450 | if (!inserted) { | 6330 | if (!inserted) { |
| 6451 | |||
| 6452 | BcFunc *func = bc_program_func(entry_ptr->idx); | 6331 | BcFunc *func = bc_program_func(entry_ptr->idx); |
| 6453 | 6332 | ||
| 6454 | // We need to reset these, so the function can be repopulated. | 6333 | // We need to reset these, so the function can be repopulated. |
| @@ -6456,8 +6335,7 @@ static void bc_program_addFunc(char *name, size_t *idx) | |||
| 6456 | bc_vec_pop_all(&func->autos); | 6335 | bc_vec_pop_all(&func->autos); |
| 6457 | bc_vec_pop_all(&func->code); | 6336 | bc_vec_pop_all(&func->code); |
| 6458 | bc_vec_pop_all(&func->labels); | 6337 | bc_vec_pop_all(&func->labels); |
| 6459 | } | 6338 | } else { |
| 6460 | else { | ||
| 6461 | bc_func_init(&f); | 6339 | bc_func_init(&f); |
| 6462 | bc_vec_push(&G.prog.fns, &f); | 6340 | bc_vec_push(&G.prog.fns, &f); |
| 6463 | } | 6341 | } |
