diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2018-12-27 18:23:58 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-12-27 18:23:58 +0100 |
commit | 10bde14292b3f0abbbf56aaaec16f391f12c0e64 (patch) | |
tree | 2520f1dd672397b60014b6d63bccaa205949ad0b | |
parent | db8d607514c684fe208e8d8cf02864731a123bf6 (diff) | |
download | busybox-w32-10bde14292b3f0abbbf56aaaec16f391f12c0e64.tar.gz busybox-w32-10bde14292b3f0abbbf56aaaec16f391f12c0e64.tar.bz2 busybox-w32-10bde14292b3f0abbbf56aaaec16f391f12c0e64.zip |
bc: rename functions common to bc and dc as xc_FOO()
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | miscutils/bc.c | 805 |
1 files changed, 397 insertions, 408 deletions
diff --git a/miscutils/bc.c b/miscutils/bc.c index 09cc45254..537612d5e 100644 --- a/miscutils/bc.c +++ b/miscutils/bc.c | |||
@@ -436,7 +436,7 @@ typedef enum BcLexType { | |||
436 | BC_LEX_LPAREN, // () are 0x28 and 0x29 | 436 | BC_LEX_LPAREN, // () are 0x28 and 0x29 |
437 | BC_LEX_RPAREN, // must be LPAREN+1: code uses (c - '(' + BC_LEX_LPAREN) | 437 | BC_LEX_RPAREN, // must be LPAREN+1: code uses (c - '(' + BC_LEX_LPAREN) |
438 | 438 | ||
439 | BC_LEX_LBRACKET, // [] are 0x5B and 5D | 439 | BC_LEX_LBRACKET, // [] are 0x5B and 0x5D |
440 | BC_LEX_COMMA, | 440 | BC_LEX_COMMA, |
441 | BC_LEX_RBRACKET, // must be LBRACKET+2: code uses (c - '[' + BC_LEX_LBRACKET) | 441 | BC_LEX_RBRACKET, // must be LBRACKET+2: code uses (c - '[' + BC_LEX_LBRACKET) |
442 | 442 | ||
@@ -632,52 +632,52 @@ static const uint8_t bc_parse_ops[] ALIGN1 = { | |||
632 | static const //BcLexType - should be this type | 632 | static const //BcLexType - should be this type |
633 | uint8_t | 633 | uint8_t |
634 | dc_char_to_LEX[] ALIGN1 = { | 634 | dc_char_to_LEX[] ALIGN1 = { |
635 | /* %&'( */ | 635 | // %&'( |
636 | XC_LEX_OP_MODULUS, XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_LPAREN, | 636 | XC_LEX_OP_MODULUS, XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_LPAREN, |
637 | /* )*+, */ | 637 | // )*+, |
638 | XC_LEX_INVALID, XC_LEX_OP_MULTIPLY, XC_LEX_OP_PLUS, XC_LEX_INVALID, | 638 | XC_LEX_INVALID, XC_LEX_OP_MULTIPLY, XC_LEX_OP_PLUS, XC_LEX_INVALID, |
639 | /* -./ */ | 639 | // -./ |
640 | XC_LEX_OP_MINUS, XC_LEX_INVALID, XC_LEX_OP_DIVIDE, | 640 | XC_LEX_OP_MINUS, XC_LEX_INVALID, XC_LEX_OP_DIVIDE, |
641 | /* 0123456789 */ | 641 | // 0123456789 |
642 | XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, | 642 | XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, |
643 | XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, | 643 | XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, |
644 | XC_LEX_INVALID, XC_LEX_INVALID, | 644 | XC_LEX_INVALID, XC_LEX_INVALID, |
645 | /* :;<=>?@ */ | 645 | // :;<=>?@ |
646 | DC_LEX_COLON, DC_LEX_SCOLON, XC_LEX_OP_REL_GT, XC_LEX_OP_REL_EQ, | 646 | DC_LEX_COLON, DC_LEX_SCOLON, XC_LEX_OP_REL_GT, XC_LEX_OP_REL_EQ, |
647 | XC_LEX_OP_REL_LT, DC_LEX_READ, XC_LEX_INVALID, | 647 | XC_LEX_OP_REL_LT, DC_LEX_READ, XC_LEX_INVALID, |
648 | /* ABCDEFGH */ | 648 | // ABCDEFGH |
649 | XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, | 649 | XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, |
650 | XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_EQ_NO_REG, XC_LEX_INVALID, | 650 | XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_EQ_NO_REG, XC_LEX_INVALID, |
651 | /* IJKLMNOP */ | 651 | // IJKLMNOP |
652 | DC_LEX_IBASE, XC_LEX_INVALID, DC_LEX_SCALE, DC_LEX_LOAD_POP, | 652 | DC_LEX_IBASE, XC_LEX_INVALID, DC_LEX_SCALE, DC_LEX_LOAD_POP, |
653 | XC_LEX_INVALID, DC_LEX_OP_BOOL_NOT, DC_LEX_OBASE, DC_LEX_PRINT_STREAM, | 653 | XC_LEX_INVALID, DC_LEX_OP_BOOL_NOT, DC_LEX_OBASE, DC_LEX_PRINT_STREAM, |
654 | /* QRSTUVWXY */ | 654 | // QRSTUVWXY |
655 | DC_LEX_NQUIT, DC_LEX_POP, DC_LEX_STORE_PUSH, XC_LEX_INVALID, XC_LEX_INVALID, | 655 | DC_LEX_NQUIT, DC_LEX_POP, DC_LEX_STORE_PUSH, XC_LEX_INVALID, XC_LEX_INVALID, |
656 | XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_SCALE_FACTOR, XC_LEX_INVALID, | 656 | XC_LEX_INVALID, XC_LEX_INVALID, DC_LEX_SCALE_FACTOR, XC_LEX_INVALID, |
657 | /* Z[\] */ | 657 | // Z[\] |
658 | DC_LEX_LENGTH, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, | 658 | DC_LEX_LENGTH, XC_LEX_INVALID, XC_LEX_INVALID, XC_LEX_INVALID, |
659 | /* ^_` */ | 659 | // ^_` |
660 | XC_LEX_OP_POWER, XC_LEX_NEG, XC_LEX_INVALID, | 660 | XC_LEX_OP_POWER, XC_LEX_NEG, XC_LEX_INVALID, |
661 | /* abcdefgh */ | 661 | // abcdefgh |
662 | DC_LEX_ASCIIFY, XC_LEX_INVALID, DC_LEX_CLEAR_STACK, DC_LEX_DUPLICATE, | 662 | DC_LEX_ASCIIFY, XC_LEX_INVALID, DC_LEX_CLEAR_STACK, DC_LEX_DUPLICATE, |
663 | DC_LEX_ELSE, DC_LEX_PRINT_STACK, XC_LEX_INVALID, XC_LEX_INVALID, | 663 | DC_LEX_ELSE, DC_LEX_PRINT_STACK, XC_LEX_INVALID, XC_LEX_INVALID, |
664 | /* ijklmnop */ | 664 | // ijklmnop |
665 | DC_LEX_STORE_IBASE, XC_LEX_INVALID, DC_LEX_STORE_SCALE, DC_LEX_LOAD, | 665 | DC_LEX_STORE_IBASE, XC_LEX_INVALID, DC_LEX_STORE_SCALE, DC_LEX_LOAD, |
666 | XC_LEX_INVALID, DC_LEX_PRINT_POP, DC_LEX_STORE_OBASE, DC_LEX_PRINT, | 666 | XC_LEX_INVALID, DC_LEX_PRINT_POP, DC_LEX_STORE_OBASE, DC_LEX_PRINT, |
667 | /* qrstuvwx */ | 667 | // qrstuvwx |
668 | DC_LEX_QUIT, DC_LEX_SWAP, DC_LEX_OP_ASSIGN, XC_LEX_INVALID, | 668 | DC_LEX_QUIT, DC_LEX_SWAP, DC_LEX_OP_ASSIGN, XC_LEX_INVALID, |
669 | XC_LEX_INVALID, DC_LEX_SQRT, XC_LEX_INVALID, DC_LEX_EXECUTE, | 669 | XC_LEX_INVALID, DC_LEX_SQRT, XC_LEX_INVALID, DC_LEX_EXECUTE, |
670 | /* yz */ | 670 | // yz |
671 | XC_LEX_INVALID, DC_LEX_STACK_LEVEL, | 671 | XC_LEX_INVALID, DC_LEX_STACK_LEVEL, |
672 | /* {|}~ */ | 672 | // {|}~ |
673 | DC_LEX_LBRACE, DC_LEX_OP_MODEXP, XC_LEX_INVALID, DC_LEX_OP_DIVMOD, | 673 | DC_LEX_LBRACE, DC_LEX_OP_MODEXP, XC_LEX_INVALID, DC_LEX_OP_DIVMOD, |
674 | }; | 674 | }; |
675 | static const //BcInst - should be this type. Using signed narrow type since DC_INST_INVALID is -1 | 675 | static const //BcInst - should be this type. Using signed narrow type since DC_INST_INVALID is -1 |
676 | int8_t | 676 | int8_t |
677 | dc_LEX_to_INST[] ALIGN1 = { // starts at XC_LEX_OP_POWER // corresponding XC/DC_LEX_xyz: | 677 | dc_LEX_to_INST[] ALIGN1 = { //starts at XC_LEX_OP_POWER // corresponding XC/DC_LEX_xyz: |
678 | XC_INST_POWER, XC_INST_MULTIPLY, // OP_POWER OP_MULTIPLY | 678 | XC_INST_POWER, XC_INST_MULTIPLY, // XC_LEX_OP_POWER XC_LEX_OP_MULTIPLY |
679 | XC_INST_DIVIDE, XC_INST_MODULUS, // OP_DIVIDE OP_MODULUS | 679 | XC_INST_DIVIDE, XC_INST_MODULUS, // XC_LEX_OP_DIVIDE XC_LEX_OP_MODULUS |
680 | XC_INST_PLUS, XC_INST_MINUS, // OP_PLUS OP_MINUS | 680 | XC_INST_PLUS, XC_INST_MINUS, // XC_LEX_OP_PLUS XC_LEX_OP_MINUS |
681 | XC_INST_BOOL_NOT, // DC_LEX_OP_BOOL_NOT | 681 | XC_INST_BOOL_NOT, // DC_LEX_OP_BOOL_NOT |
682 | DC_INST_INVALID, // DC_LEX_OP_ASSIGN | 682 | DC_INST_INVALID, // DC_LEX_OP_ASSIGN |
683 | XC_INST_REL_GT, // DC_LEX_LPAREN | 683 | XC_INST_REL_GT, // DC_LEX_LPAREN |
@@ -692,18 +692,18 @@ dc_LEX_to_INST[] ALIGN1 = { // starts at XC_LEX_OP_POWER // corresponding | |||
692 | XC_INST_SQRT, // DC_LEX_SQRT | 692 | XC_INST_SQRT, // DC_LEX_SQRT |
693 | XC_INST_REL_GE, // DC_LEX_LBRACE | 693 | XC_INST_REL_GE, // DC_LEX_LBRACE |
694 | XC_INST_REL_EQ, // DC_LEX_EQ_NO_REG | 694 | XC_INST_REL_EQ, // DC_LEX_EQ_NO_REG |
695 | DC_INST_MODEXP, DC_INST_DIVMOD, // OP_MODEXP OP_DIVMOD | 695 | DC_INST_MODEXP, DC_INST_DIVMOD, // DC_LEX_OP_MODEXP DC_LEX_OP_DIVMOD |
696 | DC_INST_INVALID, DC_INST_INVALID, // COLON ELSE | 696 | DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_COLON DC_LEX_ELSE |
697 | DC_INST_EXECUTE, // EXECUTE | 697 | DC_INST_EXECUTE, // DC_LEX_EXECUTE |
698 | DC_INST_PRINT_STACK, DC_INST_CLEAR_STACK, // PRINT_STACK CLEAR_STACK | 698 | DC_INST_PRINT_STACK, DC_INST_CLEAR_STACK, // DC_LEX_PRINT_STACK DC_LEX_CLEAR_STACK |
699 | DC_INST_STACK_LEN, DC_INST_DUPLICATE, // STACK_LEVEL DUPLICATE | 699 | DC_INST_STACK_LEN, DC_INST_DUPLICATE, // DC_LEX_STACK_LEVEL DC_LEX_DUPLICATE |
700 | DC_INST_SWAP, XC_INST_POP, // SWAP POP | 700 | DC_INST_SWAP, XC_INST_POP, // DC_LEX_SWAP DC_LEX_POP |
701 | DC_INST_ASCIIFY, DC_INST_PRINT_STREAM, // ASCIIFY PRINT_STREAM | 701 | DC_INST_ASCIIFY, DC_INST_PRINT_STREAM, // DC_LEX_ASCIIFY DC_LEX_PRINT_STREAM |
702 | DC_INST_INVALID, DC_INST_INVALID, // STORE_IBASE STORE_OBASE | 702 | DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_IBASE DC_LEX_STORE_OBASE |
703 | DC_INST_INVALID, DC_INST_INVALID, // STORE_SCALE LOAD | 703 | DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_STORE_SCALE DC_LEX_LOAD |
704 | DC_INST_INVALID, DC_INST_INVALID, // LOAD_POP STORE_PUSH | 704 | DC_INST_INVALID, DC_INST_INVALID, // DC_LEX_LOAD_POP DC_LEX_STORE_PUSH |
705 | XC_INST_PRINT, DC_INST_NQUIT, // PRINT_POP NQUIT | 705 | XC_INST_PRINT, DC_INST_NQUIT, // DC_LEX_PRINT_POP DC_LEX_NQUIT |
706 | XC_INST_SCALE_FUNC, // SCALE_FACTOR | 706 | XC_INST_SCALE_FUNC, // DC_LEX_SCALE_FACTOR |
707 | // DC_INST_INVALID in this table either means that corresponding LEX | 707 | // DC_INST_INVALID in this table either means that corresponding LEX |
708 | // is not possible for dc, or that it does not compile one-to-one | 708 | // is not possible for dc, or that it does not compile one-to-one |
709 | // to a single INST. | 709 | // to a single INST. |
@@ -825,8 +825,8 @@ struct globals { | |||
825 | #define BC_FLAG_S (1 << 2) | 825 | #define BC_FLAG_S (1 << 2) |
826 | #define BC_FLAG_Q (1 << 3) | 826 | #define BC_FLAG_Q (1 << 3) |
827 | #define BC_FLAG_L (1 << 4) | 827 | #define BC_FLAG_L (1 << 4) |
828 | #define BC_FLAG_I (1 << 5) | 828 | #define BC_FLAG_I ((1 << 5) * ENABLE_DC) |
829 | #define DC_FLAG_X (1 << 6) | 829 | #define DC_FLAG_X ((1 << 6) * ENABLE_DC) |
830 | 830 | ||
831 | #define BC_MAX(a, b) ((a) > (b) ? (a) : (b)) | 831 | #define BC_MAX(a, b) ((a) > (b) ? (a) : (b)) |
832 | #define BC_MIN(a, b) ((a) < (b) ? (a) : (b)) | 832 | #define BC_MIN(a, b) ((a) < (b) ? (a) : (b)) |
@@ -911,9 +911,6 @@ do { \ | |||
911 | return BC_STATUS_FAILURE; \ | 911 | return BC_STATUS_FAILURE; \ |
912 | } while (0) | 912 | } while (0) |
913 | #else | 913 | #else |
914 | #define QUIT_OR_RETURN_TO_MAIN quit() | ||
915 | #endif | ||
916 | |||
917 | static void quit(void) NORETURN; | 914 | static void quit(void) NORETURN; |
918 | static void quit(void) | 915 | static void quit(void) |
919 | { | 916 | { |
@@ -923,6 +920,8 @@ static void quit(void) | |||
923 | dbg_exec("quit(): exiting with exitcode SUCCESS"); | 920 | dbg_exec("quit(): exiting with exitcode SUCCESS"); |
924 | exit(0); | 921 | exit(0); |
925 | } | 922 | } |
923 | #define QUIT_OR_RETURN_TO_MAIN quit() | ||
924 | #endif | ||
926 | 925 | ||
927 | static void bc_verror_msg(const char *fmt, va_list p) | 926 | static void bc_verror_msg(const char *fmt, va_list p) |
928 | { | 927 | { |
@@ -1165,23 +1164,23 @@ static FAST_FUNC void bc_vec_free(void *vec) | |||
1165 | free(v->v); | 1164 | free(v->v); |
1166 | } | 1165 | } |
1167 | 1166 | ||
1168 | static BcFunc* bc_program_func(size_t idx) | 1167 | static BcFunc* xc_program_func(size_t idx) |
1169 | { | 1168 | { |
1170 | return bc_vec_item(&G.prog.fns, idx); | 1169 | return bc_vec_item(&G.prog.fns, idx); |
1171 | } | 1170 | } |
1172 | // BC_PROG_MAIN is zeroth element, so: | 1171 | // BC_PROG_MAIN is zeroth element, so: |
1173 | #define bc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v)) | 1172 | #define xc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v)) |
1174 | 1173 | ||
1175 | #if ENABLE_BC | 1174 | #if ENABLE_BC |
1176 | static BcFunc* bc_program_current_func(void) | 1175 | static BcFunc* bc_program_current_func(void) |
1177 | { | 1176 | { |
1178 | BcInstPtr *ip = bc_vec_top(&G.prog.exestack); | 1177 | BcInstPtr *ip = bc_vec_top(&G.prog.exestack); |
1179 | BcFunc *func = bc_program_func(ip->func); | 1178 | BcFunc *func = xc_program_func(ip->func); |
1180 | return func; | 1179 | return func; |
1181 | } | 1180 | } |
1182 | #endif | 1181 | #endif |
1183 | 1182 | ||
1184 | static char** bc_program_str(size_t idx) | 1183 | static char** xc_program_str(size_t idx) |
1185 | { | 1184 | { |
1186 | #if ENABLE_BC | 1185 | #if ENABLE_BC |
1187 | if (IS_BC) { | 1186 | if (IS_BC) { |
@@ -1192,7 +1191,7 @@ static char** bc_program_str(size_t idx) | |||
1192 | IF_DC(return bc_vec_item(&G.prog.strs, idx);) | 1191 | IF_DC(return bc_vec_item(&G.prog.strs, idx);) |
1193 | } | 1192 | } |
1194 | 1193 | ||
1195 | static char** bc_program_const(size_t idx) | 1194 | static char** xc_program_const(size_t idx) |
1196 | { | 1195 | { |
1197 | #if ENABLE_BC | 1196 | #if ENABLE_BC |
1198 | if (IS_BC) { | 1197 | if (IS_BC) { |
@@ -1637,7 +1636,7 @@ static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scal | |||
1637 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1)); | 1636 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1)); |
1638 | } | 1637 | } |
1639 | 1638 | ||
1640 | static const BcNumBinaryOp zbc_program_ops[] = { | 1639 | static const BcNumBinaryOp zxc_program_ops[] = { |
1641 | zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub, | 1640 | zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub, |
1642 | }; | 1641 | }; |
1643 | #define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS) | 1642 | #define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS) |
@@ -2481,7 +2480,7 @@ static int bad_input_byte(char c) | |||
2481 | return 0; | 2480 | return 0; |
2482 | } | 2481 | } |
2483 | 2482 | ||
2484 | static void bc_read_line(BcVec *vec, FILE *fp) | 2483 | static void xc_read_line(BcVec *vec, FILE *fp) |
2485 | { | 2484 | { |
2486 | again: | 2485 | again: |
2487 | bc_vec_pop_all(vec); | 2486 | bc_vec_pop_all(vec); |
@@ -2568,7 +2567,7 @@ static void bc_read_line(BcVec *vec, FILE *fp) | |||
2568 | // Parsing routines | 2567 | // Parsing routines |
2569 | // | 2568 | // |
2570 | 2569 | ||
2571 | static bool bc_num_strValid(const char *val, size_t base) | 2570 | static bool xc_num_strValid(const char *val, size_t base) |
2572 | { | 2571 | { |
2573 | BcDig b; | 2572 | BcDig b; |
2574 | bool radix; | 2573 | bool radix; |
@@ -2703,9 +2702,9 @@ static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t) | |||
2703 | bc_num_free(&temp); | 2702 | bc_num_free(&temp); |
2704 | } | 2703 | } |
2705 | 2704 | ||
2706 | static BC_STATUS zbc_num_parse(BcNum *n, const char *val, unsigned base_t) | 2705 | static BC_STATUS zxc_num_parse(BcNum *n, const char *val, unsigned base_t) |
2707 | { | 2706 | { |
2708 | if (!bc_num_strValid(val, base_t)) | 2707 | if (!xc_num_strValid(val, base_t)) |
2709 | RETURN_STATUS(bc_error("bad number string")); | 2708 | RETURN_STATUS(bc_error("bad number string")); |
2710 | 2709 | ||
2711 | bc_num_zero(n); | 2710 | bc_num_zero(n); |
@@ -2718,15 +2717,15 @@ static BC_STATUS zbc_num_parse(BcNum *n, const char *val, unsigned base_t) | |||
2718 | 2717 | ||
2719 | RETURN_STATUS(BC_STATUS_SUCCESS); | 2718 | RETURN_STATUS(BC_STATUS_SUCCESS); |
2720 | } | 2719 | } |
2721 | #define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__) COMMA_SUCCESS) | 2720 | #define zxc_num_parse(...) (zxc_num_parse(__VA_ARGS__) COMMA_SUCCESS) |
2722 | 2721 | ||
2723 | static bool bc_lex_more_input(void) | 2722 | static bool xc_lex_more_input(void) |
2724 | { | 2723 | { |
2725 | BcParse *p = &G.prs; | 2724 | BcParse *p = &G.prs; |
2726 | 2725 | ||
2727 | bc_vec_pop_all(&G.input_buffer); | 2726 | bc_vec_pop_all(&G.input_buffer); |
2728 | 2727 | ||
2729 | bc_read_line(&G.input_buffer, G.prs.lex_input_fp); | 2728 | xc_read_line(&G.input_buffer, G.prs.lex_input_fp); |
2730 | 2729 | ||
2731 | p->lex_inbuf = G.input_buffer.v; | 2730 | p->lex_inbuf = G.input_buffer.v; |
2732 | // bb_error_msg("G.input_buffer.len:%d '%s'", G.input_buffer.len, G.input_buffer.v); | 2731 | // bb_error_msg("G.input_buffer.len:%d '%s'", G.input_buffer.len, G.input_buffer.v); |
@@ -2769,7 +2768,7 @@ static char peek_inbuf(void) | |||
2769 | { | 2768 | { |
2770 | if (*G.prs.lex_inbuf == '\0') { | 2769 | if (*G.prs.lex_inbuf == '\0') { |
2771 | if (G.prs.lex_input_fp) | 2770 | if (G.prs.lex_input_fp) |
2772 | if (!bc_lex_more_input()) | 2771 | if (!xc_lex_more_input()) |
2773 | G.prs.lex_input_fp = NULL; | 2772 | G.prs.lex_input_fp = NULL; |
2774 | } | 2773 | } |
2775 | return *G.prs.lex_inbuf; | 2774 | return *G.prs.lex_inbuf; |
@@ -2781,7 +2780,7 @@ static char eat_inbuf(void) | |||
2781 | return c; | 2780 | return c; |
2782 | } | 2781 | } |
2783 | 2782 | ||
2784 | static void bc_lex_lineComment(void) | 2783 | static void xc_lex_lineComment(void) |
2785 | { | 2784 | { |
2786 | BcParse *p = &G.prs; | 2785 | BcParse *p = &G.prs; |
2787 | char c; | 2786 | char c; |
@@ -2795,7 +2794,7 @@ static void bc_lex_lineComment(void) | |||
2795 | p->lex_inbuf++; | 2794 | p->lex_inbuf++; |
2796 | } | 2795 | } |
2797 | 2796 | ||
2798 | static void bc_lex_whitespace(void) | 2797 | static void xc_lex_whitespace(void) |
2799 | { | 2798 | { |
2800 | BcParse *p = &G.prs; | 2799 | BcParse *p = &G.prs; |
2801 | 2800 | ||
@@ -2812,7 +2811,7 @@ static void bc_lex_whitespace(void) | |||
2812 | } | 2811 | } |
2813 | } | 2812 | } |
2814 | 2813 | ||
2815 | static BC_STATUS zbc_lex_number(char last) | 2814 | static BC_STATUS zxc_lex_number(char last) |
2816 | { | 2815 | { |
2817 | BcParse *p = &G.prs; | 2816 | BcParse *p = &G.prs; |
2818 | bool pt; | 2817 | bool pt; |
@@ -2853,9 +2852,9 @@ static BC_STATUS zbc_lex_number(char last) | |||
2853 | G.err_line = G.prs.lex_line; | 2852 | G.err_line = G.prs.lex_line; |
2854 | RETURN_STATUS(BC_STATUS_SUCCESS); | 2853 | RETURN_STATUS(BC_STATUS_SUCCESS); |
2855 | } | 2854 | } |
2856 | #define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__) COMMA_SUCCESS) | 2855 | #define zxc_lex_number(...) (zxc_lex_number(__VA_ARGS__) COMMA_SUCCESS) |
2857 | 2856 | ||
2858 | static void bc_lex_name(void) | 2857 | static void xc_lex_name(void) |
2859 | { | 2858 | { |
2860 | BcParse *p = &G.prs; | 2859 | BcParse *p = &G.prs; |
2861 | size_t i; | 2860 | size_t i; |
@@ -2893,7 +2892,7 @@ IF_DC(static BC_STATUS zdc_lex_token(void);) | |||
2893 | #define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS) | 2892 | #define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
2894 | #define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS) | 2893 | #define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
2895 | 2894 | ||
2896 | static BC_STATUS zbc_lex_next(void) | 2895 | static BC_STATUS zxc_lex_next(void) |
2897 | { | 2896 | { |
2898 | BcParse *p = &G.prs; | 2897 | BcParse *p = &G.prs; |
2899 | BcStatus s; | 2898 | BcStatus s; |
@@ -2928,13 +2927,13 @@ static BC_STATUS zbc_lex_next(void) | |||
2928 | 2927 | ||
2929 | RETURN_STATUS(s); | 2928 | RETURN_STATUS(s); |
2930 | } | 2929 | } |
2931 | #define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__) COMMA_SUCCESS) | 2930 | #define zxc_lex_next(...) (zxc_lex_next(__VA_ARGS__) COMMA_SUCCESS) |
2932 | 2931 | ||
2933 | #if ENABLE_BC | 2932 | #if ENABLE_BC |
2934 | static BC_STATUS zbc_lex_skip_if_at_NLINE(void) | 2933 | static BC_STATUS zbc_lex_skip_if_at_NLINE(void) |
2935 | { | 2934 | { |
2936 | if (G.prs.lex == XC_LEX_NLINE) | 2935 | if (G.prs.lex == XC_LEX_NLINE) |
2937 | RETURN_STATUS(zbc_lex_next()); | 2936 | RETURN_STATUS(zxc_lex_next()); |
2938 | RETURN_STATUS(BC_STATUS_SUCCESS); | 2937 | RETURN_STATUS(BC_STATUS_SUCCESS); |
2939 | } | 2938 | } |
2940 | #define zbc_lex_skip_if_at_NLINE(...) (zbc_lex_skip_if_at_NLINE(__VA_ARGS__) COMMA_SUCCESS) | 2939 | #define zbc_lex_skip_if_at_NLINE(...) (zbc_lex_skip_if_at_NLINE(__VA_ARGS__) COMMA_SUCCESS) |
@@ -2942,24 +2941,14 @@ static BC_STATUS zbc_lex_skip_if_at_NLINE(void) | |||
2942 | static BC_STATUS zbc_lex_next_and_skip_NLINE(void) | 2941 | static BC_STATUS zbc_lex_next_and_skip_NLINE(void) |
2943 | { | 2942 | { |
2944 | BcStatus s; | 2943 | BcStatus s; |
2945 | s = zbc_lex_next(); | 2944 | s = zxc_lex_next(); |
2946 | if (s) RETURN_STATUS(s); | 2945 | if (s) RETURN_STATUS(s); |
2947 | // if(cond)<newline>stmt is accepted too (but not 2+ newlines) | 2946 | // if(cond)<newline>stmt is accepted too (but not 2+ newlines) |
2948 | s = zbc_lex_skip_if_at_NLINE(); | 2947 | s = zbc_lex_skip_if_at_NLINE(); |
2949 | RETURN_STATUS(s); | 2948 | RETURN_STATUS(s); |
2950 | } | 2949 | } |
2951 | #define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS) | 2950 | #define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS) |
2952 | #endif | ||
2953 | |||
2954 | static BC_STATUS zbc_lex_text_init(const char *text) | ||
2955 | { | ||
2956 | G.prs.lex_inbuf = text; | ||
2957 | G.prs.lex = G.prs.lex_last = XC_LEX_INVALID; | ||
2958 | RETURN_STATUS(zbc_lex_next()); | ||
2959 | } | ||
2960 | #define zbc_lex_text_init(...) (zbc_lex_text_init(__VA_ARGS__) COMMA_SUCCESS) | ||
2961 | 2951 | ||
2962 | #if ENABLE_BC | ||
2963 | static BC_STATUS zbc_lex_identifier(void) | 2952 | static BC_STATUS zbc_lex_identifier(void) |
2964 | { | 2953 | { |
2965 | BcParse *p = &G.prs; | 2954 | BcParse *p = &G.prs; |
@@ -2991,7 +2980,7 @@ static BC_STATUS zbc_lex_identifier(void) | |||
2991 | RETURN_STATUS(BC_STATUS_SUCCESS); | 2980 | RETURN_STATUS(BC_STATUS_SUCCESS); |
2992 | } | 2981 | } |
2993 | 2982 | ||
2994 | bc_lex_name(); | 2983 | xc_lex_name(); |
2995 | s = BC_STATUS_SUCCESS; | 2984 | s = BC_STATUS_SUCCESS; |
2996 | 2985 | ||
2997 | if (p->lex_strnumbuf.len > 2) { | 2986 | if (p->lex_strnumbuf.len > 2) { |
@@ -3101,7 +3090,7 @@ static BC_STATUS zbc_lex_token(void) | |||
3101 | case '\f': | 3090 | case '\f': |
3102 | case '\r': | 3091 | case '\r': |
3103 | case ' ': | 3092 | case ' ': |
3104 | bc_lex_whitespace(); | 3093 | xc_lex_whitespace(); |
3105 | break; | 3094 | break; |
3106 | case '!': | 3095 | case '!': |
3107 | parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT); | 3096 | parse_lex_by_checking_eq_sign(XC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT); |
@@ -3116,7 +3105,7 @@ static BC_STATUS zbc_lex_token(void) | |||
3116 | case '#': | 3105 | case '#': |
3117 | s = zbc_POSIX_does_not_allow("'#' script comments"); | 3106 | s = zbc_POSIX_does_not_allow("'#' script comments"); |
3118 | if (s) RETURN_STATUS(s); | 3107 | if (s) RETURN_STATUS(s); |
3119 | bc_lex_lineComment(); | 3108 | xc_lex_lineComment(); |
3120 | break; | 3109 | break; |
3121 | case '%': | 3110 | case '%': |
3122 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS); | 3111 | parse_lex_by_checking_eq_sign(BC_LEX_OP_ASSIGN_MODULUS, XC_LEX_OP_MODULUS); |
@@ -3161,7 +3150,7 @@ static BC_STATUS zbc_lex_token(void) | |||
3161 | break; | 3150 | break; |
3162 | case '.': | 3151 | case '.': |
3163 | if (isdigit(*p->lex_inbuf)) | 3152 | if (isdigit(*p->lex_inbuf)) |
3164 | s = zbc_lex_number(c); | 3153 | s = zxc_lex_number(c); |
3165 | else { | 3154 | else { |
3166 | p->lex = BC_LEX_KEY_LAST; | 3155 | p->lex = BC_LEX_KEY_LAST; |
3167 | s = zbc_POSIX_does_not_allow("'.' as 'last'"); | 3156 | s = zbc_POSIX_does_not_allow("'.' as 'last'"); |
@@ -3190,7 +3179,7 @@ static BC_STATUS zbc_lex_token(void) | |||
3190 | case 'D': | 3179 | case 'D': |
3191 | case 'E': | 3180 | case 'E': |
3192 | case 'F': | 3181 | case 'F': |
3193 | s = zbc_lex_number(c); | 3182 | s = zxc_lex_number(c); |
3194 | break; | 3183 | break; |
3195 | case ';': | 3184 | case ';': |
3196 | p->lex = BC_LEX_SCOLON; | 3185 | p->lex = BC_LEX_SCOLON; |
@@ -3278,9 +3267,9 @@ static BC_STATUS zdc_lex_register(void) | |||
3278 | { | 3267 | { |
3279 | BcParse *p = &G.prs; | 3268 | BcParse *p = &G.prs; |
3280 | if (G_exreg && isspace(*p->lex_inbuf)) { | 3269 | if (G_exreg && isspace(*p->lex_inbuf)) { |
3281 | bc_lex_whitespace(); // eats whitespace (but not newline) | 3270 | xc_lex_whitespace(); // eats whitespace (but not newline) |
3282 | p->lex_inbuf++; // bc_lex_name() expects this | 3271 | p->lex_inbuf++; // xc_lex_name() expects this |
3283 | bc_lex_name(); | 3272 | xc_lex_name(); |
3284 | } else { | 3273 | } else { |
3285 | bc_vec_pop_all(&p->lex_strnumbuf); | 3274 | bc_vec_pop_all(&p->lex_strnumbuf); |
3286 | bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf++); | 3275 | bc_vec_push(&p->lex_strnumbuf, p->lex_inbuf++); |
@@ -3326,7 +3315,6 @@ static BC_STATUS zdc_lex_string(void) | |||
3326 | #undef zdc_lex_token | 3315 | #undef zdc_lex_token |
3327 | static BC_STATUS zdc_lex_token(void) | 3316 | static BC_STATUS zdc_lex_token(void) |
3328 | { | 3317 | { |
3329 | BcParse *p = &G.prs; | ||
3330 | static const //BcLexType - should be this type, but narrower type saves size: | 3318 | static const //BcLexType - should be this type, but narrower type saves size: |
3331 | uint8_t | 3319 | uint8_t |
3332 | dc_lex_regs[] ALIGN1 = { | 3320 | dc_lex_regs[] ALIGN1 = { |
@@ -3336,6 +3324,7 @@ static BC_STATUS zdc_lex_token(void) | |||
3336 | DC_LEX_STORE_PUSH, | 3324 | DC_LEX_STORE_PUSH, |
3337 | }; | 3325 | }; |
3338 | 3326 | ||
3327 | BcParse *p = &G.prs; | ||
3339 | BcStatus s; | 3328 | BcStatus s; |
3340 | char c, c2; | 3329 | char c, c2; |
3341 | size_t i; | 3330 | size_t i; |
@@ -3375,7 +3364,7 @@ static BC_STATUS zdc_lex_token(void) | |||
3375 | case '\f': | 3364 | case '\f': |
3376 | case '\r': | 3365 | case '\r': |
3377 | case ' ': | 3366 | case ' ': |
3378 | bc_lex_whitespace(); | 3367 | xc_lex_whitespace(); |
3379 | break; | 3368 | break; |
3380 | case '!': | 3369 | case '!': |
3381 | c2 = *p->lex_inbuf; | 3370 | c2 = *p->lex_inbuf; |
@@ -3390,11 +3379,11 @@ static BC_STATUS zdc_lex_token(void) | |||
3390 | p->lex_inbuf++; | 3379 | p->lex_inbuf++; |
3391 | break; | 3380 | break; |
3392 | case '#': | 3381 | case '#': |
3393 | bc_lex_lineComment(); | 3382 | xc_lex_lineComment(); |
3394 | break; | 3383 | break; |
3395 | case '.': | 3384 | case '.': |
3396 | if (isdigit(*p->lex_inbuf)) | 3385 | if (isdigit(*p->lex_inbuf)) |
3397 | s = zbc_lex_number(c); | 3386 | s = zxc_lex_number(c); |
3398 | else | 3387 | else |
3399 | s = bc_error_bad_character(c); | 3388 | s = bc_error_bad_character(c); |
3400 | break; | 3389 | break; |
@@ -3414,7 +3403,7 @@ static BC_STATUS zdc_lex_token(void) | |||
3414 | case 'D': | 3403 | case 'D': |
3415 | case 'E': | 3404 | case 'E': |
3416 | case 'F': | 3405 | case 'F': |
3417 | s = zbc_lex_number(c); | 3406 | s = zxc_lex_number(c); |
3418 | break; | 3407 | break; |
3419 | case '[': | 3408 | case '[': |
3420 | s = zdc_lex_string(); | 3409 | s = zdc_lex_string(); |
@@ -3430,14 +3419,14 @@ static BC_STATUS zdc_lex_token(void) | |||
3430 | #define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS) | 3419 | #define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
3431 | #endif // ENABLE_DC | 3420 | #endif // ENABLE_DC |
3432 | 3421 | ||
3433 | static void bc_parse_push(char i) | 3422 | static void xc_parse_push(char i) |
3434 | { | 3423 | { |
3435 | BcVec *code = &G.prs.func->code; | 3424 | BcVec *code = &G.prs.func->code; |
3436 | dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, code->len, i); | 3425 | dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, code->len, i); |
3437 | bc_vec_pushByte(code, i); | 3426 | bc_vec_pushByte(code, i); |
3438 | } | 3427 | } |
3439 | 3428 | ||
3440 | static void bc_parse_pushName(char *name) | 3429 | static void xc_parse_pushName(char *name) |
3441 | { | 3430 | { |
3442 | #if 1 | 3431 | #if 1 |
3443 | BcVec *code = &G.prs.func->code; | 3432 | BcVec *code = &G.prs.func->code; |
@@ -3450,7 +3439,7 @@ static void bc_parse_pushName(char *name) | |||
3450 | #else | 3439 | #else |
3451 | // Smaller code, but way slow: | 3440 | // Smaller code, but way slow: |
3452 | do { | 3441 | do { |
3453 | bc_parse_push(*name); | 3442 | xc_parse_push(*name); |
3454 | } while (*name++); | 3443 | } while (*name++); |
3455 | #endif | 3444 | #endif |
3456 | } | 3445 | } |
@@ -3461,7 +3450,7 @@ static void bc_parse_pushName(char *name) | |||
3461 | // (The above describes 32-bit case). | 3450 | // (The above describes 32-bit case). |
3462 | #define SMALL_INDEX_LIMIT (0x100 - sizeof(size_t)) | 3451 | #define SMALL_INDEX_LIMIT (0x100 - sizeof(size_t)) |
3463 | 3452 | ||
3464 | static void bc_parse_pushIndex(size_t idx) | 3453 | static void xc_parse_pushIndex(size_t idx) |
3465 | { | 3454 | { |
3466 | size_t mask; | 3455 | size_t mask; |
3467 | unsigned amt; | 3456 | unsigned amt; |
@@ -3479,11 +3468,11 @@ static void bc_parse_pushIndex(size_t idx) | |||
3479 | amt--; | 3468 | amt--; |
3480 | } while (amt != 0); | 3469 | } while (amt != 0); |
3481 | 3470 | ||
3482 | bc_parse_push(SMALL_INDEX_LIMIT + amt); | 3471 | xc_parse_push(SMALL_INDEX_LIMIT + amt); |
3483 | 3472 | ||
3484 | while (idx != 0) { | 3473 | while (idx != 0) { |
3485 | push_idx: | 3474 | push_idx: |
3486 | bc_parse_push((unsigned char)idx); | 3475 | xc_parse_push((unsigned char)idx); |
3487 | idx >>= 8; | 3476 | idx >>= 8; |
3488 | } | 3477 | } |
3489 | } | 3478 | } |
@@ -3491,14 +3480,14 @@ static void bc_parse_pushIndex(size_t idx) | |||
3491 | #if ENABLE_BC | 3480 | #if ENABLE_BC |
3492 | static void bc_parse_pushJUMP(size_t idx) | 3481 | static void bc_parse_pushJUMP(size_t idx) |
3493 | { | 3482 | { |
3494 | bc_parse_push(BC_INST_JUMP); | 3483 | xc_parse_push(BC_INST_JUMP); |
3495 | bc_parse_pushIndex(idx); | 3484 | xc_parse_pushIndex(idx); |
3496 | } | 3485 | } |
3497 | 3486 | ||
3498 | static void bc_parse_pushJUMP_ZERO(size_t idx) | 3487 | static void bc_parse_pushJUMP_ZERO(size_t idx) |
3499 | { | 3488 | { |
3500 | bc_parse_push(BC_INST_JUMP_ZERO); | 3489 | xc_parse_push(BC_INST_JUMP_ZERO); |
3501 | bc_parse_pushIndex(idx); | 3490 | xc_parse_pushIndex(idx); |
3502 | } | 3491 | } |
3503 | 3492 | ||
3504 | static BC_STATUS zbc_parse_pushSTR(void) | 3493 | static BC_STATUS zbc_parse_pushSTR(void) |
@@ -3506,16 +3495,16 @@ static BC_STATUS zbc_parse_pushSTR(void) | |||
3506 | BcParse *p = &G.prs; | 3495 | BcParse *p = &G.prs; |
3507 | char *str = xstrdup(p->lex_strnumbuf.v); | 3496 | char *str = xstrdup(p->lex_strnumbuf.v); |
3508 | 3497 | ||
3509 | bc_parse_push(XC_INST_STR); | 3498 | xc_parse_push(XC_INST_STR); |
3510 | bc_parse_pushIndex(p->func->strs.len); | 3499 | xc_parse_pushIndex(p->func->strs.len); |
3511 | bc_vec_push(&p->func->strs, &str); | 3500 | bc_vec_push(&p->func->strs, &str); |
3512 | 3501 | ||
3513 | RETURN_STATUS(zbc_lex_next()); | 3502 | RETURN_STATUS(zxc_lex_next()); |
3514 | } | 3503 | } |
3515 | #define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS) | 3504 | #define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS) |
3516 | #endif | 3505 | #endif |
3517 | 3506 | ||
3518 | static void bc_parse_pushNUM(void) | 3507 | static void xc_parse_pushNUM(void) |
3519 | { | 3508 | { |
3520 | BcParse *p = &G.prs; | 3509 | BcParse *p = &G.prs; |
3521 | char *num = xstrdup(p->lex_strnumbuf.v); | 3510 | char *num = xstrdup(p->lex_strnumbuf.v); |
@@ -3526,22 +3515,22 @@ static void bc_parse_pushNUM(void) | |||
3526 | #else // DC | 3515 | #else // DC |
3527 | size_t idx = bc_vec_push(&G.prog.consts, &num); | 3516 | size_t idx = bc_vec_push(&G.prog.consts, &num); |
3528 | #endif | 3517 | #endif |
3529 | bc_parse_push(XC_INST_NUM); | 3518 | xc_parse_push(XC_INST_NUM); |
3530 | bc_parse_pushIndex(idx); | 3519 | xc_parse_pushIndex(idx); |
3531 | } | 3520 | } |
3532 | 3521 | ||
3533 | static BC_STATUS zbc_parse_text_init(const char *text) | 3522 | static BC_STATUS zxc_parse_text_init(const char *text) |
3534 | { | 3523 | { |
3535 | BcParse *p = &G.prs; | 3524 | G.prs.func = xc_program_func(G.prs.fidx); |
3536 | p->func = bc_program_func(p->fidx); | 3525 | G.prs.lex_inbuf = text; |
3537 | 3526 | G.prs.lex = G.prs.lex_last = XC_LEX_INVALID; | |
3538 | RETURN_STATUS(zbc_lex_text_init(text)); | 3527 | RETURN_STATUS(zxc_lex_next()); |
3539 | } | 3528 | } |
3540 | #define zbc_parse_text_init(...) (zbc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS) | 3529 | #define zxc_parse_text_init(...) (zxc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS) |
3541 | 3530 | ||
3542 | // Called when parsing or execution detects a failure, | 3531 | // Called when parsing or execution detects a failure, |
3543 | // resets execution structures. | 3532 | // resets execution structures. |
3544 | static void bc_program_reset(void) | 3533 | static void xc_program_reset(void) |
3545 | { | 3534 | { |
3546 | BcFunc *f; | 3535 | BcFunc *f; |
3547 | BcInstPtr *ip; | 3536 | BcInstPtr *ip; |
@@ -3549,14 +3538,14 @@ static void bc_program_reset(void) | |||
3549 | bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1); | 3538 | bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1); |
3550 | bc_vec_pop_all(&G.prog.results); | 3539 | bc_vec_pop_all(&G.prog.results); |
3551 | 3540 | ||
3552 | f = bc_program_func_BC_PROG_MAIN(); | 3541 | f = xc_program_func_BC_PROG_MAIN(); |
3553 | ip = bc_vec_top(&G.prog.exestack); | 3542 | ip = bc_vec_top(&G.prog.exestack); |
3554 | ip->inst_idx = f->code.len; | 3543 | ip->inst_idx = f->code.len; |
3555 | } | 3544 | } |
3556 | 3545 | ||
3557 | // Called when parsing code detects a failure, | 3546 | // Called when parsing code detects a failure, |
3558 | // resets parsing structures. | 3547 | // resets parsing structures. |
3559 | static void bc_parse_reset(void) | 3548 | static void xc_parse_reset(void) |
3560 | { | 3549 | { |
3561 | BcParse *p = &G.prs; | 3550 | BcParse *p = &G.prs; |
3562 | if (p->fidx != BC_PROG_MAIN) { | 3551 | if (p->fidx != BC_PROG_MAIN) { |
@@ -3564,7 +3553,7 @@ static void bc_parse_reset(void) | |||
3564 | bc_func_init(p->func); | 3553 | bc_func_init(p->func); |
3565 | 3554 | ||
3566 | p->fidx = BC_PROG_MAIN; | 3555 | p->fidx = BC_PROG_MAIN; |
3567 | p->func = bc_program_func_BC_PROG_MAIN(); | 3556 | p->func = xc_program_func_BC_PROG_MAIN(); |
3568 | } | 3557 | } |
3569 | 3558 | ||
3570 | p->lex_inbuf += strlen(p->lex_inbuf); | 3559 | p->lex_inbuf += strlen(p->lex_inbuf); |
@@ -3574,10 +3563,10 @@ static void bc_parse_reset(void) | |||
3574 | IF_BC(bc_vec_pop_all(&p->conds);) | 3563 | IF_BC(bc_vec_pop_all(&p->conds);) |
3575 | IF_BC(bc_vec_pop_all(&p->ops);) | 3564 | IF_BC(bc_vec_pop_all(&p->ops);) |
3576 | 3565 | ||
3577 | bc_program_reset(); | 3566 | xc_program_reset(); |
3578 | } | 3567 | } |
3579 | 3568 | ||
3580 | static void bc_parse_free(void) | 3569 | static void xc_parse_free(void) |
3581 | { | 3570 | { |
3582 | IF_BC(bc_vec_free(&G.prs.exits);) | 3571 | IF_BC(bc_vec_free(&G.prs.exits);) |
3583 | IF_BC(bc_vec_free(&G.prs.conds);) | 3572 | IF_BC(bc_vec_free(&G.prs.conds);) |
@@ -3585,7 +3574,7 @@ static void bc_parse_free(void) | |||
3585 | bc_vec_free(&G.prs.lex_strnumbuf); | 3574 | bc_vec_free(&G.prs.lex_strnumbuf); |
3586 | } | 3575 | } |
3587 | 3576 | ||
3588 | static void bc_parse_create(size_t fidx) | 3577 | static void xc_parse_create(size_t fidx) |
3589 | { | 3578 | { |
3590 | BcParse *p = &G.prs; | 3579 | BcParse *p = &G.prs; |
3591 | memset(p, 0, sizeof(BcParse)); | 3580 | memset(p, 0, sizeof(BcParse)); |
@@ -3596,10 +3585,10 @@ static void bc_parse_create(size_t fidx) | |||
3596 | IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);) | 3585 | IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);) |
3597 | 3586 | ||
3598 | p->fidx = fidx; | 3587 | p->fidx = fidx; |
3599 | p->func = bc_program_func(fidx); | 3588 | p->func = xc_program_func(fidx); |
3600 | } | 3589 | } |
3601 | 3590 | ||
3602 | static void bc_program_add_fn(void) | 3591 | static void xc_program_add_fn(void) |
3603 | { | 3592 | { |
3604 | //size_t idx; | 3593 | //size_t idx; |
3605 | BcFunc f; | 3594 | BcFunc f; |
@@ -3630,11 +3619,11 @@ static size_t bc_program_addFunc(char *name) | |||
3630 | if (!inserted) { | 3619 | if (!inserted) { |
3631 | // There is already a function with this name. | 3620 | // There is already a function with this name. |
3632 | // It'll be redefined now, clear old definition. | 3621 | // It'll be redefined now, clear old definition. |
3633 | BcFunc *func = bc_program_func(entry_ptr->idx); | 3622 | BcFunc *func = xc_program_func(entry_ptr->idx); |
3634 | bc_func_free(func); | 3623 | bc_func_free(func); |
3635 | bc_func_init(func); | 3624 | bc_func_init(func); |
3636 | } else { | 3625 | } else { |
3637 | bc_program_add_fn(); | 3626 | xc_program_add_fn(); |
3638 | } | 3627 | } |
3639 | 3628 | ||
3640 | return idx; | 3629 | return idx; |
@@ -3695,7 +3684,7 @@ static void bc_parse_operator(BcLexType type, size_t start, size_t *nexprs) | |||
3695 | l = bc_parse_op_PREC(t - XC_LEX_1st_op); | 3684 | l = bc_parse_op_PREC(t - XC_LEX_1st_op); |
3696 | if (l >= r && (l != r || !left)) break; | 3685 | if (l >= r && (l != r || !left)) break; |
3697 | 3686 | ||
3698 | bc_parse_push(BC_TOKEN_2_INST(t)); | 3687 | xc_parse_push(BC_TOKEN_2_INST(t)); |
3699 | bc_vec_pop(&p->ops); | 3688 | bc_vec_pop(&p->ops); |
3700 | *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != XC_LEX_NEG); | 3689 | *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != XC_LEX_NEG); |
3701 | } | 3690 | } |
@@ -3713,7 +3702,7 @@ static BC_STATUS zbc_parse_rightParen(size_t ops_bgn, size_t *nexs) | |||
3713 | top = BC_PARSE_TOP_OP(p); | 3702 | top = BC_PARSE_TOP_OP(p); |
3714 | 3703 | ||
3715 | while (top != BC_LEX_LPAREN) { | 3704 | while (top != BC_LEX_LPAREN) { |
3716 | bc_parse_push(BC_TOKEN_2_INST(top)); | 3705 | xc_parse_push(BC_TOKEN_2_INST(top)); |
3717 | 3706 | ||
3718 | bc_vec_pop(&p->ops); | 3707 | bc_vec_pop(&p->ops); |
3719 | *nexs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG); | 3708 | *nexs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG); |
@@ -3738,7 +3727,7 @@ static BC_STATUS zbc_parse_params(uint8_t flags) | |||
3738 | dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex); | 3727 | dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex); |
3739 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; | 3728 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; |
3740 | 3729 | ||
3741 | s = zbc_lex_next(); | 3730 | s = zxc_lex_next(); |
3742 | if (s) RETURN_STATUS(s); | 3731 | if (s) RETURN_STATUS(s); |
3743 | 3732 | ||
3744 | nparams = 0; | 3733 | nparams = 0; |
@@ -3752,13 +3741,13 @@ static BC_STATUS zbc_parse_params(uint8_t flags) | |||
3752 | break; | 3741 | break; |
3753 | RETURN_STATUS(bc_error_bad_token()); | 3742 | RETURN_STATUS(bc_error_bad_token()); |
3754 | } | 3743 | } |
3755 | s = zbc_lex_next(); | 3744 | s = zxc_lex_next(); |
3756 | if (s) RETURN_STATUS(s); | 3745 | if (s) RETURN_STATUS(s); |
3757 | } | 3746 | } |
3758 | } | 3747 | } |
3759 | 3748 | ||
3760 | bc_parse_push(BC_INST_CALL); | 3749 | xc_parse_push(BC_INST_CALL); |
3761 | bc_parse_pushIndex(nparams); | 3750 | xc_parse_pushIndex(nparams); |
3762 | 3751 | ||
3763 | RETURN_STATUS(BC_STATUS_SUCCESS); | 3752 | RETURN_STATUS(BC_STATUS_SUCCESS); |
3764 | } | 3753 | } |
@@ -3792,9 +3781,9 @@ static BC_STATUS zbc_parse_call(char *name, uint8_t flags) | |||
3792 | free(name); | 3781 | free(name); |
3793 | 3782 | ||
3794 | entry_ptr = bc_vec_item(&G.prog.fn_map, idx); | 3783 | entry_ptr = bc_vec_item(&G.prog.fn_map, idx); |
3795 | bc_parse_pushIndex(entry_ptr->idx); | 3784 | xc_parse_pushIndex(entry_ptr->idx); |
3796 | 3785 | ||
3797 | RETURN_STATUS(zbc_lex_next()); | 3786 | RETURN_STATUS(zxc_lex_next()); |
3798 | err: | 3787 | err: |
3799 | free(name); | 3788 | free(name); |
3800 | RETURN_STATUS(s); | 3789 | RETURN_STATUS(s); |
@@ -3808,11 +3797,11 @@ static BC_STATUS zbc_parse_name(BcInst *type, uint8_t flags) | |||
3808 | char *name; | 3797 | char *name; |
3809 | 3798 | ||
3810 | name = xstrdup(p->lex_strnumbuf.v); | 3799 | name = xstrdup(p->lex_strnumbuf.v); |
3811 | s = zbc_lex_next(); | 3800 | s = zxc_lex_next(); |
3812 | if (s) goto err; | 3801 | if (s) goto err; |
3813 | 3802 | ||
3814 | if (p->lex == BC_LEX_LBRACKET) { | 3803 | if (p->lex == BC_LEX_LBRACKET) { |
3815 | s = zbc_lex_next(); | 3804 | s = zxc_lex_next(); |
3816 | if (s) goto err; | 3805 | if (s) goto err; |
3817 | 3806 | ||
3818 | if (p->lex == BC_LEX_RBRACKET) { | 3807 | if (p->lex == BC_LEX_RBRACKET) { |
@@ -3827,10 +3816,10 @@ static BC_STATUS zbc_parse_name(BcInst *type, uint8_t flags) | |||
3827 | s = zbc_parse_expr(flags); | 3816 | s = zbc_parse_expr(flags); |
3828 | if (s) goto err; | 3817 | if (s) goto err; |
3829 | } | 3818 | } |
3830 | s = zbc_lex_next(); | 3819 | s = zxc_lex_next(); |
3831 | if (s) goto err; | 3820 | if (s) goto err; |
3832 | bc_parse_push(*type); | 3821 | xc_parse_push(*type); |
3833 | bc_parse_pushName(name); | 3822 | xc_parse_pushName(name); |
3834 | free(name); | 3823 | free(name); |
3835 | } else if (p->lex == BC_LEX_LPAREN) { | 3824 | } else if (p->lex == BC_LEX_LPAREN) { |
3836 | if (flags & BC_PARSE_NOCALL) { | 3825 | if (flags & BC_PARSE_NOCALL) { |
@@ -3841,8 +3830,8 @@ static BC_STATUS zbc_parse_name(BcInst *type, uint8_t flags) | |||
3841 | s = zbc_parse_call(name, flags); | 3830 | s = zbc_parse_call(name, flags); |
3842 | } else { | 3831 | } else { |
3843 | *type = XC_INST_VAR; | 3832 | *type = XC_INST_VAR; |
3844 | bc_parse_push(XC_INST_VAR); | 3833 | xc_parse_push(XC_INST_VAR); |
3845 | bc_parse_pushName(name); | 3834 | xc_parse_pushName(name); |
3846 | free(name); | 3835 | free(name); |
3847 | } | 3836 | } |
3848 | 3837 | ||
@@ -3858,15 +3847,15 @@ static BC_STATUS zbc_parse_read(void) | |||
3858 | BcParse *p = &G.prs; | 3847 | BcParse *p = &G.prs; |
3859 | BcStatus s; | 3848 | BcStatus s; |
3860 | 3849 | ||
3861 | s = zbc_lex_next(); | 3850 | s = zxc_lex_next(); |
3862 | if (s) RETURN_STATUS(s); | 3851 | if (s) RETURN_STATUS(s); |
3863 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); | 3852 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
3864 | 3853 | ||
3865 | s = zbc_lex_next(); | 3854 | s = zxc_lex_next(); |
3866 | if (s) RETURN_STATUS(s); | 3855 | if (s) RETURN_STATUS(s); |
3867 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); | 3856 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
3868 | 3857 | ||
3869 | bc_parse_push(XC_INST_READ); | 3858 | xc_parse_push(XC_INST_READ); |
3870 | 3859 | ||
3871 | RETURN_STATUS(s); | 3860 | RETURN_STATUS(s); |
3872 | } | 3861 | } |
@@ -3877,13 +3866,13 @@ static BC_STATUS zbc_parse_builtin(BcLexType type, uint8_t flags, BcInst *prev) | |||
3877 | BcParse *p = &G.prs; | 3866 | BcParse *p = &G.prs; |
3878 | BcStatus s; | 3867 | BcStatus s; |
3879 | 3868 | ||
3880 | s = zbc_lex_next(); | 3869 | s = zxc_lex_next(); |
3881 | if (s) RETURN_STATUS(s); | 3870 | if (s) RETURN_STATUS(s); |
3882 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); | 3871 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
3883 | 3872 | ||
3884 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; | 3873 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; |
3885 | 3874 | ||
3886 | s = zbc_lex_next(); | 3875 | s = zxc_lex_next(); |
3887 | if (s) RETURN_STATUS(s); | 3876 | if (s) RETURN_STATUS(s); |
3888 | 3877 | ||
3889 | s = zbc_parse_expr(flags); | 3878 | s = zbc_parse_expr(flags); |
@@ -3892,7 +3881,7 @@ static BC_STATUS zbc_parse_builtin(BcLexType type, uint8_t flags, BcInst *prev) | |||
3892 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); | 3881 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
3893 | 3882 | ||
3894 | *prev = (type == BC_LEX_KEY_LENGTH) ? XC_INST_LENGTH : XC_INST_SQRT; | 3883 | *prev = (type == BC_LEX_KEY_LENGTH) ? XC_INST_LENGTH : XC_INST_SQRT; |
3895 | bc_parse_push(*prev); | 3884 | xc_parse_push(*prev); |
3896 | 3885 | ||
3897 | RETURN_STATUS(s); | 3886 | RETURN_STATUS(s); |
3898 | } | 3887 | } |
@@ -3903,28 +3892,28 @@ static BC_STATUS zbc_parse_scale(BcInst *type, uint8_t flags) | |||
3903 | BcParse *p = &G.prs; | 3892 | BcParse *p = &G.prs; |
3904 | BcStatus s; | 3893 | BcStatus s; |
3905 | 3894 | ||
3906 | s = zbc_lex_next(); | 3895 | s = zxc_lex_next(); |
3907 | if (s) RETURN_STATUS(s); | 3896 | if (s) RETURN_STATUS(s); |
3908 | 3897 | ||
3909 | if (p->lex != BC_LEX_LPAREN) { | 3898 | if (p->lex != BC_LEX_LPAREN) { |
3910 | *type = XC_INST_SCALE; | 3899 | *type = XC_INST_SCALE; |
3911 | bc_parse_push(XC_INST_SCALE); | 3900 | xc_parse_push(XC_INST_SCALE); |
3912 | RETURN_STATUS(BC_STATUS_SUCCESS); | 3901 | RETURN_STATUS(BC_STATUS_SUCCESS); |
3913 | } | 3902 | } |
3914 | 3903 | ||
3915 | *type = XC_INST_SCALE_FUNC; | 3904 | *type = XC_INST_SCALE_FUNC; |
3916 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); | 3905 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); |
3917 | 3906 | ||
3918 | s = zbc_lex_next(); | 3907 | s = zxc_lex_next(); |
3919 | if (s) RETURN_STATUS(s); | 3908 | if (s) RETURN_STATUS(s); |
3920 | 3909 | ||
3921 | s = zbc_parse_expr(flags); | 3910 | s = zbc_parse_expr(flags); |
3922 | if (s) RETURN_STATUS(s); | 3911 | if (s) RETURN_STATUS(s); |
3923 | if (p->lex != BC_LEX_RPAREN) | 3912 | if (p->lex != BC_LEX_RPAREN) |
3924 | RETURN_STATUS(bc_error_bad_token()); | 3913 | RETURN_STATUS(bc_error_bad_token()); |
3925 | bc_parse_push(XC_INST_SCALE_FUNC); | 3914 | xc_parse_push(XC_INST_SCALE_FUNC); |
3926 | 3915 | ||
3927 | RETURN_STATUS(zbc_lex_next()); | 3916 | RETURN_STATUS(zxc_lex_next()); |
3928 | } | 3917 | } |
3929 | #define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS) | 3918 | #define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS) |
3930 | 3919 | ||
@@ -3942,13 +3931,13 @@ static BC_STATUS zbc_parse_incdec(BcInst *prev, bool *paren_expr, | |||
3942 | || etype == XC_INST_IBASE || etype == XC_INST_OBASE | 3931 | || etype == XC_INST_IBASE || etype == XC_INST_OBASE |
3943 | ) { | 3932 | ) { |
3944 | *prev = inst = BC_INST_INC_POST + (p->lex != BC_LEX_OP_INC); | 3933 | *prev = inst = BC_INST_INC_POST + (p->lex != BC_LEX_OP_INC); |
3945 | bc_parse_push(inst); | 3934 | xc_parse_push(inst); |
3946 | s = zbc_lex_next(); | 3935 | s = zxc_lex_next(); |
3947 | } else { | 3936 | } else { |
3948 | *prev = inst = BC_INST_INC_PRE + (p->lex != BC_LEX_OP_INC); | 3937 | *prev = inst = BC_INST_INC_PRE + (p->lex != BC_LEX_OP_INC); |
3949 | *paren_expr = true; | 3938 | *paren_expr = true; |
3950 | 3939 | ||
3951 | s = zbc_lex_next(); | 3940 | s = zxc_lex_next(); |
3952 | if (s) RETURN_STATUS(s); | 3941 | if (s) RETURN_STATUS(s); |
3953 | type = p->lex; | 3942 | type = p->lex; |
3954 | 3943 | ||
@@ -3963,23 +3952,23 @@ static BC_STATUS zbc_parse_incdec(BcInst *prev, bool *paren_expr, | |||
3963 | case BC_LEX_KEY_IBASE: | 3952 | case BC_LEX_KEY_IBASE: |
3964 | case BC_LEX_KEY_LAST: | 3953 | case BC_LEX_KEY_LAST: |
3965 | case BC_LEX_KEY_OBASE: | 3954 | case BC_LEX_KEY_OBASE: |
3966 | bc_parse_push(type - BC_LEX_KEY_IBASE + XC_INST_IBASE); | 3955 | xc_parse_push(type - BC_LEX_KEY_IBASE + XC_INST_IBASE); |
3967 | s = zbc_lex_next(); | 3956 | s = zxc_lex_next(); |
3968 | break; | 3957 | break; |
3969 | case BC_LEX_KEY_SCALE: | 3958 | case BC_LEX_KEY_SCALE: |
3970 | s = zbc_lex_next(); | 3959 | s = zxc_lex_next(); |
3971 | if (s) RETURN_STATUS(s); | 3960 | if (s) RETURN_STATUS(s); |
3972 | if (p->lex == BC_LEX_LPAREN) | 3961 | if (p->lex == BC_LEX_LPAREN) |
3973 | s = bc_error_bad_token(); | 3962 | s = bc_error_bad_token(); |
3974 | else | 3963 | else |
3975 | bc_parse_push(XC_INST_SCALE); | 3964 | xc_parse_push(XC_INST_SCALE); |
3976 | break; | 3965 | break; |
3977 | default: | 3966 | default: |
3978 | s = bc_error_bad_token(); | 3967 | s = bc_error_bad_token(); |
3979 | break; | 3968 | break; |
3980 | } | 3969 | } |
3981 | 3970 | ||
3982 | if (!s) bc_parse_push(inst); | 3971 | if (!s) xc_parse_push(inst); |
3983 | } | 3972 | } |
3984 | 3973 | ||
3985 | RETURN_STATUS(s); | 3974 | RETURN_STATUS(s); |
@@ -4012,7 +4001,7 @@ static BC_STATUS zbc_parse_minus(BcInst *prev, size_t ops_bgn, | |||
4012 | BcLexType type; | 4001 | BcLexType type; |
4013 | BcInst etype = *prev; | 4002 | BcInst etype = *prev; |
4014 | 4003 | ||
4015 | s = zbc_lex_next(); | 4004 | s = zxc_lex_next(); |
4016 | if (s) RETURN_STATUS(s); | 4005 | if (s) RETURN_STATUS(s); |
4017 | 4006 | ||
4018 | type = BC_PARSE_LEAF(etype, rparen) ? XC_LEX_OP_MINUS : XC_LEX_NEG; | 4007 | type = BC_PARSE_LEAF(etype, rparen) ? XC_LEX_OP_MINUS : XC_LEX_NEG; |
@@ -4036,7 +4025,7 @@ static BC_STATUS zbc_parse_print(void) | |||
4036 | BcLexType type; | 4025 | BcLexType type; |
4037 | 4026 | ||
4038 | for (;;) { | 4027 | for (;;) { |
4039 | s = zbc_lex_next(); | 4028 | s = zxc_lex_next(); |
4040 | if (s) RETURN_STATUS(s); | 4029 | if (s) RETURN_STATUS(s); |
4041 | type = p->lex; | 4030 | type = p->lex; |
4042 | if (type == XC_LEX_STR) { | 4031 | if (type == XC_LEX_STR) { |
@@ -4045,7 +4034,7 @@ static BC_STATUS zbc_parse_print(void) | |||
4045 | s = zbc_parse_expr(0); | 4034 | s = zbc_parse_expr(0); |
4046 | } | 4035 | } |
4047 | if (s) RETURN_STATUS(s); | 4036 | if (s) RETURN_STATUS(s); |
4048 | bc_parse_push(XC_INST_PRINT_POP); | 4037 | xc_parse_push(XC_INST_PRINT_POP); |
4049 | if (p->lex != BC_LEX_COMMA) | 4038 | if (p->lex != BC_LEX_COMMA) |
4050 | break; | 4039 | break; |
4051 | } | 4040 | } |
@@ -4061,18 +4050,18 @@ static BC_STATUS zbc_parse_return(void) | |||
4061 | BcLexType t; | 4050 | BcLexType t; |
4062 | 4051 | ||
4063 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); | 4052 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
4064 | s = zbc_lex_next(); | 4053 | s = zxc_lex_next(); |
4065 | if (s) RETURN_STATUS(s); | 4054 | if (s) RETURN_STATUS(s); |
4066 | 4055 | ||
4067 | t = p->lex; | 4056 | t = p->lex; |
4068 | if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON) | 4057 | if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON) |
4069 | bc_parse_push(BC_INST_RET0); | 4058 | xc_parse_push(BC_INST_RET0); |
4070 | else { | 4059 | else { |
4071 | bool paren = (t == BC_LEX_LPAREN); | 4060 | bool paren = (t == BC_LEX_LPAREN); |
4072 | s = bc_parse_expr_empty_ok(0); | 4061 | s = bc_parse_expr_empty_ok(0); |
4073 | if (s == BC_STATUS_PARSE_EMPTY_EXP) { | 4062 | if (s == BC_STATUS_PARSE_EMPTY_EXP) { |
4074 | bc_parse_push(BC_INST_RET0); | 4063 | xc_parse_push(BC_INST_RET0); |
4075 | s = zbc_lex_next(); | 4064 | s = zxc_lex_next(); |
4076 | } | 4065 | } |
4077 | if (s) RETURN_STATUS(s); | 4066 | if (s) RETURN_STATUS(s); |
4078 | 4067 | ||
@@ -4081,7 +4070,7 @@ static BC_STATUS zbc_parse_return(void) | |||
4081 | if (s) RETURN_STATUS(s); | 4070 | if (s) RETURN_STATUS(s); |
4082 | } | 4071 | } |
4083 | 4072 | ||
4084 | bc_parse_push(XC_INST_RET); | 4073 | xc_parse_push(XC_INST_RET); |
4085 | } | 4074 | } |
4086 | 4075 | ||
4087 | dbg_lex_done("%s:%d done", __func__, __LINE__); | 4076 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
@@ -4103,11 +4092,11 @@ static BC_STATUS zbc_parse_if(void) | |||
4103 | size_t ip_idx; | 4092 | size_t ip_idx; |
4104 | 4093 | ||
4105 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); | 4094 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
4106 | s = zbc_lex_next(); | 4095 | s = zxc_lex_next(); |
4107 | if (s) RETURN_STATUS(s); | 4096 | if (s) RETURN_STATUS(s); |
4108 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); | 4097 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
4109 | 4098 | ||
4110 | s = zbc_lex_next(); | 4099 | s = zxc_lex_next(); |
4111 | if (s) RETURN_STATUS(s); | 4100 | if (s) RETURN_STATUS(s); |
4112 | s = zbc_parse_expr(BC_PARSE_REL); | 4101 | s = zbc_parse_expr(BC_PARSE_REL); |
4113 | if (s) RETURN_STATUS(s); | 4102 | if (s) RETURN_STATUS(s); |
@@ -4155,10 +4144,10 @@ static BC_STATUS zbc_parse_while(void) | |||
4155 | size_t cond_idx; | 4144 | size_t cond_idx; |
4156 | size_t ip_idx; | 4145 | size_t ip_idx; |
4157 | 4146 | ||
4158 | s = zbc_lex_next(); | 4147 | s = zxc_lex_next(); |
4159 | if (s) RETURN_STATUS(s); | 4148 | if (s) RETURN_STATUS(s); |
4160 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); | 4149 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
4161 | s = zbc_lex_next(); | 4150 | s = zxc_lex_next(); |
4162 | if (s) RETURN_STATUS(s); | 4151 | if (s) RETURN_STATUS(s); |
4163 | 4152 | ||
4164 | cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len); | 4153 | cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len); |
@@ -4197,15 +4186,15 @@ static BC_STATUS zbc_parse_for(void) | |||
4197 | size_t cond_idx, exit_idx, body_idx, update_idx; | 4186 | size_t cond_idx, exit_idx, body_idx, update_idx; |
4198 | 4187 | ||
4199 | dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex); | 4188 | dbg_lex("%s:%d p->lex:%d", __func__, __LINE__, p->lex); |
4200 | s = zbc_lex_next(); | 4189 | s = zxc_lex_next(); |
4201 | if (s) RETURN_STATUS(s); | 4190 | if (s) RETURN_STATUS(s); |
4202 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); | 4191 | if (p->lex != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
4203 | s = zbc_lex_next(); | 4192 | s = zxc_lex_next(); |
4204 | if (s) RETURN_STATUS(s); | 4193 | if (s) RETURN_STATUS(s); |
4205 | 4194 | ||
4206 | if (p->lex != BC_LEX_SCOLON) { | 4195 | if (p->lex != BC_LEX_SCOLON) { |
4207 | s = zbc_parse_expr(0); | 4196 | s = zbc_parse_expr(0); |
4208 | bc_parse_push(XC_INST_POP); | 4197 | xc_parse_push(XC_INST_POP); |
4209 | if (s) RETURN_STATUS(s); | 4198 | if (s) RETURN_STATUS(s); |
4210 | } else { | 4199 | } else { |
4211 | s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("init"); | 4200 | s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("init"); |
@@ -4213,7 +4202,7 @@ static BC_STATUS zbc_parse_for(void) | |||
4213 | } | 4202 | } |
4214 | 4203 | ||
4215 | if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token()); | 4204 | if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token()); |
4216 | s = zbc_lex_next(); | 4205 | s = zxc_lex_next(); |
4217 | if (s) RETURN_STATUS(s); | 4206 | if (s) RETURN_STATUS(s); |
4218 | 4207 | ||
4219 | cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len); | 4208 | cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len); |
@@ -4224,18 +4213,18 @@ static BC_STATUS zbc_parse_for(void) | |||
4224 | if (p->lex != BC_LEX_SCOLON) | 4213 | if (p->lex != BC_LEX_SCOLON) |
4225 | s = zbc_parse_expr(BC_PARSE_REL); | 4214 | s = zbc_parse_expr(BC_PARSE_REL); |
4226 | else { | 4215 | else { |
4227 | // Set this for the next call to bc_parse_pushNUM(). | 4216 | // Set this for the next call to xc_parse_pushNUM(). |
4228 | // This is safe to set because the current token is a semicolon, | 4217 | // This is safe to set because the current token is a semicolon, |
4229 | // which has no string requirement. | 4218 | // which has no string requirement. |
4230 | bc_vec_string(&p->lex_strnumbuf, 1, "1"); | 4219 | bc_vec_string(&p->lex_strnumbuf, 1, "1"); |
4231 | bc_parse_pushNUM(); | 4220 | xc_parse_pushNUM(); |
4232 | s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("condition"); | 4221 | s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("condition"); |
4233 | } | 4222 | } |
4234 | if (s) RETURN_STATUS(s); | 4223 | if (s) RETURN_STATUS(s); |
4235 | 4224 | ||
4236 | if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token()); | 4225 | if (p->lex != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token()); |
4237 | 4226 | ||
4238 | s = zbc_lex_next(); | 4227 | s = zxc_lex_next(); |
4239 | if (s) RETURN_STATUS(s); | 4228 | if (s) RETURN_STATUS(s); |
4240 | 4229 | ||
4241 | bc_parse_pushJUMP_ZERO(exit_idx); | 4230 | bc_parse_pushJUMP_ZERO(exit_idx); |
@@ -4248,7 +4237,7 @@ static BC_STATUS zbc_parse_for(void) | |||
4248 | s = zbc_parse_expr(0); | 4237 | s = zbc_parse_expr(0); |
4249 | if (s) RETURN_STATUS(s); | 4238 | if (s) RETURN_STATUS(s); |
4250 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); | 4239 | if (p->lex != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
4251 | bc_parse_push(XC_INST_POP); | 4240 | xc_parse_push(XC_INST_POP); |
4252 | } else { | 4241 | } else { |
4253 | s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("update"); | 4242 | s = zbc_POSIX_does_not_allow_empty_X_expression_in_for("update"); |
4254 | if (s) RETURN_STATUS(s); | 4243 | if (s) RETURN_STATUS(s); |
@@ -4290,7 +4279,7 @@ static BC_STATUS zbc_parse_break_or_continue(BcLexType type) | |||
4290 | } | 4279 | } |
4291 | bc_parse_pushJUMP(i); | 4280 | bc_parse_pushJUMP(i); |
4292 | 4281 | ||
4293 | RETURN_STATUS(zbc_lex_next()); | 4282 | RETURN_STATUS(zxc_lex_next()); |
4294 | } | 4283 | } |
4295 | #define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS) | 4284 | #define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS) |
4296 | 4285 | ||
@@ -4323,20 +4312,20 @@ static BC_STATUS zbc_parse_funcdef(void) | |||
4323 | char *name; | 4312 | char *name; |
4324 | 4313 | ||
4325 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); | 4314 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
4326 | s = zbc_lex_next(); | 4315 | s = zxc_lex_next(); |
4327 | if (s) RETURN_STATUS(s); | 4316 | if (s) RETURN_STATUS(s); |
4328 | if (p->lex != XC_LEX_NAME) | 4317 | if (p->lex != XC_LEX_NAME) |
4329 | RETURN_STATUS(bc_error("bad function definition")); | 4318 | RETURN_STATUS(bc_error("bad function definition")); |
4330 | 4319 | ||
4331 | name = xstrdup(p->lex_strnumbuf.v); | 4320 | name = xstrdup(p->lex_strnumbuf.v); |
4332 | p->fidx = bc_program_addFunc(name); | 4321 | p->fidx = bc_program_addFunc(name); |
4333 | p->func = bc_program_func(p->fidx); | 4322 | p->func = xc_program_func(p->fidx); |
4334 | 4323 | ||
4335 | s = zbc_lex_next(); | 4324 | s = zxc_lex_next(); |
4336 | if (s) RETURN_STATUS(s); | 4325 | if (s) RETURN_STATUS(s); |
4337 | if (p->lex != BC_LEX_LPAREN) | 4326 | if (p->lex != BC_LEX_LPAREN) |
4338 | RETURN_STATUS(bc_error("bad function definition")); | 4327 | RETURN_STATUS(bc_error("bad function definition")); |
4339 | s = zbc_lex_next(); | 4328 | s = zxc_lex_next(); |
4340 | if (s) RETURN_STATUS(s); | 4329 | if (s) RETURN_STATUS(s); |
4341 | 4330 | ||
4342 | while (p->lex != BC_LEX_RPAREN) { | 4331 | while (p->lex != BC_LEX_RPAREN) { |
@@ -4346,13 +4335,13 @@ static BC_STATUS zbc_parse_funcdef(void) | |||
4346 | ++p->func->nparams; | 4335 | ++p->func->nparams; |
4347 | 4336 | ||
4348 | name = xstrdup(p->lex_strnumbuf.v); | 4337 | name = xstrdup(p->lex_strnumbuf.v); |
4349 | s = zbc_lex_next(); | 4338 | s = zxc_lex_next(); |
4350 | if (s) goto err; | 4339 | if (s) goto err; |
4351 | 4340 | ||
4352 | var = p->lex != BC_LEX_LBRACKET; | 4341 | var = p->lex != BC_LEX_LBRACKET; |
4353 | 4342 | ||
4354 | if (!var) { | 4343 | if (!var) { |
4355 | s = zbc_lex_next(); | 4344 | s = zxc_lex_next(); |
4356 | if (s) goto err; | 4345 | if (s) goto err; |
4357 | 4346 | ||
4358 | if (p->lex != BC_LEX_RBRACKET) { | 4347 | if (p->lex != BC_LEX_RBRACKET) { |
@@ -4360,13 +4349,13 @@ static BC_STATUS zbc_parse_funcdef(void) | |||
4360 | goto err; | 4349 | goto err; |
4361 | } | 4350 | } |
4362 | 4351 | ||
4363 | s = zbc_lex_next(); | 4352 | s = zxc_lex_next(); |
4364 | if (s) goto err; | 4353 | if (s) goto err; |
4365 | } | 4354 | } |
4366 | 4355 | ||
4367 | comma = p->lex == BC_LEX_COMMA; | 4356 | comma = p->lex == BC_LEX_COMMA; |
4368 | if (comma) { | 4357 | if (comma) { |
4369 | s = zbc_lex_next(); | 4358 | s = zxc_lex_next(); |
4370 | if (s) goto err; | 4359 | if (s) goto err; |
4371 | } | 4360 | } |
4372 | 4361 | ||
@@ -4376,7 +4365,7 @@ static BC_STATUS zbc_parse_funcdef(void) | |||
4376 | 4365 | ||
4377 | if (comma) RETURN_STATUS(bc_error("bad function definition")); | 4366 | if (comma) RETURN_STATUS(bc_error("bad function definition")); |
4378 | 4367 | ||
4379 | s = zbc_lex_next(); | 4368 | s = zxc_lex_next(); |
4380 | if (s) RETURN_STATUS(s); | 4369 | if (s) RETURN_STATUS(s); |
4381 | 4370 | ||
4382 | if (p->lex != BC_LEX_LBRACE) { | 4371 | if (p->lex != BC_LEX_LBRACE) { |
@@ -4396,11 +4385,11 @@ static BC_STATUS zbc_parse_funcdef(void) | |||
4396 | p->in_funcdef--; | 4385 | p->in_funcdef--; |
4397 | if (s) RETURN_STATUS(s); | 4386 | if (s) RETURN_STATUS(s); |
4398 | 4387 | ||
4399 | bc_parse_push(BC_INST_RET0); | 4388 | xc_parse_push(BC_INST_RET0); |
4400 | 4389 | ||
4401 | // Subsequent code generation is into main program | 4390 | // Subsequent code generation is into main program |
4402 | p->fidx = BC_PROG_MAIN; | 4391 | p->fidx = BC_PROG_MAIN; |
4403 | p->func = bc_program_func_BC_PROG_MAIN(); | 4392 | p->func = xc_program_func_BC_PROG_MAIN(); |
4404 | 4393 | ||
4405 | dbg_lex_done("%s:%d done", __func__, __LINE__); | 4394 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
4406 | RETURN_STATUS(s); | 4395 | RETURN_STATUS(s); |
@@ -4418,7 +4407,7 @@ static BC_STATUS zbc_parse_auto(void) | |||
4418 | char *name; | 4407 | char *name; |
4419 | 4408 | ||
4420 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); | 4409 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
4421 | s = zbc_lex_next(); | 4410 | s = zxc_lex_next(); |
4422 | if (s) RETURN_STATUS(s); | 4411 | if (s) RETURN_STATUS(s); |
4423 | 4412 | ||
4424 | for (;;) { | 4413 | for (;;) { |
@@ -4428,19 +4417,19 @@ static BC_STATUS zbc_parse_auto(void) | |||
4428 | RETURN_STATUS(bc_error("bad 'auto' syntax")); | 4417 | RETURN_STATUS(bc_error("bad 'auto' syntax")); |
4429 | 4418 | ||
4430 | name = xstrdup(p->lex_strnumbuf.v); | 4419 | name = xstrdup(p->lex_strnumbuf.v); |
4431 | s = zbc_lex_next(); | 4420 | s = zxc_lex_next(); |
4432 | if (s) goto err; | 4421 | if (s) goto err; |
4433 | 4422 | ||
4434 | var = (p->lex != BC_LEX_LBRACKET); | 4423 | var = (p->lex != BC_LEX_LBRACKET); |
4435 | if (!var) { | 4424 | if (!var) { |
4436 | s = zbc_lex_next(); | 4425 | s = zxc_lex_next(); |
4437 | if (s) goto err; | 4426 | if (s) goto err; |
4438 | 4427 | ||
4439 | if (p->lex != BC_LEX_RBRACKET) { | 4428 | if (p->lex != BC_LEX_RBRACKET) { |
4440 | s = bc_error("bad 'auto' syntax"); | 4429 | s = bc_error("bad 'auto' syntax"); |
4441 | goto err; | 4430 | goto err; |
4442 | } | 4431 | } |
4443 | s = zbc_lex_next(); | 4432 | s = zxc_lex_next(); |
4444 | if (s) goto err; | 4433 | if (s) goto err; |
4445 | } | 4434 | } |
4446 | 4435 | ||
@@ -4455,7 +4444,7 @@ static BC_STATUS zbc_parse_auto(void) | |||
4455 | } | 4444 | } |
4456 | if (p->lex != BC_LEX_COMMA) | 4445 | if (p->lex != BC_LEX_COMMA) |
4457 | RETURN_STATUS(bc_error("bad 'auto' syntax")); | 4446 | RETURN_STATUS(bc_error("bad 'auto' syntax")); |
4458 | s = zbc_lex_next(); // skip comma | 4447 | s = zxc_lex_next(); // skip comma |
4459 | if (s) RETURN_STATUS(s); | 4448 | if (s) RETURN_STATUS(s); |
4460 | } | 4449 | } |
4461 | 4450 | ||
@@ -4478,17 +4467,17 @@ static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed) | |||
4478 | 4467 | ||
4479 | if (p->lex == XC_LEX_NLINE) { | 4468 | if (p->lex == XC_LEX_NLINE) { |
4480 | dbg_lex_done("%s:%d done (seen XC_LEX_NLINE)", __func__, __LINE__); | 4469 | dbg_lex_done("%s:%d done (seen XC_LEX_NLINE)", __func__, __LINE__); |
4481 | RETURN_STATUS(zbc_lex_next()); | 4470 | RETURN_STATUS(zxc_lex_next()); |
4482 | } | 4471 | } |
4483 | if (p->lex == BC_LEX_SCOLON) { | 4472 | if (p->lex == BC_LEX_SCOLON) { |
4484 | dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__); | 4473 | dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__); |
4485 | RETURN_STATUS(zbc_lex_next()); | 4474 | RETURN_STATUS(zxc_lex_next()); |
4486 | } | 4475 | } |
4487 | 4476 | ||
4488 | if (p->lex == BC_LEX_LBRACE) { | 4477 | if (p->lex == BC_LEX_LBRACE) { |
4489 | dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed); | 4478 | dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed); |
4490 | do { | 4479 | do { |
4491 | s = zbc_lex_next(); | 4480 | s = zxc_lex_next(); |
4492 | if (s) RETURN_STATUS(s); | 4481 | if (s) RETURN_STATUS(s); |
4493 | } while (p->lex == XC_LEX_NLINE); | 4482 | } while (p->lex == XC_LEX_NLINE); |
4494 | if (auto_allowed && p->lex == BC_LEX_KEY_AUTO) { | 4483 | if (auto_allowed && p->lex == BC_LEX_KEY_AUTO) { |
@@ -4501,7 +4490,7 @@ static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed) | |||
4501 | s = zbc_parse_stmt(); | 4490 | s = zbc_parse_stmt(); |
4502 | if (s) RETURN_STATUS(s); | 4491 | if (s) RETURN_STATUS(s); |
4503 | } | 4492 | } |
4504 | s = zbc_lex_next(); | 4493 | s = zxc_lex_next(); |
4505 | dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__); | 4494 | dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__); |
4506 | RETURN_STATUS(s); | 4495 | RETURN_STATUS(s); |
4507 | } | 4496 | } |
@@ -4526,7 +4515,7 @@ static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed) | |||
4526 | break; | 4515 | break; |
4527 | case XC_LEX_STR: | 4516 | case XC_LEX_STR: |
4528 | s = zbc_parse_pushSTR(); | 4517 | s = zbc_parse_pushSTR(); |
4529 | bc_parse_push(XC_INST_PRINT_STR); | 4518 | xc_parse_push(XC_INST_PRINT_STR); |
4530 | break; | 4519 | break; |
4531 | case BC_LEX_KEY_BREAK: | 4520 | case BC_LEX_KEY_BREAK: |
4532 | case BC_LEX_KEY_CONTINUE: | 4521 | case BC_LEX_KEY_CONTINUE: |
@@ -4536,8 +4525,8 @@ static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed) | |||
4536 | s = zbc_parse_for(); | 4525 | s = zbc_parse_for(); |
4537 | break; | 4526 | break; |
4538 | case BC_LEX_KEY_HALT: | 4527 | case BC_LEX_KEY_HALT: |
4539 | bc_parse_push(BC_INST_HALT); | 4528 | xc_parse_push(BC_INST_HALT); |
4540 | s = zbc_lex_next(); | 4529 | s = zxc_lex_next(); |
4541 | break; | 4530 | break; |
4542 | case BC_LEX_KEY_IF: | 4531 | case BC_LEX_KEY_IF: |
4543 | s = zbc_parse_if(); | 4532 | s = zbc_parse_if(); |
@@ -4555,7 +4544,7 @@ static BC_STATUS zbc_parse_stmt_possibly_auto(bool auto_allowed) | |||
4555 | "MAX Exponent = "BC_MAX_EXP_STR "\n" | 4544 | "MAX Exponent = "BC_MAX_EXP_STR "\n" |
4556 | "Number of vars = "BC_MAX_VARS_STR "\n" | 4545 | "Number of vars = "BC_MAX_VARS_STR "\n" |
4557 | ); | 4546 | ); |
4558 | s = zbc_lex_next(); | 4547 | s = zxc_lex_next(); |
4559 | break; | 4548 | break; |
4560 | case BC_LEX_KEY_PRINT: | 4549 | case BC_LEX_KEY_PRINT: |
4561 | s = zbc_parse_print(); | 4550 | s = zbc_parse_print(); |
@@ -4686,7 +4675,7 @@ static BcStatus bc_parse_expr_empty_ok(uint8_t flags) | |||
4686 | nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT); | 4675 | nrelops += (t >= XC_LEX_OP_REL_EQ && t <= XC_LEX_OP_REL_GT); |
4687 | prev = BC_TOKEN_2_INST(t); | 4676 | prev = BC_TOKEN_2_INST(t); |
4688 | bc_parse_operator(t, ops_bgn, &nexprs); | 4677 | bc_parse_operator(t, ops_bgn, &nexprs); |
4689 | s = zbc_lex_next(); | 4678 | s = zxc_lex_next(); |
4690 | rprn = false; | 4679 | rprn = false; |
4691 | //get_token = false; - already is | 4680 | //get_token = false; - already is |
4692 | bin_last = (t != BC_LEX_OP_BOOL_NOT); | 4681 | bin_last = (t != BC_LEX_OP_BOOL_NOT); |
@@ -4732,7 +4721,7 @@ static BcStatus bc_parse_expr_empty_ok(uint8_t flags) | |||
4732 | dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__); | 4721 | dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__); |
4733 | if (BC_PARSE_LEAF(prev, rprn)) | 4722 | if (BC_PARSE_LEAF(prev, rprn)) |
4734 | return bc_error_bad_expression(); | 4723 | return bc_error_bad_expression(); |
4735 | bc_parse_pushNUM(); | 4724 | xc_parse_pushNUM(); |
4736 | prev = XC_INST_NUM; | 4725 | prev = XC_INST_NUM; |
4737 | get_token = true; | 4726 | get_token = true; |
4738 | paren_expr = true; | 4727 | paren_expr = true; |
@@ -4746,7 +4735,7 @@ static BcStatus bc_parse_expr_empty_ok(uint8_t flags) | |||
4746 | if (BC_PARSE_LEAF(prev, rprn)) | 4735 | if (BC_PARSE_LEAF(prev, rprn)) |
4747 | return bc_error_bad_expression(); | 4736 | return bc_error_bad_expression(); |
4748 | prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE); | 4737 | prev = (char) (t - BC_LEX_KEY_IBASE + XC_INST_IBASE); |
4749 | bc_parse_push((char) prev); | 4738 | xc_parse_push((char) prev); |
4750 | get_token = true; | 4739 | get_token = true; |
4751 | paren_expr = true; | 4740 | paren_expr = true; |
4752 | rprn = bin_last = false; | 4741 | rprn = bin_last = false; |
@@ -4792,7 +4781,7 @@ static BcStatus bc_parse_expr_empty_ok(uint8_t flags) | |||
4792 | if (s || G_interrupt) // error, or ^C: stop parsing | 4781 | if (s || G_interrupt) // error, or ^C: stop parsing |
4793 | return BC_STATUS_FAILURE; | 4782 | return BC_STATUS_FAILURE; |
4794 | if (get_token) { | 4783 | if (get_token) { |
4795 | s = zbc_lex_next(); | 4784 | s = zxc_lex_next(); |
4796 | if (s) return s; | 4785 | if (s) return s; |
4797 | } | 4786 | } |
4798 | } | 4787 | } |
@@ -4805,7 +4794,7 @@ static BcStatus bc_parse_expr_empty_ok(uint8_t flags) | |||
4805 | if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN) | 4794 | if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN) |
4806 | return bc_error_bad_expression(); | 4795 | return bc_error_bad_expression(); |
4807 | 4796 | ||
4808 | bc_parse_push(BC_TOKEN_2_INST(top)); | 4797 | xc_parse_push(BC_TOKEN_2_INST(top)); |
4809 | 4798 | ||
4810 | nexprs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG); | 4799 | nexprs -= (top != BC_LEX_OP_BOOL_NOT && top != XC_LEX_NEG); |
4811 | bc_vec_pop(&p->ops); | 4800 | bc_vec_pop(&p->ops); |
@@ -4826,8 +4815,8 @@ static BcStatus bc_parse_expr_empty_ok(uint8_t flags) | |||
4826 | 4815 | ||
4827 | if (flags & BC_PARSE_PRINT) { | 4816 | if (flags & BC_PARSE_PRINT) { |
4828 | if (paren_first || !assign) | 4817 | if (paren_first || !assign) |
4829 | bc_parse_push(XC_INST_PRINT); | 4818 | xc_parse_push(XC_INST_PRINT); |
4830 | bc_parse_push(XC_INST_POP); | 4819 | xc_parse_push(XC_INST_POP); |
4831 | } | 4820 | } |
4832 | 4821 | ||
4833 | dbg_lex_done("%s:%d done", __func__, __LINE__); | 4822 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
@@ -4843,11 +4832,11 @@ static BC_STATUS zdc_parse_register(void) | |||
4843 | BcParse *p = &G.prs; | 4832 | BcParse *p = &G.prs; |
4844 | BcStatus s; | 4833 | BcStatus s; |
4845 | 4834 | ||
4846 | s = zbc_lex_next(); | 4835 | s = zxc_lex_next(); |
4847 | if (s) RETURN_STATUS(s); | 4836 | if (s) RETURN_STATUS(s); |
4848 | if (p->lex != XC_LEX_NAME) RETURN_STATUS(bc_error_bad_token()); | 4837 | if (p->lex != XC_LEX_NAME) RETURN_STATUS(bc_error_bad_token()); |
4849 | 4838 | ||
4850 | bc_parse_pushName(p->lex_strnumbuf.v); | 4839 | xc_parse_pushName(p->lex_strnumbuf.v); |
4851 | 4840 | ||
4852 | RETURN_STATUS(s); | 4841 | RETURN_STATUS(s); |
4853 | } | 4842 | } |
@@ -4862,13 +4851,13 @@ static void dc_parse_string(void) | |||
4862 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); | 4851 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
4863 | 4852 | ||
4864 | str = xstrdup(p->lex_strnumbuf.v); | 4853 | str = xstrdup(p->lex_strnumbuf.v); |
4865 | bc_parse_push(XC_INST_STR); | 4854 | xc_parse_push(XC_INST_STR); |
4866 | bc_parse_pushIndex(len); | 4855 | xc_parse_pushIndex(len); |
4867 | bc_vec_push(&G.prog.strs, &str); | 4856 | bc_vec_push(&G.prog.strs, &str); |
4868 | 4857 | ||
4869 | // Explanation needed here | 4858 | // Explanation needed here |
4870 | bc_program_add_fn(); | 4859 | xc_program_add_fn(); |
4871 | p->func = bc_program_func(p->fidx); | 4860 | p->func = xc_program_func(p->fidx); |
4872 | 4861 | ||
4873 | dbg_lex_done("%s:%d done", __func__, __LINE__); | 4862 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
4874 | } | 4863 | } |
@@ -4877,16 +4866,16 @@ static BC_STATUS zdc_parse_mem(uint8_t inst, bool name, bool store) | |||
4877 | { | 4866 | { |
4878 | BcStatus s; | 4867 | BcStatus s; |
4879 | 4868 | ||
4880 | bc_parse_push(inst); | 4869 | xc_parse_push(inst); |
4881 | if (name) { | 4870 | if (name) { |
4882 | s = zdc_parse_register(); | 4871 | s = zdc_parse_register(); |
4883 | if (s) RETURN_STATUS(s); | 4872 | if (s) RETURN_STATUS(s); |
4884 | } | 4873 | } |
4885 | 4874 | ||
4886 | if (store) { | 4875 | if (store) { |
4887 | bc_parse_push(DC_INST_SWAP); | 4876 | xc_parse_push(DC_INST_SWAP); |
4888 | bc_parse_push(XC_INST_ASSIGN); | 4877 | xc_parse_push(XC_INST_ASSIGN); |
4889 | bc_parse_push(XC_INST_POP); | 4878 | xc_parse_push(XC_INST_POP); |
4890 | } | 4879 | } |
4891 | 4880 | ||
4892 | RETURN_STATUS(BC_STATUS_SUCCESS); | 4881 | RETURN_STATUS(BC_STATUS_SUCCESS); |
@@ -4898,13 +4887,13 @@ static BC_STATUS zdc_parse_cond(uint8_t inst) | |||
4898 | BcParse *p = &G.prs; | 4887 | BcParse *p = &G.prs; |
4899 | BcStatus s; | 4888 | BcStatus s; |
4900 | 4889 | ||
4901 | bc_parse_push(inst); | 4890 | xc_parse_push(inst); |
4902 | bc_parse_push(DC_INST_EXEC_COND); | 4891 | xc_parse_push(DC_INST_EXEC_COND); |
4903 | 4892 | ||
4904 | s = zdc_parse_register(); | 4893 | s = zdc_parse_register(); |
4905 | if (s) RETURN_STATUS(s); | 4894 | if (s) RETURN_STATUS(s); |
4906 | 4895 | ||
4907 | s = zbc_lex_next(); | 4896 | s = zxc_lex_next(); |
4908 | if (s) RETURN_STATUS(s); | 4897 | if (s) RETURN_STATUS(s); |
4909 | 4898 | ||
4910 | // Note that 'else' part can not be on the next line: | 4899 | // Note that 'else' part can not be on the next line: |
@@ -4913,9 +4902,9 @@ static BC_STATUS zdc_parse_cond(uint8_t inst) | |||
4913 | if (p->lex == DC_LEX_ELSE) { | 4902 | if (p->lex == DC_LEX_ELSE) { |
4914 | s = zdc_parse_register(); | 4903 | s = zdc_parse_register(); |
4915 | if (s) RETURN_STATUS(s); | 4904 | if (s) RETURN_STATUS(s); |
4916 | s = zbc_lex_next(); | 4905 | s = zxc_lex_next(); |
4917 | } else { | 4906 | } else { |
4918 | bc_parse_push('\0'); | 4907 | xc_parse_push('\0'); |
4919 | } | 4908 | } |
4920 | 4909 | ||
4921 | RETURN_STATUS(s); | 4910 | RETURN_STATUS(s); |
@@ -4953,20 +4942,20 @@ static BC_STATUS zdc_parse_token(BcLexType t) | |||
4953 | break; | 4942 | break; |
4954 | case XC_LEX_NEG: | 4943 | case XC_LEX_NEG: |
4955 | dbg_lex("%s:%d LEX_NEG", __func__, __LINE__); | 4944 | dbg_lex("%s:%d LEX_NEG", __func__, __LINE__); |
4956 | s = zbc_lex_next(); | 4945 | s = zxc_lex_next(); |
4957 | if (s) RETURN_STATUS(s); | 4946 | if (s) RETURN_STATUS(s); |
4958 | if (G.prs.lex != XC_LEX_NUMBER) | 4947 | if (G.prs.lex != XC_LEX_NUMBER) |
4959 | RETURN_STATUS(bc_error_bad_token()); | 4948 | RETURN_STATUS(bc_error_bad_token()); |
4960 | bc_parse_pushNUM(); | 4949 | xc_parse_pushNUM(); |
4961 | bc_parse_push(XC_INST_NEG); | 4950 | xc_parse_push(XC_INST_NEG); |
4962 | break; | 4951 | break; |
4963 | case XC_LEX_NUMBER: | 4952 | case XC_LEX_NUMBER: |
4964 | dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__); | 4953 | dbg_lex("%s:%d LEX_NUMBER", __func__, __LINE__); |
4965 | bc_parse_pushNUM(); | 4954 | xc_parse_pushNUM(); |
4966 | break; | 4955 | break; |
4967 | case DC_LEX_READ: | 4956 | case DC_LEX_READ: |
4968 | dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__); | 4957 | dbg_lex("%s:%d LEX_KEY_READ", __func__, __LINE__); |
4969 | bc_parse_push(XC_INST_READ); | 4958 | xc_parse_push(XC_INST_READ); |
4970 | break; | 4959 | break; |
4971 | case DC_LEX_OP_ASSIGN: | 4960 | case DC_LEX_OP_ASSIGN: |
4972 | case DC_LEX_STORE_PUSH: | 4961 | case DC_LEX_STORE_PUSH: |
@@ -4993,7 +4982,7 @@ static BC_STATUS zdc_parse_token(BcLexType t) | |||
4993 | RETURN_STATUS(bc_error_bad_token()); | 4982 | RETURN_STATUS(bc_error_bad_token()); |
4994 | } | 4983 | } |
4995 | 4984 | ||
4996 | if (!s && get_token) s = zbc_lex_next(); | 4985 | if (!s && get_token) s = zxc_lex_next(); |
4997 | 4986 | ||
4998 | dbg_lex_done("%s:%d done", __func__, __LINE__); | 4987 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
4999 | RETURN_STATUS(s); | 4988 | RETURN_STATUS(s); |
@@ -5009,8 +4998,8 @@ static BC_STATUS zdc_parse_expr(void) | |||
5009 | if (i >= 0) { | 4998 | if (i >= 0) { |
5010 | BcInst inst = dc_LEX_to_INST[i]; | 4999 | BcInst inst = dc_LEX_to_INST[i]; |
5011 | if (inst != DC_INST_INVALID) { | 5000 | if (inst != DC_INST_INVALID) { |
5012 | bc_parse_push(inst); | 5001 | xc_parse_push(inst); |
5013 | RETURN_STATUS(zbc_lex_next()); | 5002 | RETURN_STATUS(zxc_lex_next()); |
5014 | } | 5003 | } |
5015 | } | 5004 | } |
5016 | RETURN_STATUS(zdc_parse_token(p->lex)); | 5005 | RETURN_STATUS(zdc_parse_token(p->lex)); |
@@ -5040,7 +5029,7 @@ static BC_STATUS zdc_parse_exprs_until_eof(void) | |||
5040 | #define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n))) | 5029 | #define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n))) |
5041 | #define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n))) | 5030 | #define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n))) |
5042 | 5031 | ||
5043 | static BcVec* bc_program_search(char *id, bool var) | 5032 | static BcVec* xc_program_search(char *id, bool var) |
5044 | { | 5033 | { |
5045 | BcId e, *ptr; | 5034 | BcId e, *ptr; |
5046 | BcVec *v, *map; | 5035 | BcVec *v, *map; |
@@ -5066,7 +5055,7 @@ static BcVec* bc_program_search(char *id, bool var) | |||
5066 | } | 5055 | } |
5067 | 5056 | ||
5068 | // 'num' need not be initialized on entry | 5057 | // 'num' need not be initialized on entry |
5069 | static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex) | 5058 | static BC_STATUS zxc_program_num(BcResult *r, BcNum **num, bool hex) |
5070 | { | 5059 | { |
5071 | switch (r->t) { | 5060 | switch (r->t) { |
5072 | case XC_RESULT_STR: | 5061 | case XC_RESULT_STR: |
@@ -5082,14 +5071,14 @@ static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex) | |||
5082 | unsigned base_t; | 5071 | unsigned base_t; |
5083 | size_t len; | 5072 | size_t len; |
5084 | 5073 | ||
5085 | str = *bc_program_const(r->d.id.idx); | 5074 | str = *xc_program_const(r->d.id.idx); |
5086 | len = strlen(str); | 5075 | len = strlen(str); |
5087 | 5076 | ||
5088 | bc_num_init(&r->d.n, len); | 5077 | bc_num_init(&r->d.n, len); |
5089 | 5078 | ||
5090 | hex = hex && len == 1; | 5079 | hex = hex && len == 1; |
5091 | base_t = hex ? 16 : G.prog.ib_t; | 5080 | base_t = hex ? 16 : G.prog.ib_t; |
5092 | s = zbc_num_parse(&r->d.n, str, base_t); | 5081 | s = zxc_num_parse(&r->d.n, str, base_t); |
5093 | if (s) { | 5082 | if (s) { |
5094 | bc_num_free(&r->d.n); | 5083 | bc_num_free(&r->d.n); |
5095 | RETURN_STATUS(s); | 5084 | RETURN_STATUS(s); |
@@ -5103,7 +5092,7 @@ static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex) | |||
5103 | case XC_RESULT_ARRAY_ELEM: { | 5092 | case XC_RESULT_ARRAY_ELEM: { |
5104 | BcVec *v; | 5093 | BcVec *v; |
5105 | 5094 | ||
5106 | v = bc_program_search(r->d.id.name, r->t == XC_RESULT_VAR); | 5095 | v = xc_program_search(r->d.id.name, r->t == XC_RESULT_VAR); |
5107 | 5096 | ||
5108 | if (r->t == XC_RESULT_ARRAY_ELEM) { | 5097 | if (r->t == XC_RESULT_ARRAY_ELEM) { |
5109 | v = bc_vec_top(v); | 5098 | v = bc_vec_top(v); |
@@ -5130,9 +5119,9 @@ static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex) | |||
5130 | 5119 | ||
5131 | RETURN_STATUS(BC_STATUS_SUCCESS); | 5120 | RETURN_STATUS(BC_STATUS_SUCCESS); |
5132 | } | 5121 | } |
5133 | #define zbc_program_num(...) (zbc_program_num(__VA_ARGS__) COMMA_SUCCESS) | 5122 | #define zxc_program_num(...) (zxc_program_num(__VA_ARGS__) COMMA_SUCCESS) |
5134 | 5123 | ||
5135 | static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln, | 5124 | static BC_STATUS zxc_program_binOpPrep(BcResult **l, BcNum **ln, |
5136 | BcResult **r, BcNum **rn, bool assign) | 5125 | BcResult **r, BcNum **rn, bool assign) |
5137 | { | 5126 | { |
5138 | BcStatus s; | 5127 | BcStatus s; |
@@ -5149,15 +5138,15 @@ static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln, | |||
5149 | rt = (*r)->t; | 5138 | rt = (*r)->t; |
5150 | hex = assign && (lt == XC_RESULT_IBASE || lt == XC_RESULT_OBASE); | 5139 | hex = assign && (lt == XC_RESULT_IBASE || lt == XC_RESULT_OBASE); |
5151 | 5140 | ||
5152 | s = zbc_program_num(*l, ln, false); | 5141 | s = zxc_program_num(*l, ln, false); |
5153 | if (s) RETURN_STATUS(s); | 5142 | if (s) RETURN_STATUS(s); |
5154 | s = zbc_program_num(*r, rn, hex); | 5143 | s = zxc_program_num(*r, rn, hex); |
5155 | if (s) RETURN_STATUS(s); | 5144 | if (s) RETURN_STATUS(s); |
5156 | 5145 | ||
5157 | // We run this again under these conditions in case any vector has been | 5146 | // We run this again under these conditions in case any vector has been |
5158 | // reallocated out from under the BcNums or arrays we had. | 5147 | // reallocated out from under the BcNums or arrays we had. |
5159 | if (lt == rt && (lt == XC_RESULT_VAR || lt == XC_RESULT_ARRAY_ELEM)) { | 5148 | if (lt == rt && (lt == XC_RESULT_VAR || lt == XC_RESULT_ARRAY_ELEM)) { |
5160 | s = zbc_program_num(*l, ln, false); | 5149 | s = zxc_program_num(*l, ln, false); |
5161 | if (s) RETURN_STATUS(s); | 5150 | if (s) RETURN_STATUS(s); |
5162 | } | 5151 | } |
5163 | 5152 | ||
@@ -5168,9 +5157,9 @@ static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln, | |||
5168 | 5157 | ||
5169 | RETURN_STATUS(s); | 5158 | RETURN_STATUS(s); |
5170 | } | 5159 | } |
5171 | #define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS) | 5160 | #define zxc_program_binOpPrep(...) (zxc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS) |
5172 | 5161 | ||
5173 | static void bc_program_binOpRetire(BcResult *r) | 5162 | static void xc_program_binOpRetire(BcResult *r) |
5174 | { | 5163 | { |
5175 | r->t = XC_RESULT_TEMP; | 5164 | r->t = XC_RESULT_TEMP; |
5176 | bc_vec_pop(&G.prog.results); | 5165 | bc_vec_pop(&G.prog.results); |
@@ -5178,7 +5167,7 @@ static void bc_program_binOpRetire(BcResult *r) | |||
5178 | } | 5167 | } |
5179 | 5168 | ||
5180 | // Note: *r and *n need not be initialized by caller | 5169 | // Note: *r and *n need not be initialized by caller |
5181 | static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n) | 5170 | static BC_STATUS zxc_program_prep(BcResult **r, BcNum **n) |
5182 | { | 5171 | { |
5183 | BcStatus s; | 5172 | BcStatus s; |
5184 | 5173 | ||
@@ -5186,7 +5175,7 @@ static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n) | |||
5186 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); | 5175 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
5187 | *r = bc_vec_top(&G.prog.results); | 5176 | *r = bc_vec_top(&G.prog.results); |
5188 | 5177 | ||
5189 | s = zbc_program_num(*r, n, false); | 5178 | s = zxc_program_num(*r, n, false); |
5190 | if (s) RETURN_STATUS(s); | 5179 | if (s) RETURN_STATUS(s); |
5191 | 5180 | ||
5192 | if (!BC_PROG_NUM((*r), (*n))) | 5181 | if (!BC_PROG_NUM((*r), (*n))) |
@@ -5194,37 +5183,37 @@ static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n) | |||
5194 | 5183 | ||
5195 | RETURN_STATUS(s); | 5184 | RETURN_STATUS(s); |
5196 | } | 5185 | } |
5197 | #define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__) COMMA_SUCCESS) | 5186 | #define zxc_program_prep(...) (zxc_program_prep(__VA_ARGS__) COMMA_SUCCESS) |
5198 | 5187 | ||
5199 | static void bc_program_retire(BcResult *r, BcResultType t) | 5188 | static void xc_program_retire(BcResult *r, BcResultType t) |
5200 | { | 5189 | { |
5201 | r->t = t; | 5190 | r->t = t; |
5202 | bc_result_pop_and_push(r); | 5191 | bc_result_pop_and_push(r); |
5203 | } | 5192 | } |
5204 | 5193 | ||
5205 | static BC_STATUS zbc_program_op(char inst) | 5194 | static BC_STATUS zxc_program_op(char inst) |
5206 | { | 5195 | { |
5207 | BcStatus s; | 5196 | BcStatus s; |
5208 | BcResult *opd1, *opd2, res; | 5197 | BcResult *opd1, *opd2, res; |
5209 | BcNum *n1, *n2; | 5198 | BcNum *n1, *n2; |
5210 | 5199 | ||
5211 | s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); | 5200 | s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
5212 | if (s) RETURN_STATUS(s); | 5201 | if (s) RETURN_STATUS(s); |
5213 | bc_num_init_DEF_SIZE(&res.d.n); | 5202 | bc_num_init_DEF_SIZE(&res.d.n); |
5214 | 5203 | ||
5215 | s = BC_STATUS_SUCCESS; | 5204 | s = BC_STATUS_SUCCESS; |
5216 | IF_ERROR_RETURN_POSSIBLE(s =) zbc_program_ops[inst - XC_INST_POWER](n1, n2, &res.d.n, G.prog.scale); | 5205 | IF_ERROR_RETURN_POSSIBLE(s =) zxc_program_ops[inst - XC_INST_POWER](n1, n2, &res.d.n, G.prog.scale); |
5217 | if (s) goto err; | 5206 | if (s) goto err; |
5218 | bc_program_binOpRetire(&res); | 5207 | xc_program_binOpRetire(&res); |
5219 | 5208 | ||
5220 | RETURN_STATUS(s); | 5209 | RETURN_STATUS(s); |
5221 | err: | 5210 | err: |
5222 | bc_num_free(&res.d.n); | 5211 | bc_num_free(&res.d.n); |
5223 | RETURN_STATUS(s); | 5212 | RETURN_STATUS(s); |
5224 | } | 5213 | } |
5225 | #define zbc_program_op(...) (zbc_program_op(__VA_ARGS__) COMMA_SUCCESS) | 5214 | #define zxc_program_op(...) (zxc_program_op(__VA_ARGS__) COMMA_SUCCESS) |
5226 | 5215 | ||
5227 | static BC_STATUS zbc_program_read(void) | 5216 | static BC_STATUS zxc_program_read(void) |
5228 | { | 5217 | { |
5229 | BcStatus s; | 5218 | BcStatus s; |
5230 | BcParse sv_parse; | 5219 | BcParse sv_parse; |
@@ -5233,16 +5222,16 @@ static BC_STATUS zbc_program_read(void) | |||
5233 | BcFunc *f; | 5222 | BcFunc *f; |
5234 | 5223 | ||
5235 | bc_char_vec_init(&buf); | 5224 | bc_char_vec_init(&buf); |
5236 | bc_read_line(&buf, stdin); | 5225 | xc_read_line(&buf, stdin); |
5237 | 5226 | ||
5238 | f = bc_program_func(BC_PROG_READ); | 5227 | f = xc_program_func(BC_PROG_READ); |
5239 | bc_vec_pop_all(&f->code); | 5228 | bc_vec_pop_all(&f->code); |
5240 | 5229 | ||
5241 | sv_parse = G.prs; // struct copy | 5230 | sv_parse = G.prs; // struct copy |
5242 | bc_parse_create(BC_PROG_READ); | 5231 | xc_parse_create(BC_PROG_READ); |
5243 | //G.err_line = G.prs.lex_line = 1; - not needed, error line info is not printed for read() | 5232 | //G.err_line = G.prs.lex_line = 1; - not needed, error line info is not printed for read() |
5244 | 5233 | ||
5245 | s = zbc_parse_text_init(buf.v); | 5234 | s = zxc_parse_text_init(buf.v); |
5246 | if (s) goto exec_err; | 5235 | if (s) goto exec_err; |
5247 | if (IS_BC) { | 5236 | if (IS_BC) { |
5248 | IF_BC(s = zbc_parse_expr(0)); | 5237 | IF_BC(s = zbc_parse_expr(0)); |
@@ -5260,18 +5249,18 @@ static BC_STATUS zbc_program_read(void) | |||
5260 | ip.inst_idx = 0; | 5249 | ip.inst_idx = 0; |
5261 | IF_BC(ip.results_len_before_call = G.prog.results.len;) | 5250 | IF_BC(ip.results_len_before_call = G.prog.results.len;) |
5262 | 5251 | ||
5263 | bc_parse_push(XC_INST_RET); | 5252 | xc_parse_push(XC_INST_RET); |
5264 | bc_vec_push(&G.prog.exestack, &ip); | 5253 | bc_vec_push(&G.prog.exestack, &ip); |
5265 | 5254 | ||
5266 | exec_err: | 5255 | exec_err: |
5267 | bc_parse_free(); | 5256 | xc_parse_free(); |
5268 | G.prs = sv_parse; // struct copy | 5257 | G.prs = sv_parse; // struct copy |
5269 | bc_vec_free(&buf); | 5258 | bc_vec_free(&buf); |
5270 | RETURN_STATUS(s); | 5259 | RETURN_STATUS(s); |
5271 | } | 5260 | } |
5272 | #define zbc_program_read(...) (zbc_program_read(__VA_ARGS__) COMMA_SUCCESS) | 5261 | #define zxc_program_read(...) (zxc_program_read(__VA_ARGS__) COMMA_SUCCESS) |
5273 | 5262 | ||
5274 | static size_t bc_program_index(char *code, size_t *bgn) | 5263 | static size_t xc_program_index(char *code, size_t *bgn) |
5275 | { | 5264 | { |
5276 | unsigned char *bytes = (void*)(code + *bgn); | 5265 | unsigned char *bytes = (void*)(code + *bgn); |
5277 | unsigned amt; | 5266 | unsigned amt; |
@@ -5294,7 +5283,7 @@ static size_t bc_program_index(char *code, size_t *bgn) | |||
5294 | return res; | 5283 | return res; |
5295 | } | 5284 | } |
5296 | 5285 | ||
5297 | static char *bc_program_name(char *code, size_t *bgn) | 5286 | static char *xc_program_name(char *code, size_t *bgn) |
5298 | { | 5287 | { |
5299 | code += *bgn; | 5288 | code += *bgn; |
5300 | *bgn += strlen(code) + 1; | 5289 | *bgn += strlen(code) + 1; |
@@ -5302,7 +5291,7 @@ static char *bc_program_name(char *code, size_t *bgn) | |||
5302 | return xstrdup(code); | 5291 | return xstrdup(code); |
5303 | } | 5292 | } |
5304 | 5293 | ||
5305 | static void bc_program_printString(const char *str) | 5294 | static void xc_program_printString(const char *str) |
5306 | { | 5295 | { |
5307 | #if ENABLE_DC | 5296 | #if ENABLE_DC |
5308 | if (!str[0]) { | 5297 | if (!str[0]) { |
@@ -5424,7 +5413,7 @@ static void bc_num_printDecimal(BcNum *n) | |||
5424 | 5413 | ||
5425 | typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC; | 5414 | typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC; |
5426 | 5415 | ||
5427 | static BC_STATUS zbc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNumDigitOp print) | 5416 | static BC_STATUS zxc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNumDigitOp print) |
5428 | { | 5417 | { |
5429 | BcStatus s; | 5418 | BcStatus s; |
5430 | BcVec stack; | 5419 | BcVec stack; |
@@ -5490,9 +5479,9 @@ static BC_STATUS zbc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNum | |||
5490 | bc_vec_free(&stack); | 5479 | bc_vec_free(&stack); |
5491 | RETURN_STATUS(s); | 5480 | RETURN_STATUS(s); |
5492 | } | 5481 | } |
5493 | #define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__) COMMA_SUCCESS) | 5482 | #define zxc_num_printNum(...) (zxc_num_printNum(__VA_ARGS__) COMMA_SUCCESS) |
5494 | 5483 | ||
5495 | static BC_STATUS zbc_num_printBase(BcNum *n) | 5484 | static BC_STATUS zxc_num_printBase(BcNum *n) |
5496 | { | 5485 | { |
5497 | BcStatus s; | 5486 | BcStatus s; |
5498 | size_t width; | 5487 | size_t width; |
@@ -5521,14 +5510,14 @@ static BC_STATUS zbc_num_printBase(BcNum *n) | |||
5521 | print = bc_num_printDigits; | 5510 | print = bc_num_printDigits; |
5522 | } | 5511 | } |
5523 | 5512 | ||
5524 | s = zbc_num_printNum(n, G.prog.ob_t, width, print); | 5513 | s = zxc_num_printNum(n, G.prog.ob_t, width, print); |
5525 | n->neg = neg; | 5514 | n->neg = neg; |
5526 | 5515 | ||
5527 | RETURN_STATUS(s); | 5516 | RETURN_STATUS(s); |
5528 | } | 5517 | } |
5529 | #define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__) COMMA_SUCCESS) | 5518 | #define zxc_num_printBase(...) (zxc_num_printBase(__VA_ARGS__) COMMA_SUCCESS) |
5530 | 5519 | ||
5531 | static BC_STATUS zbc_num_print(BcNum *n, bool newline) | 5520 | static BC_STATUS zxc_num_print(BcNum *n, bool newline) |
5532 | { | 5521 | { |
5533 | BcStatus s = BC_STATUS_SUCCESS; | 5522 | BcStatus s = BC_STATUS_SUCCESS; |
5534 | 5523 | ||
@@ -5540,7 +5529,7 @@ static BC_STATUS zbc_num_print(BcNum *n, bool newline) | |||
5540 | } else if (G.prog.ob_t == 10) | 5529 | } else if (G.prog.ob_t == 10) |
5541 | bc_num_printDecimal(n); | 5530 | bc_num_printDecimal(n); |
5542 | else | 5531 | else |
5543 | s = zbc_num_printBase(n); | 5532 | s = zxc_num_printBase(n); |
5544 | 5533 | ||
5545 | if (newline) { | 5534 | if (newline) { |
5546 | bb_putchar('\n'); | 5535 | bb_putchar('\n'); |
@@ -5549,9 +5538,9 @@ static BC_STATUS zbc_num_print(BcNum *n, bool newline) | |||
5549 | 5538 | ||
5550 | RETURN_STATUS(s); | 5539 | RETURN_STATUS(s); |
5551 | } | 5540 | } |
5552 | #define zbc_num_print(...) (zbc_num_print(__VA_ARGS__) COMMA_SUCCESS) | 5541 | #define zxc_num_print(...) (zxc_num_print(__VA_ARGS__) COMMA_SUCCESS) |
5553 | 5542 | ||
5554 | static BC_STATUS zbc_program_print(char inst, size_t idx) | 5543 | static BC_STATUS zxc_program_print(char inst, size_t idx) |
5555 | { | 5544 | { |
5556 | BcStatus s; | 5545 | BcStatus s; |
5557 | BcResult *r; | 5546 | BcResult *r; |
@@ -5562,11 +5551,11 @@ static BC_STATUS zbc_program_print(char inst, size_t idx) | |||
5562 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); | 5551 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
5563 | 5552 | ||
5564 | r = bc_vec_item_rev(&G.prog.results, idx); | 5553 | r = bc_vec_item_rev(&G.prog.results, idx); |
5565 | s = zbc_program_num(r, &num, false); | 5554 | s = zxc_program_num(r, &num, false); |
5566 | if (s) RETURN_STATUS(s); | 5555 | if (s) RETURN_STATUS(s); |
5567 | 5556 | ||
5568 | if (BC_PROG_NUM(r, num)) { | 5557 | if (BC_PROG_NUM(r, num)) { |
5569 | s = zbc_num_print(num, !pop); | 5558 | s = zxc_num_print(num, !pop); |
5570 | #if ENABLE_BC | 5559 | #if ENABLE_BC |
5571 | if (!s && IS_BC) bc_num_copy(&G.prog.last, num); | 5560 | if (!s && IS_BC) bc_num_copy(&G.prog.last, num); |
5572 | #endif | 5561 | #endif |
@@ -5574,7 +5563,7 @@ static BC_STATUS zbc_program_print(char inst, size_t idx) | |||
5574 | char *str; | 5563 | char *str; |
5575 | 5564 | ||
5576 | idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx; | 5565 | idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx; |
5577 | str = *bc_program_str(idx); | 5566 | str = *xc_program_str(idx); |
5578 | 5567 | ||
5579 | if (inst == XC_INST_PRINT_STR) { | 5568 | if (inst == XC_INST_PRINT_STR) { |
5580 | for (;;) { | 5569 | for (;;) { |
@@ -5585,7 +5574,7 @@ static BC_STATUS zbc_program_print(char inst, size_t idx) | |||
5585 | if (c == '\n') G.prog.nchars = 0; | 5574 | if (c == '\n') G.prog.nchars = 0; |
5586 | } | 5575 | } |
5587 | } else { | 5576 | } else { |
5588 | bc_program_printString(str); | 5577 | xc_program_printString(str); |
5589 | if (inst == XC_INST_PRINT) bb_putchar('\n'); | 5578 | if (inst == XC_INST_PRINT) bb_putchar('\n'); |
5590 | } | 5579 | } |
5591 | } | 5580 | } |
@@ -5594,35 +5583,35 @@ static BC_STATUS zbc_program_print(char inst, size_t idx) | |||
5594 | 5583 | ||
5595 | RETURN_STATUS(s); | 5584 | RETURN_STATUS(s); |
5596 | } | 5585 | } |
5597 | #define zbc_program_print(...) (zbc_program_print(__VA_ARGS__) COMMA_SUCCESS) | 5586 | #define zxc_program_print(...) (zxc_program_print(__VA_ARGS__) COMMA_SUCCESS) |
5598 | 5587 | ||
5599 | static BC_STATUS zbc_program_negate(void) | 5588 | static BC_STATUS zxc_program_negate(void) |
5600 | { | 5589 | { |
5601 | BcStatus s; | 5590 | BcStatus s; |
5602 | BcResult res, *ptr; | 5591 | BcResult res, *ptr; |
5603 | BcNum *num; | 5592 | BcNum *num; |
5604 | 5593 | ||
5605 | s = zbc_program_prep(&ptr, &num); | 5594 | s = zxc_program_prep(&ptr, &num); |
5606 | if (s) RETURN_STATUS(s); | 5595 | if (s) RETURN_STATUS(s); |
5607 | 5596 | ||
5608 | bc_num_init(&res.d.n, num->len); | 5597 | bc_num_init(&res.d.n, num->len); |
5609 | bc_num_copy(&res.d.n, num); | 5598 | bc_num_copy(&res.d.n, num); |
5610 | if (res.d.n.len) res.d.n.neg = !res.d.n.neg; | 5599 | if (res.d.n.len) res.d.n.neg = !res.d.n.neg; |
5611 | 5600 | ||
5612 | bc_program_retire(&res, XC_RESULT_TEMP); | 5601 | xc_program_retire(&res, XC_RESULT_TEMP); |
5613 | 5602 | ||
5614 | RETURN_STATUS(s); | 5603 | RETURN_STATUS(s); |
5615 | } | 5604 | } |
5616 | #define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__) COMMA_SUCCESS) | 5605 | #define zxc_program_negate(...) (zxc_program_negate(__VA_ARGS__) COMMA_SUCCESS) |
5617 | 5606 | ||
5618 | static BC_STATUS zbc_program_logical(char inst) | 5607 | static BC_STATUS zxc_program_logical(char inst) |
5619 | { | 5608 | { |
5620 | BcStatus s; | 5609 | BcStatus s; |
5621 | BcResult *opd1, *opd2, res; | 5610 | BcResult *opd1, *opd2, res; |
5622 | BcNum *n1, *n2; | 5611 | BcNum *n1, *n2; |
5623 | ssize_t cond; | 5612 | ssize_t cond; |
5624 | 5613 | ||
5625 | s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); | 5614 | s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
5626 | if (s) RETURN_STATUS(s); | 5615 | if (s) RETURN_STATUS(s); |
5627 | 5616 | ||
5628 | bc_num_init_DEF_SIZE(&res.d.n); | 5617 | bc_num_init_DEF_SIZE(&res.d.n); |
@@ -5658,11 +5647,11 @@ static BC_STATUS zbc_program_logical(char inst) | |||
5658 | if (cond) bc_num_one(&res.d.n); | 5647 | if (cond) bc_num_one(&res.d.n); |
5659 | //else bc_num_zero(&res.d.n); - already is | 5648 | //else bc_num_zero(&res.d.n); - already is |
5660 | 5649 | ||
5661 | bc_program_binOpRetire(&res); | 5650 | xc_program_binOpRetire(&res); |
5662 | 5651 | ||
5663 | RETURN_STATUS(s); | 5652 | RETURN_STATUS(s); |
5664 | } | 5653 | } |
5665 | #define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__) COMMA_SUCCESS) | 5654 | #define zxc_program_logical(...) (zxc_program_logical(__VA_ARGS__) COMMA_SUCCESS) |
5666 | 5655 | ||
5667 | #if ENABLE_DC | 5656 | #if ENABLE_DC |
5668 | static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push) | 5657 | static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push) |
@@ -5689,7 +5678,7 @@ static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push) | |||
5689 | #define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS) | 5678 | #define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS) |
5690 | #endif // ENABLE_DC | 5679 | #endif // ENABLE_DC |
5691 | 5680 | ||
5692 | static BC_STATUS zbc_program_copyToVar(char *name, bool var) | 5681 | static BC_STATUS zxc_program_copyToVar(char *name, bool var) |
5693 | { | 5682 | { |
5694 | BcStatus s; | 5683 | BcStatus s; |
5695 | BcResult *ptr, r; | 5684 | BcResult *ptr, r; |
@@ -5702,7 +5691,7 @@ static BC_STATUS zbc_program_copyToVar(char *name, bool var) | |||
5702 | ptr = bc_vec_top(&G.prog.results); | 5691 | ptr = bc_vec_top(&G.prog.results); |
5703 | if ((ptr->t == XC_RESULT_ARRAY) != !var) | 5692 | if ((ptr->t == XC_RESULT_ARRAY) != !var) |
5704 | RETURN_STATUS(bc_error_variable_is_wrong_type()); | 5693 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
5705 | v = bc_program_search(name, var); | 5694 | v = xc_program_search(name, var); |
5706 | 5695 | ||
5707 | #if ENABLE_DC | 5696 | #if ENABLE_DC |
5708 | if (ptr->t == XC_RESULT_STR && !var) | 5697 | if (ptr->t == XC_RESULT_STR && !var) |
@@ -5711,11 +5700,11 @@ static BC_STATUS zbc_program_copyToVar(char *name, bool var) | |||
5711 | RETURN_STATUS(zdc_program_assignStr(ptr, v, true)); | 5700 | RETURN_STATUS(zdc_program_assignStr(ptr, v, true)); |
5712 | #endif | 5701 | #endif |
5713 | 5702 | ||
5714 | s = zbc_program_num(ptr, &n, false); | 5703 | s = zxc_program_num(ptr, &n, false); |
5715 | if (s) RETURN_STATUS(s); | 5704 | if (s) RETURN_STATUS(s); |
5716 | 5705 | ||
5717 | // Do this once more to make sure that pointers were not invalidated. | 5706 | // Do this once more to make sure that pointers were not invalidated. |
5718 | v = bc_program_search(name, var); | 5707 | v = xc_program_search(name, var); |
5719 | 5708 | ||
5720 | if (var) { | 5709 | if (var) { |
5721 | bc_num_init_DEF_SIZE(&r.d.n); | 5710 | bc_num_init_DEF_SIZE(&r.d.n); |
@@ -5730,9 +5719,9 @@ static BC_STATUS zbc_program_copyToVar(char *name, bool var) | |||
5730 | 5719 | ||
5731 | RETURN_STATUS(s); | 5720 | RETURN_STATUS(s); |
5732 | } | 5721 | } |
5733 | #define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__) COMMA_SUCCESS) | 5722 | #define zxc_program_copyToVar(...) (zxc_program_copyToVar(__VA_ARGS__) COMMA_SUCCESS) |
5734 | 5723 | ||
5735 | static BC_STATUS zbc_program_assign(char inst) | 5724 | static BC_STATUS zxc_program_assign(char inst) |
5736 | { | 5725 | { |
5737 | BcStatus s; | 5726 | BcStatus s; |
5738 | BcResult *left, *right, res; | 5727 | BcResult *left, *right, res; |
@@ -5740,7 +5729,7 @@ static BC_STATUS zbc_program_assign(char inst) | |||
5740 | bool assign = (inst == XC_INST_ASSIGN); | 5729 | bool assign = (inst == XC_INST_ASSIGN); |
5741 | bool ib, sc; | 5730 | bool ib, sc; |
5742 | 5731 | ||
5743 | s = zbc_program_binOpPrep(&left, &l, &right, &r, assign); | 5732 | s = zxc_program_binOpPrep(&left, &l, &right, &r, assign); |
5744 | if (s) RETURN_STATUS(s); | 5733 | if (s) RETURN_STATUS(s); |
5745 | 5734 | ||
5746 | ib = left->t == XC_RESULT_IBASE; | 5735 | ib = left->t == XC_RESULT_IBASE; |
@@ -5752,7 +5741,7 @@ static BC_STATUS zbc_program_assign(char inst) | |||
5752 | 5741 | ||
5753 | if (left->t != XC_RESULT_VAR) | 5742 | if (left->t != XC_RESULT_VAR) |
5754 | RETURN_STATUS(bc_error_variable_is_wrong_type()); | 5743 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
5755 | v = bc_program_search(left->d.id.name, true); | 5744 | v = xc_program_search(left->d.id.name, true); |
5756 | 5745 | ||
5757 | RETURN_STATUS(zdc_program_assignStr(right, v, false)); | 5746 | RETURN_STATUS(zdc_program_assignStr(right, v, false)); |
5758 | } | 5747 | } |
@@ -5772,7 +5761,7 @@ static BC_STATUS zbc_program_assign(char inst) | |||
5772 | bc_num_copy(l, r); | 5761 | bc_num_copy(l, r); |
5773 | else { | 5762 | else { |
5774 | s = BC_STATUS_SUCCESS; | 5763 | s = BC_STATUS_SUCCESS; |
5775 | IF_ERROR_RETURN_POSSIBLE(s =) zbc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale); | 5764 | IF_ERROR_RETURN_POSSIBLE(s =) zxc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale); |
5776 | } | 5765 | } |
5777 | if (s) RETURN_STATUS(s); | 5766 | if (s) RETURN_STATUS(s); |
5778 | #else | 5767 | #else |
@@ -5811,29 +5800,29 @@ static BC_STATUS zbc_program_assign(char inst) | |||
5811 | 5800 | ||
5812 | bc_num_init(&res.d.n, l->len); | 5801 | bc_num_init(&res.d.n, l->len); |
5813 | bc_num_copy(&res.d.n, l); | 5802 | bc_num_copy(&res.d.n, l); |
5814 | bc_program_binOpRetire(&res); | 5803 | xc_program_binOpRetire(&res); |
5815 | 5804 | ||
5816 | RETURN_STATUS(s); | 5805 | RETURN_STATUS(s); |
5817 | } | 5806 | } |
5818 | #define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__) COMMA_SUCCESS) | 5807 | #define zxc_program_assign(...) (zxc_program_assign(__VA_ARGS__) COMMA_SUCCESS) |
5819 | 5808 | ||
5820 | #if !ENABLE_DC | 5809 | #if !ENABLE_DC |
5821 | #define bc_program_pushVar(code, bgn, pop, copy) \ | 5810 | #define xc_program_pushVar(code, bgn, pop, copy) \ |
5822 | bc_program_pushVar(code, bgn) | 5811 | xc_program_pushVar(code, bgn) |
5823 | // for bc, 'pop' and 'copy' are always false | 5812 | // for bc, 'pop' and 'copy' are always false |
5824 | #endif | 5813 | #endif |
5825 | static BC_STATUS bc_program_pushVar(char *code, size_t *bgn, | 5814 | static BC_STATUS xc_program_pushVar(char *code, size_t *bgn, |
5826 | bool pop, bool copy) | 5815 | bool pop, bool copy) |
5827 | { | 5816 | { |
5828 | BcResult r; | 5817 | BcResult r; |
5829 | char *name = bc_program_name(code, bgn); | 5818 | char *name = xc_program_name(code, bgn); |
5830 | 5819 | ||
5831 | r.t = XC_RESULT_VAR; | 5820 | r.t = XC_RESULT_VAR; |
5832 | r.d.id.name = name; | 5821 | r.d.id.name = name; |
5833 | 5822 | ||
5834 | #if ENABLE_DC | 5823 | #if ENABLE_DC |
5835 | if (pop || copy) { | 5824 | if (pop || copy) { |
5836 | BcVec *v = bc_program_search(name, true); | 5825 | BcVec *v = xc_program_search(name, true); |
5837 | BcNum *num = bc_vec_top(v); | 5826 | BcNum *num = bc_vec_top(v); |
5838 | 5827 | ||
5839 | free(name); | 5828 | free(name); |
@@ -5858,7 +5847,7 @@ static BC_STATUS bc_program_pushVar(char *code, size_t *bgn, | |||
5858 | 5847 | ||
5859 | RETURN_STATUS(BC_STATUS_SUCCESS); | 5848 | RETURN_STATUS(BC_STATUS_SUCCESS); |
5860 | } | 5849 | } |
5861 | #define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS) | 5850 | #define zxc_program_pushVar(...) (xc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS) |
5862 | 5851 | ||
5863 | static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst) | 5852 | static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst) |
5864 | { | 5853 | { |
@@ -5866,7 +5855,7 @@ static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst) | |||
5866 | BcResult r; | 5855 | BcResult r; |
5867 | BcNum *num; | 5856 | BcNum *num; |
5868 | 5857 | ||
5869 | r.d.id.name = bc_program_name(code, bgn); | 5858 | r.d.id.name = xc_program_name(code, bgn); |
5870 | 5859 | ||
5871 | if (inst == XC_INST_ARRAY) { | 5860 | if (inst == XC_INST_ARRAY) { |
5872 | r.t = XC_RESULT_ARRAY; | 5861 | r.t = XC_RESULT_ARRAY; |
@@ -5875,7 +5864,7 @@ static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst) | |||
5875 | BcResult *operand; | 5864 | BcResult *operand; |
5876 | unsigned long temp; | 5865 | unsigned long temp; |
5877 | 5866 | ||
5878 | s = zbc_program_prep(&operand, &num); | 5867 | s = zxc_program_prep(&operand, &num); |
5879 | if (s) goto err; | 5868 | if (s) goto err; |
5880 | s = zbc_num_ulong(num, &temp); | 5869 | s = zbc_num_ulong(num, &temp); |
5881 | if (s) goto err; | 5870 | if (s) goto err; |
@@ -5886,7 +5875,7 @@ static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst) | |||
5886 | } | 5875 | } |
5887 | 5876 | ||
5888 | r.d.id.idx = (size_t) temp; | 5877 | r.d.id.idx = (size_t) temp; |
5889 | bc_program_retire(&r, XC_RESULT_ARRAY_ELEM); | 5878 | xc_program_retire(&r, XC_RESULT_ARRAY_ELEM); |
5890 | } | 5879 | } |
5891 | err: | 5880 | err: |
5892 | if (s) free(r.d.id.name); | 5881 | if (s) free(r.d.id.name); |
@@ -5902,7 +5891,7 @@ static BC_STATUS zbc_program_incdec(char inst) | |||
5902 | BcNum *num; | 5891 | BcNum *num; |
5903 | char inst2 = inst; | 5892 | char inst2 = inst; |
5904 | 5893 | ||
5905 | s = zbc_program_prep(&ptr, &num); | 5894 | s = zxc_program_prep(&ptr, &num); |
5906 | if (s) RETURN_STATUS(s); | 5895 | if (s) RETURN_STATUS(s); |
5907 | 5896 | ||
5908 | if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) { | 5897 | if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) { |
@@ -5917,7 +5906,7 @@ static BC_STATUS zbc_program_incdec(char inst) | |||
5917 | : BC_INST_ASSIGN_MINUS; | 5906 | : BC_INST_ASSIGN_MINUS; |
5918 | 5907 | ||
5919 | bc_vec_push(&G.prog.results, &res); | 5908 | bc_vec_push(&G.prog.results, &res); |
5920 | s = zbc_program_assign(inst); | 5909 | s = zxc_program_assign(inst); |
5921 | if (s) RETURN_STATUS(s); | 5910 | if (s) RETURN_STATUS(s); |
5922 | 5911 | ||
5923 | if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) { | 5912 | if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) { |
@@ -5936,10 +5925,10 @@ static BC_STATUS zbc_program_call(char *code, size_t *idx) | |||
5936 | BcId *a; | 5925 | BcId *a; |
5937 | BcResult *arg; | 5926 | BcResult *arg; |
5938 | 5927 | ||
5939 | nparams = bc_program_index(code, idx); | 5928 | nparams = xc_program_index(code, idx); |
5940 | ip.inst_idx = 0; | 5929 | ip.inst_idx = 0; |
5941 | ip.func = bc_program_index(code, idx); | 5930 | ip.func = xc_program_index(code, idx); |
5942 | func = bc_program_func(ip.func); | 5931 | func = xc_program_func(ip.func); |
5943 | 5932 | ||
5944 | if (func->code.len == 0) { | 5933 | if (func->code.len == 0) { |
5945 | RETURN_STATUS(bc_error("undefined function")); | 5934 | RETURN_STATUS(bc_error("undefined function")); |
@@ -5958,7 +5947,7 @@ static BC_STATUS zbc_program_call(char *code, size_t *idx) | |||
5958 | if ((!a->idx) != (arg->t == XC_RESULT_ARRAY) || arg->t == XC_RESULT_STR) | 5947 | if ((!a->idx) != (arg->t == XC_RESULT_ARRAY) || arg->t == XC_RESULT_STR) |
5959 | RETURN_STATUS(bc_error_variable_is_wrong_type()); | 5948 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
5960 | 5949 | ||
5961 | s = zbc_program_copyToVar(a->name, a->idx); | 5950 | s = zxc_program_copyToVar(a->name, a->idx); |
5962 | if (s) RETURN_STATUS(s); | 5951 | if (s) RETURN_STATUS(s); |
5963 | } | 5952 | } |
5964 | 5953 | ||
@@ -5966,7 +5955,7 @@ static BC_STATUS zbc_program_call(char *code, size_t *idx) | |||
5966 | for (; i < func->autos.len; i++, a++) { | 5955 | for (; i < func->autos.len; i++, a++) { |
5967 | BcVec *v; | 5956 | BcVec *v; |
5968 | 5957 | ||
5969 | v = bc_program_search(a->name, a->idx); | 5958 | v = xc_program_search(a->name, a->idx); |
5970 | if (a->idx) { | 5959 | if (a->idx) { |
5971 | BcNum n2; | 5960 | BcNum n2; |
5972 | bc_num_init_DEF_SIZE(&n2); | 5961 | bc_num_init_DEF_SIZE(&n2); |
@@ -5995,7 +5984,7 @@ static BC_STATUS zbc_program_return(char inst) | |||
5995 | if (!STACK_HAS_EQUAL_OR_MORE_THAN(&G.prog.results, ip->results_len_before_call + (inst == XC_INST_RET))) | 5984 | if (!STACK_HAS_EQUAL_OR_MORE_THAN(&G.prog.results, ip->results_len_before_call + (inst == XC_INST_RET))) |
5996 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); | 5985 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
5997 | 5986 | ||
5998 | f = bc_program_func(ip->func); | 5987 | f = xc_program_func(ip->func); |
5999 | res.t = XC_RESULT_TEMP; | 5988 | res.t = XC_RESULT_TEMP; |
6000 | 5989 | ||
6001 | if (inst == XC_INST_RET) { | 5990 | if (inst == XC_INST_RET) { |
@@ -6003,7 +5992,7 @@ static BC_STATUS zbc_program_return(char inst) | |||
6003 | BcNum *num; | 5992 | BcNum *num; |
6004 | BcResult *operand = bc_vec_top(&G.prog.results); | 5993 | BcResult *operand = bc_vec_top(&G.prog.results); |
6005 | 5994 | ||
6006 | s = zbc_program_num(operand, &num, false); | 5995 | s = zxc_program_num(operand, &num, false); |
6007 | if (s) RETURN_STATUS(s); | 5996 | if (s) RETURN_STATUS(s); |
6008 | bc_num_init(&res.d.n, num->len); | 5997 | bc_num_init(&res.d.n, num->len); |
6009 | bc_num_copy(&res.d.n, num); | 5998 | bc_num_copy(&res.d.n, num); |
@@ -6016,7 +6005,7 @@ static BC_STATUS zbc_program_return(char inst) | |||
6016 | a = (void*)f->autos.v; | 6005 | a = (void*)f->autos.v; |
6017 | for (i = 0; i < f->autos.len; i++, a++) { | 6006 | for (i = 0; i < f->autos.len; i++, a++) { |
6018 | BcVec *v; | 6007 | BcVec *v; |
6019 | v = bc_program_search(a->name, a->idx); | 6008 | v = xc_program_search(a->name, a->idx); |
6020 | bc_vec_pop(v); | 6009 | bc_vec_pop(v); |
6021 | } | 6010 | } |
6022 | 6011 | ||
@@ -6029,12 +6018,12 @@ static BC_STATUS zbc_program_return(char inst) | |||
6029 | #define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS) | 6018 | #define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS) |
6030 | #endif // ENABLE_BC | 6019 | #endif // ENABLE_BC |
6031 | 6020 | ||
6032 | static unsigned long bc_program_scale(BcNum *n) | 6021 | static unsigned long xc_program_scale(BcNum *n) |
6033 | { | 6022 | { |
6034 | return (unsigned long) n->rdx; | 6023 | return (unsigned long) n->rdx; |
6035 | } | 6024 | } |
6036 | 6025 | ||
6037 | static unsigned long bc_program_len(BcNum *n) | 6026 | static unsigned long xc_program_len(BcNum *n) |
6038 | { | 6027 | { |
6039 | size_t len = n->len; | 6028 | size_t len = n->len; |
6040 | 6029 | ||
@@ -6047,7 +6036,7 @@ static unsigned long bc_program_len(BcNum *n) | |||
6047 | return len; | 6036 | return len; |
6048 | } | 6037 | } |
6049 | 6038 | ||
6050 | static BC_STATUS zbc_program_builtin(char inst) | 6039 | static BC_STATUS zxc_program_builtin(char inst) |
6051 | { | 6040 | { |
6052 | BcStatus s; | 6041 | BcStatus s; |
6053 | BcResult *opnd; | 6042 | BcResult *opnd; |
@@ -6059,7 +6048,7 @@ static BC_STATUS zbc_program_builtin(char inst) | |||
6059 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); | 6048 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
6060 | opnd = bc_vec_top(&G.prog.results); | 6049 | opnd = bc_vec_top(&G.prog.results); |
6061 | 6050 | ||
6062 | s = zbc_program_num(opnd, &num, false); | 6051 | s = zxc_program_num(opnd, &num, false); |
6063 | if (s) RETURN_STATUS(s); | 6052 | if (s) RETURN_STATUS(s); |
6064 | 6053 | ||
6065 | #if ENABLE_DC | 6054 | #if ENABLE_DC |
@@ -6081,19 +6070,19 @@ static BC_STATUS zbc_program_builtin(char inst) | |||
6081 | char **str; | 6070 | char **str; |
6082 | size_t idx = opnd->t == XC_RESULT_STR ? opnd->d.id.idx : num->rdx; | 6071 | size_t idx = opnd->t == XC_RESULT_STR ? opnd->d.id.idx : num->rdx; |
6083 | 6072 | ||
6084 | str = bc_program_str(idx); | 6073 | str = xc_program_str(idx); |
6085 | bc_num_ulong2num(&res.d.n, strlen(*str)); | 6074 | bc_num_ulong2num(&res.d.n, strlen(*str)); |
6086 | } | 6075 | } |
6087 | #endif | 6076 | #endif |
6088 | else { | 6077 | else { |
6089 | bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num)); | 6078 | bc_num_ulong2num(&res.d.n, len ? xc_program_len(num) : xc_program_scale(num)); |
6090 | } | 6079 | } |
6091 | 6080 | ||
6092 | bc_program_retire(&res, XC_RESULT_TEMP); | 6081 | xc_program_retire(&res, XC_RESULT_TEMP); |
6093 | 6082 | ||
6094 | RETURN_STATUS(s); | 6083 | RETURN_STATUS(s); |
6095 | } | 6084 | } |
6096 | #define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__) COMMA_SUCCESS) | 6085 | #define zxc_program_builtin(...) (zxc_program_builtin(__VA_ARGS__) COMMA_SUCCESS) |
6097 | 6086 | ||
6098 | #if ENABLE_DC | 6087 | #if ENABLE_DC |
6099 | static BC_STATUS zdc_program_divmod(void) | 6088 | static BC_STATUS zdc_program_divmod(void) |
@@ -6102,7 +6091,7 @@ static BC_STATUS zdc_program_divmod(void) | |||
6102 | BcResult *opd1, *opd2, res, res2; | 6091 | BcResult *opd1, *opd2, res, res2; |
6103 | BcNum *n1, *n2; | 6092 | BcNum *n1, *n2; |
6104 | 6093 | ||
6105 | s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); | 6094 | s = zxc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
6106 | if (s) RETURN_STATUS(s); | 6095 | if (s) RETURN_STATUS(s); |
6107 | 6096 | ||
6108 | bc_num_init_DEF_SIZE(&res.d.n); | 6097 | bc_num_init_DEF_SIZE(&res.d.n); |
@@ -6111,7 +6100,7 @@ static BC_STATUS zdc_program_divmod(void) | |||
6111 | s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale); | 6100 | s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale); |
6112 | if (s) goto err; | 6101 | if (s) goto err; |
6113 | 6102 | ||
6114 | bc_program_binOpRetire(&res2); | 6103 | xc_program_binOpRetire(&res2); |
6115 | res.t = XC_RESULT_TEMP; | 6104 | res.t = XC_RESULT_TEMP; |
6116 | bc_vec_push(&G.prog.results, &res); | 6105 | bc_vec_push(&G.prog.results, &res); |
6117 | 6106 | ||
@@ -6131,11 +6120,11 @@ static BC_STATUS zdc_program_modexp(void) | |||
6131 | 6120 | ||
6132 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 2)) | 6121 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 2)) |
6133 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); | 6122 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
6134 | s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false); | 6123 | s = zxc_program_binOpPrep(&r2, &n2, &r3, &n3, false); |
6135 | if (s) RETURN_STATUS(s); | 6124 | if (s) RETURN_STATUS(s); |
6136 | 6125 | ||
6137 | r1 = bc_vec_item_rev(&G.prog.results, 2); | 6126 | r1 = bc_vec_item_rev(&G.prog.results, 2); |
6138 | s = zbc_program_num(r1, &n1, false); | 6127 | s = zxc_program_num(r1, &n1, false); |
6139 | if (s) RETURN_STATUS(s); | 6128 | if (s) RETURN_STATUS(s); |
6140 | if (!BC_PROG_NUM(r1, n1)) | 6129 | if (!BC_PROG_NUM(r1, n1)) |
6141 | RETURN_STATUS(bc_error_variable_is_wrong_type()); | 6130 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
@@ -6143,11 +6132,11 @@ static BC_STATUS zdc_program_modexp(void) | |||
6143 | // Make sure that the values have their pointers updated, if necessary. | 6132 | // Make sure that the values have their pointers updated, if necessary. |
6144 | if (r1->t == XC_RESULT_VAR || r1->t == XC_RESULT_ARRAY_ELEM) { | 6133 | if (r1->t == XC_RESULT_VAR || r1->t == XC_RESULT_ARRAY_ELEM) { |
6145 | if (r1->t == r2->t) { | 6134 | if (r1->t == r2->t) { |
6146 | s = zbc_program_num(r2, &n2, false); | 6135 | s = zxc_program_num(r2, &n2, false); |
6147 | if (s) RETURN_STATUS(s); | 6136 | if (s) RETURN_STATUS(s); |
6148 | } | 6137 | } |
6149 | if (r1->t == r3->t) { | 6138 | if (r1->t == r3->t) { |
6150 | s = zbc_program_num(r3, &n3, false); | 6139 | s = zxc_program_num(r3, &n3, false); |
6151 | if (s) RETURN_STATUS(s); | 6140 | if (s) RETURN_STATUS(s); |
6152 | } | 6141 | } |
6153 | } | 6142 | } |
@@ -6157,7 +6146,7 @@ static BC_STATUS zdc_program_modexp(void) | |||
6157 | if (s) goto err; | 6146 | if (s) goto err; |
6158 | 6147 | ||
6159 | bc_vec_pop(&G.prog.results); | 6148 | bc_vec_pop(&G.prog.results); |
6160 | bc_program_binOpRetire(&res); | 6149 | xc_program_binOpRetire(&res); |
6161 | 6150 | ||
6162 | RETURN_STATUS(s); | 6151 | RETURN_STATUS(s); |
6163 | err: | 6152 | err: |
@@ -6192,7 +6181,7 @@ static BC_STATUS zdc_program_asciify(void) | |||
6192 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); | 6181 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
6193 | r = bc_vec_top(&G.prog.results); | 6182 | r = bc_vec_top(&G.prog.results); |
6194 | 6183 | ||
6195 | s = zbc_program_num(r, &num, false); | 6184 | s = zxc_program_num(r, &num, false); |
6196 | if (s) RETURN_STATUS(s); | 6185 | if (s) RETURN_STATUS(s); |
6197 | 6186 | ||
6198 | if (BC_PROG_NUM(r, num)) { | 6187 | if (BC_PROG_NUM(r, num)) { |
@@ -6219,7 +6208,7 @@ static BC_STATUS zdc_program_asciify(void) | |||
6219 | } else { | 6208 | } else { |
6220 | char *sp; | 6209 | char *sp; |
6221 | idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx; | 6210 | idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : num->rdx; |
6222 | sp = *bc_program_str(idx); | 6211 | sp = *xc_program_str(idx); |
6223 | c = sp[0]; | 6212 | c = sp[0]; |
6224 | } | 6213 | } |
6225 | 6214 | ||
@@ -6256,15 +6245,15 @@ static BC_STATUS zdc_program_printStream(void) | |||
6256 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); | 6245 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
6257 | r = bc_vec_top(&G.prog.results); | 6246 | r = bc_vec_top(&G.prog.results); |
6258 | 6247 | ||
6259 | s = zbc_program_num(r, &n, false); | 6248 | s = zxc_program_num(r, &n, false); |
6260 | if (s) RETURN_STATUS(s); | 6249 | if (s) RETURN_STATUS(s); |
6261 | 6250 | ||
6262 | if (BC_PROG_NUM(r, n)) { | 6251 | if (BC_PROG_NUM(r, n)) { |
6263 | s = zbc_num_printNum(n, 0x100, 1, dc_num_printChar); | 6252 | s = zxc_num_printNum(n, 0x100, 1, dc_num_printChar); |
6264 | } else { | 6253 | } else { |
6265 | char *str; | 6254 | char *str; |
6266 | idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : n->rdx; | 6255 | idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : n->rdx; |
6267 | str = *bc_program_str(idx); | 6256 | str = *xc_program_str(idx); |
6268 | fputs(str, stdout); | 6257 | fputs(str, stdout); |
6269 | } | 6258 | } |
6270 | 6259 | ||
@@ -6279,7 +6268,7 @@ static BC_STATUS zdc_program_nquit(void) | |||
6279 | BcNum *num; | 6268 | BcNum *num; |
6280 | unsigned long val; | 6269 | unsigned long val; |
6281 | 6270 | ||
6282 | s = zbc_program_prep(&opnd, &num); | 6271 | s = zxc_program_prep(&opnd, &num); |
6283 | if (s) RETURN_STATUS(s); | 6272 | if (s) RETURN_STATUS(s); |
6284 | s = zbc_num_ulong(num, &val); | 6273 | s = zbc_num_ulong(num, &val); |
6285 | if (s) RETURN_STATUS(s); | 6274 | if (s) RETURN_STATUS(s); |
@@ -6315,13 +6304,13 @@ static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond) | |||
6315 | BcNum *n = n; // for compiler | 6304 | BcNum *n = n; // for compiler |
6316 | bool exec; | 6305 | bool exec; |
6317 | char *name; | 6306 | char *name; |
6318 | char *then_name = bc_program_name(code, bgn); | 6307 | char *then_name = xc_program_name(code, bgn); |
6319 | char *else_name = NULL; | 6308 | char *else_name = NULL; |
6320 | 6309 | ||
6321 | if (code[*bgn] == '\0') | 6310 | if (code[*bgn] == '\0') |
6322 | (*bgn) += 1; | 6311 | (*bgn) += 1; |
6323 | else | 6312 | else |
6324 | else_name = bc_program_name(code, bgn); | 6313 | else_name = xc_program_name(code, bgn); |
6325 | 6314 | ||
6326 | exec = r->d.n.len != 0; | 6315 | exec = r->d.n.len != 0; |
6327 | name = then_name; | 6316 | name = then_name; |
@@ -6332,7 +6321,7 @@ static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond) | |||
6332 | 6321 | ||
6333 | if (exec) { | 6322 | if (exec) { |
6334 | BcVec *v; | 6323 | BcVec *v; |
6335 | v = bc_program_search(name, true); | 6324 | v = xc_program_search(name, true); |
6336 | n = bc_vec_top(v); | 6325 | n = bc_vec_top(v); |
6337 | } | 6326 | } |
6338 | 6327 | ||
@@ -6351,7 +6340,7 @@ static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond) | |||
6351 | sidx = r->d.id.idx; | 6340 | sidx = r->d.id.idx; |
6352 | } else if (r->t == XC_RESULT_VAR) { | 6341 | } else if (r->t == XC_RESULT_VAR) { |
6353 | BcNum *n; | 6342 | BcNum *n; |
6354 | s = zbc_program_num(r, &n, false); | 6343 | s = zxc_program_num(r, &n, false); |
6355 | if (s || !BC_PROG_STR(n)) goto exit; | 6344 | if (s || !BC_PROG_STR(n)) goto exit; |
6356 | sidx = n->rdx; | 6345 | sidx = n->rdx; |
6357 | } else | 6346 | } else |
@@ -6360,24 +6349,24 @@ static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond) | |||
6360 | 6349 | ||
6361 | fidx = sidx + BC_PROG_REQ_FUNCS; | 6350 | fidx = sidx + BC_PROG_REQ_FUNCS; |
6362 | 6351 | ||
6363 | f = bc_program_func(fidx); | 6352 | f = xc_program_func(fidx); |
6364 | 6353 | ||
6365 | if (f->code.len == 0) { | 6354 | if (f->code.len == 0) { |
6366 | BcParse sv_parse; | 6355 | BcParse sv_parse; |
6367 | char *str; | 6356 | char *str; |
6368 | 6357 | ||
6369 | sv_parse = G.prs; // struct copy | 6358 | sv_parse = G.prs; // struct copy |
6370 | bc_parse_create(fidx); | 6359 | xc_parse_create(fidx); |
6371 | str = *bc_program_str(sidx); | 6360 | str = *xc_program_str(sidx); |
6372 | s = zbc_parse_text_init(str); | 6361 | s = zxc_parse_text_init(str); |
6373 | if (s) goto err; | 6362 | if (s) goto err; |
6374 | 6363 | ||
6375 | s = zdc_parse_exprs_until_eof(); | 6364 | s = zdc_parse_exprs_until_eof(); |
6376 | if (s) goto err; | 6365 | if (s) goto err; |
6377 | bc_parse_push(DC_INST_POP_EXEC); | 6366 | xc_parse_push(DC_INST_POP_EXEC); |
6378 | if (G.prs.lex != XC_LEX_EOF) | 6367 | if (G.prs.lex != XC_LEX_EOF) |
6379 | s = bc_error_bad_expression(); | 6368 | s = bc_error_bad_expression(); |
6380 | bc_parse_free(); | 6369 | xc_parse_free(); |
6381 | G.prs = sv_parse; // struct copy | 6370 | G.prs = sv_parse; // struct copy |
6382 | if (s) { | 6371 | if (s) { |
6383 | err: | 6372 | err: |
@@ -6400,7 +6389,7 @@ static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond) | |||
6400 | #define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS) | 6389 | #define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS) |
6401 | #endif // ENABLE_DC | 6390 | #endif // ENABLE_DC |
6402 | 6391 | ||
6403 | static void bc_program_pushGlobal(char inst) | 6392 | static void xc_program_pushGlobal(char inst) |
6404 | { | 6393 | { |
6405 | BcResult res; | 6394 | BcResult res; |
6406 | unsigned long val; | 6395 | unsigned long val; |
@@ -6418,11 +6407,11 @@ static void bc_program_pushGlobal(char inst) | |||
6418 | bc_vec_push(&G.prog.results, &res); | 6407 | bc_vec_push(&G.prog.results, &res); |
6419 | } | 6408 | } |
6420 | 6409 | ||
6421 | static BC_STATUS zbc_program_exec(void) | 6410 | static BC_STATUS zxc_program_exec(void) |
6422 | { | 6411 | { |
6423 | BcResult r, *ptr; | 6412 | BcResult r, *ptr; |
6424 | BcInstPtr *ip = bc_vec_top(&G.prog.exestack); | 6413 | BcInstPtr *ip = bc_vec_top(&G.prog.exestack); |
6425 | BcFunc *func = bc_program_func(ip->func); | 6414 | BcFunc *func = xc_program_func(ip->func); |
6426 | char *code = func->code.v; | 6415 | char *code = func->code.v; |
6427 | 6416 | ||
6428 | dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d", | 6417 | dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d", |
@@ -6438,18 +6427,18 @@ static BC_STATUS zbc_program_exec(void) | |||
6438 | BcNum *num; | 6427 | BcNum *num; |
6439 | bool zero; | 6428 | bool zero; |
6440 | dbg_exec("BC_INST_JUMP_ZERO:"); | 6429 | dbg_exec("BC_INST_JUMP_ZERO:"); |
6441 | s = zbc_program_prep(&ptr, &num); | 6430 | s = zxc_program_prep(&ptr, &num); |
6442 | if (s) RETURN_STATUS(s); | 6431 | if (s) RETURN_STATUS(s); |
6443 | zero = (bc_num_cmp(num, &G.prog.zero) == 0); | 6432 | zero = (bc_num_cmp(num, &G.prog.zero) == 0); |
6444 | bc_vec_pop(&G.prog.results); | 6433 | bc_vec_pop(&G.prog.results); |
6445 | if (!zero) { | 6434 | if (!zero) { |
6446 | bc_program_index(code, &ip->inst_idx); | 6435 | xc_program_index(code, &ip->inst_idx); |
6447 | break; | 6436 | break; |
6448 | } | 6437 | } |
6449 | // else: fall through | 6438 | // else: fall through |
6450 | } | 6439 | } |
6451 | case BC_INST_JUMP: { | 6440 | case BC_INST_JUMP: { |
6452 | size_t idx = bc_program_index(code, &ip->inst_idx); | 6441 | size_t idx = xc_program_index(code, &ip->inst_idx); |
6453 | size_t *addr = bc_vec_item(&func->labels, idx); | 6442 | size_t *addr = bc_vec_item(&func->labels, idx); |
6454 | dbg_exec("BC_INST_JUMP: to %ld", (long)*addr); | 6443 | dbg_exec("BC_INST_JUMP: to %ld", (long)*addr); |
6455 | ip->inst_idx = *addr; | 6444 | ip->inst_idx = *addr; |
@@ -6485,15 +6474,15 @@ static BC_STATUS zbc_program_exec(void) | |||
6485 | case XC_INST_REL_LT: | 6474 | case XC_INST_REL_LT: |
6486 | case XC_INST_REL_GT: | 6475 | case XC_INST_REL_GT: |
6487 | dbg_exec("BC_INST_BOOL:"); | 6476 | dbg_exec("BC_INST_BOOL:"); |
6488 | s = zbc_program_logical(inst); | 6477 | s = zxc_program_logical(inst); |
6489 | break; | 6478 | break; |
6490 | case XC_INST_READ: | 6479 | case XC_INST_READ: |
6491 | dbg_exec("XC_INST_READ:"); | 6480 | dbg_exec("XC_INST_READ:"); |
6492 | s = zbc_program_read(); | 6481 | s = zxc_program_read(); |
6493 | goto read_updated_ip; | 6482 | goto read_updated_ip; |
6494 | case XC_INST_VAR: | 6483 | case XC_INST_VAR: |
6495 | dbg_exec("XC_INST_VAR:"); | 6484 | dbg_exec("XC_INST_VAR:"); |
6496 | s = zbc_program_pushVar(code, &ip->inst_idx, false, false); | 6485 | s = zxc_program_pushVar(code, &ip->inst_idx, false, false); |
6497 | break; | 6486 | break; |
6498 | case XC_INST_ARRAY_ELEM: | 6487 | case XC_INST_ARRAY_ELEM: |
6499 | case XC_INST_ARRAY: | 6488 | case XC_INST_ARRAY: |
@@ -6511,18 +6500,18 @@ static BC_STATUS zbc_program_exec(void) | |||
6511 | case XC_INST_OBASE: | 6500 | case XC_INST_OBASE: |
6512 | case XC_INST_SCALE: | 6501 | case XC_INST_SCALE: |
6513 | dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE); | 6502 | dbg_exec("XC_INST_internalvar(%d):", inst - XC_INST_IBASE); |
6514 | bc_program_pushGlobal(inst); | 6503 | xc_program_pushGlobal(inst); |
6515 | break; | 6504 | break; |
6516 | case XC_INST_SCALE_FUNC: | 6505 | case XC_INST_SCALE_FUNC: |
6517 | case XC_INST_LENGTH: | 6506 | case XC_INST_LENGTH: |
6518 | case XC_INST_SQRT: | 6507 | case XC_INST_SQRT: |
6519 | dbg_exec("BC_INST_builtin:"); | 6508 | dbg_exec("BC_INST_builtin:"); |
6520 | s = zbc_program_builtin(inst); | 6509 | s = zxc_program_builtin(inst); |
6521 | break; | 6510 | break; |
6522 | case XC_INST_NUM: | 6511 | case XC_INST_NUM: |
6523 | dbg_exec("XC_INST_NUM:"); | 6512 | dbg_exec("XC_INST_NUM:"); |
6524 | r.t = XC_RESULT_CONSTANT; | 6513 | r.t = XC_RESULT_CONSTANT; |
6525 | r.d.id.idx = bc_program_index(code, &ip->inst_idx); | 6514 | r.d.id.idx = xc_program_index(code, &ip->inst_idx); |
6526 | bc_vec_push(&G.prog.results, &r); | 6515 | bc_vec_push(&G.prog.results, &r); |
6527 | break; | 6516 | break; |
6528 | case XC_INST_POP: | 6517 | case XC_INST_POP: |
@@ -6536,12 +6525,12 @@ static BC_STATUS zbc_program_exec(void) | |||
6536 | case XC_INST_PRINT_POP: | 6525 | case XC_INST_PRINT_POP: |
6537 | case XC_INST_PRINT_STR: | 6526 | case XC_INST_PRINT_STR: |
6538 | dbg_exec("XC_INST_PRINTxyz:"); | 6527 | dbg_exec("XC_INST_PRINTxyz:"); |
6539 | s = zbc_program_print(inst, 0); | 6528 | s = zxc_program_print(inst, 0); |
6540 | break; | 6529 | break; |
6541 | case XC_INST_STR: | 6530 | case XC_INST_STR: |
6542 | dbg_exec("XC_INST_STR:"); | 6531 | dbg_exec("XC_INST_STR:"); |
6543 | r.t = XC_RESULT_STR; | 6532 | r.t = XC_RESULT_STR; |
6544 | r.d.id.idx = bc_program_index(code, &ip->inst_idx); | 6533 | r.d.id.idx = xc_program_index(code, &ip->inst_idx); |
6545 | bc_vec_push(&G.prog.results, &r); | 6534 | bc_vec_push(&G.prog.results, &r); |
6546 | break; | 6535 | break; |
6547 | case XC_INST_POWER: | 6536 | case XC_INST_POWER: |
@@ -6551,23 +6540,23 @@ static BC_STATUS zbc_program_exec(void) | |||
6551 | case XC_INST_PLUS: | 6540 | case XC_INST_PLUS: |
6552 | case XC_INST_MINUS: | 6541 | case XC_INST_MINUS: |
6553 | dbg_exec("BC_INST_binaryop:"); | 6542 | dbg_exec("BC_INST_binaryop:"); |
6554 | s = zbc_program_op(inst); | 6543 | s = zxc_program_op(inst); |
6555 | break; | 6544 | break; |
6556 | case XC_INST_BOOL_NOT: { | 6545 | case XC_INST_BOOL_NOT: { |
6557 | BcNum *num; | 6546 | BcNum *num; |
6558 | dbg_exec("XC_INST_BOOL_NOT:"); | 6547 | dbg_exec("XC_INST_BOOL_NOT:"); |
6559 | s = zbc_program_prep(&ptr, &num); | 6548 | s = zxc_program_prep(&ptr, &num); |
6560 | if (s) RETURN_STATUS(s); | 6549 | if (s) RETURN_STATUS(s); |
6561 | bc_num_init_DEF_SIZE(&r.d.n); | 6550 | bc_num_init_DEF_SIZE(&r.d.n); |
6562 | if (bc_num_cmp(num, &G.prog.zero) == 0) | 6551 | if (bc_num_cmp(num, &G.prog.zero) == 0) |
6563 | bc_num_one(&r.d.n); | 6552 | bc_num_one(&r.d.n); |
6564 | //else bc_num_zero(&r.d.n); - already is | 6553 | //else bc_num_zero(&r.d.n); - already is |
6565 | bc_program_retire(&r, XC_RESULT_TEMP); | 6554 | xc_program_retire(&r, XC_RESULT_TEMP); |
6566 | break; | 6555 | break; |
6567 | } | 6556 | } |
6568 | case XC_INST_NEG: | 6557 | case XC_INST_NEG: |
6569 | dbg_exec("XC_INST_NEG:"); | 6558 | dbg_exec("XC_INST_NEG:"); |
6570 | s = zbc_program_negate(); | 6559 | s = zxc_program_negate(); |
6571 | break; | 6560 | break; |
6572 | #if ENABLE_BC | 6561 | #if ENABLE_BC |
6573 | case BC_INST_ASSIGN_POWER: | 6562 | case BC_INST_ASSIGN_POWER: |
@@ -6579,7 +6568,7 @@ static BC_STATUS zbc_program_exec(void) | |||
6579 | #endif | 6568 | #endif |
6580 | case XC_INST_ASSIGN: | 6569 | case XC_INST_ASSIGN: |
6581 | dbg_exec("BC_INST_ASSIGNxyz:"); | 6570 | dbg_exec("BC_INST_ASSIGNxyz:"); |
6582 | s = zbc_program_assign(inst); | 6571 | s = zxc_program_assign(inst); |
6583 | break; | 6572 | break; |
6584 | #if ENABLE_DC | 6573 | #if ENABLE_DC |
6585 | case DC_INST_POP_EXEC: | 6574 | case DC_INST_POP_EXEC: |
@@ -6603,7 +6592,7 @@ static BC_STATUS zbc_program_exec(void) | |||
6603 | size_t idx; | 6592 | size_t idx; |
6604 | dbg_exec("DC_INST_PRINT_STACK:"); | 6593 | dbg_exec("DC_INST_PRINT_STACK:"); |
6605 | for (idx = 0; idx < G.prog.results.len; ++idx) { | 6594 | for (idx = 0; idx < G.prog.results.len; ++idx) { |
6606 | s = zbc_program_print(XC_INST_PRINT, idx); | 6595 | s = zxc_program_print(XC_INST_PRINT, idx); |
6607 | if (s) break; | 6596 | if (s) break; |
6608 | } | 6597 | } |
6609 | break; | 6598 | break; |
@@ -6647,12 +6636,12 @@ static BC_STATUS zbc_program_exec(void) | |||
6647 | case DC_INST_LOAD: | 6636 | case DC_INST_LOAD: |
6648 | case DC_INST_PUSH_VAR: { | 6637 | case DC_INST_PUSH_VAR: { |
6649 | bool copy = inst == DC_INST_LOAD; | 6638 | bool copy = inst == DC_INST_LOAD; |
6650 | s = zbc_program_pushVar(code, &ip->inst_idx, true, copy); | 6639 | s = zxc_program_pushVar(code, &ip->inst_idx, true, copy); |
6651 | break; | 6640 | break; |
6652 | } | 6641 | } |
6653 | case DC_INST_PUSH_TO_VAR: { | 6642 | case DC_INST_PUSH_TO_VAR: { |
6654 | char *name = bc_program_name(code, &ip->inst_idx); | 6643 | char *name = xc_program_name(code, &ip->inst_idx); |
6655 | s = zbc_program_copyToVar(name, true); | 6644 | s = zxc_program_copyToVar(name, true); |
6656 | free(name); | 6645 | free(name); |
6657 | break; | 6646 | break; |
6658 | } | 6647 | } |
@@ -6670,13 +6659,13 @@ static BC_STATUS zbc_program_exec(void) | |||
6670 | read_updated_ip: | 6659 | read_updated_ip: |
6671 | // Instruction stack has changed, read new pointers | 6660 | // Instruction stack has changed, read new pointers |
6672 | ip = bc_vec_top(&G.prog.exestack); | 6661 | ip = bc_vec_top(&G.prog.exestack); |
6673 | func = bc_program_func(ip->func); | 6662 | func = xc_program_func(ip->func); |
6674 | code = func->code.v; | 6663 | code = func->code.v; |
6675 | dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->inst_idx); | 6664 | dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->inst_idx); |
6676 | } | 6665 | } |
6677 | 6666 | ||
6678 | if (s || G_interrupt) { | 6667 | if (s || G_interrupt) { |
6679 | bc_program_reset(); | 6668 | xc_program_reset(); |
6680 | RETURN_STATUS(s); | 6669 | RETURN_STATUS(s); |
6681 | } | 6670 | } |
6682 | 6671 | ||
@@ -6685,9 +6674,9 @@ static BC_STATUS zbc_program_exec(void) | |||
6685 | 6674 | ||
6686 | RETURN_STATUS(BC_STATUS_SUCCESS); | 6675 | RETURN_STATUS(BC_STATUS_SUCCESS); |
6687 | } | 6676 | } |
6688 | #define zbc_program_exec(...) (zbc_program_exec(__VA_ARGS__) COMMA_SUCCESS) | 6677 | #define zxc_program_exec(...) (zxc_program_exec(__VA_ARGS__) COMMA_SUCCESS) |
6689 | 6678 | ||
6690 | static unsigned bc_vm_envLen(const char *var) | 6679 | static unsigned xc_vm_envLen(const char *var) |
6691 | { | 6680 | { |
6692 | char *lenv; | 6681 | char *lenv; |
6693 | unsigned len; | 6682 | unsigned len; |
@@ -6703,12 +6692,12 @@ static unsigned bc_vm_envLen(const char *var) | |||
6703 | return len; | 6692 | return len; |
6704 | } | 6693 | } |
6705 | 6694 | ||
6706 | static BC_STATUS zbc_vm_process(const char *text) | 6695 | static BC_STATUS zxc_vm_process(const char *text) |
6707 | { | 6696 | { |
6708 | BcStatus s; | 6697 | BcStatus s; |
6709 | 6698 | ||
6710 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); | 6699 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
6711 | s = zbc_parse_text_init(text); // does the first zbc_lex_next() | 6700 | s = zxc_parse_text_init(text); // does the first zxc_lex_next() |
6712 | if (s) RETURN_STATUS(s); | 6701 | if (s) RETURN_STATUS(s); |
6713 | 6702 | ||
6714 | IF_BC(check_eof:) | 6703 | IF_BC(check_eof:) |
@@ -6722,7 +6711,7 @@ static BC_STATUS zbc_vm_process(const char *text) | |||
6722 | if (G.prs.lex == BC_LEX_SCOLON | 6711 | if (G.prs.lex == BC_LEX_SCOLON |
6723 | || G.prs.lex == XC_LEX_NLINE | 6712 | || G.prs.lex == XC_LEX_NLINE |
6724 | ) { | 6713 | ) { |
6725 | s = zbc_lex_next(); | 6714 | s = zxc_lex_next(); |
6726 | if (s) goto err; | 6715 | if (s) goto err; |
6727 | goto check_eof; | 6716 | goto check_eof; |
6728 | } | 6717 | } |
@@ -6746,14 +6735,14 @@ static BC_STATUS zbc_vm_process(const char *text) | |||
6746 | goto err; | 6735 | goto err; |
6747 | } | 6736 | } |
6748 | // The above logic is fragile. Check these examples: | 6737 | // The above logic is fragile. Check these examples: |
6749 | // - interative read() still works | 6738 | // - interactive read() still works |
6750 | #endif | 6739 | #endif |
6751 | } else { | 6740 | } else { |
6752 | #if ENABLE_DC | 6741 | #if ENABLE_DC |
6753 | // Most of dc parsing assumes all whitespace, | 6742 | // Most of dc parsing assumes all whitespace, |
6754 | // including '\n', is eaten. | 6743 | // including '\n', is eaten. |
6755 | while (G.prs.lex == XC_LEX_NLINE) { | 6744 | while (G.prs.lex == XC_LEX_NLINE) { |
6756 | s = zbc_lex_next(); | 6745 | s = zxc_lex_next(); |
6757 | if (s) goto err; | 6746 | if (s) goto err; |
6758 | if (G.prs.lex == XC_LEX_EOF) | 6747 | if (G.prs.lex == XC_LEX_EOF) |
6759 | goto done; | 6748 | goto done; |
@@ -6763,14 +6752,14 @@ static BC_STATUS zbc_vm_process(const char *text) | |||
6763 | } | 6752 | } |
6764 | if (s || G_interrupt) { | 6753 | if (s || G_interrupt) { |
6765 | err: | 6754 | err: |
6766 | bc_parse_reset(); // includes bc_program_reset() | 6755 | xc_parse_reset(); // includes xc_program_reset() |
6767 | RETURN_STATUS(BC_STATUS_FAILURE); | 6756 | RETURN_STATUS(BC_STATUS_FAILURE); |
6768 | } | 6757 | } |
6769 | 6758 | ||
6770 | dbg_lex("%s:%d executing...", __func__, __LINE__); | 6759 | dbg_lex("%s:%d executing...", __func__, __LINE__); |
6771 | s = zbc_program_exec(); | 6760 | s = zxc_program_exec(); |
6772 | if (s) { | 6761 | if (s) { |
6773 | bc_program_reset(); | 6762 | xc_program_reset(); |
6774 | break; | 6763 | break; |
6775 | } | 6764 | } |
6776 | 6765 | ||
@@ -6781,7 +6770,7 @@ static BC_STATUS zbc_vm_process(const char *text) | |||
6781 | if (ip->func != BC_PROG_MAIN) | 6770 | if (ip->func != BC_PROG_MAIN) |
6782 | bb_error_msg_and_die("BUG:not MAIN"); | 6771 | bb_error_msg_and_die("BUG:not MAIN"); |
6783 | #endif | 6772 | #endif |
6784 | f = bc_program_func_BC_PROG_MAIN(); | 6773 | f = xc_program_func_BC_PROG_MAIN(); |
6785 | // bc discards strings, constants and code after each | 6774 | // bc discards strings, constants and code after each |
6786 | // top-level statement in the "main program". | 6775 | // top-level statement in the "main program". |
6787 | // This prevents "yes 1 | bc" from growing its memory | 6776 | // This prevents "yes 1 | bc" from growing its memory |
@@ -6824,9 +6813,9 @@ static BC_STATUS zbc_vm_process(const char *text) | |||
6824 | dbg_lex_done("%s:%d done", __func__, __LINE__); | 6813 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
6825 | RETURN_STATUS(s); | 6814 | RETURN_STATUS(s); |
6826 | } | 6815 | } |
6827 | #define zbc_vm_process(...) (zbc_vm_process(__VA_ARGS__) COMMA_SUCCESS) | 6816 | #define zxc_vm_process(...) (zxc_vm_process(__VA_ARGS__) COMMA_SUCCESS) |
6828 | 6817 | ||
6829 | static BC_STATUS zbc_vm_execute_FILE(FILE *fp, const char *filename) | 6818 | static BC_STATUS zxc_vm_execute_FILE(FILE *fp, const char *filename) |
6830 | { | 6819 | { |
6831 | // So far bc/dc have no way to include a file from another file, | 6820 | // So far bc/dc have no way to include a file from another file, |
6832 | // therefore we know G.prs.lex_filename == NULL on entry | 6821 | // therefore we know G.prs.lex_filename == NULL on entry |
@@ -6838,7 +6827,7 @@ static BC_STATUS zbc_vm_execute_FILE(FILE *fp, const char *filename) | |||
6838 | G.err_line = G.prs.lex_line = 1; | 6827 | G.err_line = G.prs.lex_line = 1; |
6839 | 6828 | ||
6840 | do { | 6829 | do { |
6841 | s = zbc_vm_process(""); | 6830 | s = zxc_vm_process(""); |
6842 | // We do not stop looping on errors here if reading stdin. | 6831 | // We do not stop looping on errors here if reading stdin. |
6843 | // Example: start interactive bc and enter "return". | 6832 | // Example: start interactive bc and enter "return". |
6844 | // It should say "'return' not in a function" | 6833 | // It should say "'return' not in a function" |
@@ -6847,20 +6836,20 @@ static BC_STATUS zbc_vm_execute_FILE(FILE *fp, const char *filename) | |||
6847 | G.prs.lex_filename = NULL; | 6836 | G.prs.lex_filename = NULL; |
6848 | RETURN_STATUS(s); | 6837 | RETURN_STATUS(s); |
6849 | } | 6838 | } |
6850 | #define zbc_vm_execute_FILE(...) (zbc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS) | 6839 | #define zxc_vm_execute_FILE(...) (zxc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS) |
6851 | 6840 | ||
6852 | static BC_STATUS zbc_vm_file(const char *file) | 6841 | static BC_STATUS zxc_vm_file(const char *file) |
6853 | { | 6842 | { |
6854 | BcStatus s; | 6843 | BcStatus s; |
6855 | FILE *fp; | 6844 | FILE *fp; |
6856 | 6845 | ||
6857 | fp = xfopen_for_read(file); | 6846 | fp = xfopen_for_read(file); |
6858 | s = zbc_vm_execute_FILE(fp, file); | 6847 | s = zxc_vm_execute_FILE(fp, file); |
6859 | fclose(fp); | 6848 | fclose(fp); |
6860 | 6849 | ||
6861 | RETURN_STATUS(s); | 6850 | RETURN_STATUS(s); |
6862 | } | 6851 | } |
6863 | #define zbc_vm_file(...) (zbc_vm_file(__VA_ARGS__) COMMA_SUCCESS) | 6852 | #define zxc_vm_file(...) (zxc_vm_file(__VA_ARGS__) COMMA_SUCCESS) |
6864 | 6853 | ||
6865 | #if ENABLE_BC | 6854 | #if ENABLE_BC |
6866 | static void bc_vm_info(void) | 6855 | static void bc_vm_info(void) |
@@ -7106,7 +7095,7 @@ static const char bc_lib[] ALIGN1 = { | |||
7106 | }; | 7095 | }; |
7107 | #endif // ENABLE_BC | 7096 | #endif // ENABLE_BC |
7108 | 7097 | ||
7109 | static BC_STATUS zbc_vm_exec(void) | 7098 | static BC_STATUS zxc_vm_exec(void) |
7110 | { | 7099 | { |
7111 | char **fname; | 7100 | char **fname; |
7112 | BcStatus s; | 7101 | BcStatus s; |
@@ -7117,7 +7106,7 @@ static BC_STATUS zbc_vm_exec(void) | |||
7117 | // We know that internal library is not buggy, | 7106 | // We know that internal library is not buggy, |
7118 | // thus error checking is normally disabled. | 7107 | // thus error checking is normally disabled. |
7119 | # define DEBUG_LIB 0 | 7108 | # define DEBUG_LIB 0 |
7120 | s = zbc_vm_process(bc_lib); | 7109 | s = zxc_vm_process(bc_lib); |
7121 | if (DEBUG_LIB && s) RETURN_STATUS(s); | 7110 | if (DEBUG_LIB && s) RETURN_STATUS(s); |
7122 | } | 7111 | } |
7123 | #endif | 7112 | #endif |
@@ -7125,7 +7114,7 @@ static BC_STATUS zbc_vm_exec(void) | |||
7125 | s = BC_STATUS_SUCCESS; | 7114 | s = BC_STATUS_SUCCESS; |
7126 | fname = (void*)G.files.v; | 7115 | fname = (void*)G.files.v; |
7127 | for (i = 0; i < G.files.len; i++) { | 7116 | for (i = 0; i < G.files.len; i++) { |
7128 | s = zbc_vm_file(*fname++); | 7117 | s = zxc_vm_file(*fname++); |
7129 | if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) { | 7118 | if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) { |
7130 | // Debug config, non-interactive mode: | 7119 | // Debug config, non-interactive mode: |
7131 | // return all the way back to main. | 7120 | // return all the way back to main. |
@@ -7136,14 +7125,14 @@ static BC_STATUS zbc_vm_exec(void) | |||
7136 | } | 7125 | } |
7137 | 7126 | ||
7138 | if (IS_BC || (option_mask32 & BC_FLAG_I)) | 7127 | if (IS_BC || (option_mask32 & BC_FLAG_I)) |
7139 | s = zbc_vm_execute_FILE(stdin, /*filename:*/ NULL); | 7128 | s = zxc_vm_execute_FILE(stdin, /*filename:*/ NULL); |
7140 | 7129 | ||
7141 | RETURN_STATUS(s); | 7130 | RETURN_STATUS(s); |
7142 | } | 7131 | } |
7143 | #define zbc_vm_exec(...) (zbc_vm_exec(__VA_ARGS__) COMMA_SUCCESS) | 7132 | #define zxc_vm_exec(...) (zxc_vm_exec(__VA_ARGS__) COMMA_SUCCESS) |
7144 | 7133 | ||
7145 | #if ENABLE_FEATURE_CLEAN_UP | 7134 | #if ENABLE_FEATURE_CLEAN_UP |
7146 | static void bc_program_free(void) | 7135 | static void xc_program_free(void) |
7147 | { | 7136 | { |
7148 | bc_vec_free(&G.prog.fns); | 7137 | bc_vec_free(&G.prog.fns); |
7149 | IF_BC(bc_vec_free(&G.prog.fn_map);) | 7138 | IF_BC(bc_vec_free(&G.prog.fn_map);) |
@@ -7161,16 +7150,16 @@ static void bc_program_free(void) | |||
7161 | bc_vec_free(&G.input_buffer); | 7150 | bc_vec_free(&G.input_buffer); |
7162 | } | 7151 | } |
7163 | 7152 | ||
7164 | static void bc_vm_free(void) | 7153 | static void xc_vm_free(void) |
7165 | { | 7154 | { |
7166 | bc_vec_free(&G.files); | 7155 | bc_vec_free(&G.files); |
7167 | bc_program_free(); | 7156 | xc_program_free(); |
7168 | bc_parse_free(); | 7157 | xc_parse_free(); |
7169 | free(G.env_args); | 7158 | free(G.env_args); |
7170 | } | 7159 | } |
7171 | #endif | 7160 | #endif |
7172 | 7161 | ||
7173 | static void bc_program_init(void) | 7162 | static void xc_program_init(void) |
7174 | { | 7163 | { |
7175 | BcInstPtr ip; | 7164 | BcInstPtr ip; |
7176 | 7165 | ||
@@ -7200,8 +7189,8 @@ static void bc_program_init(void) | |||
7200 | IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read() | 7189 | IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read() |
7201 | } else { | 7190 | } else { |
7202 | // in dc, functions have no names | 7191 | // in dc, functions have no names |
7203 | bc_program_add_fn(); | 7192 | xc_program_add_fn(); |
7204 | bc_program_add_fn(); | 7193 | xc_program_add_fn(); |
7205 | } | 7194 | } |
7206 | 7195 | ||
7207 | bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free); | 7196 | bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free); |
@@ -7219,17 +7208,17 @@ static void bc_program_init(void) | |||
7219 | bc_char_vec_init(&G.input_buffer); | 7208 | bc_char_vec_init(&G.input_buffer); |
7220 | } | 7209 | } |
7221 | 7210 | ||
7222 | static int bc_vm_init(const char *env_len) | 7211 | static int xc_vm_init(const char *env_len) |
7223 | { | 7212 | { |
7224 | #if ENABLE_FEATURE_EDITING | 7213 | #if ENABLE_FEATURE_EDITING |
7225 | G.line_input_state = new_line_input_t(DO_HISTORY); | 7214 | G.line_input_state = new_line_input_t(DO_HISTORY); |
7226 | #endif | 7215 | #endif |
7227 | G.prog.len = bc_vm_envLen(env_len); | 7216 | G.prog.len = xc_vm_envLen(env_len); |
7228 | 7217 | ||
7229 | bc_vec_init(&G.files, sizeof(char *), NULL); | 7218 | bc_vec_init(&G.files, sizeof(char *), NULL); |
7219 | xc_program_init(); | ||
7230 | IF_BC(if (IS_BC) bc_vm_envArgs();) | 7220 | IF_BC(if (IS_BC) bc_vm_envArgs();) |
7231 | bc_program_init(); | 7221 | xc_parse_create(BC_PROG_MAIN); |
7232 | bc_parse_create(BC_PROG_MAIN); | ||
7233 | 7222 | ||
7234 | //TODO: in GNU bc, the check is (isatty(0) && isatty(1)), | 7223 | //TODO: in GNU bc, the check is (isatty(0) && isatty(1)), |
7235 | //-i option unconditionally enables this regardless of isatty(): | 7224 | //-i option unconditionally enables this regardless of isatty(): |
@@ -7261,13 +7250,13 @@ static int bc_vm_init(const char *env_len) | |||
7261 | return 0; // "not a tty" | 7250 | return 0; // "not a tty" |
7262 | } | 7251 | } |
7263 | 7252 | ||
7264 | static BcStatus bc_vm_run(void) | 7253 | static BcStatus xc_vm_run(void) |
7265 | { | 7254 | { |
7266 | BcStatus st = zbc_vm_exec(); | 7255 | BcStatus st = zxc_vm_exec(); |
7267 | #if ENABLE_FEATURE_CLEAN_UP | 7256 | #if ENABLE_FEATURE_CLEAN_UP |
7268 | if (G_exiting) // it was actually "halt" or "quit" | 7257 | if (G_exiting) // it was actually "halt" or "quit" |
7269 | st = EXIT_SUCCESS; | 7258 | st = EXIT_SUCCESS; |
7270 | bc_vm_free(); | 7259 | xc_vm_free(); |
7271 | # if ENABLE_FEATURE_EDITING | 7260 | # if ENABLE_FEATURE_EDITING |
7272 | free_line_input_t(G.line_input_state); | 7261 | free_line_input_t(G.line_input_state); |
7273 | # endif | 7262 | # endif |
@@ -7285,14 +7274,14 @@ int bc_main(int argc UNUSED_PARAM, char **argv) | |||
7285 | 7274 | ||
7286 | INIT_G(); | 7275 | INIT_G(); |
7287 | 7276 | ||
7288 | is_tty = bc_vm_init("BC_LINE_LENGTH"); | 7277 | is_tty = xc_vm_init("BC_LINE_LENGTH"); |
7289 | 7278 | ||
7290 | bc_args(argv); | 7279 | bc_args(argv); |
7291 | 7280 | ||
7292 | if (is_tty && !(option_mask32 & BC_FLAG_Q)) | 7281 | if (is_tty && !(option_mask32 & BC_FLAG_Q)) |
7293 | bc_vm_info(); | 7282 | bc_vm_info(); |
7294 | 7283 | ||
7295 | return bc_vm_run(); | 7284 | return xc_vm_run(); |
7296 | } | 7285 | } |
7297 | #endif | 7286 | #endif |
7298 | 7287 | ||
@@ -7314,7 +7303,7 @@ int dc_main(int argc UNUSED_PARAM, char **argv) | |||
7314 | // |123\ | | 7303 | // |123\ | |
7315 | // |456 | | 7304 | // |456 | |
7316 | // Do the same, or it's a bug? | 7305 | // Do the same, or it's a bug? |
7317 | bc_vm_init("DC_LINE_LENGTH"); | 7306 | xc_vm_init("DC_LINE_LENGTH"); |
7318 | 7307 | ||
7319 | // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs | 7308 | // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs |
7320 | noscript = BC_FLAG_I; | 7309 | noscript = BC_FLAG_I; |
@@ -7325,12 +7314,12 @@ int dc_main(int argc UNUSED_PARAM, char **argv) | |||
7325 | switch (n) { | 7314 | switch (n) { |
7326 | case 'e': | 7315 | case 'e': |
7327 | noscript = 0; | 7316 | noscript = 0; |
7328 | n = zbc_vm_process(optarg); | 7317 | n = zxc_vm_process(optarg); |
7329 | if (n) return n; | 7318 | if (n) return n; |
7330 | break; | 7319 | break; |
7331 | case 'f': | 7320 | case 'f': |
7332 | noscript = 0; | 7321 | noscript = 0; |
7333 | n = zbc_vm_file(optarg); | 7322 | n = zxc_vm_file(optarg); |
7334 | if (n) return n; | 7323 | if (n) return n; |
7335 | break; | 7324 | break; |
7336 | case 'x': | 7325 | case 'x': |
@@ -7349,7 +7338,7 @@ int dc_main(int argc UNUSED_PARAM, char **argv) | |||
7349 | 7338 | ||
7350 | option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin | 7339 | option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin |
7351 | 7340 | ||
7352 | return bc_vm_run(); | 7341 | return xc_vm_run(); |
7353 | } | 7342 | } |
7354 | #endif | 7343 | #endif |
7355 | 7344 | ||