diff options
-rw-r--r-- | miscutils/bc.c | 395 |
1 files changed, 201 insertions, 194 deletions
diff --git a/miscutils/bc.c b/miscutils/bc.c index c756d8cd6..4e5ba804f 100644 --- a/miscutils/bc.c +++ b/miscutils/bc.c | |||
@@ -976,20 +976,29 @@ static void bc_verror_msg(const char *fmt, va_list p) | |||
976 | } | 976 | } |
977 | } | 977 | } |
978 | 978 | ||
979 | #if ENABLE_FEATURE_BC_SIGNALS | 979 | // In configurations where errors abort instead of propagating error |
980 | // return code up the call chain, functions returning BC_STATUS | ||
981 | // actually don't return anything, they always succeed and return "void". | ||
982 | // A macro wrapper is provided, which makes this statement work: | ||
983 | // s = zbc_func(...) | ||
984 | // and makes it visible to the compiler that s is always zero, | ||
985 | // allowing compiler to optimize dead code after the statement. | ||
986 | // | ||
987 | // To make code more readable, each such function has a "z" | ||
988 | // ("always returning zero") prefix, i.e. zbc_foo or zdc_foo. | ||
989 | // | ||
990 | #if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP | ||
980 | # define ERRORFUNC /*nothing*/ | 991 | # define ERRORFUNC /*nothing*/ |
981 | # define ERROR_RETURN(a) a | 992 | # define ERROR_RETURN(a) a |
982 | # define ERRORS_ARE_FATAL 0 | 993 | # define ERRORS_ARE_FATAL 0 |
994 | # define BC_STATUS BcStatus | ||
995 | # define RETURN_STATUS(v) return (v) | ||
983 | #else | 996 | #else |
984 | # if ENABLE_FEATURE_CLEAN_UP | 997 | # define ERRORFUNC NORETURN |
985 | # define ERRORFUNC /*nothing*/ | 998 | # define ERROR_RETURN(a) /*nothing*/ |
986 | # define ERROR_RETURN(a) a | 999 | # define ERRORS_ARE_FATAL 1 |
987 | # define ERRORS_ARE_FATAL 0 | 1000 | # define BC_STATUS void |
988 | # else | 1001 | # define RETURN_STATUS(v) do { ((void)(v)); return; } while (0) |
989 | # define ERRORFUNC NORETURN | ||
990 | # define ERROR_RETURN(a) /*nothing*/ | ||
991 | # define ERRORS_ARE_FATAL 1 | ||
992 | # endif | ||
993 | #endif | 1002 | #endif |
994 | 1003 | ||
995 | static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...) | 1004 | static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...) |
@@ -1456,12 +1465,12 @@ static void bc_num_copy(BcNum *d, BcNum *s) | |||
1456 | } | 1465 | } |
1457 | } | 1466 | } |
1458 | 1467 | ||
1459 | static BcStatus bc_num_ulong(BcNum *n, unsigned long *result_p) | 1468 | static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p) |
1460 | { | 1469 | { |
1461 | size_t i; | 1470 | size_t i; |
1462 | unsigned long pow, result; | 1471 | unsigned long pow, result; |
1463 | 1472 | ||
1464 | if (n->neg) return bc_error("negative number"); | 1473 | if (n->neg) RETURN_STATUS(bc_error("negative number")); |
1465 | 1474 | ||
1466 | for (result = 0, pow = 1, i = n->rdx; i < n->len; ++i) { | 1475 | for (result = 0, pow = 1, i = n->rdx; i < n->len; ++i) { |
1467 | 1476 | ||
@@ -1471,16 +1480,16 @@ static BcStatus bc_num_ulong(BcNum *n, unsigned long *result_p) | |||
1471 | pow *= 10; | 1480 | pow *= 10; |
1472 | 1481 | ||
1473 | if (result < prev || pow < powprev) | 1482 | if (result < prev || pow < powprev) |
1474 | return bc_error("overflow"); | 1483 | RETURN_STATUS(bc_error("overflow")); |
1475 | prev = result; | 1484 | prev = result; |
1476 | powprev = pow; | 1485 | powprev = pow; |
1477 | } | 1486 | } |
1478 | *result_p = result; | 1487 | *result_p = result; |
1479 | 1488 | ||
1480 | return BC_STATUS_SUCCESS; | 1489 | RETURN_STATUS(BC_STATUS_SUCCESS); |
1481 | } | 1490 | } |
1482 | #if ERRORS_ARE_FATAL | 1491 | #if ERRORS_ARE_FATAL |
1483 | # define bc_num_ulong(...) (bc_num_ulong(__VA_ARGS__), BC_STATUS_SUCCESS) | 1492 | # define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__), BC_STATUS_SUCCESS) |
1484 | #endif | 1493 | #endif |
1485 | 1494 | ||
1486 | static void bc_num_ulong2num(BcNum *n, unsigned long val) | 1495 | static void bc_num_ulong2num(BcNum *n, unsigned long val) |
@@ -1658,14 +1667,14 @@ static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a, | |||
1658 | bc_num_clean(b); | 1667 | bc_num_clean(b); |
1659 | } | 1668 | } |
1660 | 1669 | ||
1661 | static BcStatus bc_num_shift(BcNum *n, size_t places) | 1670 | static BC_STATUS zbc_num_shift(BcNum *n, size_t places) |
1662 | { | 1671 | { |
1663 | if (places == 0 || n->len == 0) return BC_STATUS_SUCCESS; | 1672 | if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS); |
1664 | 1673 | ||
1665 | // This check makes sense only if size_t is (much) larger than BC_MAX_NUM. | 1674 | // This check makes sense only if size_t is (much) larger than BC_MAX_NUM. |
1666 | if (SIZE_MAX > (BC_MAX_NUM | 0xff)) { | 1675 | if (SIZE_MAX > (BC_MAX_NUM | 0xff)) { |
1667 | if (places + n->len > BC_MAX_NUM) | 1676 | if (places + n->len > BC_MAX_NUM) |
1668 | return bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"); | 1677 | RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]")); |
1669 | } | 1678 | } |
1670 | 1679 | ||
1671 | if (n->rdx >= places) | 1680 | if (n->rdx >= places) |
@@ -1677,10 +1686,10 @@ static BcStatus bc_num_shift(BcNum *n, size_t places) | |||
1677 | 1686 | ||
1678 | bc_num_clean(n); | 1687 | bc_num_clean(n); |
1679 | 1688 | ||
1680 | return BC_STATUS_SUCCESS; | 1689 | RETURN_STATUS(BC_STATUS_SUCCESS); |
1681 | } | 1690 | } |
1682 | #if ERRORS_ARE_FATAL | 1691 | #if ERRORS_ARE_FATAL |
1683 | # define bc_num_shift(...) (bc_num_shift(__VA_ARGS__), BC_STATUS_SUCCESS) | 1692 | # define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__), BC_STATUS_SUCCESS) |
1684 | #endif | 1693 | #endif |
1685 | 1694 | ||
1686 | static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) | 1695 | static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) |
@@ -1912,9 +1921,9 @@ static FAST_FUNC BcStatus bc_num_k(BcNum *restrict a, BcNum *restrict b, | |||
1912 | s = bc_num_sub(&temp, &z2, &z1, 0); | 1921 | s = bc_num_sub(&temp, &z2, &z1, 0); |
1913 | if (s) goto err; | 1922 | if (s) goto err; |
1914 | 1923 | ||
1915 | s = bc_num_shift(&z0, max2 * 2); | 1924 | s = zbc_num_shift(&z0, max2 * 2); |
1916 | if (s) goto err; | 1925 | if (s) goto err; |
1917 | s = bc_num_shift(&z1, max2); | 1926 | s = zbc_num_shift(&z1, max2); |
1918 | if (s) goto err; | 1927 | if (s) goto err; |
1919 | s = bc_num_add(&z0, &z1, &temp, 0); | 1928 | s = bc_num_add(&z0, &z1, &temp, 0); |
1920 | if (s) goto err; | 1929 | if (s) goto err; |
@@ -1952,9 +1961,9 @@ static FAST_FUNC BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t | |||
1952 | bc_num_copy(&cpb, b); | 1961 | bc_num_copy(&cpb, b); |
1953 | cpa.neg = cpb.neg = false; | 1962 | cpa.neg = cpb.neg = false; |
1954 | 1963 | ||
1955 | s = bc_num_shift(&cpa, maxrdx); | 1964 | s = zbc_num_shift(&cpa, maxrdx); |
1956 | if (s) goto err; | 1965 | if (s) goto err; |
1957 | s = bc_num_shift(&cpb, maxrdx); | 1966 | s = zbc_num_shift(&cpb, maxrdx); |
1958 | if (s) goto err; | 1967 | if (s) goto err; |
1959 | s = bc_num_k(&cpa, &cpb, c); | 1968 | s = bc_num_k(&cpa, &cpb, c); |
1960 | if (s) goto err; | 1969 | if (s) goto err; |
@@ -2127,7 +2136,7 @@ static FAST_FUNC BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t | |||
2127 | neg = b->neg; | 2136 | neg = b->neg; |
2128 | b->neg = false; | 2137 | b->neg = false; |
2129 | 2138 | ||
2130 | s = bc_num_ulong(b, &pow); | 2139 | s = zbc_num_ulong(b, &pow); |
2131 | if (s) return s; | 2140 | if (s) return s; |
2132 | 2141 | ||
2133 | bc_num_init(©, a->len); | 2142 | bc_num_init(©, a->len); |
@@ -2289,7 +2298,7 @@ static void bc_num_printDecimal(BcNum *n) | |||
2289 | bc_num_printHex((size_t) n->num[i], 1, i == rdx); | 2298 | bc_num_printHex((size_t) n->num[i], 1, i == rdx); |
2290 | } | 2299 | } |
2291 | 2300 | ||
2292 | static BcStatus bc_num_printNum(BcNum *n, BcNum *base, size_t width, BcNumDigitOp print) | 2301 | static BC_STATUS zbc_num_printNum(BcNum *n, BcNum *base, size_t width, BcNumDigitOp print) |
2293 | { | 2302 | { |
2294 | BcStatus s; | 2303 | BcStatus s; |
2295 | BcVec stack; | 2304 | BcVec stack; |
@@ -2300,7 +2309,7 @@ static BcStatus bc_num_printNum(BcNum *n, BcNum *base, size_t width, BcNumDigitO | |||
2300 | 2309 | ||
2301 | if (n->len == 0) { | 2310 | if (n->len == 0) { |
2302 | print(0, width, false); | 2311 | print(0, width, false); |
2303 | return BC_STATUS_SUCCESS; | 2312 | RETURN_STATUS(BC_STATUS_SUCCESS); |
2304 | } | 2313 | } |
2305 | 2314 | ||
2306 | bc_vec_init(&stack, sizeof(long), NULL); | 2315 | bc_vec_init(&stack, sizeof(long), NULL); |
@@ -2318,7 +2327,7 @@ static BcStatus bc_num_printNum(BcNum *n, BcNum *base, size_t width, BcNumDigitO | |||
2318 | while (intp.len != 0) { | 2327 | while (intp.len != 0) { |
2319 | s = bc_num_divmod(&intp, base, &intp, &digit, 0); | 2328 | s = bc_num_divmod(&intp, base, &intp, &digit, 0); |
2320 | if (s) goto err; | 2329 | if (s) goto err; |
2321 | s = bc_num_ulong(&digit, &dig); | 2330 | s = zbc_num_ulong(&digit, &dig); |
2322 | if (s) goto err; | 2331 | if (s) goto err; |
2323 | bc_vec_push(&stack, &dig); | 2332 | bc_vec_push(&stack, &dig); |
2324 | } | 2333 | } |
@@ -2333,7 +2342,7 @@ static BcStatus bc_num_printNum(BcNum *n, BcNum *base, size_t width, BcNumDigitO | |||
2333 | for (radix = true; frac_len.len <= n->rdx; radix = false) { | 2342 | for (radix = true; frac_len.len <= n->rdx; radix = false) { |
2334 | s = bc_num_mul(&fracp, base, &fracp, n->rdx); | 2343 | s = bc_num_mul(&fracp, base, &fracp, n->rdx); |
2335 | if (s) goto err; | 2344 | if (s) goto err; |
2336 | s = bc_num_ulong(&fracp, &dig); | 2345 | s = zbc_num_ulong(&fracp, &dig); |
2337 | if (s) goto err; | 2346 | if (s) goto err; |
2338 | bc_num_ulong2num(&intp, dig); | 2347 | bc_num_ulong2num(&intp, dig); |
2339 | s = bc_num_sub(&fracp, &intp, &fracp, 0); | 2348 | s = bc_num_sub(&fracp, &intp, &fracp, 0); |
@@ -2349,13 +2358,13 @@ err: | |||
2349 | bc_num_free(&fracp); | 2358 | bc_num_free(&fracp); |
2350 | bc_num_free(&intp); | 2359 | bc_num_free(&intp); |
2351 | bc_vec_free(&stack); | 2360 | bc_vec_free(&stack); |
2352 | return s; | 2361 | RETURN_STATUS(s); |
2353 | } | 2362 | } |
2354 | #if ERRORS_ARE_FATAL | 2363 | #if ERRORS_ARE_FATAL |
2355 | # define bc_num_printNum(...) (bc_num_printNum(__VA_ARGS__), BC_STATUS_SUCCESS) | 2364 | # define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__), BC_STATUS_SUCCESS) |
2356 | #endif | 2365 | #endif |
2357 | 2366 | ||
2358 | static BcStatus bc_num_printBase(BcNum *n) | 2367 | static BC_STATUS zbc_num_printBase(BcNum *n) |
2359 | { | 2368 | { |
2360 | BcStatus s; | 2369 | BcStatus s; |
2361 | size_t width, i; | 2370 | size_t width, i; |
@@ -2379,22 +2388,22 @@ static BcStatus bc_num_printBase(BcNum *n) | |||
2379 | print = bc_num_printDigits; | 2388 | print = bc_num_printDigits; |
2380 | } | 2389 | } |
2381 | 2390 | ||
2382 | s = bc_num_printNum(n, &G.prog.ob, width, print); | 2391 | s = zbc_num_printNum(n, &G.prog.ob, width, print); |
2383 | n->neg = neg; | 2392 | n->neg = neg; |
2384 | 2393 | ||
2385 | return s; | 2394 | RETURN_STATUS(s); |
2386 | } | 2395 | } |
2387 | #if ERRORS_ARE_FATAL | 2396 | #if ERRORS_ARE_FATAL |
2388 | # define bc_num_printBase(...) (bc_num_printBase(__VA_ARGS__), BC_STATUS_SUCCESS) | 2397 | # define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__), BC_STATUS_SUCCESS) |
2389 | #endif | 2398 | #endif |
2390 | 2399 | ||
2391 | #if ENABLE_DC | 2400 | #if ENABLE_DC |
2392 | static BcStatus bc_num_stream(BcNum *n, BcNum *base) | 2401 | static BC_STATUS zbc_num_stream(BcNum *n, BcNum *base) |
2393 | { | 2402 | { |
2394 | return bc_num_printNum(n, base, 1, bc_num_printChar); | 2403 | RETURN_STATUS(zbc_num_printNum(n, base, 1, bc_num_printChar)); |
2395 | } | 2404 | } |
2396 | #if ERRORS_ARE_FATAL | 2405 | #if ERRORS_ARE_FATAL |
2397 | # define bc_num_stream(...) (bc_num_stream(__VA_ARGS__), BC_STATUS_SUCCESS) | 2406 | # define zbc_num_stream(...) (zbc_num_stream(__VA_ARGS__), BC_STATUS_SUCCESS) |
2398 | #endif | 2407 | #endif |
2399 | #endif | 2408 | #endif |
2400 | 2409 | ||
@@ -2528,11 +2537,11 @@ int_err: | |||
2528 | bc_num_free(&temp); | 2537 | bc_num_free(&temp); |
2529 | } | 2538 | } |
2530 | 2539 | ||
2531 | static BcStatus bc_num_parse(BcNum *n, const char *val, BcNum *base, | 2540 | static BC_STATUS zbc_num_parse(BcNum *n, const char *val, BcNum *base, |
2532 | size_t base_t) | 2541 | size_t base_t) |
2533 | { | 2542 | { |
2534 | if (!bc_num_strValid(val, base_t)) | 2543 | if (!bc_num_strValid(val, base_t)) |
2535 | return bc_error("bad number string"); | 2544 | RETURN_STATUS(bc_error("bad number string")); |
2536 | 2545 | ||
2537 | bc_num_zero(n); | 2546 | bc_num_zero(n); |
2538 | while (*val == '0') val++; | 2547 | while (*val == '0') val++; |
@@ -2542,13 +2551,13 @@ static BcStatus bc_num_parse(BcNum *n, const char *val, BcNum *base, | |||
2542 | else | 2551 | else |
2543 | bc_num_parseBase(n, val, base); | 2552 | bc_num_parseBase(n, val, base); |
2544 | 2553 | ||
2545 | return BC_STATUS_SUCCESS; | 2554 | RETURN_STATUS(BC_STATUS_SUCCESS); |
2546 | } | 2555 | } |
2547 | #if ERRORS_ARE_FATAL | 2556 | #if ERRORS_ARE_FATAL |
2548 | # define bc_num_parse(...) (bc_num_parse(__VA_ARGS__), BC_STATUS_SUCCESS) | 2557 | # define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__), BC_STATUS_SUCCESS) |
2549 | #endif | 2558 | #endif |
2550 | 2559 | ||
2551 | static BcStatus bc_num_print(BcNum *n, bool newline) | 2560 | static BC_STATUS zbc_num_print(BcNum *n, bool newline) |
2552 | { | 2561 | { |
2553 | BcStatus s = BC_STATUS_SUCCESS; | 2562 | BcStatus s = BC_STATUS_SUCCESS; |
2554 | 2563 | ||
@@ -2561,17 +2570,17 @@ static BcStatus bc_num_print(BcNum *n, bool newline) | |||
2561 | else if (G.prog.ob_t == 10) | 2570 | else if (G.prog.ob_t == 10) |
2562 | bc_num_printDecimal(n); | 2571 | bc_num_printDecimal(n); |
2563 | else | 2572 | else |
2564 | s = bc_num_printBase(n); | 2573 | s = zbc_num_printBase(n); |
2565 | 2574 | ||
2566 | if (newline) { | 2575 | if (newline) { |
2567 | bb_putchar('\n'); | 2576 | bb_putchar('\n'); |
2568 | G.prog.nchars = 0; | 2577 | G.prog.nchars = 0; |
2569 | } | 2578 | } |
2570 | 2579 | ||
2571 | return s; | 2580 | RETURN_STATUS(s); |
2572 | } | 2581 | } |
2573 | #if ERRORS_ARE_FATAL | 2582 | #if ERRORS_ARE_FATAL |
2574 | # define bc_num_print(...) (bc_num_print(__VA_ARGS__), BC_STATUS_SUCCESS) | 2583 | # define zbc_num_print(...) (zbc_num_print(__VA_ARGS__), BC_STATUS_SUCCESS) |
2575 | #endif | 2584 | #endif |
2576 | 2585 | ||
2577 | static FAST_FUNC BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) | 2586 | static FAST_FUNC BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
@@ -2794,14 +2803,14 @@ err: | |||
2794 | #endif // ENABLE_DC | 2803 | #endif // ENABLE_DC |
2795 | 2804 | ||
2796 | #if ENABLE_BC | 2805 | #if ENABLE_BC |
2797 | static BcStatus bc_func_insert(BcFunc *f, char *name, bool var) | 2806 | static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var) |
2798 | { | 2807 | { |
2799 | BcId a; | 2808 | BcId a; |
2800 | size_t i; | 2809 | size_t i; |
2801 | 2810 | ||
2802 | for (i = 0; i < f->autos.len; ++i) { | 2811 | for (i = 0; i < f->autos.len; ++i) { |
2803 | if (strcmp(name, ((BcId *) bc_vec_item(&f->autos, i))->name) == 0) | 2812 | if (strcmp(name, ((BcId *) bc_vec_item(&f->autos, i))->name) == 0) |
2804 | return bc_error("function parameter or auto var has the same name as another"); | 2813 | RETURN_STATUS(bc_error("function parameter or auto var has the same name as another")); |
2805 | } | 2814 | } |
2806 | 2815 | ||
2807 | a.idx = var; | 2816 | a.idx = var; |
@@ -2809,10 +2818,10 @@ static BcStatus bc_func_insert(BcFunc *f, char *name, bool var) | |||
2809 | 2818 | ||
2810 | bc_vec_push(&f->autos, &a); | 2819 | bc_vec_push(&f->autos, &a); |
2811 | 2820 | ||
2812 | return BC_STATUS_SUCCESS; | 2821 | RETURN_STATUS(BC_STATUS_SUCCESS); |
2813 | } | 2822 | } |
2814 | #if ERRORS_ARE_FATAL | 2823 | #if ERRORS_ARE_FATAL |
2815 | # define bc_func_insert(...) (bc_func_insert(__VA_ARGS__), BC_STATUS_SUCCESS) | 2824 | # define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__), BC_STATUS_SUCCESS) |
2816 | #endif | 2825 | #endif |
2817 | #endif | 2826 | #endif |
2818 | 2827 | ||
@@ -2963,7 +2972,7 @@ static void bc_lex_whitespace(BcLex *l) | |||
2963 | for (c = l->buf[l->i]; c != '\n' && isspace(c); c = l->buf[++l->i]); | 2972 | for (c = l->buf[l->i]; c != '\n' && isspace(c); c = l->buf[++l->i]); |
2964 | } | 2973 | } |
2965 | 2974 | ||
2966 | static BcStatus bc_lex_number(BcLex *l, char start) | 2975 | static BC_STATUS zbc_lex_number(BcLex *l, char start) |
2967 | { | 2976 | { |
2968 | const char *buf = l->buf + l->i; | 2977 | const char *buf = l->buf + l->i; |
2969 | size_t len, hits = 0, bslashes = 0, i = 0, j; | 2978 | size_t len, hits = 0, bslashes = 0, i = 0, j; |
@@ -2992,7 +3001,7 @@ static BcStatus bc_lex_number(BcLex *l, char start) | |||
2992 | // This check makes sense only if size_t is (much) larger than BC_MAX_NUM. | 3001 | // This check makes sense only if size_t is (much) larger than BC_MAX_NUM. |
2993 | if (SIZE_MAX > (BC_MAX_NUM | 0xff)) { | 3002 | if (SIZE_MAX > (BC_MAX_NUM | 0xff)) { |
2994 | if (len > BC_MAX_NUM) | 3003 | if (len > BC_MAX_NUM) |
2995 | return bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"); | 3004 | RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]")); |
2996 | } | 3005 | } |
2997 | 3006 | ||
2998 | bc_vec_pop_all(&l->t.v); | 3007 | bc_vec_pop_all(&l->t.v); |
@@ -3017,10 +3026,10 @@ static BcStatus bc_lex_number(BcLex *l, char start) | |||
3017 | bc_vec_pushZeroByte(&l->t.v); | 3026 | bc_vec_pushZeroByte(&l->t.v); |
3018 | l->i += i; | 3027 | l->i += i; |
3019 | 3028 | ||
3020 | return BC_STATUS_SUCCESS; | 3029 | RETURN_STATUS(BC_STATUS_SUCCESS); |
3021 | } | 3030 | } |
3022 | #if ERRORS_ARE_FATAL | 3031 | #if ERRORS_ARE_FATAL |
3023 | # define bc_lex_number(...) (bc_lex_number(__VA_ARGS__), BC_STATUS_SUCCESS) | 3032 | # define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__), BC_STATUS_SUCCESS) |
3024 | #endif | 3033 | #endif |
3025 | 3034 | ||
3026 | static void bc_lex_name(BcLex *l) | 3035 | static void bc_lex_name(BcLex *l) |
@@ -3184,7 +3193,7 @@ static void bc_lex_assign(BcLex *l, BcLexType with, BcLexType without) | |||
3184 | l->t.t = without; | 3193 | l->t.t = without; |
3185 | } | 3194 | } |
3186 | 3195 | ||
3187 | static BcStatus bc_lex_comment(BcLex *l) | 3196 | static BC_STATUS zbc_lex_comment(BcLex *l) |
3188 | { | 3197 | { |
3189 | size_t i, nls = 0; | 3198 | size_t i, nls = 0; |
3190 | const char *buf = l->buf; | 3199 | const char *buf = l->buf; |
@@ -3202,7 +3211,7 @@ static BcStatus bc_lex_comment(BcLex *l) | |||
3202 | } | 3211 | } |
3203 | if (c == '\0') { | 3212 | if (c == '\0') { |
3204 | l->i = i; | 3213 | l->i = i; |
3205 | return bc_error("comment end could not be found"); | 3214 | RETURN_STATUS(bc_error("comment end could not be found")); |
3206 | } | 3215 | } |
3207 | nls += (c == '\n'); | 3216 | nls += (c == '\n'); |
3208 | i++; | 3217 | i++; |
@@ -3212,10 +3221,10 @@ static BcStatus bc_lex_comment(BcLex *l) | |||
3212 | l->line += nls; | 3221 | l->line += nls; |
3213 | G.err_line = l->line; | 3222 | G.err_line = l->line; |
3214 | 3223 | ||
3215 | return BC_STATUS_SUCCESS; | 3224 | RETURN_STATUS(BC_STATUS_SUCCESS); |
3216 | } | 3225 | } |
3217 | #if ERRORS_ARE_FATAL | 3226 | #if ERRORS_ARE_FATAL |
3218 | # define bc_lex_comment(...) (bc_lex_comment(__VA_ARGS__), BC_STATUS_SUCCESS) | 3227 | # define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__), BC_STATUS_SUCCESS) |
3219 | #endif | 3228 | #endif |
3220 | 3229 | ||
3221 | static FAST_FUNC BcStatus bc_lex_token(BcLex *l) | 3230 | static FAST_FUNC BcStatus bc_lex_token(BcLex *l) |
@@ -3343,7 +3352,7 @@ static FAST_FUNC BcStatus bc_lex_token(BcLex *l) | |||
3343 | case '.': | 3352 | case '.': |
3344 | { | 3353 | { |
3345 | if (isdigit(l->buf[l->i])) | 3354 | if (isdigit(l->buf[l->i])) |
3346 | s = bc_lex_number(l, c); | 3355 | s = zbc_lex_number(l, c); |
3347 | else { | 3356 | else { |
3348 | l->t.t = BC_LEX_KEY_LAST; | 3357 | l->t.t = BC_LEX_KEY_LAST; |
3349 | s = bc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result"); | 3358 | s = bc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result"); |
@@ -3355,7 +3364,7 @@ static FAST_FUNC BcStatus bc_lex_token(BcLex *l) | |||
3355 | { | 3364 | { |
3356 | c2 = l->buf[l->i]; | 3365 | c2 = l->buf[l->i]; |
3357 | if (c2 == '*') | 3366 | if (c2 == '*') |
3358 | s = bc_lex_comment(l); | 3367 | s = zbc_lex_comment(l); |
3359 | else | 3368 | else |
3360 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE); | 3369 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE); |
3361 | break; | 3370 | break; |
@@ -3378,7 +3387,7 @@ static FAST_FUNC BcStatus bc_lex_token(BcLex *l) | |||
3378 | case 'E': | 3387 | case 'E': |
3379 | case 'F': | 3388 | case 'F': |
3380 | { | 3389 | { |
3381 | s = bc_lex_number(l, c); | 3390 | s = zbc_lex_number(l, c); |
3382 | break; | 3391 | break; |
3383 | } | 3392 | } |
3384 | 3393 | ||
@@ -3500,17 +3509,14 @@ static FAST_FUNC BcStatus bc_lex_token(BcLex *l) | |||
3500 | #endif // ENABLE_BC | 3509 | #endif // ENABLE_BC |
3501 | 3510 | ||
3502 | #if ENABLE_DC | 3511 | #if ENABLE_DC |
3503 | static BcStatus dc_lex_register(BcLex *l) | 3512 | static BC_STATUS zdc_lex_register(BcLex *l) |
3504 | { | 3513 | { |
3505 | BcStatus s = BC_STATUS_SUCCESS; | ||
3506 | |||
3507 | if (isspace(l->buf[l->i - 1])) { | 3514 | if (isspace(l->buf[l->i - 1])) { |
3508 | bc_lex_whitespace(l); | 3515 | bc_lex_whitespace(l); |
3509 | ++l->i; | 3516 | ++l->i; |
3510 | if (!G_exreg) | 3517 | if (!G_exreg) |
3511 | s = bc_error("extended register"); | 3518 | RETURN_STATUS(bc_error("extended register")); |
3512 | else | 3519 | bc_lex_name(l); |
3513 | bc_lex_name(l); | ||
3514 | } | 3520 | } |
3515 | else { | 3521 | else { |
3516 | bc_vec_pop_all(&l->t.v); | 3522 | bc_vec_pop_all(&l->t.v); |
@@ -3519,13 +3525,13 @@ static BcStatus dc_lex_register(BcLex *l) | |||
3519 | l->t.t = BC_LEX_NAME; | 3525 | l->t.t = BC_LEX_NAME; |
3520 | } | 3526 | } |
3521 | 3527 | ||
3522 | return s; | 3528 | RETURN_STATUS(BC_STATUS_SUCCESS); |
3523 | } | 3529 | } |
3524 | #if ERRORS_ARE_FATAL | 3530 | #if ERRORS_ARE_FATAL |
3525 | # define dc_lex_register(...) (dc_lex_register(__VA_ARGS__), BC_STATUS_SUCCESS) | 3531 | # define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__), BC_STATUS_SUCCESS) |
3526 | #endif | 3532 | #endif |
3527 | 3533 | ||
3528 | static BcStatus dc_lex_string(BcLex *l) | 3534 | static BC_STATUS zdc_lex_string(BcLex *l) |
3529 | { | 3535 | { |
3530 | size_t depth = 1, nls = 0, i = l->i; | 3536 | size_t depth = 1, nls = 0, i = l->i; |
3531 | char c; | 3537 | char c; |
@@ -3544,24 +3550,24 @@ static BcStatus dc_lex_string(BcLex *l) | |||
3544 | 3550 | ||
3545 | if (c == '\0') { | 3551 | if (c == '\0') { |
3546 | l->i = i; | 3552 | l->i = i; |
3547 | return bc_error("string end could not be found"); | 3553 | RETURN_STATUS(bc_error("string end could not be found")); |
3548 | } | 3554 | } |
3549 | 3555 | ||
3550 | bc_vec_pushZeroByte(&l->t.v); | 3556 | bc_vec_pushZeroByte(&l->t.v); |
3551 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. | 3557 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
3552 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { | 3558 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
3553 | if (i - l->i > BC_MAX_STRING) | 3559 | if (i - l->i > BC_MAX_STRING) |
3554 | return bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"); | 3560 | RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]")); |
3555 | } | 3561 | } |
3556 | 3562 | ||
3557 | l->i = i; | 3563 | l->i = i; |
3558 | l->line += nls; | 3564 | l->line += nls; |
3559 | G.err_line = l->line; | 3565 | G.err_line = l->line; |
3560 | 3566 | ||
3561 | return BC_STATUS_SUCCESS; | 3567 | RETURN_STATUS(BC_STATUS_SUCCESS); |
3562 | } | 3568 | } |
3563 | #if ERRORS_ARE_FATAL | 3569 | #if ERRORS_ARE_FATAL |
3564 | # define dc_lex_string(...) (dc_lex_string(__VA_ARGS__), BC_STATUS_SUCCESS) | 3570 | # define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__), BC_STATUS_SUCCESS) |
3565 | #endif | 3571 | #endif |
3566 | 3572 | ||
3567 | static FAST_FUNC BcStatus dc_lex_token(BcLex *l) | 3573 | static FAST_FUNC BcStatus dc_lex_token(BcLex *l) |
@@ -3572,7 +3578,7 @@ static FAST_FUNC BcStatus dc_lex_token(BcLex *l) | |||
3572 | 3578 | ||
3573 | for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) { | 3579 | for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) { |
3574 | if (l->t.last == dc_lex_regs[i]) | 3580 | if (l->t.last == dc_lex_regs[i]) |
3575 | return dc_lex_register(l); | 3581 | return zdc_lex_register(l); |
3576 | } | 3582 | } |
3577 | 3583 | ||
3578 | if (c >= '%' && c <= '~' && | 3584 | if (c >= '%' && c <= '~' && |
@@ -3628,7 +3634,7 @@ static FAST_FUNC BcStatus dc_lex_token(BcLex *l) | |||
3628 | case '.': | 3634 | case '.': |
3629 | { | 3635 | { |
3630 | if (isdigit(l->buf[l->i])) | 3636 | if (isdigit(l->buf[l->i])) |
3631 | s = bc_lex_number(l, c); | 3637 | s = zbc_lex_number(l, c); |
3632 | else | 3638 | else |
3633 | s = bc_error_bad_character(c); | 3639 | s = bc_error_bad_character(c); |
3634 | break; | 3640 | break; |
@@ -3651,13 +3657,13 @@ static FAST_FUNC BcStatus dc_lex_token(BcLex *l) | |||
3651 | case 'E': | 3657 | case 'E': |
3652 | case 'F': | 3658 | case 'F': |
3653 | { | 3659 | { |
3654 | s = bc_lex_number(l, c); | 3660 | s = zbc_lex_number(l, c); |
3655 | break; | 3661 | break; |
3656 | } | 3662 | } |
3657 | 3663 | ||
3658 | case '[': | 3664 | case '[': |
3659 | { | 3665 | { |
3660 | s = dc_lex_string(l); | 3666 | s = zdc_lex_string(l); |
3661 | break; | 3667 | break; |
3662 | } | 3668 | } |
3663 | 3669 | ||
@@ -4599,7 +4605,7 @@ static BcStatus bc_parse_func(BcParse *p) | |||
4599 | if (s) goto err; | 4605 | if (s) goto err; |
4600 | } | 4606 | } |
4601 | 4607 | ||
4602 | s = bc_func_insert(p->func, name, var); | 4608 | s = zbc_func_insert(p->func, name, var); |
4603 | if (s) goto err; | 4609 | if (s) goto err; |
4604 | } | 4610 | } |
4605 | 4611 | ||
@@ -4661,7 +4667,7 @@ static BcStatus bc_parse_auto(BcParse *p) | |||
4661 | if (s) goto err; | 4667 | if (s) goto err; |
4662 | } | 4668 | } |
4663 | 4669 | ||
4664 | s = bc_func_insert(p->func, name, var); | 4670 | s = zbc_func_insert(p->func, name, var); |
4665 | if (s) goto err; | 4671 | if (s) goto err; |
4666 | } | 4672 | } |
4667 | 4673 | ||
@@ -5453,7 +5459,7 @@ static BcVec* bc_program_search(char *id, bool var) | |||
5453 | return bc_vec_item(v, ptr->idx); | 5459 | return bc_vec_item(v, ptr->idx); |
5454 | } | 5460 | } |
5455 | 5461 | ||
5456 | static BcStatus bc_program_num(BcResult *r, BcNum **num, bool hex) | 5462 | static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex) |
5457 | { | 5463 | { |
5458 | switch (r->t) { | 5464 | switch (r->t) { |
5459 | 5465 | ||
@@ -5479,11 +5485,11 @@ static BcStatus bc_program_num(BcResult *r, BcNum **num, bool hex) | |||
5479 | hex = hex && len == 1; | 5485 | hex = hex && len == 1; |
5480 | base = hex ? &G.prog.hexb : &G.prog.ib; | 5486 | base = hex ? &G.prog.hexb : &G.prog.ib; |
5481 | base_t = hex ? BC_NUM_MAX_IBASE : G.prog.ib_t; | 5487 | base_t = hex ? BC_NUM_MAX_IBASE : G.prog.ib_t; |
5482 | s = bc_num_parse(&r->d.n, *str, base, base_t); | 5488 | s = zbc_num_parse(&r->d.n, *str, base, base_t); |
5483 | 5489 | ||
5484 | if (s) { | 5490 | if (s) { |
5485 | bc_num_free(&r->d.n); | 5491 | bc_num_free(&r->d.n); |
5486 | return s; | 5492 | RETURN_STATUS(s); |
5487 | } | 5493 | } |
5488 | 5494 | ||
5489 | *num = &r->d.n; | 5495 | *num = &r->d.n; |
@@ -5524,13 +5530,13 @@ static BcStatus bc_program_num(BcResult *r, BcNum **num, bool hex) | |||
5524 | } | 5530 | } |
5525 | } | 5531 | } |
5526 | 5532 | ||
5527 | return BC_STATUS_SUCCESS; | 5533 | RETURN_STATUS(BC_STATUS_SUCCESS); |
5528 | } | 5534 | } |
5529 | #if ERRORS_ARE_FATAL | 5535 | #if ERRORS_ARE_FATAL |
5530 | # define bc_program_num(...) (bc_program_num(__VA_ARGS__), BC_STATUS_SUCCESS) | 5536 | # define zbc_program_num(...) (zbc_program_num(__VA_ARGS__), BC_STATUS_SUCCESS) |
5531 | #endif | 5537 | #endif |
5532 | 5538 | ||
5533 | static BcStatus bc_program_binOpPrep(BcResult **l, BcNum **ln, | 5539 | static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln, |
5534 | BcResult **r, BcNum **rn, bool assign) | 5540 | BcResult **r, BcNum **rn, bool assign) |
5535 | { | 5541 | { |
5536 | BcStatus s; | 5542 | BcStatus s; |
@@ -5538,7 +5544,7 @@ static BcStatus bc_program_binOpPrep(BcResult **l, BcNum **ln, | |||
5538 | BcResultType lt, rt; | 5544 | BcResultType lt, rt; |
5539 | 5545 | ||
5540 | if (!BC_PROG_STACK(&G.prog.results, 2)) | 5546 | if (!BC_PROG_STACK(&G.prog.results, 2)) |
5541 | return bc_error_stack_has_too_few_elements(); | 5547 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
5542 | 5548 | ||
5543 | *r = bc_vec_item_rev(&G.prog.results, 0); | 5549 | *r = bc_vec_item_rev(&G.prog.results, 0); |
5544 | *l = bc_vec_item_rev(&G.prog.results, 1); | 5550 | *l = bc_vec_item_rev(&G.prog.results, 1); |
@@ -5547,27 +5553,27 @@ static BcStatus bc_program_binOpPrep(BcResult **l, BcNum **ln, | |||
5547 | rt = (*r)->t; | 5553 | rt = (*r)->t; |
5548 | hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE); | 5554 | hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE); |
5549 | 5555 | ||
5550 | s = bc_program_num(*l, ln, false); | 5556 | s = zbc_program_num(*l, ln, false); |
5551 | if (s) return s; | 5557 | if (s) RETURN_STATUS(s); |
5552 | s = bc_program_num(*r, rn, hex); | 5558 | s = zbc_program_num(*r, rn, hex); |
5553 | if (s) return s; | 5559 | if (s) RETURN_STATUS(s); |
5554 | 5560 | ||
5555 | // We run this again under these conditions in case any vector has been | 5561 | // We run this again under these conditions in case any vector has been |
5556 | // reallocated out from under the BcNums or arrays we had. | 5562 | // reallocated out from under the BcNums or arrays we had. |
5557 | if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) { | 5563 | if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) { |
5558 | s = bc_program_num(*l, ln, false); | 5564 | s = zbc_program_num(*l, ln, false); |
5559 | if (s) return s; | 5565 | if (s) RETURN_STATUS(s); |
5560 | } | 5566 | } |
5561 | 5567 | ||
5562 | if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR)) | 5568 | if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR)) |
5563 | return bc_error_variable_is_wrong_type(); | 5569 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
5564 | if (!assign && !BC_PROG_NUM((*r), (*ln))) | 5570 | if (!assign && !BC_PROG_NUM((*r), (*ln))) |
5565 | return bc_error_variable_is_wrong_type(); | 5571 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
5566 | 5572 | ||
5567 | return s; | 5573 | RETURN_STATUS(s); |
5568 | } | 5574 | } |
5569 | #if ERRORS_ARE_FATAL | 5575 | #if ERRORS_ARE_FATAL |
5570 | # define bc_program_binOpPrep(...) (bc_program_binOpPrep(__VA_ARGS__), BC_STATUS_SUCCESS) | 5576 | # define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__), BC_STATUS_SUCCESS) |
5571 | #endif | 5577 | #endif |
5572 | 5578 | ||
5573 | static void bc_program_binOpRetire(BcResult *r) | 5579 | static void bc_program_binOpRetire(BcResult *r) |
@@ -5578,24 +5584,24 @@ static void bc_program_binOpRetire(BcResult *r) | |||
5578 | bc_vec_push(&G.prog.results, r); | 5584 | bc_vec_push(&G.prog.results, r); |
5579 | } | 5585 | } |
5580 | 5586 | ||
5581 | static BcStatus bc_program_prep(BcResult **r, BcNum **n) | 5587 | static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n) |
5582 | { | 5588 | { |
5583 | BcStatus s; | 5589 | BcStatus s; |
5584 | 5590 | ||
5585 | if (!BC_PROG_STACK(&G.prog.results, 1)) | 5591 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
5586 | return bc_error_stack_has_too_few_elements(); | 5592 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
5587 | *r = bc_vec_top(&G.prog.results); | 5593 | *r = bc_vec_top(&G.prog.results); |
5588 | 5594 | ||
5589 | s = bc_program_num(*r, n, false); | 5595 | s = zbc_program_num(*r, n, false); |
5590 | if (s) return s; | 5596 | if (s) RETURN_STATUS(s); |
5591 | 5597 | ||
5592 | if (!BC_PROG_NUM((*r), (*n))) | 5598 | if (!BC_PROG_NUM((*r), (*n))) |
5593 | return bc_error_variable_is_wrong_type(); | 5599 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
5594 | 5600 | ||
5595 | return s; | 5601 | RETURN_STATUS(s); |
5596 | } | 5602 | } |
5597 | #if ERRORS_ARE_FATAL | 5603 | #if ERRORS_ARE_FATAL |
5598 | # define bc_program_prep(...) (bc_program_prep(__VA_ARGS__), BC_STATUS_SUCCESS) | 5604 | # define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__), BC_STATUS_SUCCESS) |
5599 | #endif | 5605 | #endif |
5600 | 5606 | ||
5601 | static void bc_program_retire(BcResult *r, BcResultType t) | 5607 | static void bc_program_retire(BcResult *r, BcResultType t) |
@@ -5611,7 +5617,7 @@ static BcStatus bc_program_op(char inst) | |||
5611 | BcResult *opd1, *opd2, res; | 5617 | BcResult *opd1, *opd2, res; |
5612 | BcNum *n1, *n2 = NULL; | 5618 | BcNum *n1, *n2 = NULL; |
5613 | 5619 | ||
5614 | s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); | 5620 | s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
5615 | if (s) return s; | 5621 | if (s) return s; |
5616 | bc_num_init_DEF_SIZE(&res.d.n); | 5622 | bc_num_init_DEF_SIZE(&res.d.n); |
5617 | 5623 | ||
@@ -5795,7 +5801,7 @@ static void bc_program_printString(const char *str) | |||
5795 | } | 5801 | } |
5796 | } | 5802 | } |
5797 | 5803 | ||
5798 | static BcStatus bc_program_print(char inst, size_t idx) | 5804 | static BC_STATUS zbc_program_print(char inst, size_t idx) |
5799 | { | 5805 | { |
5800 | BcStatus s; | 5806 | BcStatus s; |
5801 | BcResult *r; | 5807 | BcResult *r; |
@@ -5803,15 +5809,15 @@ static BcStatus bc_program_print(char inst, size_t idx) | |||
5803 | bool pop = inst != BC_INST_PRINT; | 5809 | bool pop = inst != BC_INST_PRINT; |
5804 | 5810 | ||
5805 | if (!BC_PROG_STACK(&G.prog.results, idx + 1)) | 5811 | if (!BC_PROG_STACK(&G.prog.results, idx + 1)) |
5806 | return bc_error_stack_has_too_few_elements(); | 5812 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
5807 | 5813 | ||
5808 | r = bc_vec_item_rev(&G.prog.results, idx); | 5814 | r = bc_vec_item_rev(&G.prog.results, idx); |
5809 | num = NULL; // is this NULL necessary? | 5815 | num = NULL; // is this NULL necessary? |
5810 | s = bc_program_num(r, &num, false); | 5816 | s = zbc_program_num(r, &num, false); |
5811 | if (s) return s; | 5817 | if (s) RETURN_STATUS(s); |
5812 | 5818 | ||
5813 | if (BC_PROG_NUM(r, num)) { | 5819 | if (BC_PROG_NUM(r, num)) { |
5814 | s = bc_num_print(num, !pop); | 5820 | s = zbc_num_print(num, !pop); |
5815 | if (!s) bc_num_copy(&G.prog.last, num); | 5821 | if (!s) bc_num_copy(&G.prog.last, num); |
5816 | } | 5822 | } |
5817 | else { | 5823 | else { |
@@ -5837,20 +5843,20 @@ static BcStatus bc_program_print(char inst, size_t idx) | |||
5837 | 5843 | ||
5838 | if (!s && pop) bc_vec_pop(&G.prog.results); | 5844 | if (!s && pop) bc_vec_pop(&G.prog.results); |
5839 | 5845 | ||
5840 | return s; | 5846 | RETURN_STATUS(s); |
5841 | } | 5847 | } |
5842 | #if ERRORS_ARE_FATAL | 5848 | #if ERRORS_ARE_FATAL |
5843 | # define bc_program_print(...) (bc_program_print(__VA_ARGS__), BC_STATUS_SUCCESS) | 5849 | # define zbc_program_print(...) (zbc_program_print(__VA_ARGS__), BC_STATUS_SUCCESS) |
5844 | #endif | 5850 | #endif |
5845 | 5851 | ||
5846 | static BcStatus bc_program_negate(void) | 5852 | static BC_STATUS zbc_program_negate(void) |
5847 | { | 5853 | { |
5848 | BcStatus s; | 5854 | BcStatus s; |
5849 | BcResult res, *ptr; | 5855 | BcResult res, *ptr; |
5850 | BcNum *num = NULL; | 5856 | BcNum *num = NULL; |
5851 | 5857 | ||
5852 | s = bc_program_prep(&ptr, &num); | 5858 | s = zbc_program_prep(&ptr, &num); |
5853 | if (s) return s; | 5859 | if (s) RETURN_STATUS(s); |
5854 | 5860 | ||
5855 | bc_num_init(&res.d.n, num->len); | 5861 | bc_num_init(&res.d.n, num->len); |
5856 | bc_num_copy(&res.d.n, num); | 5862 | bc_num_copy(&res.d.n, num); |
@@ -5858,10 +5864,10 @@ static BcStatus bc_program_negate(void) | |||
5858 | 5864 | ||
5859 | bc_program_retire(&res, BC_RESULT_TEMP); | 5865 | bc_program_retire(&res, BC_RESULT_TEMP); |
5860 | 5866 | ||
5861 | return s; | 5867 | RETURN_STATUS(s); |
5862 | } | 5868 | } |
5863 | #if ERRORS_ARE_FATAL | 5869 | #if ERRORS_ARE_FATAL |
5864 | # define bc_program_negate(...) (bc_program_negate(__VA_ARGS__), BC_STATUS_SUCCESS) | 5870 | # define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__), BC_STATUS_SUCCESS) |
5865 | #endif | 5871 | #endif |
5866 | 5872 | ||
5867 | static BcStatus bc_program_logical(char inst) | 5873 | static BcStatus bc_program_logical(char inst) |
@@ -5872,7 +5878,7 @@ static BcStatus bc_program_logical(char inst) | |||
5872 | bool cond = 0; | 5878 | bool cond = 0; |
5873 | ssize_t cmp; | 5879 | ssize_t cmp; |
5874 | 5880 | ||
5875 | s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); | 5881 | s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
5876 | if (s) return s; | 5882 | if (s) return s; |
5877 | bc_num_init_DEF_SIZE(&res.d.n); | 5883 | bc_num_init_DEF_SIZE(&res.d.n); |
5878 | 5884 | ||
@@ -5932,7 +5938,7 @@ static BcStatus bc_program_logical(char inst) | |||
5932 | } | 5938 | } |
5933 | 5939 | ||
5934 | #if ENABLE_DC | 5940 | #if ENABLE_DC |
5935 | static BcStatus bc_program_assignStr(BcResult *r, BcVec *v, | 5941 | static BC_STATUS zbc_program_assignStr(BcResult *r, BcVec *v, |
5936 | bool push) | 5942 | bool push) |
5937 | { | 5943 | { |
5938 | BcNum n2; | 5944 | BcNum n2; |
@@ -5944,7 +5950,7 @@ static BcStatus bc_program_assignStr(BcResult *r, BcVec *v, | |||
5944 | 5950 | ||
5945 | if (!push) { | 5951 | if (!push) { |
5946 | if (!BC_PROG_STACK(&G.prog.results, 2)) | 5952 | if (!BC_PROG_STACK(&G.prog.results, 2)) |
5947 | return bc_error_stack_has_too_few_elements(); | 5953 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
5948 | bc_vec_pop(v); | 5954 | bc_vec_pop(v); |
5949 | bc_vec_pop(&G.prog.results); | 5955 | bc_vec_pop(&G.prog.results); |
5950 | } | 5956 | } |
@@ -5954,14 +5960,14 @@ static BcStatus bc_program_assignStr(BcResult *r, BcVec *v, | |||
5954 | bc_vec_push(&G.prog.results, &res); | 5960 | bc_vec_push(&G.prog.results, &res); |
5955 | bc_vec_push(v, &n2); | 5961 | bc_vec_push(v, &n2); |
5956 | 5962 | ||
5957 | return BC_STATUS_SUCCESS; | 5963 | RETURN_STATUS(BC_STATUS_SUCCESS); |
5958 | } | 5964 | } |
5959 | #if ERRORS_ARE_FATAL | 5965 | #if ERRORS_ARE_FATAL |
5960 | # define bc_program_assignStr(...) (bc_program_assignStr(__VA_ARGS__), BC_STATUS_SUCCESS) | 5966 | # define zbc_program_assignStr(...) (zbc_program_assignStr(__VA_ARGS__), BC_STATUS_SUCCESS) |
5961 | #endif | 5967 | #endif |
5962 | #endif // ENABLE_DC | 5968 | #endif // ENABLE_DC |
5963 | 5969 | ||
5964 | static BcStatus bc_program_copyToVar(char *name, bool var) | 5970 | static BC_STATUS zbc_program_copyToVar(char *name, bool var) |
5965 | { | 5971 | { |
5966 | BcStatus s; | 5972 | BcStatus s; |
5967 | BcResult *ptr, r; | 5973 | BcResult *ptr, r; |
@@ -5969,21 +5975,22 @@ static BcStatus bc_program_copyToVar(char *name, bool var) | |||
5969 | BcNum *n; | 5975 | BcNum *n; |
5970 | 5976 | ||
5971 | if (!BC_PROG_STACK(&G.prog.results, 1)) | 5977 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
5972 | return bc_error_stack_has_too_few_elements(); | 5978 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
5973 | 5979 | ||
5974 | ptr = bc_vec_top(&G.prog.results); | 5980 | ptr = bc_vec_top(&G.prog.results); |
5975 | if ((ptr->t == BC_RESULT_ARRAY) != !var) | 5981 | if ((ptr->t == BC_RESULT_ARRAY) != !var) |
5976 | return bc_error_variable_is_wrong_type(); | 5982 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
5977 | v = bc_program_search(name, var); | 5983 | v = bc_program_search(name, var); |
5978 | 5984 | ||
5979 | #if ENABLE_DC | 5985 | #if ENABLE_DC |
5980 | if (ptr->t == BC_RESULT_STR && !var) | 5986 | if (ptr->t == BC_RESULT_STR && !var) |
5981 | return bc_error_variable_is_wrong_type(); | 5987 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
5982 | if (ptr->t == BC_RESULT_STR) return bc_program_assignStr(ptr, v, true); | 5988 | if (ptr->t == BC_RESULT_STR) |
5989 | RETURN_STATUS(zbc_program_assignStr(ptr, v, true)); | ||
5983 | #endif | 5990 | #endif |
5984 | 5991 | ||
5985 | s = bc_program_num(ptr, &n, false); | 5992 | s = zbc_program_num(ptr, &n, false); |
5986 | if (s) return s; | 5993 | if (s) RETURN_STATUS(s); |
5987 | 5994 | ||
5988 | // Do this once more to make sure that pointers were not invalidated. | 5995 | // Do this once more to make sure that pointers were not invalidated. |
5989 | v = bc_program_search(name, var); | 5996 | v = bc_program_search(name, var); |
@@ -6000,10 +6007,10 @@ static BcStatus bc_program_copyToVar(char *name, bool var) | |||
6000 | bc_vec_push(v, &r.d); | 6007 | bc_vec_push(v, &r.d); |
6001 | bc_vec_pop(&G.prog.results); | 6008 | bc_vec_pop(&G.prog.results); |
6002 | 6009 | ||
6003 | return s; | 6010 | RETURN_STATUS(s); |
6004 | } | 6011 | } |
6005 | #if ERRORS_ARE_FATAL | 6012 | #if ERRORS_ARE_FATAL |
6006 | # define bc_program_copyToVar(...) (bc_program_copyToVar(__VA_ARGS__), BC_STATUS_SUCCESS) | 6013 | # define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__), BC_STATUS_SUCCESS) |
6007 | #endif | 6014 | #endif |
6008 | 6015 | ||
6009 | static BcStatus bc_program_assign(char inst) | 6016 | static BcStatus bc_program_assign(char inst) |
@@ -6013,7 +6020,7 @@ static BcStatus bc_program_assign(char inst) | |||
6013 | BcNum *l = NULL, *r = NULL; | 6020 | BcNum *l = NULL, *r = NULL; |
6014 | bool assign = inst == BC_INST_ASSIGN, ib, sc; | 6021 | bool assign = inst == BC_INST_ASSIGN, ib, sc; |
6015 | 6022 | ||
6016 | s = bc_program_binOpPrep(&left, &l, &right, &r, assign); | 6023 | s = zbc_program_binOpPrep(&left, &l, &right, &r, assign); |
6017 | if (s) return s; | 6024 | if (s) return s; |
6018 | 6025 | ||
6019 | ib = left->t == BC_RESULT_IBASE; | 6026 | ib = left->t == BC_RESULT_IBASE; |
@@ -6029,7 +6036,7 @@ static BcStatus bc_program_assign(char inst) | |||
6029 | return bc_error_variable_is_wrong_type(); | 6036 | return bc_error_variable_is_wrong_type(); |
6030 | v = bc_program_search(left->d.id.name, true); | 6037 | v = bc_program_search(left->d.id.name, true); |
6031 | 6038 | ||
6032 | return bc_program_assignStr(right, v, false); | 6039 | return zbc_program_assignStr(right, v, false); |
6033 | } | 6040 | } |
6034 | #endif | 6041 | #endif |
6035 | 6042 | ||
@@ -6066,7 +6073,7 @@ static BcStatus bc_program_assign(char inst) | |||
6066 | size_t *ptr; | 6073 | size_t *ptr; |
6067 | unsigned long val, max; | 6074 | unsigned long val, max; |
6068 | 6075 | ||
6069 | s = bc_num_ulong(l, &val); | 6076 | s = zbc_num_ulong(l, &val); |
6070 | if (s) | 6077 | if (s) |
6071 | return s; | 6078 | return s; |
6072 | s = left->t - BC_RESULT_IBASE; | 6079 | s = left->t - BC_RESULT_IBASE; |
@@ -6149,7 +6156,7 @@ static BcStatus bc_program_pushVar(char *code, size_t *bgn, | |||
6149 | return s; | 6156 | return s; |
6150 | } | 6157 | } |
6151 | 6158 | ||
6152 | static BcStatus bc_program_pushArray(char *code, size_t *bgn, | 6159 | static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, |
6153 | char inst) | 6160 | char inst) |
6154 | { | 6161 | { |
6155 | BcStatus s = BC_STATUS_SUCCESS; | 6162 | BcStatus s = BC_STATUS_SUCCESS; |
@@ -6167,9 +6174,9 @@ static BcStatus bc_program_pushArray(char *code, size_t *bgn, | |||
6167 | BcResult *operand; | 6174 | BcResult *operand; |
6168 | unsigned long temp; | 6175 | unsigned long temp; |
6169 | 6176 | ||
6170 | s = bc_program_prep(&operand, &num); | 6177 | s = zbc_program_prep(&operand, &num); |
6171 | if (s) goto err; | 6178 | if (s) goto err; |
6172 | s = bc_num_ulong(num, &temp); | 6179 | s = zbc_num_ulong(num, &temp); |
6173 | if (s) goto err; | 6180 | if (s) goto err; |
6174 | 6181 | ||
6175 | if (temp > BC_MAX_DIM) { | 6182 | if (temp > BC_MAX_DIM) { |
@@ -6183,22 +6190,22 @@ static BcStatus bc_program_pushArray(char *code, size_t *bgn, | |||
6183 | 6190 | ||
6184 | err: | 6191 | err: |
6185 | if (s) free(r.d.id.name); | 6192 | if (s) free(r.d.id.name); |
6186 | return s; | 6193 | RETURN_STATUS(s); |
6187 | } | 6194 | } |
6188 | #if ERRORS_ARE_FATAL | 6195 | #if ERRORS_ARE_FATAL |
6189 | # define bc_program_pushArray(...) (bc_program_pushArray(__VA_ARGS__), BC_STATUS_SUCCESS) | 6196 | # define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__), BC_STATUS_SUCCESS) |
6190 | #endif | 6197 | #endif |
6191 | 6198 | ||
6192 | #if ENABLE_BC | 6199 | #if ENABLE_BC |
6193 | static BcStatus bc_program_incdec(char inst) | 6200 | static BC_STATUS zbc_program_incdec(char inst) |
6194 | { | 6201 | { |
6195 | BcStatus s; | 6202 | BcStatus s; |
6196 | BcResult *ptr, res, copy; | 6203 | BcResult *ptr, res, copy; |
6197 | BcNum *num = NULL; | 6204 | BcNum *num = NULL; |
6198 | char inst2 = inst; | 6205 | char inst2 = inst; |
6199 | 6206 | ||
6200 | s = bc_program_prep(&ptr, &num); | 6207 | s = zbc_program_prep(&ptr, &num); |
6201 | if (s) return s; | 6208 | if (s) RETURN_STATUS(s); |
6202 | 6209 | ||
6203 | if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) { | 6210 | if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) { |
6204 | copy.t = BC_RESULT_TEMP; | 6211 | copy.t = BC_RESULT_TEMP; |
@@ -6219,13 +6226,13 @@ static BcStatus bc_program_incdec(char inst) | |||
6219 | bc_vec_push(&G.prog.results, ©); | 6226 | bc_vec_push(&G.prog.results, ©); |
6220 | } | 6227 | } |
6221 | 6228 | ||
6222 | return s; | 6229 | RETURN_STATUS(s); |
6223 | } | 6230 | } |
6224 | #if ERRORS_ARE_FATAL | 6231 | #if ERRORS_ARE_FATAL |
6225 | # define bc_program_incdec(...) (bc_program_incdec(__VA_ARGS__), BC_STATUS_SUCCESS) | 6232 | # define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__), BC_STATUS_SUCCESS) |
6226 | #endif | 6233 | #endif |
6227 | 6234 | ||
6228 | static BcStatus bc_program_call(char *code, size_t *idx) | 6235 | static BC_STATUS zbc_program_call(char *code, size_t *idx) |
6229 | { | 6236 | { |
6230 | BcInstPtr ip; | 6237 | BcInstPtr ip; |
6231 | size_t i, nparams = bc_program_index(code, idx); | 6238 | size_t i, nparams = bc_program_index(code, idx); |
@@ -6239,10 +6246,10 @@ static BcStatus bc_program_call(char *code, size_t *idx) | |||
6239 | func = bc_program_func(ip.func); | 6246 | func = bc_program_func(ip.func); |
6240 | 6247 | ||
6241 | if (func->code.len == 0) { | 6248 | if (func->code.len == 0) { |
6242 | return bc_error("undefined function"); | 6249 | RETURN_STATUS(bc_error("undefined function")); |
6243 | } | 6250 | } |
6244 | if (nparams != func->nparams) { | 6251 | if (nparams != func->nparams) { |
6245 | return bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams); | 6252 | RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams)); |
6246 | } | 6253 | } |
6247 | ip.len = G.prog.results.len - nparams; | 6254 | ip.len = G.prog.results.len - nparams; |
6248 | 6255 | ||
@@ -6253,10 +6260,10 @@ static BcStatus bc_program_call(char *code, size_t *idx) | |||
6253 | arg = bc_vec_top(&G.prog.results); | 6260 | arg = bc_vec_top(&G.prog.results); |
6254 | 6261 | ||
6255 | if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR) | 6262 | if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR) |
6256 | return bc_error_variable_is_wrong_type(); | 6263 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
6257 | 6264 | ||
6258 | s = bc_program_copyToVar(a->name, a->idx); | 6265 | s = zbc_program_copyToVar(a->name, a->idx); |
6259 | if (s) return s; | 6266 | if (s) RETURN_STATUS(s); |
6260 | } | 6267 | } |
6261 | 6268 | ||
6262 | for (; i < func->autos.len; ++i) { | 6269 | for (; i < func->autos.len; ++i) { |
@@ -6277,13 +6284,13 @@ static BcStatus bc_program_call(char *code, size_t *idx) | |||
6277 | 6284 | ||
6278 | bc_vec_push(&G.prog.stack, &ip); | 6285 | bc_vec_push(&G.prog.stack, &ip); |
6279 | 6286 | ||
6280 | return BC_STATUS_SUCCESS; | 6287 | RETURN_STATUS(BC_STATUS_SUCCESS); |
6281 | } | 6288 | } |
6282 | #if ERRORS_ARE_FATAL | 6289 | #if ERRORS_ARE_FATAL |
6283 | # define bc_program_call(...) (bc_program_call(__VA_ARGS__), BC_STATUS_SUCCESS) | 6290 | # define zbc_program_call(...) (zbc_program_call(__VA_ARGS__), BC_STATUS_SUCCESS) |
6284 | #endif | 6291 | #endif |
6285 | 6292 | ||
6286 | static BcStatus bc_program_return(char inst) | 6293 | static BC_STATUS zbc_program_return(char inst) |
6287 | { | 6294 | { |
6288 | BcResult res; | 6295 | BcResult res; |
6289 | BcFunc *f; | 6296 | BcFunc *f; |
@@ -6291,7 +6298,7 @@ static BcStatus bc_program_return(char inst) | |||
6291 | BcInstPtr *ip = bc_vec_top(&G.prog.stack); | 6298 | BcInstPtr *ip = bc_vec_top(&G.prog.stack); |
6292 | 6299 | ||
6293 | if (!BC_PROG_STACK(&G.prog.results, ip->len + inst == BC_INST_RET)) | 6300 | if (!BC_PROG_STACK(&G.prog.results, ip->len + inst == BC_INST_RET)) |
6294 | return bc_error_stack_has_too_few_elements(); | 6301 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
6295 | 6302 | ||
6296 | f = bc_program_func(ip->func); | 6303 | f = bc_program_func(ip->func); |
6297 | res.t = BC_RESULT_TEMP; | 6304 | res.t = BC_RESULT_TEMP; |
@@ -6301,8 +6308,8 @@ static BcStatus bc_program_return(char inst) | |||
6301 | BcNum *num; | 6308 | BcNum *num; |
6302 | BcResult *operand = bc_vec_top(&G.prog.results); | 6309 | BcResult *operand = bc_vec_top(&G.prog.results); |
6303 | 6310 | ||
6304 | s = bc_program_num(operand, &num, false); | 6311 | s = zbc_program_num(operand, &num, false); |
6305 | if (s) return s; | 6312 | if (s) RETURN_STATUS(s); |
6306 | bc_num_init(&res.d.n, num->len); | 6313 | bc_num_init(&res.d.n, num->len); |
6307 | bc_num_copy(&res.d.n, num); | 6314 | bc_num_copy(&res.d.n, num); |
6308 | } | 6315 | } |
@@ -6324,10 +6331,10 @@ static BcStatus bc_program_return(char inst) | |||
6324 | bc_vec_push(&G.prog.results, &res); | 6331 | bc_vec_push(&G.prog.results, &res); |
6325 | bc_vec_pop(&G.prog.stack); | 6332 | bc_vec_pop(&G.prog.stack); |
6326 | 6333 | ||
6327 | return BC_STATUS_SUCCESS; | 6334 | RETURN_STATUS(BC_STATUS_SUCCESS); |
6328 | } | 6335 | } |
6329 | #if ERRORS_ARE_FATAL | 6336 | #if ERRORS_ARE_FATAL |
6330 | # define bc_program_return(...) (bc_program_return(__VA_ARGS__), BC_STATUS_SUCCESS) | 6337 | # define zbc_program_return(...) (zbc_program_return(__VA_ARGS__), BC_STATUS_SUCCESS) |
6331 | #endif | 6338 | #endif |
6332 | #endif // ENABLE_BC | 6339 | #endif // ENABLE_BC |
6333 | 6340 | ||
@@ -6361,7 +6368,7 @@ static BcStatus bc_program_builtin(char inst) | |||
6361 | return bc_error_stack_has_too_few_elements(); | 6368 | return bc_error_stack_has_too_few_elements(); |
6362 | opnd = bc_vec_top(&G.prog.results); | 6369 | opnd = bc_vec_top(&G.prog.results); |
6363 | 6370 | ||
6364 | s = bc_program_num(opnd, &num, false); | 6371 | s = zbc_program_num(opnd, &num, false); |
6365 | if (s) return s; | 6372 | if (s) return s; |
6366 | 6373 | ||
6367 | #if ENABLE_DC | 6374 | #if ENABLE_DC |
@@ -6403,7 +6410,7 @@ static BcStatus bc_program_divmod(void) | |||
6403 | BcResult *opd1, *opd2, res, res2; | 6410 | BcResult *opd1, *opd2, res, res2; |
6404 | BcNum *n1, *n2 = NULL; | 6411 | BcNum *n1, *n2 = NULL; |
6405 | 6412 | ||
6406 | s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); | 6413 | s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
6407 | if (s) return s; | 6414 | if (s) return s; |
6408 | 6415 | ||
6409 | bc_num_init_DEF_SIZE(&res.d.n); | 6416 | bc_num_init_DEF_SIZE(&res.d.n); |
@@ -6432,11 +6439,11 @@ static BcStatus bc_program_modexp(void) | |||
6432 | 6439 | ||
6433 | if (!BC_PROG_STACK(&G.prog.results, 3)) | 6440 | if (!BC_PROG_STACK(&G.prog.results, 3)) |
6434 | return bc_error_stack_has_too_few_elements(); | 6441 | return bc_error_stack_has_too_few_elements(); |
6435 | s = bc_program_binOpPrep(&r2, &n2, &r3, &n3, false); | 6442 | s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false); |
6436 | if (s) return s; | 6443 | if (s) return s; |
6437 | 6444 | ||
6438 | r1 = bc_vec_item_rev(&G.prog.results, 2); | 6445 | r1 = bc_vec_item_rev(&G.prog.results, 2); |
6439 | s = bc_program_num(r1, &n1, false); | 6446 | s = zbc_program_num(r1, &n1, false); |
6440 | if (s) return s; | 6447 | if (s) return s; |
6441 | if (!BC_PROG_NUM(r1, n1)) | 6448 | if (!BC_PROG_NUM(r1, n1)) |
6442 | return bc_error_variable_is_wrong_type(); | 6449 | return bc_error_variable_is_wrong_type(); |
@@ -6445,12 +6452,12 @@ static BcStatus bc_program_modexp(void) | |||
6445 | if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) { | 6452 | if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) { |
6446 | 6453 | ||
6447 | if (r1->t == r2->t) { | 6454 | if (r1->t == r2->t) { |
6448 | s = bc_program_num(r2, &n2, false); | 6455 | s = zbc_program_num(r2, &n2, false); |
6449 | if (s) return s; | 6456 | if (s) return s; |
6450 | } | 6457 | } |
6451 | 6458 | ||
6452 | if (r1->t == r3->t) { | 6459 | if (r1->t == r3->t) { |
6453 | s = bc_program_num(r3, &n3, false); | 6460 | s = zbc_program_num(r3, &n3, false); |
6454 | if (s) return s; | 6461 | if (s) return s; |
6455 | } | 6462 | } |
6456 | } | 6463 | } |
@@ -6495,7 +6502,7 @@ static BcStatus bc_program_asciify(void) | |||
6495 | r = bc_vec_top(&G.prog.results); | 6502 | r = bc_vec_top(&G.prog.results); |
6496 | 6503 | ||
6497 | num = NULL; // TODO: is this NULL needed? | 6504 | num = NULL; // TODO: is this NULL needed? |
6498 | s = bc_program_num(r, &num, false); | 6505 | s = zbc_program_num(r, &num, false); |
6499 | if (s) return s; | 6506 | if (s) return s; |
6500 | 6507 | ||
6501 | if (BC_PROG_NUM(r, num)) { | 6508 | if (BC_PROG_NUM(r, num)) { |
@@ -6506,7 +6513,7 @@ static BcStatus bc_program_asciify(void) | |||
6506 | 6513 | ||
6507 | s = bc_num_mod(&n, &G.prog.strmb, &n, 0); | 6514 | s = bc_num_mod(&n, &G.prog.strmb, &n, 0); |
6508 | if (s) goto num_err; | 6515 | if (s) goto num_err; |
6509 | s = bc_num_ulong(&n, &val); | 6516 | s = zbc_num_ulong(&n, &val); |
6510 | if (s) goto num_err; | 6517 | if (s) goto num_err; |
6511 | 6518 | ||
6512 | c = (char) val; | 6519 | c = (char) val; |
@@ -6552,7 +6559,7 @@ num_err: | |||
6552 | return s; | 6559 | return s; |
6553 | } | 6560 | } |
6554 | 6561 | ||
6555 | static BcStatus bc_program_printStream(void) | 6562 | static BC_STATUS zbc_program_printStream(void) |
6556 | { | 6563 | { |
6557 | BcStatus s; | 6564 | BcStatus s; |
6558 | BcResult *r; | 6565 | BcResult *r; |
@@ -6561,52 +6568,52 @@ static BcStatus bc_program_printStream(void) | |||
6561 | char *str; | 6568 | char *str; |
6562 | 6569 | ||
6563 | if (!BC_PROG_STACK(&G.prog.results, 1)) | 6570 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
6564 | return bc_error_stack_has_too_few_elements(); | 6571 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
6565 | r = bc_vec_top(&G.prog.results); | 6572 | r = bc_vec_top(&G.prog.results); |
6566 | 6573 | ||
6567 | s = bc_program_num(r, &n, false); | 6574 | s = zbc_program_num(r, &n, false); |
6568 | if (s) return s; | 6575 | if (s) RETURN_STATUS(s); |
6569 | 6576 | ||
6570 | if (BC_PROG_NUM(r, n)) | 6577 | if (BC_PROG_NUM(r, n)) |
6571 | s = bc_num_stream(n, &G.prog.strmb); | 6578 | s = zbc_num_stream(n, &G.prog.strmb); |
6572 | else { | 6579 | else { |
6573 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx; | 6580 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx; |
6574 | str = *bc_program_str(idx); | 6581 | str = *bc_program_str(idx); |
6575 | printf("%s", str); | 6582 | printf("%s", str); |
6576 | } | 6583 | } |
6577 | 6584 | ||
6578 | return s; | 6585 | RETURN_STATUS(s); |
6579 | } | 6586 | } |
6580 | #if ERRORS_ARE_FATAL | 6587 | #if ERRORS_ARE_FATAL |
6581 | # define bc_program_printStream(...) (bc_program_printStream(__VA_ARGS__), BC_STATUS_SUCCESS) | 6588 | # define zbc_program_printStream(...) (zbc_program_printStream(__VA_ARGS__), BC_STATUS_SUCCESS) |
6582 | #endif | 6589 | #endif |
6583 | 6590 | ||
6584 | static BcStatus bc_program_nquit(void) | 6591 | static BC_STATUS zbc_program_nquit(void) |
6585 | { | 6592 | { |
6586 | BcStatus s; | 6593 | BcStatus s; |
6587 | BcResult *opnd; | 6594 | BcResult *opnd; |
6588 | BcNum *num = NULL; | 6595 | BcNum *num = NULL; |
6589 | unsigned long val; | 6596 | unsigned long val; |
6590 | 6597 | ||
6591 | s = bc_program_prep(&opnd, &num); | 6598 | s = zbc_program_prep(&opnd, &num); |
6592 | if (s) return s; | 6599 | if (s) RETURN_STATUS(s); |
6593 | s = bc_num_ulong(num, &val); | 6600 | s = zbc_num_ulong(num, &val); |
6594 | if (s) return s; | 6601 | if (s) RETURN_STATUS(s); |
6595 | 6602 | ||
6596 | bc_vec_pop(&G.prog.results); | 6603 | bc_vec_pop(&G.prog.results); |
6597 | 6604 | ||
6598 | if (G.prog.stack.len < val) | 6605 | if (G.prog.stack.len < val) |
6599 | return bc_error_stack_has_too_few_elements(); | 6606 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
6600 | if (G.prog.stack.len == val) { | 6607 | if (G.prog.stack.len == val) { |
6601 | QUIT_OR_RETURN_TO_MAIN; | 6608 | QUIT_OR_RETURN_TO_MAIN; |
6602 | } | 6609 | } |
6603 | 6610 | ||
6604 | bc_vec_npop(&G.prog.stack, val); | 6611 | bc_vec_npop(&G.prog.stack, val); |
6605 | 6612 | ||
6606 | return s; | 6613 | RETURN_STATUS(s); |
6607 | } | 6614 | } |
6608 | #if ERRORS_ARE_FATAL | 6615 | #if ERRORS_ARE_FATAL |
6609 | # define bc_program_nquit(...) (bc_program_nquit(__VA_ARGS__), BC_STATUS_SUCCESS) | 6616 | # define zbc_program_nquit(...) (zbc_program_nquit(__VA_ARGS__), BC_STATUS_SUCCESS) |
6610 | #endif | 6617 | #endif |
6611 | 6618 | ||
6612 | static BcStatus bc_program_execStr(char *code, size_t *bgn, | 6619 | static BcStatus bc_program_execStr(char *code, size_t *bgn, |
@@ -6665,7 +6672,7 @@ static BcStatus bc_program_execStr(char *code, size_t *bgn, | |||
6665 | sidx = r->d.id.idx; | 6672 | sidx = r->d.id.idx; |
6666 | } else if (r->t == BC_RESULT_VAR) { | 6673 | } else if (r->t == BC_RESULT_VAR) { |
6667 | BcNum *n; | 6674 | BcNum *n; |
6668 | s = bc_program_num(r, &n, false); | 6675 | s = zbc_program_num(r, &n, false); |
6669 | if (s || !BC_PROG_STR(n)) goto exit; | 6676 | if (s || !BC_PROG_STR(n)) goto exit; |
6670 | sidx = n->rdx; | 6677 | sidx = n->rdx; |
6671 | } else | 6678 | } else |
@@ -6776,7 +6783,7 @@ static BcStatus bc_program_exec(void) | |||
6776 | switch (inst) { | 6783 | switch (inst) { |
6777 | #if ENABLE_BC | 6784 | #if ENABLE_BC |
6778 | case BC_INST_JUMP_ZERO: | 6785 | case BC_INST_JUMP_ZERO: |
6779 | s = bc_program_prep(&ptr, &num); | 6786 | s = zbc_program_prep(&ptr, &num); |
6780 | if (s) return s; | 6787 | if (s) return s; |
6781 | cond = !bc_num_cmp(num, &G.prog.zero); | 6788 | cond = !bc_num_cmp(num, &G.prog.zero); |
6782 | bc_vec_pop(&G.prog.results); | 6789 | bc_vec_pop(&G.prog.results); |
@@ -6789,20 +6796,20 @@ static BcStatus bc_program_exec(void) | |||
6789 | break; | 6796 | break; |
6790 | } | 6797 | } |
6791 | case BC_INST_CALL: | 6798 | case BC_INST_CALL: |
6792 | s = bc_program_call(code, &ip->idx); | 6799 | s = zbc_program_call(code, &ip->idx); |
6793 | break; | 6800 | break; |
6794 | case BC_INST_INC_PRE: | 6801 | case BC_INST_INC_PRE: |
6795 | case BC_INST_DEC_PRE: | 6802 | case BC_INST_DEC_PRE: |
6796 | case BC_INST_INC_POST: | 6803 | case BC_INST_INC_POST: |
6797 | case BC_INST_DEC_POST: | 6804 | case BC_INST_DEC_POST: |
6798 | s = bc_program_incdec(inst); | 6805 | s = zbc_program_incdec(inst); |
6799 | break; | 6806 | break; |
6800 | case BC_INST_HALT: | 6807 | case BC_INST_HALT: |
6801 | QUIT_OR_RETURN_TO_MAIN; | 6808 | QUIT_OR_RETURN_TO_MAIN; |
6802 | break; | 6809 | break; |
6803 | case BC_INST_RET: | 6810 | case BC_INST_RET: |
6804 | case BC_INST_RET0: | 6811 | case BC_INST_RET0: |
6805 | s = bc_program_return(inst); | 6812 | s = zbc_program_return(inst); |
6806 | break; | 6813 | break; |
6807 | case BC_INST_BOOL_OR: | 6814 | case BC_INST_BOOL_OR: |
6808 | case BC_INST_BOOL_AND: | 6815 | case BC_INST_BOOL_AND: |
@@ -6823,7 +6830,7 @@ static BcStatus bc_program_exec(void) | |||
6823 | break; | 6830 | break; |
6824 | case BC_INST_ARRAY_ELEM: | 6831 | case BC_INST_ARRAY_ELEM: |
6825 | case BC_INST_ARRAY: | 6832 | case BC_INST_ARRAY: |
6826 | s = bc_program_pushArray(code, &ip->idx, inst); | 6833 | s = zbc_program_pushArray(code, &ip->idx, inst); |
6827 | break; | 6834 | break; |
6828 | case BC_INST_LAST: | 6835 | case BC_INST_LAST: |
6829 | r.t = BC_RESULT_LAST; | 6836 | r.t = BC_RESULT_LAST; |
@@ -6856,7 +6863,7 @@ static BcStatus bc_program_exec(void) | |||
6856 | case BC_INST_PRINT: | 6863 | case BC_INST_PRINT: |
6857 | case BC_INST_PRINT_POP: | 6864 | case BC_INST_PRINT_POP: |
6858 | case BC_INST_PRINT_STR: | 6865 | case BC_INST_PRINT_STR: |
6859 | s = bc_program_print(inst, 0); | 6866 | s = zbc_program_print(inst, 0); |
6860 | break; | 6867 | break; |
6861 | case BC_INST_STR: | 6868 | case BC_INST_STR: |
6862 | r.t = BC_RESULT_STR; | 6869 | r.t = BC_RESULT_STR; |
@@ -6872,7 +6879,7 @@ static BcStatus bc_program_exec(void) | |||
6872 | s = bc_program_op(inst); | 6879 | s = bc_program_op(inst); |
6873 | break; | 6880 | break; |
6874 | case BC_INST_BOOL_NOT: | 6881 | case BC_INST_BOOL_NOT: |
6875 | s = bc_program_prep(&ptr, &num); | 6882 | s = zbc_program_prep(&ptr, &num); |
6876 | if (s) return s; | 6883 | if (s) return s; |
6877 | bc_num_init_DEF_SIZE(&r.d.n); | 6884 | bc_num_init_DEF_SIZE(&r.d.n); |
6878 | if (!bc_num_cmp(num, &G.prog.zero)) | 6885 | if (!bc_num_cmp(num, &G.prog.zero)) |
@@ -6881,7 +6888,7 @@ static BcStatus bc_program_exec(void) | |||
6881 | bc_program_retire(&r, BC_RESULT_TEMP); | 6888 | bc_program_retire(&r, BC_RESULT_TEMP); |
6882 | break; | 6889 | break; |
6883 | case BC_INST_NEG: | 6890 | case BC_INST_NEG: |
6884 | s = bc_program_negate(); | 6891 | s = zbc_program_negate(); |
6885 | break; | 6892 | break; |
6886 | #if ENABLE_BC | 6893 | #if ENABLE_BC |
6887 | case BC_INST_ASSIGN_POWER: | 6894 | case BC_INST_ASSIGN_POWER: |
@@ -6909,7 +6916,7 @@ static BcStatus bc_program_exec(void) | |||
6909 | case BC_INST_PRINT_STACK: { | 6916 | case BC_INST_PRINT_STACK: { |
6910 | size_t idx; | 6917 | size_t idx; |
6911 | for (idx = 0; idx < G.prog.results.len; ++idx) { | 6918 | for (idx = 0; idx < G.prog.results.len; ++idx) { |
6912 | s = bc_program_print(BC_INST_PRINT, idx); | 6919 | s = zbc_program_print(BC_INST_PRINT, idx); |
6913 | if (s) break; | 6920 | if (s) break; |
6914 | } | 6921 | } |
6915 | break; | 6922 | break; |
@@ -6942,7 +6949,7 @@ static BcStatus bc_program_exec(void) | |||
6942 | s = bc_program_asciify(); | 6949 | s = bc_program_asciify(); |
6943 | break; | 6950 | break; |
6944 | case BC_INST_PRINT_STREAM: | 6951 | case BC_INST_PRINT_STREAM: |
6945 | s = bc_program_printStream(); | 6952 | s = zbc_program_printStream(); |
6946 | break; | 6953 | break; |
6947 | case BC_INST_LOAD: | 6954 | case BC_INST_LOAD: |
6948 | case BC_INST_PUSH_VAR: { | 6955 | case BC_INST_PUSH_VAR: { |
@@ -6952,7 +6959,7 @@ static BcStatus bc_program_exec(void) | |||
6952 | } | 6959 | } |
6953 | case BC_INST_PUSH_TO_VAR: { | 6960 | case BC_INST_PUSH_TO_VAR: { |
6954 | char *name = bc_program_name(code, &ip->idx); | 6961 | char *name = bc_program_name(code, &ip->idx); |
6955 | s = bc_program_copyToVar(name, true); | 6962 | s = zbc_program_copyToVar(name, true); |
6956 | free(name); | 6963 | free(name); |
6957 | break; | 6964 | break; |
6958 | } | 6965 | } |
@@ -6962,7 +6969,7 @@ static BcStatus bc_program_exec(void) | |||
6962 | bc_vec_npop(&G.prog.stack, 2); | 6969 | bc_vec_npop(&G.prog.stack, 2); |
6963 | break; | 6970 | break; |
6964 | case BC_INST_NQUIT: | 6971 | case BC_INST_NQUIT: |
6965 | s = bc_program_nquit(); | 6972 | s = zbc_program_nquit(); |
6966 | break; | 6973 | break; |
6967 | #endif // ENABLE_DC | 6974 | #endif // ENABLE_DC |
6968 | } | 6975 | } |