diff options
-rw-r--r-- | miscutils/bc.c | 67 |
1 files changed, 27 insertions, 40 deletions
diff --git a/miscutils/bc.c b/miscutils/bc.c index ef21ab063..d2713ceee 100644 --- a/miscutils/bc.c +++ b/miscutils/bc.c | |||
@@ -379,8 +379,6 @@ typedef struct BcInstPtr { | |||
379 | size_t len; | 379 | size_t len; |
380 | } BcInstPtr; | 380 | } BcInstPtr; |
381 | 381 | ||
382 | static int bc_id_cmp(const void *e1, const void *e2); | ||
383 | |||
384 | // BC_LEX_NEG is not used in lexing; it is only for parsing. | 382 | // BC_LEX_NEG is not used in lexing; it is only for parsing. |
385 | typedef enum BcLexType { | 383 | typedef enum BcLexType { |
386 | 384 | ||
@@ -656,34 +654,6 @@ typedef struct BcParse { | |||
656 | 654 | ||
657 | } BcParse; | 655 | } BcParse; |
658 | 656 | ||
659 | #if ENABLE_BC | ||
660 | |||
661 | static BcStatus bc_lex_token(BcLex *l); | ||
662 | |||
663 | #define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops))) | ||
664 | #define BC_PARSE_LEAF(p, rparen) \ | ||
665 | (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \ | ||
666 | (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST) | ||
667 | |||
668 | // We can calculate the conversion between tokens and exprs by subtracting the | ||
669 | // position of the first operator in the lex enum and adding the position of the | ||
670 | // first in the expr enum. Note: This only works for binary operators. | ||
671 | #define BC_PARSE_TOKEN_INST(t) ((char) ((t) -BC_LEX_NEG + BC_INST_NEG)) | ||
672 | |||
673 | static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next); | ||
674 | |||
675 | #endif // ENABLE_BC | ||
676 | |||
677 | #if ENABLE_DC | ||
678 | |||
679 | #define DC_PARSE_BUF_LEN ((int) (sizeof(uint32_t) * CHAR_BIT)) | ||
680 | |||
681 | static BcStatus dc_lex_token(BcLex *l); | ||
682 | |||
683 | static BcStatus dc_parse_expr(BcParse *p, uint8_t flags); | ||
684 | |||
685 | #endif // ENABLE_DC | ||
686 | |||
687 | typedef struct BcProgram { | 657 | typedef struct BcProgram { |
688 | 658 | ||
689 | size_t len; | 659 | size_t len; |
@@ -1165,6 +1135,16 @@ static void bc_vec_free(void *vec) | |||
1165 | free(v->v); | 1135 | free(v->v); |
1166 | } | 1136 | } |
1167 | 1137 | ||
1138 | static int bc_id_cmp(const void *e1, const void *e2) | ||
1139 | { | ||
1140 | return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name); | ||
1141 | } | ||
1142 | |||
1143 | static void bc_id_free(void *id) | ||
1144 | { | ||
1145 | free(((BcId *) id)->name); | ||
1146 | } | ||
1147 | |||
1168 | static size_t bc_map_find(const BcVec *v, const void *ptr) | 1148 | static size_t bc_map_find(const BcVec *v, const void *ptr) |
1169 | { | 1149 | { |
1170 | size_t low = 0, high = v->len; | 1150 | size_t low = 0, high = v->len; |
@@ -2665,16 +2645,6 @@ err: | |||
2665 | } | 2645 | } |
2666 | #endif // ENABLE_DC | 2646 | #endif // ENABLE_DC |
2667 | 2647 | ||
2668 | static int bc_id_cmp(const void *e1, const void *e2) | ||
2669 | { | ||
2670 | return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name); | ||
2671 | } | ||
2672 | |||
2673 | static void bc_id_free(void *id) | ||
2674 | { | ||
2675 | free(((BcId *) id)->name); | ||
2676 | } | ||
2677 | |||
2678 | static BcStatus bc_func_insert(BcFunc *f, char *name, bool var) | 2648 | static BcStatus bc_func_insert(BcFunc *f, char *name, bool var) |
2679 | { | 2649 | { |
2680 | BcId a; | 2650 | BcId a; |
@@ -3632,8 +3602,20 @@ static void bc_parse_create(BcParse *p, size_t func, | |||
3632 | } | 3602 | } |
3633 | 3603 | ||
3634 | #if ENABLE_BC | 3604 | #if ENABLE_BC |
3605 | |||
3606 | #define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops))) | ||
3607 | #define BC_PARSE_LEAF(p, rparen) \ | ||
3608 | (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \ | ||
3609 | (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST) | ||
3610 | |||
3611 | // We can calculate the conversion between tokens and exprs by subtracting the | ||
3612 | // position of the first operator in the lex enum and adding the position of the | ||
3613 | // first in the expr enum. Note: This only works for binary operators. | ||
3614 | #define BC_PARSE_TOKEN_INST(t) ((char) ((t) -BC_LEX_NEG + BC_INST_NEG)) | ||
3615 | |||
3635 | static BcStatus bc_parse_else(BcParse *p); | 3616 | static BcStatus bc_parse_else(BcParse *p); |
3636 | static BcStatus bc_parse_stmt(BcParse *p); | 3617 | static BcStatus bc_parse_stmt(BcParse *p); |
3618 | static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next); | ||
3637 | 3619 | ||
3638 | static BcStatus bc_parse_operator(BcParse *p, BcLexType type, size_t start, | 3620 | static BcStatus bc_parse_operator(BcParse *p, BcLexType type, size_t start, |
3639 | size_t *nexprs, bool next) | 3621 | size_t *nexprs, bool next) |
@@ -4971,9 +4953,13 @@ static BcStatus bc_parse_expression(BcParse *p, uint8_t flags) | |||
4971 | { | 4953 | { |
4972 | return bc_parse_expr(p, flags, bc_parse_next_read); | 4954 | return bc_parse_expr(p, flags, bc_parse_next_read); |
4973 | } | 4955 | } |
4956 | |||
4974 | #endif // ENABLE_BC | 4957 | #endif // ENABLE_BC |
4975 | 4958 | ||
4976 | #if ENABLE_DC | 4959 | #if ENABLE_DC |
4960 | |||
4961 | #define DC_PARSE_BUF_LEN ((int) (sizeof(uint32_t) * CHAR_BIT)) | ||
4962 | |||
4977 | static BcStatus dc_parse_register(BcParse *p) | 4963 | static BcStatus dc_parse_register(BcParse *p) |
4978 | { | 4964 | { |
4979 | BcStatus s; | 4965 | BcStatus s; |
@@ -5196,6 +5182,7 @@ static void dc_parse_init(BcParse *p, size_t func) | |||
5196 | { | 5182 | { |
5197 | bc_parse_create(p, func, dc_parse_parse, dc_lex_token); | 5183 | bc_parse_create(p, func, dc_parse_parse, dc_lex_token); |
5198 | } | 5184 | } |
5185 | |||
5199 | #endif // ENABLE_DC | 5186 | #endif // ENABLE_DC |
5200 | 5187 | ||
5201 | static void common_parse_init(BcParse *p, size_t func) | 5188 | static void common_parse_init(BcParse *p, size_t func) |