Gavin Howard | 5715b04 | 2018-02-12 16:11:42 -0700 | [diff] [blame] | 1 | /* |
Gavin Howard | b5904bf | 2018-02-20 13:28:18 -0700 | [diff] [blame] | 2 | * ***************************************************************************** |
Gavin Howard | 5715b04 | 2018-02-12 16:11:42 -0700 | [diff] [blame] | 3 | * |
Gavin Howard | b5904bf | 2018-02-20 13:28:18 -0700 | [diff] [blame] | 4 | * Copyright 2018 Gavin D. Howard |
Gavin Howard | 5715b04 | 2018-02-12 16:11:42 -0700 | [diff] [blame] | 5 | * |
| 6 | * Permission to use, copy, modify, and/or distribute this software for any |
| 7 | * purpose with or without fee is hereby granted. |
| 8 | * |
| 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH |
| 10 | * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 11 | * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 12 | * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 13 | * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR |
| 14 | * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 15 | * PERFORMANCE OF THIS SOFTWARE. |
| 16 | * |
Gavin Howard | b5904bf | 2018-02-20 13:28:18 -0700 | [diff] [blame] | 17 | * ***************************************************************************** |
Gavin Howard | 5715b04 | 2018-02-12 16:11:42 -0700 | [diff] [blame] | 18 | * |
Gavin Howard | 4ffe5a9 | 2018-09-26 20:58:31 -0600 | [diff] [blame] | 19 | * Definitions for bc. |
Gavin Howard | 5715b04 | 2018-02-12 16:11:42 -0700 | [diff] [blame] | 20 | * |
| 21 | */ |
| 22 | |
Gavin Howard | 4ffe5a9 | 2018-09-26 20:58:31 -0600 | [diff] [blame] | 23 | #ifndef BC_BC_H |
| 24 | #define BC_BC_H |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 25 | |
Gavin Howard | 1545ffe | 2018-12-24 11:26:46 -0700 | [diff] [blame] | 26 | #if BC_ENABLED |
| 27 | |
Gavin Howard | c1902aa | 2018-12-20 15:54:28 -0700 | [diff] [blame] | 28 | #include <limits.h> |
Gavin Howard | 707a4e3 | 2018-10-03 16:36:41 -0600 | [diff] [blame] | 29 | #include <stdbool.h> |
| 30 | |
Gavin Howard | 2949306 | 2018-03-20 19:57:37 -0600 | [diff] [blame] | 31 | #include <status.h> |
Gavin Howard | 707a4e3 | 2018-10-03 16:36:41 -0600 | [diff] [blame] | 32 | #include <lex.h> |
| 33 | #include <parse.h> |
Gavin Howard | 6839e4d | 2018-03-20 21:24:52 -0600 | [diff] [blame] | 34 | |
Gavin Howard | eb9a822 | 2018-03-13 09:25:56 -0600 | [diff] [blame] | 35 | // ** Exclude start. ** |
Gavin Howard | 773c86b | 2018-11-02 14:07:19 -0600 | [diff] [blame] | 36 | // ** Busybox exclude start. ** |
Gavin Howard | a84ad99 | 2018-12-03 19:11:06 -0700 | [diff] [blame] | 37 | int bc_main(int argc, char **argv); |
| 38 | |
Gavin Howard | d555167 | 2018-09-22 19:52:42 -0600 | [diff] [blame] | 39 | extern const char bc_help[]; |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 40 | extern const char bc_lib[]; |
| 41 | extern const char* bc_lib_name; |
| 42 | extern const char bc_lib2[]; |
| 43 | extern const char* bc_lib2_name; |
Gavin Howard | 773c86b | 2018-11-02 14:07:19 -0600 | [diff] [blame] | 44 | // ** Busybox exclude end. ** |
Gavin Howard | 707a4e3 | 2018-10-03 16:36:41 -0600 | [diff] [blame] | 45 | // ** Exclude end. ** |
| 46 | |
| 47 | typedef struct BcLexKeyword { |
Gavin Howard | c1902aa | 2018-12-20 15:54:28 -0700 | [diff] [blame] | 48 | uchar data; |
Gavin Howard | 707a4e3 | 2018-10-03 16:36:41 -0600 | [diff] [blame] | 49 | const char name[9]; |
Gavin Howard | 707a4e3 | 2018-10-03 16:36:41 -0600 | [diff] [blame] | 50 | } BcLexKeyword; |
| 51 | |
Gavin Howard | 4662d6c | 2018-12-20 16:31:31 -0700 | [diff] [blame] | 52 | #define BC_LEX_CHAR_MSB(bit) ((bit) << (CHAR_BIT - 1)) |
| 53 | |
Gavin Howard | fbd2b4a | 2018-12-27 12:11:41 -0700 | [diff] [blame] | 54 | #define BC_LEX_KW_POSIX(kw) ((kw)->data & (BC_LEX_CHAR_MSB(1))) |
| 55 | #define BC_LEX_KW_LEN(kw) ((size_t) ((kw)->data & ~(BC_LEX_CHAR_MSB(1)))) |
Gavin Howard | 4662d6c | 2018-12-20 16:31:31 -0700 | [diff] [blame] | 56 | |
Gavin Howard | c1902aa | 2018-12-20 15:54:28 -0700 | [diff] [blame] | 57 | #define BC_LEX_KW_ENTRY(a, b, c) \ |
Gavin Howard | ed38e94 | 2018-12-21 15:09:13 -0700 | [diff] [blame] | 58 | { .data = ((b) & ~(BC_LEX_CHAR_MSB(1))) | BC_LEX_CHAR_MSB(c),.name = a } |
Gavin Howard | 707a4e3 | 2018-10-03 16:36:41 -0600 | [diff] [blame] | 59 | |
Gavin Howard | 76d7942 | 2018-12-31 14:18:49 -0700 | [diff] [blame] | 60 | #define bc_lex_posixErr(l, e) (bc_vm_posixError((e), (l)->line)) |
| 61 | #define bc_lex_vposixErr(l, e, ...) \ |
| 62 | (bc_vm_posixError((e), (l)->line, __VA_ARGS__)) |
| 63 | |
Gavin Howard | c70bd50 | 2018-12-31 17:28:32 -0700 | [diff] [blame] | 64 | extern const BcLexKeyword bc_lex_kws[]; |
| 65 | extern const size_t bc_lex_kws_len; |
Gavin Howard | 707a4e3 | 2018-10-03 16:36:41 -0600 | [diff] [blame] | 66 | |
| 67 | BcStatus bc_lex_token(BcLex *l); |
| 68 | |
Gavin Howard | b15fa03 | 2019-01-02 14:42:36 -0700 | [diff] [blame] | 69 | #define BC_PARSE_TOP_FLAG_PTR(p) ((uint16_t*) bc_vec_top(&(p)->flags)) |
| 70 | #define BC_PARSE_TOP_FLAG(p) (*(BC_PARSE_TOP_FLAG_PTR(p))) |
| 71 | |
| 72 | #define BC_PARSE_FLAG_BRACE (1<<0) |
| 73 | #define BC_PARSE_BRACE(p) (BC_PARSE_TOP_FLAG(p) & BC_PARSE_FLAG_BRACE) |
| 74 | |
| 75 | #define BC_PARSE_FLAG_FUNC_INNER (1<<1) |
| 76 | #define BC_PARSE_FUNC_INNER(p) (BC_PARSE_TOP_FLAG(p) & BC_PARSE_FLAG_FUNC_INNER) |
| 77 | |
| 78 | #define BC_PARSE_FLAG_FUNC (1<<2) |
| 79 | #define BC_PARSE_FUNC(p) (BC_PARSE_TOP_FLAG(p) & BC_PARSE_FLAG_FUNC) |
| 80 | |
| 81 | #define BC_PARSE_FLAG_BODY (1<<3) |
| 82 | #define BC_PARSE_BODY(p) (BC_PARSE_TOP_FLAG(p) & BC_PARSE_FLAG_BODY) |
| 83 | |
| 84 | #define BC_PARSE_FLAG_LOOP (1<<4) |
| 85 | #define BC_PARSE_LOOP(p) (BC_PARSE_TOP_FLAG(p) & BC_PARSE_FLAG_LOOP) |
| 86 | |
| 87 | #define BC_PARSE_FLAG_LOOP_INNER (1<<5) |
| 88 | #define BC_PARSE_LOOP_INNER(p) (BC_PARSE_TOP_FLAG(p) & BC_PARSE_FLAG_LOOP_INNER) |
| 89 | |
| 90 | #define BC_PARSE_FLAG_IF (1<<6) |
| 91 | #define BC_PARSE_IF(p) (BC_PARSE_TOP_FLAG(p) & BC_PARSE_FLAG_IF) |
| 92 | |
| 93 | #define BC_PARSE_FLAG_ELSE (1<<7) |
| 94 | #define BC_PARSE_ELSE(p) (BC_PARSE_TOP_FLAG(p) & BC_PARSE_FLAG_ELSE) |
| 95 | |
| 96 | #define BC_PARSE_FLAG_IF_END (1<<8) |
| 97 | #define BC_PARSE_IF_END(p) (BC_PARSE_TOP_FLAG(p) & BC_PARSE_FLAG_IF_END) |
| 98 | |
| 99 | #define BC_PARSE_NO_EXEC(p) ((p)->flags.len != 1 || BC_PARSE_TOP_FLAG(p) != 0) |
| 100 | |
Gavin Howard | 7ed50c7 | 2019-01-14 11:20:12 -0700 | [diff] [blame] | 101 | #define BC_PARSE_DELIMITER(t) \ |
| 102 | ((t) == BC_LEX_SCOLON || (t) == BC_LEX_NLINE || (t) == BC_LEX_EOF) |
Gavin Howard | b15fa03 | 2019-01-02 14:42:36 -0700 | [diff] [blame] | 103 | |
Gavin Howard | 63fdbfa | 2019-01-16 09:47:04 -0700 | [diff] [blame] | 104 | #define BC_PARSE_BLOCK_STMT(f) \ |
| 105 | ((f) & (BC_PARSE_FLAG_ELSE | BC_PARSE_FLAG_LOOP_INNER)) |
| 106 | |
Gavin Howard | 2b4fde9 | 2018-12-24 16:02:24 -0700 | [diff] [blame] | 107 | #define BC_PARSE_OP(p, l) (((p) & ~(BC_LEX_CHAR_MSB(1))) | (BC_LEX_CHAR_MSB(l))) |
Gavin Howard | 4662d6c | 2018-12-20 16:31:31 -0700 | [diff] [blame] | 108 | |
Gavin Howard | 1c6cfb8 | 2019-01-21 11:42:34 -0700 | [diff] [blame^] | 109 | #define BC_PARSE_OP_DATA(t) bc_parse_ops[((t) - BC_LEX_OP_INC)] |
| 110 | #define BC_PARSE_OP_LEFT(op) (BC_PARSE_OP_DATA(op) & BC_LEX_CHAR_MSB(1)) |
| 111 | #define BC_PARSE_OP_PREC(op) (BC_PARSE_OP_DATA(op) & ~(BC_LEX_CHAR_MSB(1))) |
Gavin Howard | 4662d6c | 2018-12-20 16:31:31 -0700 | [diff] [blame] | 112 | |
Gavin Howard | 675b5cc | 2018-12-20 16:55:36 -0700 | [diff] [blame] | 113 | #define BC_PARSE_EXPR_ENTRY(e1, e2, e3, e4, e5, e6, e7, e8) \ |
| 114 | (((e1) << 7) | ((e2) << 6) | ((e3) << 5) | ((e4) << 4) | \ |
| 115 | ((e5) << 3) | ((e6) << 2) | ((e7) << 1) | ((e8) << 0)) |
| 116 | |
| 117 | #define BC_PARSE_EXPR(i) \ |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 118 | (bc_parse_exprs[(((i) & (uchar) ~(0x07)) >> 3)] & (1 << (7 - ((i) & 0x07)))) |
Gavin Howard | 675b5cc | 2018-12-20 16:55:36 -0700 | [diff] [blame] | 119 | |
Gavin Howard | 890d0c0 | 2018-10-30 16:34:50 -0600 | [diff] [blame] | 120 | #define BC_PARSE_TOP_OP(p) (*((BcLexType*) bc_vec_top(&(p)->ops))) |
Gavin Howard | b6e5a17 | 2018-12-28 17:41:15 -0700 | [diff] [blame] | 121 | #define BC_PARSE_LEAF(prev, bin_last, rparen) \ |
| 122 | (!(bin_last) && ((rparen) || bc_parse_inst_isLeaf(prev))) |
Gavin Howard | f81f904 | 2018-12-27 14:19:53 -0700 | [diff] [blame] | 123 | #define BC_PARSE_INST_VAR(t) \ |
| 124 | ((t) == BC_INST_VAR || (t) == BC_INST_ARRAY_ELEM || (t) == BC_INST_LAST || \ |
| 125 | (t) == BC_INST_SCALE || (t) == BC_INST_IBASE || (t) == BC_INST_OBASE) |
Gavin Howard | 707a4e3 | 2018-10-03 16:36:41 -0600 | [diff] [blame] | 126 | |
Gavin Howard | e522888 | 2019-01-10 16:08:04 -0700 | [diff] [blame] | 127 | #define BC_PARSE_PREV_PREFIX(p) \ |
| 128 | ((p) >= BC_INST_INC_PRE && (p) <= BC_INST_BOOL_NOT) |
| 129 | #define BC_PARSE_OP_PREFIX(t) ((t) == BC_LEX_OP_BOOL_NOT || (t) == BC_LEX_NEG) |
Gavin Howard | ac7a3c9 | 2019-01-10 13:26:29 -0700 | [diff] [blame] | 130 | |
Gavin Howard | 707a4e3 | 2018-10-03 16:36:41 -0600 | [diff] [blame] | 131 | // We can calculate the conversion between tokens and exprs by subtracting the |
Gavin Howard | 954f9b6 | 2018-12-19 15:22:20 -0700 | [diff] [blame] | 132 | // position of the first operator in the lex enum and adding the position of |
| 133 | // the first in the expr enum. Note: This only works for binary operators. |
| 134 | #define BC_PARSE_TOKEN_INST(t) ((uchar) ((t) - BC_LEX_NEG + BC_INST_NEG)) |
Gavin Howard | 707a4e3 | 2018-10-03 16:36:41 -0600 | [diff] [blame] | 135 | |
Gavin Howard | 76d7942 | 2018-12-31 14:18:49 -0700 | [diff] [blame] | 136 | #define bc_parse_posixErr(p, e) (bc_vm_posixError((e), (p)->l.line)) |
| 137 | |
Gavin Howard | 707a4e3 | 2018-10-03 16:36:41 -0600 | [diff] [blame] | 138 | // ** Exclude start. ** |
Gavin Howard | 773c86b | 2018-11-02 14:07:19 -0600 | [diff] [blame] | 139 | // ** Busybox exclude start. ** |
Gavin Howard | 7536dcf | 2018-12-15 19:27:09 -0700 | [diff] [blame] | 140 | BcStatus bc_parse_expr(BcParse *p, uint8_t flags); |
Gavin Howard | 773c86b | 2018-11-02 14:07:19 -0600 | [diff] [blame] | 141 | // ** Busybox exclude end. ** |
Gavin Howard | 707a4e3 | 2018-10-03 16:36:41 -0600 | [diff] [blame] | 142 | // ** Exclude end. ** |
| 143 | |
Gavin Howard | 9d2497d | 2018-10-29 16:10:21 -0600 | [diff] [blame] | 144 | BcStatus bc_parse_parse(BcParse *p); |
Gavin Howard | 7536dcf | 2018-12-15 19:27:09 -0700 | [diff] [blame] | 145 | BcStatus bc_parse_expr_status(BcParse *p, uint8_t flags, BcParseNext next); |
Gavin Howard | ff66e29 | 2018-03-28 12:47:58 -0600 | [diff] [blame] | 146 | |
Gavin Howard | 1066709 | 2018-12-20 11:52:55 -0700 | [diff] [blame] | 147 | #if BC_ENABLE_SIGNALS |
| 148 | extern const char bc_sig_msg[]; |
Gavin Howard | 1066709 | 2018-12-20 11:52:55 -0700 | [diff] [blame] | 149 | #endif // BC_ENABLE_SIGNALS |
| 150 | |
Gavin Howard | a73c11b | 2018-12-17 11:30:39 -0700 | [diff] [blame] | 151 | extern const char* const bc_parse_const1; |
Gavin Howard | 675b5cc | 2018-12-20 16:55:36 -0700 | [diff] [blame] | 152 | extern const uint8_t bc_parse_exprs[]; |
Gavin Howard | c1902aa | 2018-12-20 15:54:28 -0700 | [diff] [blame] | 153 | extern const uchar bc_parse_ops[]; |
Gavin Howard | f89006a | 2018-10-29 13:01:51 -0600 | [diff] [blame] | 154 | extern const BcParseNext bc_parse_next_expr; |
| 155 | extern const BcParseNext bc_parse_next_param; |
| 156 | extern const BcParseNext bc_parse_next_print; |
| 157 | extern const BcParseNext bc_parse_next_rel; |
| 158 | extern const BcParseNext bc_parse_next_elem; |
| 159 | extern const BcParseNext bc_parse_next_for; |
| 160 | extern const BcParseNext bc_parse_next_read; |
| 161 | |
Gavin Howard | df4fe79 | 2018-10-03 16:48:30 -0600 | [diff] [blame] | 162 | #endif // BC_ENABLED |
| 163 | |
Gavin Howard | 4ffe5a9 | 2018-09-26 20:58:31 -0600 | [diff] [blame] | 164 | #endif // BC_BC_H |