Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
| 4 | * Copyright (c) 2018 Gavin D. Howard and contributors. |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5 | */ |
| 6 | //config:config BC |
| 7 | //config: bool "bc (45 kb; 49 kb when combined with dc)" |
| 8 | //config: default y |
| 9 | //config: help |
| 10 | //config: bc is a command-line, arbitrary-precision calculator with a |
| 11 | //config: Turing-complete language. See the GNU bc manual |
| 12 | //config: (https://www.gnu.org/software/bc/manual/bc.html) and bc spec |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 13 | //config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html). |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 14 | //config: |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 15 | //config: This bc has five differences to the GNU bc: |
| 16 | //config: 1) The period (.) is a shortcut for "last", as in the BSD bc. |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 17 | //config: 2) Arrays are copied before being passed as arguments to |
| 18 | //config: functions. This behavior is required by the bc spec. |
| 19 | //config: 3) Arrays can be passed to the builtin "length" function to get |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 20 | //config: the number of elements in the array. This prints "1": |
| 21 | //config: a[0] = 0; length(a[]) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 22 | //config: 4) The precedence of the boolean "not" operator (!) is equal to |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 23 | //config: that of the unary minus (-) negation operator. This still |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 24 | //config: allows POSIX-compliant scripts to work while somewhat |
| 25 | //config: preserving expected behavior (versus C) and making parsing |
| 26 | //config: easier. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 27 | //config: 5) "read()" accepts expressions, not only numeric literals. |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 28 | //config: |
| 29 | //config: Options: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 30 | //config: -i --interactive force interactive mode |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 31 | //config: -q --quiet don't print version and copyright |
| 32 | //config: -s --standard error if any non-POSIX extensions are used |
| 33 | //config: -w --warn warn if any non-POSIX extensions are used |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 34 | //config: -l --mathlib use predefined math routines: |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 35 | //config: s(expr) sine in radians |
| 36 | //config: c(expr) cosine in radians |
| 37 | //config: a(expr) arctangent, returning radians |
| 38 | //config: l(expr) natural log |
| 39 | //config: e(expr) raises e to the power of expr |
| 40 | //config: j(n, x) Bessel function of integer order n of x |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 41 | //config: |
| 42 | //config:config DC |
| 43 | //config: bool "dc (38 kb; 49 kb when combined with bc)" |
| 44 | //config: default y |
| 45 | //config: help |
| 46 | //config: dc is a reverse-polish notation command-line calculator which |
| 47 | //config: supports unlimited precision arithmetic. See the FreeBSD man page |
| 48 | //config: (https://www.unix.com/man-page/FreeBSD/1/dc/) and GNU dc manual |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 49 | //config: (https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html). |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 50 | //config: |
| 51 | //config: This dc has a few differences from the two above: |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 52 | //config: 1) When printing a byte stream (command "P"), this dc follows what |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 53 | //config: the FreeBSD dc does. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 54 | //config: 2) Implements the GNU extensions for divmod ("~") and |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 55 | //config: modular exponentiation ("|"). |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 56 | //config: 3) Implements all FreeBSD extensions, except for "J" and "M". |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 57 | //config: 4) Like the FreeBSD dc, this dc supports extended registers. |
| 58 | //config: However, they are implemented differently. When it encounters |
| 59 | //config: whitespace where a register should be, it skips the whitespace. |
| 60 | //config: If the character following is not a lowercase letter, an error |
| 61 | //config: is issued. Otherwise, the register name is parsed by the |
| 62 | //config: following regex: |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 63 | //config: [a-z][a-z0-9_]* |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 64 | //config: This generally means that register names will be surrounded by |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 65 | //config: whitespace. Examples: |
| 66 | //config: l idx s temp L index S temp2 < do_thing |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 67 | //config: Also note that, like the FreeBSD dc, extended registers are not |
| 68 | //config: allowed unless the "-x" option is given. |
| 69 | //config: |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 70 | //config:config FEATURE_DC_SMALL |
| 71 | //config: bool "Minimal dc implementation (4.2 kb), not using bc code base" |
| 72 | //config: depends on DC && !BC |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 73 | //config: default n |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 74 | //config: |
| 75 | //config:config FEATURE_DC_LIBM |
| 76 | //config: bool "Enable power and exp functions (requires libm)" |
| 77 | //config: default y |
| 78 | //config: depends on FEATURE_DC_SMALL |
| 79 | //config: help |
| 80 | //config: Enable power and exp functions. |
| 81 | //config: NOTE: This will require libm to be present for linking. |
| 82 | //config: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 83 | //config:config FEATURE_BC_SIGNALS |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 84 | //config: bool "Interactive mode (+4kb)" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 85 | //config: default y |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 86 | //config: depends on (BC || DC) && !FEATURE_DC_SMALL |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 87 | //config: help |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 88 | //config: Enable interactive mode: when started on a tty, |
| 89 | //config: ^C interrupts execution and returns to command line, |
| 90 | //config: errors also return to command line instead of exiting, |
| 91 | //config: line editing with history is available. |
| 92 | //config: |
| 93 | //config: With this option off, input can still be taken from tty, |
| 94 | //config: but all errors are fatal, ^C is fatal, |
| 95 | //config: tty is treated exactly the same as any other |
| 96 | //config: standard input (IOW: no line editing). |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 97 | //config: |
| 98 | //config:config FEATURE_BC_LONG_OPTIONS |
| 99 | //config: bool "Enable bc/dc long options" |
| 100 | //config: default y |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 101 | //config: depends on (BC || DC) && !FEATURE_DC_SMALL |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 102 | //config: help |
| 103 | //config: Enable long options for bc and dc. |
| 104 | |
| 105 | //applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 106 | //applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 107 | |
| 108 | //kbuild:lib-$(CONFIG_BC) += bc.o |
| 109 | //kbuild:lib-$(CONFIG_DC) += bc.o |
| 110 | |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 111 | //See www.gnu.org/software/bc/manual/bc.html |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 112 | //usage:#define bc_trivial_usage |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 113 | //usage: "[-sqliw] FILE..." |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 114 | //usage: |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 115 | //usage:#define bc_full_usage "\n" |
| 116 | //usage: "\nArbitrary precision calculator" |
| 117 | //usage: "\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 118 | ///////: "\n -i Interactive" - has no effect for now |
| 119 | //usage: "\n -q Quiet" |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 120 | //usage: "\n -l Load standard math library" |
| 121 | //usage: "\n -s Be POSIX compatible" |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 122 | //usage: "\n -w Warn if extensions are used" |
| 123 | ///////: "\n -v Version" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 124 | //usage: "\n" |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 125 | //usage: "\n$BC_LINE_LENGTH changes output width" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 126 | //usage: |
| 127 | //usage:#define bc_example_usage |
| 128 | //usage: "3 + 4.129\n" |
| 129 | //usage: "1903 - 2893\n" |
| 130 | //usage: "-129 * 213.28935\n" |
| 131 | //usage: "12 / -1932\n" |
| 132 | //usage: "12 % 12\n" |
| 133 | //usage: "34 ^ 189\n" |
| 134 | //usage: "scale = 13\n" |
| 135 | //usage: "ibase = 2\n" |
| 136 | //usage: "obase = A\n" |
| 137 | //usage: |
| 138 | //usage:#define dc_trivial_usage |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 139 | //usage: IF_NOT_FEATURE_DC_SMALL("[-x] ")"[-eSCRIPT]... [-fFILE]... [FILE]..." |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 140 | //usage: |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 141 | //usage:#define dc_full_usage "\n" |
| 142 | //usage: "\nTiny RPN calculator. Operations:" |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 143 | //usage: "\n+, -, *, /, %, ~, ^," IF_NOT_FEATURE_DC_SMALL(" |,") |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 144 | //usage: "\np - print top of the stack (without popping)" |
| 145 | //usage: "\nf - print entire stack" |
| 146 | //usage: "\nk - pop the value and set the precision" |
| 147 | //usage: "\ni - pop the value and set input radix" |
| 148 | //usage: "\no - pop the value and set output radix" |
| 149 | //usage: "\nExamples: dc -e'2 2 + p' -> 4, dc -e'8 8 * 2 2 + / p' -> 16" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 150 | //usage: |
| 151 | //usage:#define dc_example_usage |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 152 | //usage: "$ dc -e'2 2 + p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 153 | //usage: "4\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 154 | //usage: "$ dc -e'8 8 \\* 2 2 + / p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 155 | //usage: "16\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 156 | //usage: "$ dc -e'0 1 & p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 157 | //usage: "0\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 158 | //usage: "$ dc -e'0 1 | p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 159 | //usage: "1\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 160 | //usage: "$ echo '72 9 / 8 * p' | dc\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 161 | //usage: "64\n" |
| 162 | |
| 163 | #include "libbb.h" |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 164 | #include "common_bufsiz.h" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 165 | |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 166 | #if ENABLE_FEATURE_DC_SMALL |
| 167 | # include "dc.c" |
| 168 | #else |
| 169 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 170 | #define DEBUG_LEXER 0 |
| 171 | |
| 172 | #if DEBUG_LEXER |
| 173 | static unsigned lex_indent; |
| 174 | #define dbg_lex(...) \ |
| 175 | do { \ |
| 176 | fprintf(stderr, "%*s", lex_indent, ""); \ |
| 177 | bb_error_msg(__VA_ARGS__); \ |
| 178 | } while (0) |
| 179 | #define dbg_lex_enter(...) \ |
| 180 | do { \ |
| 181 | dbg_lex(__VA_ARGS__); \ |
| 182 | lex_indent++; \ |
| 183 | } while (0) |
| 184 | #define dbg_lex_done(...) \ |
| 185 | do { \ |
| 186 | lex_indent--; \ |
| 187 | dbg_lex(__VA_ARGS__); \ |
| 188 | } while (0) |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 189 | #else |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 190 | # define dbg_lex(...) ((void)0) |
| 191 | # define dbg_lex_enter(...) ((void)0) |
| 192 | # define dbg_lex_done(...) ((void)0) |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 193 | #endif |
| 194 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 195 | typedef enum BcStatus { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 196 | BC_STATUS_SUCCESS = 0, |
| 197 | BC_STATUS_FAILURE = 1, |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 198 | BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr_empty_ok() uses this |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 199 | } BcStatus; |
| 200 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 201 | #define BC_VEC_INVALID_IDX ((size_t) -1) |
| 202 | #define BC_VEC_START_CAP (1 << 5) |
| 203 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 204 | typedef void (*BcVecFree)(void *) FAST_FUNC; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 205 | |
| 206 | typedef struct BcVec { |
| 207 | char *v; |
| 208 | size_t len; |
| 209 | size_t cap; |
| 210 | size_t size; |
| 211 | BcVecFree dtor; |
| 212 | } BcVec; |
| 213 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 214 | typedef signed char BcDig; |
| 215 | |
| 216 | typedef struct BcNum { |
| 217 | BcDig *restrict num; |
| 218 | size_t rdx; |
| 219 | size_t len; |
| 220 | size_t cap; |
| 221 | bool neg; |
| 222 | } BcNum; |
| 223 | |
Denys Vlasenko | 2fa11b6 | 2018-12-06 12:34:39 +0100 | [diff] [blame] | 224 | #define BC_NUM_MIN_BASE ((unsigned long) 2) |
| 225 | #define BC_NUM_MAX_IBASE ((unsigned long) 16) |
| 226 | // larger value might speed up BIGNUM calculations a bit: |
| 227 | #define BC_NUM_DEF_SIZE (16) |
| 228 | #define BC_NUM_PRINT_WIDTH (69) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 229 | |
Denys Vlasenko | 2fa11b6 | 2018-12-06 12:34:39 +0100 | [diff] [blame] | 230 | #define BC_NUM_KARATSUBA_LEN (32) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 231 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 232 | typedef enum BcInst { |
| 233 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 234 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 235 | BC_INST_INC_PRE, |
| 236 | BC_INST_DEC_PRE, |
| 237 | BC_INST_INC_POST, |
| 238 | BC_INST_DEC_POST, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 239 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 240 | |
| 241 | BC_INST_NEG, |
| 242 | |
| 243 | BC_INST_POWER, |
| 244 | BC_INST_MULTIPLY, |
| 245 | BC_INST_DIVIDE, |
| 246 | BC_INST_MODULUS, |
| 247 | BC_INST_PLUS, |
| 248 | BC_INST_MINUS, |
| 249 | |
| 250 | BC_INST_REL_EQ, |
| 251 | BC_INST_REL_LE, |
| 252 | BC_INST_REL_GE, |
| 253 | BC_INST_REL_NE, |
| 254 | BC_INST_REL_LT, |
| 255 | BC_INST_REL_GT, |
| 256 | |
| 257 | BC_INST_BOOL_NOT, |
| 258 | BC_INST_BOOL_OR, |
| 259 | BC_INST_BOOL_AND, |
| 260 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 261 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 262 | BC_INST_ASSIGN_POWER, |
| 263 | BC_INST_ASSIGN_MULTIPLY, |
| 264 | BC_INST_ASSIGN_DIVIDE, |
| 265 | BC_INST_ASSIGN_MODULUS, |
| 266 | BC_INST_ASSIGN_PLUS, |
| 267 | BC_INST_ASSIGN_MINUS, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 268 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 269 | BC_INST_ASSIGN, |
| 270 | |
| 271 | BC_INST_NUM, |
| 272 | BC_INST_VAR, |
| 273 | BC_INST_ARRAY_ELEM, |
| 274 | BC_INST_ARRAY, |
| 275 | |
| 276 | BC_INST_SCALE_FUNC, |
| 277 | BC_INST_IBASE, |
| 278 | BC_INST_SCALE, |
| 279 | BC_INST_LAST, |
| 280 | BC_INST_LENGTH, |
| 281 | BC_INST_READ, |
| 282 | BC_INST_OBASE, |
| 283 | BC_INST_SQRT, |
| 284 | |
| 285 | BC_INST_PRINT, |
| 286 | BC_INST_PRINT_POP, |
| 287 | BC_INST_STR, |
| 288 | BC_INST_PRINT_STR, |
| 289 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 290 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 291 | BC_INST_JUMP, |
| 292 | BC_INST_JUMP_ZERO, |
| 293 | |
| 294 | BC_INST_CALL, |
| 295 | |
| 296 | BC_INST_RET, |
| 297 | BC_INST_RET0, |
| 298 | |
| 299 | BC_INST_HALT, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 300 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 301 | |
| 302 | BC_INST_POP, |
| 303 | BC_INST_POP_EXEC, |
| 304 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 305 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 306 | BC_INST_MODEXP, |
| 307 | BC_INST_DIVMOD, |
| 308 | |
| 309 | BC_INST_EXECUTE, |
| 310 | BC_INST_EXEC_COND, |
| 311 | |
| 312 | BC_INST_ASCIIFY, |
| 313 | BC_INST_PRINT_STREAM, |
| 314 | |
| 315 | BC_INST_PRINT_STACK, |
| 316 | BC_INST_CLEAR_STACK, |
| 317 | BC_INST_STACK_LEN, |
| 318 | BC_INST_DUPLICATE, |
| 319 | BC_INST_SWAP, |
| 320 | |
| 321 | BC_INST_LOAD, |
| 322 | BC_INST_PUSH_VAR, |
| 323 | BC_INST_PUSH_TO_VAR, |
| 324 | |
| 325 | BC_INST_QUIT, |
| 326 | BC_INST_NQUIT, |
| 327 | |
| 328 | BC_INST_INVALID = -1, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 329 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 330 | |
| 331 | } BcInst; |
| 332 | |
| 333 | typedef struct BcId { |
| 334 | char *name; |
| 335 | size_t idx; |
| 336 | } BcId; |
| 337 | |
| 338 | typedef struct BcFunc { |
| 339 | BcVec code; |
| 340 | BcVec labels; |
| 341 | size_t nparams; |
| 342 | BcVec autos; |
| 343 | } BcFunc; |
| 344 | |
| 345 | typedef enum BcResultType { |
| 346 | |
| 347 | BC_RESULT_TEMP, |
| 348 | |
| 349 | BC_RESULT_VAR, |
| 350 | BC_RESULT_ARRAY_ELEM, |
| 351 | BC_RESULT_ARRAY, |
| 352 | |
| 353 | BC_RESULT_STR, |
| 354 | |
| 355 | BC_RESULT_IBASE, |
| 356 | BC_RESULT_SCALE, |
| 357 | BC_RESULT_LAST, |
| 358 | |
| 359 | // These are between to calculate ibase, obase, and last from instructions. |
| 360 | BC_RESULT_CONSTANT, |
| 361 | BC_RESULT_ONE, |
| 362 | |
| 363 | BC_RESULT_OBASE, |
| 364 | |
| 365 | } BcResultType; |
| 366 | |
| 367 | typedef union BcResultData { |
| 368 | BcNum n; |
| 369 | BcVec v; |
| 370 | BcId id; |
| 371 | } BcResultData; |
| 372 | |
| 373 | typedef struct BcResult { |
| 374 | BcResultType t; |
| 375 | BcResultData d; |
| 376 | } BcResult; |
| 377 | |
| 378 | typedef struct BcInstPtr { |
| 379 | size_t func; |
| 380 | size_t idx; |
| 381 | size_t len; |
| 382 | } BcInstPtr; |
| 383 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 384 | // BC_LEX_NEG is not used in lexing; it is only for parsing. |
| 385 | typedef enum BcLexType { |
| 386 | |
| 387 | BC_LEX_EOF, |
| 388 | BC_LEX_INVALID, |
| 389 | |
| 390 | BC_LEX_OP_INC, |
| 391 | BC_LEX_OP_DEC, |
| 392 | |
| 393 | BC_LEX_NEG, |
| 394 | |
| 395 | BC_LEX_OP_POWER, |
| 396 | BC_LEX_OP_MULTIPLY, |
| 397 | BC_LEX_OP_DIVIDE, |
| 398 | BC_LEX_OP_MODULUS, |
| 399 | BC_LEX_OP_PLUS, |
| 400 | BC_LEX_OP_MINUS, |
| 401 | |
| 402 | BC_LEX_OP_REL_EQ, |
| 403 | BC_LEX_OP_REL_LE, |
| 404 | BC_LEX_OP_REL_GE, |
| 405 | BC_LEX_OP_REL_NE, |
| 406 | BC_LEX_OP_REL_LT, |
| 407 | BC_LEX_OP_REL_GT, |
| 408 | |
| 409 | BC_LEX_OP_BOOL_NOT, |
| 410 | BC_LEX_OP_BOOL_OR, |
| 411 | BC_LEX_OP_BOOL_AND, |
| 412 | |
| 413 | BC_LEX_OP_ASSIGN_POWER, |
| 414 | BC_LEX_OP_ASSIGN_MULTIPLY, |
| 415 | BC_LEX_OP_ASSIGN_DIVIDE, |
| 416 | BC_LEX_OP_ASSIGN_MODULUS, |
| 417 | BC_LEX_OP_ASSIGN_PLUS, |
| 418 | BC_LEX_OP_ASSIGN_MINUS, |
| 419 | BC_LEX_OP_ASSIGN, |
| 420 | |
| 421 | BC_LEX_NLINE, |
| 422 | BC_LEX_WHITESPACE, |
| 423 | |
| 424 | BC_LEX_LPAREN, |
| 425 | BC_LEX_RPAREN, |
| 426 | |
| 427 | BC_LEX_LBRACKET, |
| 428 | BC_LEX_COMMA, |
| 429 | BC_LEX_RBRACKET, |
| 430 | |
| 431 | BC_LEX_LBRACE, |
| 432 | BC_LEX_SCOLON, |
| 433 | BC_LEX_RBRACE, |
| 434 | |
| 435 | BC_LEX_STR, |
| 436 | BC_LEX_NAME, |
| 437 | BC_LEX_NUMBER, |
| 438 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 439 | BC_LEX_KEY_1st_keyword, |
| 440 | BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 441 | BC_LEX_KEY_BREAK, |
| 442 | BC_LEX_KEY_CONTINUE, |
| 443 | BC_LEX_KEY_DEFINE, |
| 444 | BC_LEX_KEY_ELSE, |
| 445 | BC_LEX_KEY_FOR, |
| 446 | BC_LEX_KEY_HALT, |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 447 | // code uses "type - BC_LEX_KEY_IBASE + BC_INST_IBASE" construct, |
| 448 | BC_LEX_KEY_IBASE, // relative order should match for: BC_INST_IBASE |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 449 | BC_LEX_KEY_IF, |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 450 | BC_LEX_KEY_LAST, // relative order should match for: BC_INST_LAST |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 451 | BC_LEX_KEY_LENGTH, |
| 452 | BC_LEX_KEY_LIMITS, |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 453 | BC_LEX_KEY_OBASE, // relative order should match for: BC_INST_OBASE |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 454 | BC_LEX_KEY_PRINT, |
| 455 | BC_LEX_KEY_QUIT, |
| 456 | BC_LEX_KEY_READ, |
| 457 | BC_LEX_KEY_RETURN, |
| 458 | BC_LEX_KEY_SCALE, |
| 459 | BC_LEX_KEY_SQRT, |
| 460 | BC_LEX_KEY_WHILE, |
| 461 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 462 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 463 | BC_LEX_EQ_NO_REG, |
| 464 | BC_LEX_OP_MODEXP, |
| 465 | BC_LEX_OP_DIVMOD, |
| 466 | |
| 467 | BC_LEX_COLON, |
| 468 | BC_LEX_ELSE, |
| 469 | BC_LEX_EXECUTE, |
| 470 | BC_LEX_PRINT_STACK, |
| 471 | BC_LEX_CLEAR_STACK, |
| 472 | BC_LEX_STACK_LEVEL, |
| 473 | BC_LEX_DUPLICATE, |
| 474 | BC_LEX_SWAP, |
| 475 | BC_LEX_POP, |
| 476 | |
| 477 | BC_LEX_ASCIIFY, |
| 478 | BC_LEX_PRINT_STREAM, |
| 479 | |
| 480 | BC_LEX_STORE_IBASE, |
| 481 | BC_LEX_STORE_SCALE, |
| 482 | BC_LEX_LOAD, |
| 483 | BC_LEX_LOAD_POP, |
| 484 | BC_LEX_STORE_PUSH, |
| 485 | BC_LEX_STORE_OBASE, |
| 486 | BC_LEX_PRINT_POP, |
| 487 | BC_LEX_NQUIT, |
| 488 | BC_LEX_SCALE_FACTOR, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 489 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 490 | } BcLexType; |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 491 | // must match order of BC_LEX_KEY_foo etc above |
| 492 | #if ENABLE_BC |
| 493 | struct BcLexKeyword { |
| 494 | char name8[8]; |
| 495 | }; |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 496 | #define BC_LEX_KW_ENTRY(a, b) \ |
| 497 | { .name8 = a /*, .posix = b */ } |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 498 | static const struct BcLexKeyword bc_lex_kws[20] = { |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 499 | BC_LEX_KW_ENTRY("auto" , 1), // 0 |
| 500 | BC_LEX_KW_ENTRY("break" , 1), // 1 |
| 501 | BC_LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL |
| 502 | BC_LEX_KW_ENTRY("define" , 1), // 3 |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 503 | BC_LEX_KW_ENTRY("else" , 0), // 4 |
| 504 | BC_LEX_KW_ENTRY("for" , 1), // 5 |
| 505 | BC_LEX_KW_ENTRY("halt" , 0), // 6 |
| 506 | BC_LEX_KW_ENTRY("ibase" , 1), // 7 |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 507 | BC_LEX_KW_ENTRY("if" , 1), // 8 |
| 508 | BC_LEX_KW_ENTRY("last" , 0), // 9 |
| 509 | BC_LEX_KW_ENTRY("length" , 1), // 10 |
| 510 | BC_LEX_KW_ENTRY("limits" , 0), // 11 |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 511 | BC_LEX_KW_ENTRY("obase" , 1), // 12 |
| 512 | BC_LEX_KW_ENTRY("print" , 0), // 13 |
| 513 | BC_LEX_KW_ENTRY("quit" , 1), // 14 |
| 514 | BC_LEX_KW_ENTRY("read" , 0), // 15 |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 515 | BC_LEX_KW_ENTRY("return" , 1), // 16 |
| 516 | BC_LEX_KW_ENTRY("scale" , 1), // 17 |
| 517 | BC_LEX_KW_ENTRY("sqrt" , 1), // 18 |
| 518 | BC_LEX_KW_ENTRY("while" , 1), // 19 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 519 | }; |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 520 | #undef BC_LEX_KW_ENTRY |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 521 | enum { |
| 522 | POSIX_KWORD_MASK = 0 |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 523 | | (1 << 0) // 0 |
| 524 | | (1 << 1) // 1 |
| 525 | | (0 << 2) // 2 |
| 526 | | (1 << 3) // 3 |
| 527 | | (0 << 4) // 4 |
| 528 | | (1 << 5) // 5 |
| 529 | | (0 << 6) // 6 |
| 530 | | (1 << 7) // 7 |
| 531 | | (1 << 8) // 8 |
| 532 | | (0 << 9) // 9 |
| 533 | | (1 << 10) // 10 |
| 534 | | (0 << 11) // 11 |
| 535 | | (1 << 12) // 12 |
| 536 | | (0 << 13) // 13 |
| 537 | | (1 << 14) // 14 |
| 538 | | (0 << 15) // 15 |
| 539 | | (1 << 16) // 16 |
| 540 | | (1 << 17) // 17 |
| 541 | | (1 << 18) // 18 |
| 542 | | (1 << 19) // 19 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 543 | }; |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 544 | #define bc_lex_kws_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK) |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 545 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 546 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 547 | #if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP |
| 548 | # define BC_STATUS BcStatus |
| 549 | #else |
| 550 | # define BC_STATUS void |
| 551 | #endif |
| 552 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 553 | typedef struct BcLex { |
| 554 | |
| 555 | const char *buf; |
| 556 | size_t i; |
| 557 | size_t line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 558 | size_t len; |
| 559 | bool newline; |
| 560 | |
| 561 | struct { |
| 562 | BcLexType t; |
| 563 | BcLexType last; |
| 564 | BcVec v; |
| 565 | } t; |
| 566 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 567 | } BcLex; |
| 568 | |
| 569 | #define BC_PARSE_STREND ((char) UCHAR_MAX) |
| 570 | |
Denys Vlasenko | e55a572 | 2018-12-06 12:47:17 +0100 | [diff] [blame] | 571 | #define BC_PARSE_REL (1 << 0) |
| 572 | #define BC_PARSE_PRINT (1 << 1) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 573 | #define BC_PARSE_NOCALL (1 << 2) |
| 574 | #define BC_PARSE_NOREAD (1 << 3) |
Denys Vlasenko | e55a572 | 2018-12-06 12:47:17 +0100 | [diff] [blame] | 575 | #define BC_PARSE_ARRAY (1 << 4) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 576 | |
| 577 | #define BC_PARSE_TOP_FLAG_PTR(parse) ((uint8_t *) bc_vec_top(&(parse)->flags)) |
| 578 | #define BC_PARSE_TOP_FLAG(parse) (*(BC_PARSE_TOP_FLAG_PTR(parse))) |
| 579 | |
| 580 | #define BC_PARSE_FLAG_FUNC_INNER (1 << 0) |
| 581 | #define BC_PARSE_FUNC_INNER(parse) \ |
| 582 | (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC_INNER) |
| 583 | |
| 584 | #define BC_PARSE_FLAG_FUNC (1 << 1) |
| 585 | #define BC_PARSE_FUNC(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC) |
| 586 | |
| 587 | #define BC_PARSE_FLAG_BODY (1 << 2) |
| 588 | #define BC_PARSE_BODY(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_BODY) |
| 589 | |
| 590 | #define BC_PARSE_FLAG_LOOP (1 << 3) |
| 591 | #define BC_PARSE_LOOP(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP) |
| 592 | |
| 593 | #define BC_PARSE_FLAG_LOOP_INNER (1 << 4) |
| 594 | #define BC_PARSE_LOOP_INNER(parse) \ |
| 595 | (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP_INNER) |
| 596 | |
| 597 | #define BC_PARSE_FLAG_IF (1 << 5) |
| 598 | #define BC_PARSE_IF(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF) |
| 599 | |
| 600 | #define BC_PARSE_FLAG_ELSE (1 << 6) |
| 601 | #define BC_PARSE_ELSE(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_ELSE) |
| 602 | |
| 603 | #define BC_PARSE_FLAG_IF_END (1 << 7) |
| 604 | #define BC_PARSE_IF_END(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF_END) |
| 605 | |
| 606 | #define BC_PARSE_CAN_EXEC(parse) \ |
| 607 | (!(BC_PARSE_TOP_FLAG(parse) & \ |
| 608 | (BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_BODY | \ |
| 609 | BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER | BC_PARSE_FLAG_IF | \ |
| 610 | BC_PARSE_FLAG_ELSE | BC_PARSE_FLAG_IF_END))) |
| 611 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 612 | struct BcParse; |
| 613 | |
| 614 | struct BcProgram; |
| 615 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 616 | typedef struct BcParse { |
| 617 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 618 | BcLex l; |
| 619 | |
| 620 | BcVec flags; |
| 621 | |
| 622 | BcVec exits; |
| 623 | BcVec conds; |
| 624 | |
| 625 | BcVec ops; |
| 626 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 627 | BcFunc *func; |
| 628 | size_t fidx; |
| 629 | |
| 630 | size_t nbraces; |
| 631 | bool auto_part; |
| 632 | |
| 633 | } BcParse; |
| 634 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 635 | typedef struct BcProgram { |
| 636 | |
| 637 | size_t len; |
| 638 | size_t scale; |
| 639 | |
| 640 | BcNum ib; |
| 641 | size_t ib_t; |
| 642 | BcNum ob; |
| 643 | size_t ob_t; |
| 644 | |
| 645 | BcNum hexb; |
| 646 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 647 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 648 | BcNum strmb; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 649 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 650 | |
| 651 | BcVec results; |
| 652 | BcVec stack; |
| 653 | |
| 654 | BcVec fns; |
| 655 | BcVec fn_map; |
| 656 | |
| 657 | BcVec vars; |
| 658 | BcVec var_map; |
| 659 | |
| 660 | BcVec arrs; |
| 661 | BcVec arr_map; |
| 662 | |
| 663 | BcVec strs; |
| 664 | BcVec consts; |
| 665 | |
| 666 | const char *file; |
| 667 | |
| 668 | BcNum last; |
| 669 | BcNum zero; |
| 670 | BcNum one; |
| 671 | |
| 672 | size_t nchars; |
| 673 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 674 | } BcProgram; |
| 675 | |
| 676 | #define BC_PROG_STACK(s, n) ((s)->len >= ((size_t) n)) |
| 677 | |
| 678 | #define BC_PROG_MAIN (0) |
| 679 | #define BC_PROG_READ (1) |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 680 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 681 | #define BC_PROG_REQ_FUNCS (2) |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 682 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 683 | |
| 684 | #define BC_PROG_STR(n) (!(n)->num && !(n)->cap) |
| 685 | #define BC_PROG_NUM(r, n) \ |
| 686 | ((r)->t != BC_RESULT_ARRAY && (r)->t != BC_RESULT_STR && !BC_PROG_STR(n)) |
| 687 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 688 | #define BC_FLAG_W (1 << 0) |
| 689 | #define BC_FLAG_V (1 << 1) |
| 690 | #define BC_FLAG_S (1 << 2) |
| 691 | #define BC_FLAG_Q (1 << 3) |
| 692 | #define BC_FLAG_L (1 << 4) |
| 693 | #define BC_FLAG_I (1 << 5) |
| 694 | #define DC_FLAG_X (1 << 6) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 695 | |
| 696 | #define BC_MAX(a, b) ((a) > (b) ? (a) : (b)) |
| 697 | #define BC_MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 698 | |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 699 | #define BC_MAX_OBASE ((unsigned) 999) |
| 700 | #define BC_MAX_DIM ((unsigned) INT_MAX) |
| 701 | #define BC_MAX_SCALE ((unsigned) UINT_MAX) |
| 702 | #define BC_MAX_STRING ((unsigned) UINT_MAX - 1) |
| 703 | #define BC_MAX_NUM BC_MAX_STRING |
| 704 | // Unused apart from "limits" message. Just show a "biggish number" there. |
| 705 | //#define BC_MAX_NAME BC_MAX_STRING |
| 706 | //#define BC_MAX_EXP ((unsigned long) LONG_MAX) |
| 707 | //#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1) |
| 708 | #define BC_MAX_NAME_STR "999999999" |
| 709 | #define BC_MAX_EXP_STR "999999999" |
| 710 | #define BC_MAX_VARS_STR "999999999" |
| 711 | |
| 712 | #define BC_MAX_OBASE_STR "999" |
| 713 | |
| 714 | #if INT_MAX == 2147483647 |
| 715 | # define BC_MAX_DIM_STR "2147483647" |
| 716 | #elif INT_MAX == 9223372036854775807 |
| 717 | # define BC_MAX_DIM_STR "9223372036854775807" |
| 718 | #else |
| 719 | # error Strange INT_MAX |
| 720 | #endif |
| 721 | |
| 722 | #if UINT_MAX == 4294967295 |
| 723 | # define BC_MAX_SCALE_STR "4294967295" |
| 724 | # define BC_MAX_STRING_STR "4294967294" |
| 725 | #elif UINT_MAX == 18446744073709551615 |
| 726 | # define BC_MAX_SCALE_STR "18446744073709551615" |
| 727 | # define BC_MAX_STRING_STR "18446744073709551614" |
| 728 | #else |
| 729 | # error Strange UINT_MAX |
| 730 | #endif |
| 731 | #define BC_MAX_NUM_STR BC_MAX_STRING_STR |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 732 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 733 | struct globals { |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 734 | IF_FEATURE_BC_SIGNALS(smallint ttyin;) |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 735 | IF_FEATURE_CLEAN_UP(smallint exiting;) |
Denys Vlasenko | 69171dc | 2018-12-12 00:29:24 +0100 | [diff] [blame] | 736 | smallint in_read; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 737 | |
| 738 | BcParse prs; |
| 739 | BcProgram prog; |
| 740 | |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 741 | // For error messages. Can be set to current parsed line, |
| 742 | // or [TODO] to current executing line (can be before last parsed one) |
| 743 | unsigned err_line; |
| 744 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 745 | BcVec files; |
| 746 | |
| 747 | char *env_args; |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 748 | |
| 749 | #if ENABLE_FEATURE_EDITING |
| 750 | line_input_t *line_input_state; |
| 751 | #endif |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 752 | } FIX_ALIASING; |
| 753 | #define G (*ptr_to_globals) |
| 754 | #define INIT_G() do { \ |
| 755 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 756 | } while (0) |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 757 | #define FREE_G() do { \ |
| 758 | FREE_PTR_TO_GLOBALS(); \ |
| 759 | } while (0) |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 760 | #define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S)) |
| 761 | #define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W)) |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 762 | #define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X)) |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 763 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | b9c321d | 2018-12-07 12:41:42 +0100 | [diff] [blame] | 764 | # define G_interrupt bb_got_signal |
| 765 | # define G_ttyin G.ttyin |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 766 | #else |
Denys Vlasenko | b9c321d | 2018-12-07 12:41:42 +0100 | [diff] [blame] | 767 | # define G_interrupt 0 |
| 768 | # define G_ttyin 0 |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 769 | #endif |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 770 | #if ENABLE_FEATURE_CLEAN_UP |
| 771 | # define G_exiting G.exiting |
| 772 | #else |
| 773 | # define G_exiting 0 |
| 774 | #endif |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 775 | #define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b')) |
Denys Vlasenko | 40534bb | 2018-12-13 16:59:24 +0100 | [diff] [blame] | 776 | #define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b')) |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 777 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 778 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 779 | |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 780 | // This is a bit array that corresponds to token types. An entry is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 781 | // true if the token is valid in an expression, false otherwise. |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 782 | enum { |
| 783 | BC_PARSE_EXPRS_BITS = 0 |
| 784 | + ((uint64_t)((0 << 0)+(0 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (0*8)) |
| 785 | + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (1*8)) |
| 786 | + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (2*8)) |
| 787 | + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(0 << 3)+(0 << 4)+(1 << 5)+(1 << 6)+(0 << 7)) << (3*8)) |
| 788 | + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(1 << 6)+(1 << 7)) << (4*8)) |
| 789 | + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (5*8)) |
| 790 | + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (6*8)) |
| 791 | + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(0 << 3) ) << (7*8)) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 792 | }; |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 793 | static ALWAYS_INLINE long bc_parse_exprs(unsigned i) |
| 794 | { |
| 795 | #if ULONG_MAX > 0xffffffff |
| 796 | // 64-bit version (will not work correctly for 32-bit longs!) |
| 797 | return BC_PARSE_EXPRS_BITS & (1UL << i); |
| 798 | #else |
| 799 | // 32-bit version |
| 800 | unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS; |
| 801 | if (i >= 32) { |
| 802 | m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32); |
| 803 | i &= 31; |
| 804 | } |
| 805 | return m & (1UL << i); |
| 806 | #endif |
| 807 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 808 | |
| 809 | // This is an array of data for operators that correspond to token types. |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 810 | static const uint8_t bc_parse_ops[] = { |
| 811 | #define OP(p,l) ((int)(l) * 0x10 + (p)) |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 812 | OP(0, false), OP( 0, false ), // inc dec |
| 813 | OP(1, false), // neg |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 814 | OP(2, false), |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 815 | OP(3, true ), OP( 3, true ), OP( 3, true ), // pow mul div |
| 816 | OP(4, true ), OP( 4, true ), // mod + - |
| 817 | OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < > |
| 818 | OP(1, false), // not |
| 819 | OP(7, true ), OP( 7, true ), // or and |
| 820 | OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= += |
| 821 | OP(5, false), OP( 5, false ), // -= = |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 822 | #undef OP |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 823 | }; |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 824 | #define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f) |
| 825 | #define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 826 | |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 827 | // Byte array of up to 4 BC_LEX's, packed into 32-bit word |
| 828 | typedef uint32_t BcParseNext; |
| 829 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 830 | // These identify what tokens can come after expressions in certain cases. |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 831 | enum { |
| 832 | #define BC_PARSE_NEXT4(a,b,c,d) ( (a) | ((b)<<8) | ((c)<<16) | ((((d)|0x80)<<24)) ) |
| 833 | #define BC_PARSE_NEXT2(a,b) BC_PARSE_NEXT4(a,b,0xff,0xff) |
| 834 | #define BC_PARSE_NEXT1(a) BC_PARSE_NEXT4(a,0xff,0xff,0xff) |
| 835 | bc_parse_next_expr = BC_PARSE_NEXT4(BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_RBRACE, BC_LEX_EOF), |
| 836 | bc_parse_next_param = BC_PARSE_NEXT2(BC_LEX_RPAREN, BC_LEX_COMMA), |
| 837 | bc_parse_next_print = BC_PARSE_NEXT4(BC_LEX_COMMA, BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_EOF), |
| 838 | bc_parse_next_rel = BC_PARSE_NEXT1(BC_LEX_RPAREN), |
| 839 | bc_parse_next_elem = BC_PARSE_NEXT1(BC_LEX_RBRACKET), |
| 840 | bc_parse_next_for = BC_PARSE_NEXT1(BC_LEX_SCOLON), |
| 841 | bc_parse_next_read = BC_PARSE_NEXT2(BC_LEX_NLINE, BC_LEX_EOF), |
| 842 | #undef BC_PARSE_NEXT4 |
| 843 | #undef BC_PARSE_NEXT2 |
| 844 | #undef BC_PARSE_NEXT1 |
| 845 | }; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 846 | #endif // ENABLE_BC |
| 847 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 848 | #if ENABLE_DC |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 849 | static const //BcLexType - should be this type, but narrower type saves size: |
| 850 | uint8_t |
| 851 | dc_lex_regs[] = { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 852 | BC_LEX_OP_REL_EQ, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_NE, |
| 853 | BC_LEX_OP_REL_LT, BC_LEX_OP_REL_GT, BC_LEX_SCOLON, BC_LEX_COLON, |
| 854 | BC_LEX_ELSE, BC_LEX_LOAD, BC_LEX_LOAD_POP, BC_LEX_OP_ASSIGN, |
| 855 | BC_LEX_STORE_PUSH, |
| 856 | }; |
| 857 | |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 858 | static const //BcLexType - should be this type |
| 859 | uint8_t |
| 860 | dc_lex_tokens[] = { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 861 | BC_LEX_OP_MODULUS, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_LPAREN, |
| 862 | BC_LEX_INVALID, BC_LEX_OP_MULTIPLY, BC_LEX_OP_PLUS, BC_LEX_INVALID, |
| 863 | BC_LEX_OP_MINUS, BC_LEX_INVALID, BC_LEX_OP_DIVIDE, |
| 864 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, |
| 865 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, |
| 866 | BC_LEX_INVALID, BC_LEX_INVALID, |
| 867 | BC_LEX_COLON, BC_LEX_SCOLON, BC_LEX_OP_REL_GT, BC_LEX_OP_REL_EQ, |
| 868 | BC_LEX_OP_REL_LT, BC_LEX_KEY_READ, BC_LEX_INVALID, |
| 869 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, |
| 870 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_EQ_NO_REG, BC_LEX_INVALID, |
| 871 | BC_LEX_KEY_IBASE, BC_LEX_INVALID, BC_LEX_KEY_SCALE, BC_LEX_LOAD_POP, |
| 872 | BC_LEX_INVALID, BC_LEX_OP_BOOL_NOT, BC_LEX_KEY_OBASE, BC_LEX_PRINT_STREAM, |
| 873 | BC_LEX_NQUIT, BC_LEX_POP, BC_LEX_STORE_PUSH, BC_LEX_INVALID, BC_LEX_INVALID, |
| 874 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_SCALE_FACTOR, BC_LEX_INVALID, |
| 875 | BC_LEX_KEY_LENGTH, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, |
| 876 | BC_LEX_OP_POWER, BC_LEX_NEG, BC_LEX_INVALID, |
| 877 | BC_LEX_ASCIIFY, BC_LEX_INVALID, BC_LEX_CLEAR_STACK, BC_LEX_DUPLICATE, |
| 878 | BC_LEX_ELSE, BC_LEX_PRINT_STACK, BC_LEX_INVALID, BC_LEX_INVALID, |
| 879 | BC_LEX_STORE_IBASE, BC_LEX_INVALID, BC_LEX_STORE_SCALE, BC_LEX_LOAD, |
| 880 | BC_LEX_INVALID, BC_LEX_PRINT_POP, BC_LEX_STORE_OBASE, BC_LEX_KEY_PRINT, |
| 881 | BC_LEX_KEY_QUIT, BC_LEX_SWAP, BC_LEX_OP_ASSIGN, BC_LEX_INVALID, |
| 882 | BC_LEX_INVALID, BC_LEX_KEY_SQRT, BC_LEX_INVALID, BC_LEX_EXECUTE, |
| 883 | BC_LEX_INVALID, BC_LEX_STACK_LEVEL, |
| 884 | BC_LEX_LBRACE, BC_LEX_OP_MODEXP, BC_LEX_INVALID, BC_LEX_OP_DIVMOD, |
| 885 | BC_LEX_INVALID |
| 886 | }; |
| 887 | |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 888 | static const //BcInst - should be this type. Using signed narrow type since BC_INST_INVALID is -1 |
| 889 | int8_t |
| 890 | dc_parse_insts[] = { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 891 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE, |
| 892 | BC_INST_INVALID, BC_INST_POWER, BC_INST_MULTIPLY, BC_INST_DIVIDE, |
| 893 | BC_INST_MODULUS, BC_INST_PLUS, BC_INST_MINUS, |
| 894 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 895 | BC_INST_INVALID, BC_INST_INVALID, |
| 896 | BC_INST_BOOL_NOT, BC_INST_INVALID, BC_INST_INVALID, |
| 897 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 898 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 899 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GT, BC_INST_INVALID, |
| 900 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE, |
| 901 | BC_INST_INVALID, BC_INST_INVALID, |
| 902 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 903 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 904 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_IBASE, |
| 905 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_LENGTH, BC_INST_INVALID, |
| 906 | BC_INST_OBASE, BC_INST_PRINT, BC_INST_QUIT, BC_INST_INVALID, |
| 907 | BC_INST_INVALID, BC_INST_SCALE, BC_INST_SQRT, BC_INST_INVALID, |
| 908 | BC_INST_REL_EQ, BC_INST_MODEXP, BC_INST_DIVMOD, BC_INST_INVALID, |
| 909 | BC_INST_INVALID, BC_INST_EXECUTE, BC_INST_PRINT_STACK, BC_INST_CLEAR_STACK, |
| 910 | BC_INST_STACK_LEN, BC_INST_DUPLICATE, BC_INST_SWAP, BC_INST_POP, |
| 911 | BC_INST_ASCIIFY, BC_INST_PRINT_STREAM, BC_INST_INVALID, BC_INST_INVALID, |
| 912 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 913 | BC_INST_PRINT, BC_INST_NQUIT, BC_INST_SCALE_FUNC, |
| 914 | }; |
| 915 | #endif // ENABLE_DC |
| 916 | |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 917 | // In configurations where errors abort instead of propagating error |
| 918 | // return code up the call chain, functions returning BC_STATUS |
| 919 | // actually don't return anything, they always succeed and return "void". |
| 920 | // A macro wrapper is provided, which makes this statement work: |
| 921 | // s = zbc_func(...) |
| 922 | // and makes it visible to the compiler that s is always zero, |
| 923 | // allowing compiler to optimize dead code after the statement. |
| 924 | // |
| 925 | // To make code more readable, each such function has a "z" |
| 926 | // ("always returning zero") prefix, i.e. zbc_foo or zdc_foo. |
| 927 | // |
| 928 | #if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 929 | # define ERRORS_ARE_FATAL 0 |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 930 | # define ERRORFUNC /*nothing*/ |
| 931 | # define ERROR_RETURN(a) a |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 932 | //moved up: # define BC_STATUS BcStatus |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 933 | # define RETURN_STATUS(v) return (v) |
| 934 | #else |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 935 | # define ERRORS_ARE_FATAL 1 |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 936 | # define ERRORFUNC NORETURN |
| 937 | # define ERROR_RETURN(a) /*nothing*/ |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 938 | //moved up: # define BC_STATUS void |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 939 | # define RETURN_STATUS(v) do { ((void)(v)); return; } while (0) |
| 940 | #endif |
| 941 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 942 | #define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg)) |
| 943 | #define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1) |
| 944 | #define BC_NUM_INT(n) ((n)->len - (n)->rdx) |
| 945 | //#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1) |
| 946 | static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b) |
| 947 | { |
| 948 | return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1; |
| 949 | } |
| 950 | //#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1) |
| 951 | static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale) |
| 952 | { |
| 953 | return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1; |
| 954 | } |
| 955 | |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 956 | typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC; |
| 957 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 958 | typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC; |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 959 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 960 | static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale, |
| 961 | BcNumBinaryOp op, size_t req); |
| 962 | static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 963 | static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 964 | static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 965 | static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 966 | static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 967 | static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 968 | |
| 969 | static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 970 | { |
| 971 | BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s; |
| 972 | (void) scale; |
| 973 | RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b))); |
| 974 | } |
| 975 | |
| 976 | static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 977 | { |
| 978 | BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a; |
| 979 | (void) scale; |
| 980 | RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b))); |
| 981 | } |
| 982 | |
| 983 | static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 984 | { |
| 985 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 986 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req)); |
| 987 | } |
| 988 | |
| 989 | static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 990 | { |
| 991 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 992 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req)); |
| 993 | } |
| 994 | |
| 995 | static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 996 | { |
| 997 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 998 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req)); |
| 999 | } |
| 1000 | |
| 1001 | static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1002 | { |
| 1003 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1)); |
| 1004 | } |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 1005 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1006 | static const BcNumBinaryOp zbc_program_ops[] = { |
| 1007 | zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1008 | }; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1009 | #if ERRORS_ARE_FATAL |
| 1010 | # define zbc_num_add(...) (zbc_num_add(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 1011 | # define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 1012 | # define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 1013 | # define zbc_num_div(...) (zbc_num_div(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 1014 | # define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 1015 | # define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 1016 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1017 | |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 1018 | static void fflush_and_check(void) |
| 1019 | { |
| 1020 | fflush_all(); |
| 1021 | if (ferror(stdout) || ferror(stderr)) |
| 1022 | bb_perror_msg_and_die("output error"); |
| 1023 | } |
| 1024 | |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 1025 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 1026 | #define QUIT_OR_RETURN_TO_MAIN \ |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 1027 | do { \ |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 1028 | IF_FEATURE_BC_SIGNALS(G_ttyin = 0;) /* do not loop in main loop anymore */ \ |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 1029 | G_exiting = 1; \ |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 1030 | return BC_STATUS_FAILURE; \ |
| 1031 | } while (0) |
| 1032 | #else |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 1033 | #define QUIT_OR_RETURN_TO_MAIN quit() |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 1034 | #endif |
| 1035 | |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 1036 | static void quit(void) NORETURN; |
| 1037 | static void quit(void) |
| 1038 | { |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 1039 | if (ferror(stdin)) |
| 1040 | bb_perror_msg_and_die("input error"); |
| 1041 | fflush_and_check(); |
| 1042 | exit(0); |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 1043 | } |
| 1044 | |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 1045 | static void bc_verror_msg(const char *fmt, va_list p) |
| 1046 | { |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 1047 | const char *sv = sv; // for compiler |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 1048 | if (G.prog.file) { |
| 1049 | sv = applet_name; |
| 1050 | applet_name = xasprintf("%s: %s:%u", applet_name, G.prog.file, G.err_line); |
| 1051 | } |
| 1052 | bb_verror_msg(fmt, p, NULL); |
| 1053 | if (G.prog.file) { |
| 1054 | free((char*)applet_name); |
| 1055 | applet_name = sv; |
| 1056 | } |
| 1057 | } |
| 1058 | |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1059 | static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...) |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1060 | { |
| 1061 | va_list p; |
| 1062 | |
| 1063 | va_start(p, fmt); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 1064 | bc_verror_msg(fmt, p); |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1065 | va_end(p); |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 1066 | |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 1067 | if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin) |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1068 | exit(1); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1069 | ERROR_RETURN(return BC_STATUS_FAILURE;) |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1070 | } |
| 1071 | |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1072 | #if ENABLE_BC |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1073 | static NOINLINE int bc_posix_error_fmt(const char *fmt, ...) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1074 | { |
| 1075 | va_list p; |
| 1076 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1077 | // Are non-POSIX constructs totally ok? |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 1078 | if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W))) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1079 | return BC_STATUS_SUCCESS; // yes |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1080 | |
| 1081 | va_start(p, fmt); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 1082 | bc_verror_msg(fmt, p); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1083 | va_end(p); |
| 1084 | |
| 1085 | // Do we treat non-POSIX constructs as errors? |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 1086 | if (!(option_mask32 & BC_FLAG_S)) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1087 | return BC_STATUS_SUCCESS; // no, it's a warning |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 1088 | if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1089 | exit(1); |
| 1090 | return BC_STATUS_FAILURE; |
| 1091 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1092 | #endif |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1093 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1094 | // We use error functions with "return bc_error(FMT[, PARAMS])" idiom. |
| 1095 | // This idiom begs for tail-call optimization, but for it to work, |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1096 | // function must not have caller-cleaned parameters on stack. |
| 1097 | // Unfortunately, vararg function API does exactly that on most arches. |
| 1098 | // Thus, use these shims for the cases when we have no vararg PARAMS: |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1099 | static ERRORFUNC int bc_error(const char *msg) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1100 | { |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1101 | ERROR_RETURN(return) bc_error_fmt("%s", msg); |
| 1102 | } |
| 1103 | static ERRORFUNC int bc_error_bad_character(char c) |
| 1104 | { |
| 1105 | ERROR_RETURN(return) bc_error_fmt("bad character '%c'", c); |
| 1106 | } |
| 1107 | static ERRORFUNC int bc_error_bad_expression(void) |
| 1108 | { |
| 1109 | ERROR_RETURN(return) bc_error("bad expression"); |
| 1110 | } |
| 1111 | static ERRORFUNC int bc_error_bad_token(void) |
| 1112 | { |
| 1113 | ERROR_RETURN(return) bc_error("bad token"); |
| 1114 | } |
| 1115 | static ERRORFUNC int bc_error_stack_has_too_few_elements(void) |
| 1116 | { |
| 1117 | ERROR_RETURN(return) bc_error("stack has too few elements"); |
| 1118 | } |
| 1119 | static ERRORFUNC int bc_error_variable_is_wrong_type(void) |
| 1120 | { |
| 1121 | ERROR_RETURN(return) bc_error("variable is wrong type"); |
| 1122 | } |
| 1123 | static ERRORFUNC int bc_error_nested_read_call(void) |
| 1124 | { |
| 1125 | ERROR_RETURN(return) bc_error("read() call inside of a read() call"); |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1126 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1127 | #if ENABLE_BC |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 1128 | static int bc_POSIX_requires(const char *msg) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1129 | { |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 1130 | return bc_posix_error_fmt("POSIX requires %s", msg); |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1131 | } |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 1132 | static int bc_POSIX_does_not_allow(const char *msg) |
| 1133 | { |
| 1134 | return bc_posix_error_fmt("%s%s", "POSIX does not allow ", msg); |
| 1135 | } |
| 1136 | static int bc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg) |
| 1137 | { |
| 1138 | return bc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; the following is bad:", msg); |
| 1139 | } |
| 1140 | static int bc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg) |
| 1141 | { |
| 1142 | return bc_posix_error_fmt("%san empty %s expression in a for loop", "POSIX does not allow ", msg); |
| 1143 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1144 | #endif |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1145 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1146 | static void bc_vec_grow(BcVec *v, size_t n) |
| 1147 | { |
| 1148 | size_t cap = v->cap * 2; |
| 1149 | while (cap < v->len + n) cap *= 2; |
| 1150 | v->v = xrealloc(v->v, v->size * cap); |
| 1151 | v->cap = cap; |
| 1152 | } |
| 1153 | |
| 1154 | static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor) |
| 1155 | { |
| 1156 | v->size = esize; |
| 1157 | v->cap = BC_VEC_START_CAP; |
| 1158 | v->len = 0; |
| 1159 | v->dtor = dtor; |
| 1160 | v->v = xmalloc(esize * BC_VEC_START_CAP); |
| 1161 | } |
| 1162 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1163 | static void bc_char_vec_init(BcVec *v) |
| 1164 | { |
| 1165 | bc_vec_init(v, sizeof(char), NULL); |
| 1166 | } |
| 1167 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1168 | static void bc_vec_expand(BcVec *v, size_t req) |
| 1169 | { |
| 1170 | if (v->cap < req) { |
| 1171 | v->v = xrealloc(v->v, v->size * req); |
| 1172 | v->cap = req; |
| 1173 | } |
| 1174 | } |
| 1175 | |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 1176 | static void bc_vec_pop(BcVec *v) |
| 1177 | { |
| 1178 | v->len--; |
| 1179 | if (v->dtor) |
| 1180 | v->dtor(v->v + (v->size * v->len)); |
| 1181 | } |
Denys Vlasenko | 2fa11b6 | 2018-12-06 12:34:39 +0100 | [diff] [blame] | 1182 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1183 | static void bc_vec_npop(BcVec *v, size_t n) |
| 1184 | { |
| 1185 | if (!v->dtor) |
| 1186 | v->len -= n; |
| 1187 | else { |
| 1188 | size_t len = v->len - n; |
| 1189 | while (v->len > len) v->dtor(v->v + (v->size * --v->len)); |
| 1190 | } |
| 1191 | } |
| 1192 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1193 | static void bc_vec_pop_all(BcVec *v) |
| 1194 | { |
| 1195 | bc_vec_npop(v, v->len); |
| 1196 | } |
| 1197 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1198 | static void bc_vec_push(BcVec *v, const void *data) |
| 1199 | { |
| 1200 | if (v->len + 1 > v->cap) bc_vec_grow(v, 1); |
| 1201 | memmove(v->v + (v->size * v->len), data, v->size); |
| 1202 | v->len += 1; |
| 1203 | } |
| 1204 | |
| 1205 | static void bc_vec_pushByte(BcVec *v, char data) |
| 1206 | { |
| 1207 | bc_vec_push(v, &data); |
| 1208 | } |
| 1209 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1210 | static void bc_vec_pushZeroByte(BcVec *v) |
| 1211 | { |
| 1212 | //bc_vec_pushByte(v, '\0'); |
| 1213 | // better: |
| 1214 | bc_vec_push(v, &const_int_0); |
| 1215 | } |
| 1216 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1217 | static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx) |
| 1218 | { |
| 1219 | if (idx == v->len) |
| 1220 | bc_vec_push(v, data); |
| 1221 | else { |
| 1222 | |
| 1223 | char *ptr; |
| 1224 | |
| 1225 | if (v->len == v->cap) bc_vec_grow(v, 1); |
| 1226 | |
| 1227 | ptr = v->v + v->size * idx; |
| 1228 | |
| 1229 | memmove(ptr + v->size, ptr, v->size * (v->len++ - idx)); |
| 1230 | memmove(ptr, data, v->size); |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | static void bc_vec_string(BcVec *v, size_t len, const char *str) |
| 1235 | { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1236 | bc_vec_pop_all(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1237 | bc_vec_expand(v, len + 1); |
| 1238 | memcpy(v->v, str, len); |
| 1239 | v->len = len; |
| 1240 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1241 | bc_vec_pushZeroByte(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1242 | } |
| 1243 | |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 1244 | #if ENABLE_FEATURE_BC_SIGNALS && ENABLE_FEATURE_EDITING |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1245 | static void bc_vec_concat(BcVec *v, const char *str) |
| 1246 | { |
Denys Vlasenko | 8b4cf0d | 2018-12-10 15:12:58 +0100 | [diff] [blame] | 1247 | size_t len, slen; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1248 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1249 | if (v->len == 0) bc_vec_pushZeroByte(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1250 | |
Denys Vlasenko | 8b4cf0d | 2018-12-10 15:12:58 +0100 | [diff] [blame] | 1251 | slen = strlen(str); |
| 1252 | len = v->len + slen; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1253 | |
Denys Vlasenko | 8b4cf0d | 2018-12-10 15:12:58 +0100 | [diff] [blame] | 1254 | if (v->cap < len) bc_vec_grow(v, slen); |
Denys Vlasenko | 1ff8862 | 2018-12-06 12:06:16 +0100 | [diff] [blame] | 1255 | strcpy(v->v + v->len - 1, str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1256 | |
| 1257 | v->len = len; |
| 1258 | } |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 1259 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1260 | |
| 1261 | static void *bc_vec_item(const BcVec *v, size_t idx) |
| 1262 | { |
| 1263 | return v->v + v->size * idx; |
| 1264 | } |
| 1265 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 1266 | static char** bc_program_str(size_t idx) |
| 1267 | { |
| 1268 | return bc_vec_item(&G.prog.strs, idx); |
| 1269 | } |
| 1270 | |
| 1271 | static BcFunc* bc_program_func(size_t idx) |
| 1272 | { |
| 1273 | return bc_vec_item(&G.prog.fns, idx); |
| 1274 | } |
| 1275 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1276 | static void *bc_vec_item_rev(const BcVec *v, size_t idx) |
| 1277 | { |
| 1278 | return v->v + v->size * (v->len - idx - 1); |
| 1279 | } |
| 1280 | |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 1281 | static void *bc_vec_top(const BcVec *v) |
| 1282 | { |
| 1283 | return v->v + v->size * (v->len - 1); |
| 1284 | } |
| 1285 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1286 | static FAST_FUNC void bc_vec_free(void *vec) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1287 | { |
| 1288 | BcVec *v = (BcVec *) vec; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1289 | bc_vec_pop_all(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1290 | free(v->v); |
| 1291 | } |
| 1292 | |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 1293 | static int bc_id_cmp(const void *e1, const void *e2) |
| 1294 | { |
| 1295 | return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name); |
| 1296 | } |
| 1297 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1298 | static FAST_FUNC void bc_id_free(void *id) |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 1299 | { |
| 1300 | free(((BcId *) id)->name); |
| 1301 | } |
| 1302 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1303 | static size_t bc_map_find(const BcVec *v, const void *ptr) |
| 1304 | { |
| 1305 | size_t low = 0, high = v->len; |
| 1306 | |
| 1307 | while (low < high) { |
| 1308 | |
| 1309 | size_t mid = (low + high) / 2; |
| 1310 | BcId *id = bc_vec_item(v, mid); |
| 1311 | int result = bc_id_cmp(ptr, id); |
| 1312 | |
| 1313 | if (result == 0) |
| 1314 | return mid; |
| 1315 | else if (result < 0) |
| 1316 | high = mid; |
| 1317 | else |
| 1318 | low = mid + 1; |
| 1319 | } |
| 1320 | |
| 1321 | return low; |
| 1322 | } |
| 1323 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1324 | static int bc_map_insert(BcVec *v, const void *ptr, size_t *i) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1325 | { |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1326 | size_t n = *i = bc_map_find(v, ptr); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1327 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1328 | if (n == v->len) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1329 | bc_vec_push(v, ptr); |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1330 | else if (!bc_id_cmp(ptr, bc_vec_item(v, n))) |
| 1331 | return 0; // "was not inserted" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1332 | else |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1333 | bc_vec_pushAt(v, ptr, n); |
| 1334 | return 1; // "was inserted" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1335 | } |
| 1336 | |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1337 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1338 | static size_t bc_map_index(const BcVec *v, const void *ptr) |
| 1339 | { |
| 1340 | size_t i = bc_map_find(v, ptr); |
| 1341 | if (i >= v->len) return BC_VEC_INVALID_IDX; |
| 1342 | return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i; |
| 1343 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1344 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1345 | |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 1346 | static int bad_input_byte(char c) |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1347 | { |
| 1348 | if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'? |
| 1349 | || c > 0x7e |
| 1350 | ) { |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1351 | bc_error_fmt("illegal character 0x%02x", c); |
| 1352 | return 1; |
| 1353 | } |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1354 | return 0; |
| 1355 | } |
| 1356 | |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 1357 | // Note: it _appends_ data from the stdin to vec. |
Denys Vlasenko | 8a89247 | 2018-12-12 22:43:58 +0100 | [diff] [blame] | 1358 | static void bc_read_line(BcVec *vec) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1359 | { |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 1360 | again: |
Denys Vlasenko | 915c72b | 2018-12-13 19:28:41 +0100 | [diff] [blame] | 1361 | fflush_and_check(); |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1362 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1363 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | 915c72b | 2018-12-13 19:28:41 +0100 | [diff] [blame] | 1364 | if (G_interrupt) { // ^C was pressed |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1365 | intr: |
Denys Vlasenko | 915c72b | 2018-12-13 19:28:41 +0100 | [diff] [blame] | 1366 | G_interrupt = 0; |
| 1367 | // GNU bc says "interrupted execution." |
| 1368 | // GNU dc says "Interrupt!" |
| 1369 | fputs("\ninterrupted execution\n", stderr); |
| 1370 | } |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 1371 | |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1372 | # if ENABLE_FEATURE_EDITING |
Denys Vlasenko | 915c72b | 2018-12-13 19:28:41 +0100 | [diff] [blame] | 1373 | if (G_ttyin) { |
| 1374 | int n, i; |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1375 | # define line_buf bb_common_bufsiz1 |
Denys Vlasenko | 915c72b | 2018-12-13 19:28:41 +0100 | [diff] [blame] | 1376 | n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE); |
| 1377 | if (n <= 0) { // read errors or EOF, or ^D, or ^C |
| 1378 | if (n == 0) // ^C |
| 1379 | goto intr; |
Denys Vlasenko | e755e30 | 2018-12-13 22:25:28 +0100 | [diff] [blame] | 1380 | bc_vec_pushZeroByte(vec); |
| 1381 | return; |
Denys Vlasenko | 915c72b | 2018-12-13 19:28:41 +0100 | [diff] [blame] | 1382 | } |
| 1383 | i = 0; |
| 1384 | for (;;) { |
| 1385 | char c = line_buf[i++]; |
| 1386 | if (!c) break; |
| 1387 | if (bad_input_byte(c)) goto again; |
| 1388 | } |
| 1389 | bc_vec_concat(vec, line_buf); |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1390 | # undef line_buf |
Denys Vlasenko | 915c72b | 2018-12-13 19:28:41 +0100 | [diff] [blame] | 1391 | } else |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1392 | # endif |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1393 | #endif |
Denys Vlasenko | 915c72b | 2018-12-13 19:28:41 +0100 | [diff] [blame] | 1394 | { |
| 1395 | int c; |
| 1396 | bool bad_chars = 0; |
| 1397 | size_t len = vec->len; |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 1398 | |
Denys Vlasenko | 915c72b | 2018-12-13 19:28:41 +0100 | [diff] [blame] | 1399 | IF_FEATURE_BC_SIGNALS(errno = 0;) |
| 1400 | do { |
| 1401 | c = fgetc(stdin); |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1402 | #if ENABLE_FEATURE_BC_SIGNALS && !ENABLE_FEATURE_EDITING |
Denys Vlasenko | 915c72b | 2018-12-13 19:28:41 +0100 | [diff] [blame] | 1403 | // Both conditions appear simultaneously, check both just in case |
| 1404 | if (errno == EINTR || G_interrupt) { |
| 1405 | // ^C was pressed |
| 1406 | clearerr(stdin); |
| 1407 | goto intr; |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 1408 | } |
Denys Vlasenko | 915c72b | 2018-12-13 19:28:41 +0100 | [diff] [blame] | 1409 | #endif |
| 1410 | if (c == EOF) { |
| 1411 | if (ferror(stdin)) |
| 1412 | quit(); // this emits error message |
| 1413 | // Note: EOF does not append '\n', therefore: |
| 1414 | // printf 'print 123\n' | bc - works |
| 1415 | // printf 'print 123' | bc - fails (syntax error) |
| 1416 | break; |
| 1417 | } |
| 1418 | bad_chars |= bad_input_byte(c); |
| 1419 | bc_vec_pushByte(vec, (char)c); |
| 1420 | } while (c != '\n'); |
| 1421 | if (bad_chars) { |
| 1422 | // Bad chars on this line, ignore entire line |
| 1423 | vec->len = len; |
| 1424 | goto again; |
Denys Vlasenko | ed84935 | 2018-12-06 10:26:13 +0100 | [diff] [blame] | 1425 | } |
Denys Vlasenko | 915c72b | 2018-12-13 19:28:41 +0100 | [diff] [blame] | 1426 | bc_vec_pushZeroByte(vec); |
| 1427 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1428 | } |
| 1429 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 1430 | static char* bc_read_file(const char *path) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1431 | { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 1432 | char *buf; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 1433 | size_t size = ((size_t) -1); |
| 1434 | size_t i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1435 | |
Denys Vlasenko | 4c9455f | 2018-12-06 15:21:39 +0100 | [diff] [blame] | 1436 | // Never returns NULL (dies on errors) |
| 1437 | buf = xmalloc_xopen_read_close(path, &size); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1438 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 1439 | for (i = 0; i < size; ++i) { |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1440 | char c = buf[i]; |
| 1441 | if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'? |
| 1442 | || c > 0x7e |
| 1443 | ) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 1444 | free(buf); |
| 1445 | buf = NULL; |
| 1446 | break; |
| 1447 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1448 | } |
| 1449 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 1450 | return buf; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1451 | } |
| 1452 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1453 | static void bc_num_setToZero(BcNum *n, size_t scale) |
| 1454 | { |
| 1455 | n->len = 0; |
| 1456 | n->neg = false; |
| 1457 | n->rdx = scale; |
| 1458 | } |
| 1459 | |
| 1460 | static void bc_num_zero(BcNum *n) |
| 1461 | { |
| 1462 | bc_num_setToZero(n, 0); |
| 1463 | } |
| 1464 | |
| 1465 | static void bc_num_one(BcNum *n) |
| 1466 | { |
| 1467 | bc_num_setToZero(n, 0); |
| 1468 | n->len = 1; |
| 1469 | n->num[0] = 1; |
| 1470 | } |
| 1471 | |
| 1472 | static void bc_num_ten(BcNum *n) |
| 1473 | { |
| 1474 | bc_num_setToZero(n, 0); |
| 1475 | n->len = 2; |
| 1476 | n->num[0] = 0; |
| 1477 | n->num[1] = 1; |
| 1478 | } |
| 1479 | |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1480 | // Note: this also sets BcNum to zero |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1481 | static void bc_num_init(BcNum *n, size_t req) |
| 1482 | { |
| 1483 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1484 | //memset(n, 0, sizeof(BcNum)); - cleared by assignments below |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1485 | n->num = xmalloc(req); |
| 1486 | n->cap = req; |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1487 | n->rdx = 0; |
| 1488 | n->len = 0; |
| 1489 | n->neg = false; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1490 | } |
| 1491 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 1492 | static void bc_num_init_DEF_SIZE(BcNum *n) |
| 1493 | { |
| 1494 | bc_num_init(n, BC_NUM_DEF_SIZE); |
| 1495 | } |
| 1496 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1497 | static void bc_num_expand(BcNum *n, size_t req) |
| 1498 | { |
| 1499 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
| 1500 | if (req > n->cap) { |
| 1501 | n->num = xrealloc(n->num, req); |
| 1502 | n->cap = req; |
| 1503 | } |
| 1504 | } |
| 1505 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1506 | static FAST_FUNC void bc_num_free(void *num) |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1507 | { |
| 1508 | free(((BcNum *) num)->num); |
| 1509 | } |
| 1510 | |
| 1511 | static void bc_num_copy(BcNum *d, BcNum *s) |
| 1512 | { |
| 1513 | if (d != s) { |
| 1514 | bc_num_expand(d, s->cap); |
| 1515 | d->len = s->len; |
| 1516 | d->neg = s->neg; |
| 1517 | d->rdx = s->rdx; |
| 1518 | memcpy(d->num, s->num, sizeof(BcDig) * d->len); |
| 1519 | } |
| 1520 | } |
| 1521 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1522 | static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p) |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1523 | { |
| 1524 | size_t i; |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1525 | unsigned long pow, result; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1526 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1527 | if (n->neg) RETURN_STATUS(bc_error("negative number")); |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1528 | |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1529 | for (result = 0, pow = 1, i = n->rdx; i < n->len; ++i) { |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1530 | |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1531 | unsigned long prev = result, powprev = pow; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1532 | |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1533 | result += ((unsigned long) n->num[i]) * pow; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1534 | pow *= 10; |
| 1535 | |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1536 | if (result < prev || pow < powprev) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1537 | RETURN_STATUS(bc_error("overflow")); |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1538 | prev = result; |
| 1539 | powprev = pow; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1540 | } |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1541 | *result_p = result; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1542 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1543 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1544 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 1545 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1546 | # define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 1547 | #endif |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1548 | |
| 1549 | static void bc_num_ulong2num(BcNum *n, unsigned long val) |
| 1550 | { |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1551 | BcDig *ptr; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1552 | |
| 1553 | bc_num_zero(n); |
| 1554 | |
| 1555 | if (val == 0) return; |
| 1556 | |
Denys Vlasenko | b696d9e | 2018-12-10 12:22:15 +0100 | [diff] [blame] | 1557 | if (ULONG_MAX == 0xffffffffUL) |
| 1558 | bc_num_expand(n, 10); // 10 digits: 4294967295 |
Denys Vlasenko | c665c18 | 2018-12-10 15:15:42 +0100 | [diff] [blame] | 1559 | if (ULONG_MAX == 0xffffffffffffffffULL) |
Denys Vlasenko | b696d9e | 2018-12-10 12:22:15 +0100 | [diff] [blame] | 1560 | bc_num_expand(n, 20); // 20 digits: 18446744073709551615 |
Denys Vlasenko | c665c18 | 2018-12-10 15:15:42 +0100 | [diff] [blame] | 1561 | BUILD_BUG_ON(ULONG_MAX > 0xffffffffffffffffULL); |
Denys Vlasenko | b696d9e | 2018-12-10 12:22:15 +0100 | [diff] [blame] | 1562 | |
| 1563 | ptr = n->num; |
| 1564 | for (;;) { |
| 1565 | n->len++; |
| 1566 | *ptr++ = val % 10; |
| 1567 | val /= 10; |
| 1568 | if (val == 0) break; |
| 1569 | } |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1570 | } |
| 1571 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1572 | static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1573 | size_t len) |
| 1574 | { |
| 1575 | size_t i, j; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1576 | for (i = 0; i < len; ++i) { |
| 1577 | for (a[i] -= b[i], j = 0; a[i + j] < 0;) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1578 | a[i + j++] += 10; |
| 1579 | a[i + j] -= 1; |
| 1580 | } |
| 1581 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len) |
| 1585 | { |
| 1586 | size_t i; |
| 1587 | int c = 0; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1588 | for (i = len - 1; i < len && !(c = a[i] - b[i]); --i); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1589 | return BC_NUM_NEG(i + 1, c < 0); |
| 1590 | } |
| 1591 | |
| 1592 | static ssize_t bc_num_cmp(BcNum *a, BcNum *b) |
| 1593 | { |
| 1594 | size_t i, min, a_int, b_int, diff; |
| 1595 | BcDig *max_num, *min_num; |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1596 | bool a_max, neg; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1597 | ssize_t cmp; |
| 1598 | |
| 1599 | if (a == b) return 0; |
| 1600 | if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg); |
| 1601 | if (b->len == 0) return BC_NUM_NEG(1, a->neg); |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1602 | |
| 1603 | if (a->neg != b->neg) // signs of a and b differ |
| 1604 | // +a,-b = a>b = 1 or -a,+b = a<b = -1 |
| 1605 | return (int)b->neg - (int)a->neg; |
| 1606 | neg = a->neg; // 1 if both negative, 0 if both positive |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1607 | |
| 1608 | a_int = BC_NUM_INT(a); |
| 1609 | b_int = BC_NUM_INT(b); |
| 1610 | a_int -= b_int; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1611 | |
| 1612 | if (a_int != 0) return (ssize_t) a_int; |
| 1613 | |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1614 | a_max = (a->rdx > b->rdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1615 | if (a_max) { |
| 1616 | min = b->rdx; |
| 1617 | diff = a->rdx - b->rdx; |
| 1618 | max_num = a->num + diff; |
| 1619 | min_num = b->num; |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1620 | // neg = (a_max == neg); - NOP (maps 1->1 and 0->0) |
| 1621 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1622 | min = a->rdx; |
| 1623 | diff = b->rdx - a->rdx; |
| 1624 | max_num = b->num + diff; |
| 1625 | min_num = a->num; |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1626 | neg = !neg; // same as "neg = (a_max == neg)" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | cmp = bc_num_compare(max_num, min_num, b_int + min); |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1630 | if (cmp != 0) return BC_NUM_NEG(cmp, neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1631 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1632 | for (max_num -= diff, i = diff - 1; i < diff; --i) { |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1633 | if (max_num[i]) return BC_NUM_NEG(1, neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1634 | } |
| 1635 | |
| 1636 | return 0; |
| 1637 | } |
| 1638 | |
| 1639 | static void bc_num_truncate(BcNum *n, size_t places) |
| 1640 | { |
| 1641 | if (places == 0) return; |
| 1642 | |
| 1643 | n->rdx -= places; |
| 1644 | |
| 1645 | if (n->len != 0) { |
| 1646 | n->len -= places; |
| 1647 | memmove(n->num, n->num + places, n->len * sizeof(BcDig)); |
| 1648 | } |
| 1649 | } |
| 1650 | |
| 1651 | static void bc_num_extend(BcNum *n, size_t places) |
| 1652 | { |
| 1653 | size_t len = n->len + places; |
| 1654 | |
| 1655 | if (places != 0) { |
| 1656 | |
| 1657 | if (n->cap < len) bc_num_expand(n, len); |
| 1658 | |
| 1659 | memmove(n->num + places, n->num, sizeof(BcDig) * n->len); |
| 1660 | memset(n->num, 0, sizeof(BcDig) * places); |
| 1661 | |
| 1662 | n->len += places; |
| 1663 | n->rdx += places; |
| 1664 | } |
| 1665 | } |
| 1666 | |
| 1667 | static void bc_num_clean(BcNum *n) |
| 1668 | { |
| 1669 | while (n->len > 0 && n->num[n->len - 1] == 0) --n->len; |
| 1670 | if (n->len == 0) |
| 1671 | n->neg = false; |
| 1672 | else if (n->len < n->rdx) |
| 1673 | n->len = n->rdx; |
| 1674 | } |
| 1675 | |
| 1676 | static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2) |
| 1677 | { |
| 1678 | if (n->rdx < scale) |
| 1679 | bc_num_extend(n, scale - n->rdx); |
| 1680 | else |
| 1681 | bc_num_truncate(n, n->rdx - scale); |
| 1682 | |
| 1683 | bc_num_clean(n); |
| 1684 | if (n->len != 0) n->neg = !neg1 != !neg2; |
| 1685 | } |
| 1686 | |
| 1687 | static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a, |
| 1688 | BcNum *restrict b) |
| 1689 | { |
| 1690 | if (idx < n->len) { |
| 1691 | |
| 1692 | b->len = n->len - idx; |
| 1693 | a->len = idx; |
| 1694 | a->rdx = b->rdx = 0; |
| 1695 | |
| 1696 | memcpy(b->num, n->num + idx, b->len * sizeof(BcDig)); |
| 1697 | memcpy(a->num, n->num, idx * sizeof(BcDig)); |
| 1698 | } |
| 1699 | else { |
| 1700 | bc_num_zero(b); |
| 1701 | bc_num_copy(a, n); |
| 1702 | } |
| 1703 | |
| 1704 | bc_num_clean(a); |
| 1705 | bc_num_clean(b); |
| 1706 | } |
| 1707 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1708 | static BC_STATUS zbc_num_shift(BcNum *n, size_t places) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1709 | { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1710 | if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 1711 | |
| 1712 | // This check makes sense only if size_t is (much) larger than BC_MAX_NUM. |
| 1713 | if (SIZE_MAX > (BC_MAX_NUM | 0xff)) { |
| 1714 | if (places + n->len > BC_MAX_NUM) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1715 | RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]")); |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 1716 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1717 | |
| 1718 | if (n->rdx >= places) |
| 1719 | n->rdx -= places; |
| 1720 | else { |
| 1721 | bc_num_extend(n, places - n->rdx); |
| 1722 | n->rdx = 0; |
| 1723 | } |
| 1724 | |
| 1725 | bc_num_clean(n); |
| 1726 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1727 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1728 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 1729 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1730 | # define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 1731 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1732 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1733 | static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1734 | { |
| 1735 | BcNum one; |
| 1736 | BcDig num[2]; |
| 1737 | |
| 1738 | one.cap = 2; |
| 1739 | one.num = num; |
| 1740 | bc_num_one(&one); |
| 1741 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1742 | RETURN_STATUS(zbc_num_div(&one, a, b, scale)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1743 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1744 | #if ERRORS_ARE_FATAL |
| 1745 | # define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 1746 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1747 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1748 | static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1749 | { |
| 1750 | BcDig *ptr, *ptr_a, *ptr_b, *ptr_c; |
| 1751 | size_t i, max, min_rdx, min_int, diff, a_int, b_int; |
| 1752 | int carry, in; |
| 1753 | |
| 1754 | // Because this function doesn't need to use scale (per the bc spec), |
| 1755 | // I am hijacking it to say whether it's doing an add or a subtract. |
| 1756 | |
| 1757 | if (a->len == 0) { |
| 1758 | bc_num_copy(c, b); |
| 1759 | if (sub && c->len) c->neg = !c->neg; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1760 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1761 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1762 | if (b->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1763 | bc_num_copy(c, a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1764 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1765 | } |
| 1766 | |
| 1767 | c->neg = a->neg; |
| 1768 | c->rdx = BC_MAX(a->rdx, b->rdx); |
| 1769 | min_rdx = BC_MIN(a->rdx, b->rdx); |
| 1770 | c->len = 0; |
| 1771 | |
| 1772 | if (a->rdx > b->rdx) { |
| 1773 | diff = a->rdx - b->rdx; |
| 1774 | ptr = a->num; |
| 1775 | ptr_a = a->num + diff; |
| 1776 | ptr_b = b->num; |
| 1777 | } |
| 1778 | else { |
| 1779 | diff = b->rdx - a->rdx; |
| 1780 | ptr = b->num; |
| 1781 | ptr_a = a->num; |
| 1782 | ptr_b = b->num + diff; |
| 1783 | } |
| 1784 | |
| 1785 | for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i]; |
| 1786 | |
| 1787 | ptr_c += diff; |
| 1788 | a_int = BC_NUM_INT(a); |
| 1789 | b_int = BC_NUM_INT(b); |
| 1790 | |
| 1791 | if (a_int > b_int) { |
| 1792 | min_int = b_int; |
| 1793 | max = a_int; |
| 1794 | ptr = ptr_a; |
| 1795 | } |
| 1796 | else { |
| 1797 | min_int = a_int; |
| 1798 | max = b_int; |
| 1799 | ptr = ptr_b; |
| 1800 | } |
| 1801 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1802 | for (carry = 0, i = 0; i < min_rdx + min_int; ++i, ++c->len) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1803 | in = ((int) ptr_a[i]) + ((int) ptr_b[i]) + carry; |
| 1804 | carry = in / 10; |
| 1805 | ptr_c[i] = (BcDig)(in % 10); |
| 1806 | } |
| 1807 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1808 | for (; i < max + min_rdx; ++i, ++c->len) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1809 | in = ((int) ptr[i]) + carry; |
| 1810 | carry = in / 10; |
| 1811 | ptr_c[i] = (BcDig)(in % 10); |
| 1812 | } |
| 1813 | |
| 1814 | if (carry != 0) c->num[c->len++] = (BcDig) carry; |
| 1815 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1816 | RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary() |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1817 | } |
| 1818 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1819 | static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1820 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1821 | ssize_t cmp; |
| 1822 | BcNum *minuend, *subtrahend; |
| 1823 | size_t start; |
| 1824 | bool aneg, bneg, neg; |
| 1825 | |
| 1826 | // Because this function doesn't need to use scale (per the bc spec), |
| 1827 | // I am hijacking it to say whether it's doing an add or a subtract. |
| 1828 | |
| 1829 | if (a->len == 0) { |
| 1830 | bc_num_copy(c, b); |
| 1831 | if (sub && c->len) c->neg = !c->neg; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1832 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1833 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1834 | if (b->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1835 | bc_num_copy(c, a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1836 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | aneg = a->neg; |
| 1840 | bneg = b->neg; |
| 1841 | a->neg = b->neg = false; |
| 1842 | |
| 1843 | cmp = bc_num_cmp(a, b); |
| 1844 | |
| 1845 | a->neg = aneg; |
| 1846 | b->neg = bneg; |
| 1847 | |
| 1848 | if (cmp == 0) { |
| 1849 | bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx)); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1850 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1851 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1852 | if (cmp > 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1853 | neg = a->neg; |
| 1854 | minuend = a; |
| 1855 | subtrahend = b; |
| 1856 | } |
| 1857 | else { |
| 1858 | neg = b->neg; |
| 1859 | if (sub) neg = !neg; |
| 1860 | minuend = b; |
| 1861 | subtrahend = a; |
| 1862 | } |
| 1863 | |
| 1864 | bc_num_copy(c, minuend); |
| 1865 | c->neg = neg; |
| 1866 | |
| 1867 | if (c->rdx < subtrahend->rdx) { |
| 1868 | bc_num_extend(c, subtrahend->rdx - c->rdx); |
| 1869 | start = 0; |
| 1870 | } |
| 1871 | else |
| 1872 | start = c->rdx - subtrahend->rdx; |
| 1873 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1874 | bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1875 | |
| 1876 | bc_num_clean(c); |
| 1877 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1878 | RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary() |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1879 | } |
| 1880 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1881 | static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1882 | BcNum *restrict c) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1883 | #if ERRORS_ARE_FATAL |
| 1884 | # define zbc_num_k(...) (zbc_num_k(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 1885 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1886 | { |
| 1887 | BcStatus s; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1888 | size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1889 | BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1890 | bool aone; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1891 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1892 | if (a->len == 0 || b->len == 0) { |
| 1893 | bc_num_zero(c); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1894 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1895 | } |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1896 | aone = BC_NUM_ONE(a); |
| 1897 | if (aone || BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1898 | bc_num_copy(c, aone ? b : a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1899 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1900 | } |
| 1901 | |
| 1902 | if (a->len + b->len < BC_NUM_KARATSUBA_LEN || |
| 1903 | a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN) |
| 1904 | { |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1905 | size_t i, j, len; |
Denys Vlasenko | b3cb901 | 2018-12-05 19:05:32 +0100 | [diff] [blame] | 1906 | unsigned carry; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1907 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1908 | bc_num_expand(c, a->len + b->len + 1); |
| 1909 | |
| 1910 | memset(c->num, 0, sizeof(BcDig) * c->cap); |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1911 | c->len = len = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1912 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1913 | for (i = 0; i < b->len; ++i) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1914 | |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1915 | carry = 0; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1916 | for (j = 0; j < a->len; ++j) { |
Denys Vlasenko | b3cb901 | 2018-12-05 19:05:32 +0100 | [diff] [blame] | 1917 | unsigned in = c->num[i + j]; |
| 1918 | in += ((unsigned) a->num[j]) * ((unsigned) b->num[i]) + carry; |
| 1919 | // note: compilers prefer _unsigned_ div/const |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1920 | carry = in / 10; |
| 1921 | c->num[i + j] = (BcDig)(in % 10); |
| 1922 | } |
| 1923 | |
| 1924 | c->num[i + j] += (BcDig) carry; |
| 1925 | len = BC_MAX(len, i + j + !!carry); |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 1926 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1927 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 1928 | // a=2^1000000 |
| 1929 | // a*a <- without check below, this will not be interruptible |
| 1930 | if (G_interrupt) return BC_STATUS_FAILURE; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1931 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | c->len = len; |
| 1935 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1936 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1937 | } |
| 1938 | |
| 1939 | bc_num_init(&l1, max); |
| 1940 | bc_num_init(&h1, max); |
| 1941 | bc_num_init(&l2, max); |
| 1942 | bc_num_init(&h2, max); |
| 1943 | bc_num_init(&m1, max); |
| 1944 | bc_num_init(&m2, max); |
| 1945 | bc_num_init(&z0, max); |
| 1946 | bc_num_init(&z1, max); |
| 1947 | bc_num_init(&z2, max); |
| 1948 | bc_num_init(&temp, max + max); |
| 1949 | |
| 1950 | bc_num_split(a, max2, &l1, &h1); |
| 1951 | bc_num_split(b, max2, &l2, &h2); |
| 1952 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1953 | s = zbc_num_add(&h1, &l1, &m1, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1954 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1955 | s = zbc_num_add(&h2, &l2, &m2, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1956 | if (s) goto err; |
| 1957 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1958 | s = zbc_num_k(&h1, &h2, &z0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1959 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1960 | s = zbc_num_k(&m1, &m2, &z1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1961 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1962 | s = zbc_num_k(&l1, &l2, &z2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1963 | if (s) goto err; |
| 1964 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1965 | s = zbc_num_sub(&z1, &z0, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1966 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1967 | s = zbc_num_sub(&temp, &z2, &z1, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1968 | if (s) goto err; |
| 1969 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1970 | s = zbc_num_shift(&z0, max2 * 2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1971 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1972 | s = zbc_num_shift(&z1, max2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1973 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1974 | s = zbc_num_add(&z0, &z1, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1975 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1976 | s = zbc_num_add(&temp, &z2, c, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1977 | |
| 1978 | err: |
| 1979 | bc_num_free(&temp); |
| 1980 | bc_num_free(&z2); |
| 1981 | bc_num_free(&z1); |
| 1982 | bc_num_free(&z0); |
| 1983 | bc_num_free(&m2); |
| 1984 | bc_num_free(&m1); |
| 1985 | bc_num_free(&h2); |
| 1986 | bc_num_free(&l2); |
| 1987 | bc_num_free(&h1); |
| 1988 | bc_num_free(&l1); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1989 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1990 | } |
| 1991 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1992 | static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1993 | { |
| 1994 | BcStatus s; |
| 1995 | BcNum cpa, cpb; |
| 1996 | size_t maxrdx = BC_MAX(a->rdx, b->rdx); |
| 1997 | |
| 1998 | scale = BC_MAX(scale, a->rdx); |
| 1999 | scale = BC_MAX(scale, b->rdx); |
| 2000 | scale = BC_MIN(a->rdx + b->rdx, scale); |
| 2001 | maxrdx = BC_MAX(maxrdx, scale); |
| 2002 | |
| 2003 | bc_num_init(&cpa, a->len); |
| 2004 | bc_num_init(&cpb, b->len); |
| 2005 | |
| 2006 | bc_num_copy(&cpa, a); |
| 2007 | bc_num_copy(&cpb, b); |
| 2008 | cpa.neg = cpb.neg = false; |
| 2009 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2010 | s = zbc_num_shift(&cpa, maxrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2011 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2012 | s = zbc_num_shift(&cpb, maxrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2013 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2014 | s = zbc_num_k(&cpa, &cpb, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2015 | if (s) goto err; |
| 2016 | |
| 2017 | maxrdx += scale; |
| 2018 | bc_num_expand(c, c->len + maxrdx); |
| 2019 | |
| 2020 | if (c->len < maxrdx) { |
| 2021 | memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig)); |
| 2022 | c->len += maxrdx; |
| 2023 | } |
| 2024 | |
| 2025 | c->rdx = maxrdx; |
| 2026 | bc_num_retireMul(c, scale, a->neg, b->neg); |
| 2027 | |
| 2028 | err: |
| 2029 | bc_num_free(&cpb); |
| 2030 | bc_num_free(&cpa); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2031 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2032 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2033 | #if ERRORS_ARE_FATAL |
| 2034 | # define zbc_num_m(...) (zbc_num_m(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 2035 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2036 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2037 | static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2038 | { |
| 2039 | BcStatus s = BC_STATUS_SUCCESS; |
| 2040 | BcDig *n, *p, q; |
| 2041 | size_t len, end, i; |
| 2042 | BcNum cp; |
| 2043 | bool zero = true; |
| 2044 | |
| 2045 | if (b->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2046 | RETURN_STATUS(bc_error("divide by zero")); |
| 2047 | if (a->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2048 | bc_num_setToZero(c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2049 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2050 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2051 | if (BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2052 | bc_num_copy(c, a); |
| 2053 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2054 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2055 | } |
| 2056 | |
| 2057 | bc_num_init(&cp, BC_NUM_MREQ(a, b, scale)); |
| 2058 | bc_num_copy(&cp, a); |
| 2059 | len = b->len; |
| 2060 | |
| 2061 | if (len > cp.len) { |
| 2062 | bc_num_expand(&cp, len + 2); |
| 2063 | bc_num_extend(&cp, len - cp.len); |
| 2064 | } |
| 2065 | |
| 2066 | if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx); |
| 2067 | cp.rdx -= b->rdx; |
| 2068 | if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx); |
| 2069 | |
| 2070 | if (b->rdx == b->len) { |
| 2071 | for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1]; |
| 2072 | len -= i - 1; |
| 2073 | } |
| 2074 | |
| 2075 | if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1); |
| 2076 | |
| 2077 | // We want an extra zero in front to make things simpler. |
| 2078 | cp.num[cp.len++] = 0; |
| 2079 | end = cp.len - len; |
| 2080 | |
| 2081 | bc_num_expand(c, cp.len); |
| 2082 | |
| 2083 | bc_num_zero(c); |
| 2084 | memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig)); |
| 2085 | c->rdx = cp.rdx; |
| 2086 | c->len = cp.len; |
| 2087 | p = b->num; |
| 2088 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2089 | for (i = end - 1; !s && i < end; --i) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2090 | n = cp.num + i; |
| 2091 | for (q = 0; (!s && n[len] != 0) || bc_num_compare(n, p, len) >= 0; ++q) |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2092 | bc_num_subArrays(n, p, len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2093 | c->num[i] = q; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2094 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 2095 | // a=2^100000 |
| 2096 | // scale=40000 |
| 2097 | // 1/a <- without check below, this will not be interruptible |
| 2098 | if (G_interrupt) { |
| 2099 | s = BC_STATUS_FAILURE; |
| 2100 | break; |
| 2101 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2102 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2103 | } |
| 2104 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2105 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2106 | bc_num_free(&cp); |
| 2107 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2108 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2109 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2110 | #if ERRORS_ARE_FATAL |
| 2111 | # define zbc_num_d(...) (zbc_num_d(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 2112 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2113 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2114 | static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2115 | BcNum *restrict d, size_t scale, size_t ts) |
| 2116 | { |
| 2117 | BcStatus s; |
| 2118 | BcNum temp; |
| 2119 | bool neg; |
| 2120 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2121 | if (b->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2122 | RETURN_STATUS(bc_error("divide by zero")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2123 | |
| 2124 | if (a->len == 0) { |
| 2125 | bc_num_setToZero(d, ts); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2126 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2127 | } |
| 2128 | |
| 2129 | bc_num_init(&temp, d->cap); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2130 | s = zbc_num_d(a, b, c, scale); |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 2131 | if (s) goto err; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2132 | |
| 2133 | if (scale != 0) scale = ts; |
| 2134 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2135 | s = zbc_num_m(c, b, &temp, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2136 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2137 | s = zbc_num_sub(a, &temp, d, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2138 | if (s) goto err; |
| 2139 | |
| 2140 | if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx); |
| 2141 | |
| 2142 | neg = d->neg; |
| 2143 | bc_num_retireMul(d, ts, a->neg, b->neg); |
| 2144 | d->neg = neg; |
| 2145 | |
| 2146 | err: |
| 2147 | bc_num_free(&temp); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2148 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2149 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2150 | #if ERRORS_ARE_FATAL |
| 2151 | # define zbc_num_r(...) (zbc_num_r(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 2152 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2153 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2154 | static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2155 | { |
| 2156 | BcStatus s; |
| 2157 | BcNum c1; |
| 2158 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 2159 | |
| 2160 | bc_num_init(&c1, len); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2161 | s = zbc_num_r(a, b, &c1, c, scale, ts); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2162 | bc_num_free(&c1); |
| 2163 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2164 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2165 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2166 | #if ERRORS_ARE_FATAL |
| 2167 | # define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 2168 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2169 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2170 | static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2171 | { |
| 2172 | BcStatus s = BC_STATUS_SUCCESS; |
| 2173 | BcNum copy; |
| 2174 | unsigned long pow; |
| 2175 | size_t i, powrdx, resrdx; |
| 2176 | bool neg, zero; |
| 2177 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2178 | if (b->rdx) RETURN_STATUS(bc_error("non integer number")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2179 | |
| 2180 | if (b->len == 0) { |
| 2181 | bc_num_one(c); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2182 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2183 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2184 | if (a->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2185 | bc_num_setToZero(c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2186 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2187 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2188 | if (BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2189 | if (!b->neg) |
| 2190 | bc_num_copy(c, a); |
| 2191 | else |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2192 | s = zbc_num_inv(a, c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2193 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2194 | } |
| 2195 | |
| 2196 | neg = b->neg; |
| 2197 | b->neg = false; |
| 2198 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2199 | s = zbc_num_ulong(b, &pow); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2200 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2201 | |
| 2202 | bc_num_init(©, a->len); |
| 2203 | bc_num_copy(©, a); |
| 2204 | |
Denys Vlasenko | 2d615fe | 2018-12-07 16:22:45 +0100 | [diff] [blame] | 2205 | if (!neg) { |
| 2206 | if (a->rdx > scale) |
| 2207 | scale = a->rdx; |
| 2208 | if (a->rdx * pow < scale) |
| 2209 | scale = a->rdx * pow; |
| 2210 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2211 | |
| 2212 | b->neg = neg; |
| 2213 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2214 | for (powrdx = a->rdx; !(pow & 1); pow >>= 1) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2215 | powrdx <<= 1; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2216 | s = zbc_num_mul(©, ©, ©, powrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2217 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2218 | // Not needed: zbc_num_mul() has a check for ^C: |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 2219 | //if (G_interrupt) { |
| 2220 | // s = BC_STATUS_FAILURE; |
| 2221 | // goto err; |
| 2222 | //} |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2223 | } |
| 2224 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2225 | bc_num_copy(c, ©); |
| 2226 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2227 | for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2228 | |
| 2229 | powrdx <<= 1; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2230 | s = zbc_num_mul(©, ©, ©, powrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2231 | if (s) goto err; |
| 2232 | |
| 2233 | if (pow & 1) { |
| 2234 | resrdx += powrdx; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2235 | s = zbc_num_mul(c, ©, c, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2236 | if (s) goto err; |
| 2237 | } |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2238 | // Not needed: zbc_num_mul() has a check for ^C: |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 2239 | //if (G_interrupt) { |
| 2240 | // s = BC_STATUS_FAILURE; |
| 2241 | // goto err; |
| 2242 | //} |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2243 | } |
| 2244 | |
| 2245 | if (neg) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2246 | s = zbc_num_inv(c, c, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2247 | if (s) goto err; |
| 2248 | } |
| 2249 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2250 | if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale); |
| 2251 | |
| 2252 | // We can't use bc_num_clean() here. |
| 2253 | for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i]; |
| 2254 | if (zero) bc_num_setToZero(c, scale); |
| 2255 | |
| 2256 | err: |
| 2257 | bc_num_free(©); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2258 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2259 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2260 | #if ERRORS_ARE_FATAL |
| 2261 | # define zbc_num_p(...) (zbc_num_p(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 2262 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2263 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2264 | static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2265 | BcNumBinaryOp op, size_t req) |
| 2266 | { |
| 2267 | BcStatus s; |
| 2268 | BcNum num2, *ptr_a, *ptr_b; |
| 2269 | bool init = false; |
| 2270 | |
| 2271 | if (c == a) { |
| 2272 | ptr_a = &num2; |
| 2273 | memcpy(ptr_a, c, sizeof(BcNum)); |
| 2274 | init = true; |
| 2275 | } |
| 2276 | else |
| 2277 | ptr_a = a; |
| 2278 | |
| 2279 | if (c == b) { |
| 2280 | ptr_b = &num2; |
| 2281 | if (c != a) { |
| 2282 | memcpy(ptr_b, c, sizeof(BcNum)); |
| 2283 | init = true; |
| 2284 | } |
| 2285 | } |
| 2286 | else |
| 2287 | ptr_b = b; |
| 2288 | |
| 2289 | if (init) |
| 2290 | bc_num_init(c, req); |
| 2291 | else |
| 2292 | bc_num_expand(c, req); |
| 2293 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2294 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 2295 | ERROR_RETURN(s =) op(ptr_a, ptr_b, c, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2296 | |
| 2297 | if (init) bc_num_free(&num2); |
| 2298 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2299 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2300 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2301 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2302 | # define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2303 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2304 | |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2305 | static bool bc_num_strValid(const char *val, size_t base) |
| 2306 | { |
| 2307 | BcDig b; |
| 2308 | bool radix; |
| 2309 | |
| 2310 | b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A'); |
| 2311 | radix = false; |
| 2312 | for (;;) { |
| 2313 | BcDig c = *val++; |
| 2314 | if (c == '\0') |
| 2315 | break; |
| 2316 | if (c == '.') { |
| 2317 | if (radix) return false; |
| 2318 | radix = true; |
| 2319 | continue; |
| 2320 | } |
| 2321 | if (c < '0' || c >= b || (c > '9' && c < 'A')) |
| 2322 | return false; |
| 2323 | } |
| 2324 | return true; |
| 2325 | } |
| 2326 | |
| 2327 | // Note: n is already "bc_num_zero()"ed, |
| 2328 | // leading zeroes in "val" are removed |
| 2329 | static void bc_num_parseDecimal(BcNum *n, const char *val) |
| 2330 | { |
| 2331 | size_t len, i; |
| 2332 | const char *ptr; |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2333 | |
| 2334 | len = strlen(val); |
| 2335 | if (len == 0) |
| 2336 | return; |
| 2337 | |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2338 | bc_num_expand(n, len); |
| 2339 | |
| 2340 | ptr = strchr(val, '.'); |
| 2341 | |
| 2342 | n->rdx = 0; |
| 2343 | if (ptr != NULL) |
| 2344 | n->rdx = (size_t)((val + len) - (ptr + 1)); |
| 2345 | |
Denys Vlasenko | dafbc2c | 2018-12-10 15:38:52 +0100 | [diff] [blame] | 2346 | for (i = 0; val[i]; ++i) { |
| 2347 | if (val[i] != '0' && val[i] != '.') { |
| 2348 | // Not entirely zero value - convert it, and exit |
| 2349 | i = len - 1; |
| 2350 | for (;;) { |
| 2351 | n->num[n->len] = val[i] - '0'; |
| 2352 | ++n->len; |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2353 | skip_dot: |
Denys Vlasenko | 87b49be | 2018-12-14 16:24:01 +0100 | [diff] [blame] | 2354 | if (i == 0) break; |
| 2355 | if (val[--i] == '.') goto skip_dot; |
Denys Vlasenko | dafbc2c | 2018-12-10 15:38:52 +0100 | [diff] [blame] | 2356 | } |
| 2357 | break; |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2358 | } |
| 2359 | } |
Denys Vlasenko | 87b49be | 2018-12-14 16:24:01 +0100 | [diff] [blame] | 2360 | // if for() exits without hitting if(), the value is entirely zero |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2361 | } |
| 2362 | |
| 2363 | // Note: n is already "bc_num_zero()"ed, |
| 2364 | // leading zeroes in "val" are removed |
| 2365 | static void bc_num_parseBase(BcNum *n, const char *val, BcNum *base) |
| 2366 | { |
| 2367 | BcStatus s; |
| 2368 | BcNum temp, mult, result; |
| 2369 | BcDig c = '\0'; |
| 2370 | unsigned long v; |
| 2371 | size_t i, digits; |
| 2372 | |
| 2373 | for (i = 0; ; ++i) { |
| 2374 | if (val[i] == '\0') |
| 2375 | return; |
| 2376 | if (val[i] != '.' && val[i] != '0') |
| 2377 | break; |
| 2378 | } |
| 2379 | |
| 2380 | bc_num_init_DEF_SIZE(&temp); |
| 2381 | bc_num_init_DEF_SIZE(&mult); |
| 2382 | |
| 2383 | for (;;) { |
| 2384 | c = *val++; |
| 2385 | if (c == '\0') goto int_err; |
| 2386 | if (c == '.') break; |
| 2387 | |
| 2388 | v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10); |
| 2389 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2390 | s = zbc_num_mul(n, base, &mult, 0); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2391 | if (s) goto int_err; |
| 2392 | bc_num_ulong2num(&temp, v); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2393 | s = zbc_num_add(&mult, &temp, n, 0); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2394 | if (s) goto int_err; |
| 2395 | } |
| 2396 | |
| 2397 | bc_num_init(&result, base->len); |
| 2398 | //bc_num_zero(&result); - already is |
| 2399 | bc_num_one(&mult); |
| 2400 | |
| 2401 | digits = 0; |
| 2402 | for (;;) { |
| 2403 | c = *val++; |
| 2404 | if (c == '\0') break; |
| 2405 | digits++; |
| 2406 | |
| 2407 | v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10); |
| 2408 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2409 | s = zbc_num_mul(&result, base, &result, 0); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2410 | if (s) goto err; |
| 2411 | bc_num_ulong2num(&temp, v); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2412 | s = zbc_num_add(&result, &temp, &result, 0); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2413 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2414 | s = zbc_num_mul(&mult, base, &mult, 0); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2415 | if (s) goto err; |
| 2416 | } |
| 2417 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2418 | s = zbc_num_div(&result, &mult, &result, digits); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2419 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2420 | s = zbc_num_add(n, &result, n, digits); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2421 | if (s) goto err; |
| 2422 | |
| 2423 | if (n->len != 0) { |
| 2424 | if (n->rdx < digits) bc_num_extend(n, digits - n->rdx); |
| 2425 | } else |
| 2426 | bc_num_zero(n); |
| 2427 | |
| 2428 | err: |
| 2429 | bc_num_free(&result); |
| 2430 | int_err: |
| 2431 | bc_num_free(&mult); |
| 2432 | bc_num_free(&temp); |
| 2433 | } |
| 2434 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2435 | static BC_STATUS zbc_num_parse(BcNum *n, const char *val, BcNum *base, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2436 | size_t base_t) |
| 2437 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2438 | if (!bc_num_strValid(val, base_t)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2439 | RETURN_STATUS(bc_error("bad number string")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2440 | |
Denys Vlasenko | 4a024c7 | 2018-12-09 13:21:54 +0100 | [diff] [blame] | 2441 | bc_num_zero(n); |
| 2442 | while (*val == '0') val++; |
| 2443 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2444 | if (base_t == 10) |
| 2445 | bc_num_parseDecimal(n, val); |
| 2446 | else |
| 2447 | bc_num_parseBase(n, val, base); |
| 2448 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2449 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2450 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2451 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2452 | # define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2453 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2454 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2455 | static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2456 | { |
| 2457 | BcStatus s; |
| 2458 | BcNum num1, num2, half, f, fprime, *x0, *x1, *temp; |
| 2459 | size_t pow, len, digs, digs1, resrdx, req, times = 0; |
| 2460 | ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX; |
| 2461 | |
| 2462 | req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1; |
| 2463 | bc_num_expand(b, req); |
| 2464 | |
| 2465 | if (a->len == 0) { |
| 2466 | bc_num_setToZero(b, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2467 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2468 | } |
| 2469 | else if (a->neg) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2470 | RETURN_STATUS(bc_error("negative number")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2471 | else if (BC_NUM_ONE(a)) { |
| 2472 | bc_num_one(b); |
| 2473 | bc_num_extend(b, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2474 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2475 | } |
| 2476 | |
| 2477 | scale = BC_MAX(scale, a->rdx) + 1; |
| 2478 | len = a->len + scale; |
| 2479 | |
| 2480 | bc_num_init(&num1, len); |
| 2481 | bc_num_init(&num2, len); |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 2482 | bc_num_init_DEF_SIZE(&half); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2483 | |
| 2484 | bc_num_one(&half); |
| 2485 | half.num[0] = 5; |
| 2486 | half.rdx = 1; |
| 2487 | |
| 2488 | bc_num_init(&f, len); |
| 2489 | bc_num_init(&fprime, len); |
| 2490 | |
| 2491 | x0 = &num1; |
| 2492 | x1 = &num2; |
| 2493 | |
| 2494 | bc_num_one(x0); |
| 2495 | pow = BC_NUM_INT(a); |
| 2496 | |
| 2497 | if (pow) { |
| 2498 | |
| 2499 | if (pow & 1) |
| 2500 | x0->num[0] = 2; |
| 2501 | else |
| 2502 | x0->num[0] = 6; |
| 2503 | |
| 2504 | pow -= 2 - (pow & 1); |
| 2505 | |
| 2506 | bc_num_extend(x0, pow); |
| 2507 | |
| 2508 | // Make sure to move the radix back. |
| 2509 | x0->rdx -= pow; |
| 2510 | } |
| 2511 | |
| 2512 | x0->rdx = digs = digs1 = 0; |
| 2513 | resrdx = scale + 2; |
| 2514 | len = BC_NUM_INT(x0) + resrdx - 1; |
| 2515 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2516 | while (cmp != 0 || digs < len) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2517 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2518 | s = zbc_num_div(a, x0, &f, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2519 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2520 | s = zbc_num_add(x0, &f, &fprime, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2521 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2522 | s = zbc_num_mul(&fprime, &half, x1, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2523 | if (s) goto err; |
| 2524 | |
| 2525 | cmp = bc_num_cmp(x1, x0); |
| 2526 | digs = x1->len - (unsigned long long) llabs(cmp); |
| 2527 | |
| 2528 | if (cmp == cmp2 && digs == digs1) |
| 2529 | times += 1; |
| 2530 | else |
| 2531 | times = 0; |
| 2532 | |
| 2533 | resrdx += times > 4; |
| 2534 | |
| 2535 | cmp2 = cmp1; |
| 2536 | cmp1 = cmp; |
| 2537 | digs1 = digs; |
| 2538 | |
| 2539 | temp = x0; |
| 2540 | x0 = x1; |
| 2541 | x1 = temp; |
| 2542 | } |
| 2543 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2544 | bc_num_copy(b, x0); |
| 2545 | scale -= 1; |
| 2546 | if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale); |
| 2547 | |
| 2548 | err: |
| 2549 | bc_num_free(&fprime); |
| 2550 | bc_num_free(&f); |
| 2551 | bc_num_free(&half); |
| 2552 | bc_num_free(&num2); |
| 2553 | bc_num_free(&num1); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2554 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2555 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2556 | #if ERRORS_ARE_FATAL |
| 2557 | # define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 2558 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2559 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2560 | static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2561 | size_t scale) |
| 2562 | { |
| 2563 | BcStatus s; |
| 2564 | BcNum num2, *ptr_a; |
| 2565 | bool init = false; |
| 2566 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 2567 | |
| 2568 | if (c == a) { |
| 2569 | memcpy(&num2, c, sizeof(BcNum)); |
| 2570 | ptr_a = &num2; |
| 2571 | bc_num_init(c, len); |
| 2572 | init = true; |
| 2573 | } |
| 2574 | else { |
| 2575 | ptr_a = a; |
| 2576 | bc_num_expand(c, len); |
| 2577 | } |
| 2578 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2579 | s = zbc_num_r(ptr_a, b, c, d, scale, ts); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2580 | |
| 2581 | if (init) bc_num_free(&num2); |
| 2582 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2583 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2584 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2585 | #if ERRORS_ARE_FATAL |
| 2586 | # define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 2587 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2588 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2589 | #if ENABLE_DC |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2590 | static BC_STATUS zbc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2591 | { |
| 2592 | BcStatus s; |
| 2593 | BcNum base, exp, two, temp; |
| 2594 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2595 | if (c->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2596 | RETURN_STATUS(bc_error("divide by zero")); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2597 | if (a->rdx || b->rdx || c->rdx) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2598 | RETURN_STATUS(bc_error("non integer number")); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2599 | if (b->neg) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2600 | RETURN_STATUS(bc_error("negative number")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2601 | |
| 2602 | bc_num_expand(d, c->len); |
| 2603 | bc_num_init(&base, c->len); |
| 2604 | bc_num_init(&exp, b->len); |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 2605 | bc_num_init_DEF_SIZE(&two); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2606 | bc_num_init(&temp, b->len); |
| 2607 | |
| 2608 | bc_num_one(&two); |
| 2609 | two.num[0] = 2; |
| 2610 | bc_num_one(d); |
| 2611 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2612 | s = zbc_num_rem(a, c, &base, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2613 | if (s) goto err; |
| 2614 | bc_num_copy(&exp, b); |
| 2615 | |
| 2616 | while (exp.len != 0) { |
| 2617 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2618 | s = zbc_num_divmod(&exp, &two, &exp, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2619 | if (s) goto err; |
| 2620 | |
| 2621 | if (BC_NUM_ONE(&temp)) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2622 | s = zbc_num_mul(d, &base, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2623 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2624 | s = zbc_num_rem(&temp, c, d, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2625 | if (s) goto err; |
| 2626 | } |
| 2627 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2628 | s = zbc_num_mul(&base, &base, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2629 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2630 | s = zbc_num_rem(&temp, c, &base, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2631 | if (s) goto err; |
| 2632 | } |
| 2633 | |
| 2634 | err: |
| 2635 | bc_num_free(&temp); |
| 2636 | bc_num_free(&two); |
| 2637 | bc_num_free(&exp); |
| 2638 | bc_num_free(&base); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2639 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2640 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2641 | #if ERRORS_ARE_FATAL |
| 2642 | # define zbc_num_modexp(...) (zbc_num_modexp(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 2643 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2644 | #endif // ENABLE_DC |
| 2645 | |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 2646 | #if ENABLE_BC |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2647 | static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2648 | { |
| 2649 | BcId a; |
| 2650 | size_t i; |
| 2651 | |
| 2652 | for (i = 0; i < f->autos.len; ++i) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2653 | if (strcmp(name, ((BcId *) bc_vec_item(&f->autos, i))->name) == 0) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2654 | RETURN_STATUS(bc_error("function parameter or auto var has the same name as another")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2655 | } |
| 2656 | |
| 2657 | a.idx = var; |
| 2658 | a.name = name; |
| 2659 | |
| 2660 | bc_vec_push(&f->autos, &a); |
| 2661 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2662 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2663 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2664 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2665 | # define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2666 | #endif |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 2667 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2668 | |
| 2669 | static void bc_func_init(BcFunc *f) |
| 2670 | { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 2671 | bc_char_vec_init(&f->code); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2672 | bc_vec_init(&f->autos, sizeof(BcId), bc_id_free); |
| 2673 | bc_vec_init(&f->labels, sizeof(size_t), NULL); |
| 2674 | f->nparams = 0; |
| 2675 | } |
| 2676 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2677 | static FAST_FUNC void bc_func_free(void *func) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2678 | { |
| 2679 | BcFunc *f = (BcFunc *) func; |
| 2680 | bc_vec_free(&f->code); |
| 2681 | bc_vec_free(&f->autos); |
| 2682 | bc_vec_free(&f->labels); |
| 2683 | } |
| 2684 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2685 | static void bc_array_expand(BcVec *a, size_t len); |
| 2686 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2687 | static void bc_array_init(BcVec *a, bool nums) |
| 2688 | { |
| 2689 | if (nums) |
| 2690 | bc_vec_init(a, sizeof(BcNum), bc_num_free); |
| 2691 | else |
| 2692 | bc_vec_init(a, sizeof(BcVec), bc_vec_free); |
| 2693 | bc_array_expand(a, 1); |
| 2694 | } |
| 2695 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2696 | static void bc_array_expand(BcVec *a, size_t len) |
| 2697 | { |
| 2698 | BcResultData data; |
| 2699 | |
| 2700 | if (a->size == sizeof(BcNum) && a->dtor == bc_num_free) { |
| 2701 | while (len > a->len) { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 2702 | bc_num_init_DEF_SIZE(&data.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2703 | bc_vec_push(a, &data.n); |
| 2704 | } |
| 2705 | } |
| 2706 | else { |
| 2707 | while (len > a->len) { |
| 2708 | bc_array_init(&data.v, true); |
| 2709 | bc_vec_push(a, &data.v); |
| 2710 | } |
| 2711 | } |
| 2712 | } |
| 2713 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2714 | static void bc_array_copy(BcVec *d, const BcVec *s) |
| 2715 | { |
| 2716 | size_t i; |
| 2717 | |
| 2718 | bc_vec_pop_all(d); |
| 2719 | bc_vec_expand(d, s->cap); |
| 2720 | d->len = s->len; |
| 2721 | |
| 2722 | for (i = 0; i < s->len; ++i) { |
| 2723 | BcNum *dnum = bc_vec_item(d, i), *snum = bc_vec_item(s, i); |
| 2724 | bc_num_init(dnum, snum->len); |
| 2725 | bc_num_copy(dnum, snum); |
| 2726 | } |
| 2727 | } |
| 2728 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2729 | static FAST_FUNC void bc_string_free(void *string) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2730 | { |
| 2731 | free(*((char **) string)); |
| 2732 | } |
| 2733 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2734 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2735 | static void bc_result_copy(BcResult *d, BcResult *src) |
| 2736 | { |
| 2737 | d->t = src->t; |
| 2738 | |
| 2739 | switch (d->t) { |
| 2740 | |
| 2741 | case BC_RESULT_TEMP: |
| 2742 | case BC_RESULT_IBASE: |
| 2743 | case BC_RESULT_SCALE: |
| 2744 | case BC_RESULT_OBASE: |
| 2745 | { |
| 2746 | bc_num_init(&d->d.n, src->d.n.len); |
| 2747 | bc_num_copy(&d->d.n, &src->d.n); |
| 2748 | break; |
| 2749 | } |
| 2750 | |
| 2751 | case BC_RESULT_VAR: |
| 2752 | case BC_RESULT_ARRAY: |
| 2753 | case BC_RESULT_ARRAY_ELEM: |
| 2754 | { |
| 2755 | d->d.id.name = xstrdup(src->d.id.name); |
| 2756 | break; |
| 2757 | } |
| 2758 | |
| 2759 | case BC_RESULT_CONSTANT: |
| 2760 | case BC_RESULT_LAST: |
| 2761 | case BC_RESULT_ONE: |
| 2762 | case BC_RESULT_STR: |
| 2763 | { |
| 2764 | memcpy(&d->d.n, &src->d.n, sizeof(BcNum)); |
| 2765 | break; |
| 2766 | } |
| 2767 | } |
| 2768 | } |
| 2769 | #endif // ENABLE_DC |
| 2770 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2771 | static FAST_FUNC void bc_result_free(void *result) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2772 | { |
| 2773 | BcResult *r = (BcResult *) result; |
| 2774 | |
| 2775 | switch (r->t) { |
| 2776 | |
| 2777 | case BC_RESULT_TEMP: |
| 2778 | case BC_RESULT_IBASE: |
| 2779 | case BC_RESULT_SCALE: |
| 2780 | case BC_RESULT_OBASE: |
| 2781 | { |
| 2782 | bc_num_free(&r->d.n); |
| 2783 | break; |
| 2784 | } |
| 2785 | |
| 2786 | case BC_RESULT_VAR: |
| 2787 | case BC_RESULT_ARRAY: |
| 2788 | case BC_RESULT_ARRAY_ELEM: |
| 2789 | { |
| 2790 | free(r->d.id.name); |
| 2791 | break; |
| 2792 | } |
| 2793 | |
| 2794 | default: |
| 2795 | { |
| 2796 | // Do nothing. |
| 2797 | break; |
| 2798 | } |
| 2799 | } |
| 2800 | } |
| 2801 | |
| 2802 | static void bc_lex_lineComment(BcLex *l) |
| 2803 | { |
| 2804 | l->t.t = BC_LEX_WHITESPACE; |
| 2805 | while (l->i < l->len && l->buf[l->i++] != '\n'); |
| 2806 | --l->i; |
| 2807 | } |
| 2808 | |
| 2809 | static void bc_lex_whitespace(BcLex *l) |
| 2810 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2811 | l->t.t = BC_LEX_WHITESPACE; |
Denys Vlasenko | 89198a9 | 2018-12-13 21:31:29 +0100 | [diff] [blame] | 2812 | for (;;) { |
| 2813 | char c = l->buf[l->i]; |
| 2814 | if (c == '\n' || !isspace(c)) |
| 2815 | break; |
| 2816 | l->i++; |
| 2817 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2818 | } |
| 2819 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2820 | static BC_STATUS zbc_lex_number(BcLex *l, char start) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2821 | { |
| 2822 | const char *buf = l->buf + l->i; |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2823 | size_t len, bslashes, i, ccnt; |
| 2824 | bool pt; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2825 | |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2826 | pt = (start == '.'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2827 | l->t.t = BC_LEX_NUMBER; |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2828 | bslashes = 0; |
| 2829 | ccnt = i = 0; |
| 2830 | for (;;) { |
| 2831 | char c = buf[i]; |
| 2832 | if (c == '\0') |
| 2833 | break; |
| 2834 | if (c == '\\' && buf[i + 1] == '\n') { |
| 2835 | i += 2; |
| 2836 | bslashes++; |
| 2837 | continue; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2838 | } |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2839 | if (!isdigit(c) && (c < 'A' || c > 'F')) { |
| 2840 | if (c != '.') break; |
| 2841 | // if '.' was already seen, stop on second one: |
| 2842 | if (pt) break; |
| 2843 | pt = 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2844 | } |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2845 | // buf[i] is one of "0-9A-F." |
| 2846 | i++; |
| 2847 | if (c != '.') |
| 2848 | ccnt = i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2849 | } |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2850 | //i is buf[i] index of the first not-yet-parsed char |
| 2851 | l->i += i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2852 | |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2853 | //ccnt is the number of chars in the number string, excluding possible |
| 2854 | //trailing "." and possible following trailing "\<newline>"(s). |
| 2855 | len = ccnt - bslashes * 2 + 1; // +1 byte for NUL termination |
| 2856 | |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 2857 | // This check makes sense only if size_t is (much) larger than BC_MAX_NUM. |
| 2858 | if (SIZE_MAX > (BC_MAX_NUM | 0xff)) { |
| 2859 | if (len > BC_MAX_NUM) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2860 | RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]")); |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 2861 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2862 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 2863 | bc_vec_pop_all(&l->t.v); |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2864 | bc_vec_expand(&l->t.v, 1 + len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2865 | bc_vec_push(&l->t.v, &start); |
| 2866 | |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2867 | while (ccnt != 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2868 | // If we have hit a backslash, skip it. We don't have |
| 2869 | // to check for a newline because it's guaranteed. |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2870 | if (*buf == '\\') { |
| 2871 | buf += 2; |
| 2872 | ccnt -= 2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2873 | continue; |
| 2874 | } |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2875 | bc_vec_push(&l->t.v, buf); |
| 2876 | buf++; |
| 2877 | ccnt--; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2878 | } |
| 2879 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 2880 | bc_vec_pushZeroByte(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2881 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2882 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2883 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2884 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2885 | # define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2886 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2887 | |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 2888 | static void bc_lex_name(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2889 | { |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 2890 | size_t i; |
| 2891 | const char *buf; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2892 | |
| 2893 | l->t.t = BC_LEX_NAME; |
| 2894 | |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 2895 | i = 0; |
| 2896 | buf = l->buf + l->i - 1; |
| 2897 | for (;;) { |
| 2898 | char c = buf[i]; |
| 2899 | if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break; |
| 2900 | i++; |
| 2901 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2902 | |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 2903 | #if 0 // We do not protect against people with gigabyte-long names |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 2904 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
| 2905 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
| 2906 | if (i > BC_MAX_STRING) |
| 2907 | return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]"); |
| 2908 | } |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 2909 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2910 | bc_vec_string(&l->t.v, i, buf); |
| 2911 | |
| 2912 | // Increment the index. We minus 1 because it has already been incremented. |
| 2913 | l->i += i - 1; |
| 2914 | |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 2915 | //return BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2916 | } |
| 2917 | |
Denys Vlasenko | c2265f5 | 2018-12-12 23:10:08 +0100 | [diff] [blame] | 2918 | static void bc_lex_init(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2919 | { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 2920 | bc_char_vec_init(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2921 | } |
| 2922 | |
| 2923 | static void bc_lex_free(BcLex *l) |
| 2924 | { |
| 2925 | bc_vec_free(&l->t.v); |
| 2926 | } |
| 2927 | |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 2928 | static void bc_lex_file(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2929 | { |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 2930 | G.err_line = l->line = 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2931 | l->newline = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2932 | } |
| 2933 | |
Denys Vlasenko | e755e30 | 2018-12-13 22:25:28 +0100 | [diff] [blame] | 2934 | IF_BC(static BC_STATUS zbc_lex_token(BcLex *l);) |
| 2935 | IF_DC(static BC_STATUS zdc_lex_token(BcLex *l);) |
Denys Vlasenko | c2265f5 | 2018-12-12 23:10:08 +0100 | [diff] [blame] | 2936 | |
| 2937 | static BC_STATUS zcommon_lex_token(BcLex *l) |
| 2938 | { |
| 2939 | if (IS_BC) { |
| 2940 | IF_BC(RETURN_STATUS(zbc_lex_token(l));) |
| 2941 | } |
| 2942 | IF_DC(RETURN_STATUS(zdc_lex_token(l));) |
| 2943 | } |
| 2944 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 2945 | static BC_STATUS zbc_lex_next(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2946 | { |
| 2947 | BcStatus s; |
| 2948 | |
| 2949 | l->t.last = l->t.t; |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 2950 | if (l->t.last == BC_LEX_EOF) RETURN_STATUS(bc_error("end of file")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2951 | |
| 2952 | l->line += l->newline; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 2953 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2954 | l->t.t = BC_LEX_EOF; |
| 2955 | |
| 2956 | l->newline = (l->i == l->len); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 2957 | if (l->newline) RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2958 | |
| 2959 | // Loop until failure or we don't have whitespace. This |
| 2960 | // is so the parser doesn't get inundated with whitespace. |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 2961 | s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2962 | do { |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 2963 | dbg_lex("next string to parse:'%.*s'", |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 2964 | (int)(strchrnul(l->buf + l->i, '\n') - (l->buf + l->i)), |
| 2965 | l->buf + l->i); |
Denys Vlasenko | c2265f5 | 2018-12-12 23:10:08 +0100 | [diff] [blame] | 2966 | ERROR_RETURN(s =) zcommon_lex_token(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2967 | } while (!s && l->t.t == BC_LEX_WHITESPACE); |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame^] | 2968 | dbg_lex("l->t.t from string:%d", l->t.t); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2969 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 2970 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2971 | } |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 2972 | #if ERRORS_ARE_FATAL |
| 2973 | # define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 2974 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2975 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 2976 | static BC_STATUS zbc_lex_text_init(BcLex *l, const char *text) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2977 | { |
| 2978 | l->buf = text; |
| 2979 | l->i = 0; |
| 2980 | l->len = strlen(text); |
| 2981 | l->t.t = l->t.last = BC_LEX_INVALID; |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 2982 | RETURN_STATUS(zbc_lex_next(l)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2983 | } |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 2984 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 2985 | # define zbc_lex_text_init(...) (zbc_lex_text_init(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 2986 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2987 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2988 | #if ENABLE_BC |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 2989 | static BC_STATUS zbc_lex_identifier(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2990 | { |
| 2991 | BcStatus s; |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 2992 | unsigned i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2993 | const char *buf = l->buf + l->i - 1; |
| 2994 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 2995 | for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) { |
| 2996 | const char *keyword8 = bc_lex_kws[i].name8; |
| 2997 | unsigned j = 0; |
| 2998 | while (buf[j] != '\0' && buf[j] == keyword8[j]) { |
| 2999 | j++; |
| 3000 | if (j == 8) goto match; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3001 | } |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3002 | if (keyword8[j] != '\0') |
| 3003 | continue; |
| 3004 | match: |
| 3005 | // buf starts with keyword bc_lex_kws[i] |
| 3006 | l->t.t = BC_LEX_KEY_1st_keyword + i; |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 3007 | if (!bc_lex_kws_POSIX(i)) { |
Denys Vlasenko | 0d7e46b | 2018-12-05 18:31:19 +0100 | [diff] [blame] | 3008 | s = bc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3009 | ERROR_RETURN(if (s) RETURN_STATUS(s);) |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3010 | } |
| 3011 | |
| 3012 | // We minus 1 because the index has already been incremented. |
| 3013 | l->i += j - 1; |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3014 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3015 | } |
| 3016 | |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 3017 | bc_lex_name(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3018 | |
Denys Vlasenko | 0d7e46b | 2018-12-05 18:31:19 +0100 | [diff] [blame] | 3019 | if (l->t.v.len > 2) { |
| 3020 | // Prevent this: |
| 3021 | // >>> qwe=1 |
| 3022 | // bc: POSIX only allows one character names; the following is bad: 'qwe=1 |
| 3023 | // ' |
| 3024 | unsigned len = strchrnul(buf, '\n') - buf; |
| 3025 | s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf); |
| 3026 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3027 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3028 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3029 | } |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3030 | #if ERRORS_ARE_FATAL |
| 3031 | # define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 3032 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3033 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3034 | static BC_STATUS zbc_lex_string(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3035 | { |
| 3036 | size_t len, nls = 0, i = l->i; |
| 3037 | char c; |
| 3038 | |
| 3039 | l->t.t = BC_LEX_STR; |
| 3040 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3041 | for (c = l->buf[i]; c != 0 && c != '"'; c = l->buf[++i]) |
| 3042 | nls += (c == '\n'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3043 | |
| 3044 | if (c == '\0') { |
| 3045 | l->i = i; |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3046 | RETURN_STATUS(bc_error("string end could not be found")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3047 | } |
| 3048 | |
| 3049 | len = i - l->i; |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 3050 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
| 3051 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
| 3052 | if (len > BC_MAX_STRING) |
Denys Vlasenko | 9811ad0 | 2018-12-12 23:25:13 +0100 | [diff] [blame] | 3053 | RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]")); |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 3054 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3055 | bc_vec_string(&l->t.v, len, l->buf + l->i); |
| 3056 | |
| 3057 | l->i = i + 1; |
| 3058 | l->line += nls; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3059 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3060 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3061 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3062 | } |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3063 | #if ERRORS_ARE_FATAL |
| 3064 | # define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 3065 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3066 | |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 3067 | static void bc_lex_assign(BcLex *l, unsigned with_and_without) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3068 | { |
| 3069 | if (l->buf[l->i] == '=') { |
| 3070 | ++l->i; |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 3071 | with_and_without >>= 8; // store "with" value |
| 3072 | } // else store "without" value |
| 3073 | l->t.t = (with_and_without & 0xff); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3074 | } |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 3075 | #define bc_lex_assign(l, with, without) \ |
| 3076 | bc_lex_assign(l, ((with)<<8)|(without)) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3077 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3078 | static BC_STATUS zbc_lex_comment(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3079 | { |
| 3080 | size_t i, nls = 0; |
| 3081 | const char *buf = l->buf; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3082 | |
| 3083 | l->t.t = BC_LEX_WHITESPACE; |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3084 | i = ++l->i; |
| 3085 | for (;;) { |
| 3086 | char c = buf[i]; |
| 3087 | check_star: |
| 3088 | if (c == '*') { |
| 3089 | c = buf[++i]; |
| 3090 | if (c == '/') |
| 3091 | break; |
| 3092 | goto check_star; |
| 3093 | } |
| 3094 | if (c == '\0') { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3095 | l->i = i; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3096 | RETURN_STATUS(bc_error("comment end could not be found")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3097 | } |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3098 | nls += (c == '\n'); |
| 3099 | i++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3100 | } |
| 3101 | |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3102 | l->i = i + 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3103 | l->line += nls; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3104 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3105 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3106 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3107 | } |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 3108 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3109 | # define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 3110 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3111 | |
Denys Vlasenko | c2265f5 | 2018-12-12 23:10:08 +0100 | [diff] [blame] | 3112 | static BC_STATUS zbc_lex_token(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3113 | { |
| 3114 | BcStatus s = BC_STATUS_SUCCESS; |
| 3115 | char c = l->buf[l->i++], c2; |
| 3116 | |
| 3117 | // This is the workhorse of the lexer. |
| 3118 | switch (c) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3119 | case '\0': |
| 3120 | case '\n': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3121 | l->newline = true; |
| 3122 | l->t.t = !c ? BC_LEX_EOF : BC_LEX_NLINE; |
| 3123 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3124 | case '\t': |
| 3125 | case '\v': |
| 3126 | case '\f': |
| 3127 | case '\r': |
| 3128 | case ' ': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3129 | bc_lex_whitespace(l); |
| 3130 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3131 | case '!': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3132 | bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3133 | if (l->t.t == BC_LEX_OP_BOOL_NOT) { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3134 | s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!"); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3135 | ERROR_RETURN(if (s) RETURN_STATUS(s);) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3136 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3137 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3138 | case '"': |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3139 | s = zbc_lex_string(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3140 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3141 | case '#': |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3142 | s = bc_POSIX_does_not_allow("'#' script comments"); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3143 | ERROR_RETURN(if (s) RETURN_STATUS(s);) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3144 | bc_lex_lineComment(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3145 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3146 | case '%': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3147 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS); |
| 3148 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3149 | case '&': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3150 | c2 = l->buf[l->i]; |
| 3151 | if (c2 == '&') { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3152 | s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&"); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3153 | ERROR_RETURN(if (s) RETURN_STATUS(s);) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3154 | ++l->i; |
| 3155 | l->t.t = BC_LEX_OP_BOOL_AND; |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3156 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3157 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3158 | s = bc_error_bad_character('&'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3159 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3160 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3161 | case '(': |
| 3162 | case ')': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3163 | l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN); |
| 3164 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3165 | case '*': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3166 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY); |
| 3167 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3168 | case '+': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3169 | c2 = l->buf[l->i]; |
| 3170 | if (c2 == '+') { |
| 3171 | ++l->i; |
| 3172 | l->t.t = BC_LEX_OP_INC; |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3173 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3174 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS); |
| 3175 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3176 | case ',': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3177 | l->t.t = BC_LEX_COMMA; |
| 3178 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3179 | case '-': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3180 | c2 = l->buf[l->i]; |
| 3181 | if (c2 == '-') { |
| 3182 | ++l->i; |
| 3183 | l->t.t = BC_LEX_OP_DEC; |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3184 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3185 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS); |
| 3186 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3187 | case '.': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3188 | if (isdigit(l->buf[l->i])) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3189 | s = zbc_lex_number(l, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3190 | else { |
| 3191 | l->t.t = BC_LEX_KEY_LAST; |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3192 | s = bc_POSIX_does_not_allow("a period ('.') as a shortcut for the last result"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3193 | } |
| 3194 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3195 | case '/': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3196 | c2 = l->buf[l->i]; |
| 3197 | if (c2 == '*') |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3198 | s = zbc_lex_comment(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3199 | else |
| 3200 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE); |
| 3201 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3202 | case '0': |
| 3203 | case '1': |
| 3204 | case '2': |
| 3205 | case '3': |
| 3206 | case '4': |
| 3207 | case '5': |
| 3208 | case '6': |
| 3209 | case '7': |
| 3210 | case '8': |
| 3211 | case '9': |
| 3212 | case 'A': |
| 3213 | case 'B': |
| 3214 | case 'C': |
| 3215 | case 'D': |
| 3216 | case 'E': |
| 3217 | case 'F': |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3218 | s = zbc_lex_number(l, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3219 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3220 | case ';': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3221 | l->t.t = BC_LEX_SCOLON; |
| 3222 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3223 | case '<': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3224 | bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT); |
| 3225 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3226 | case '=': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3227 | bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN); |
| 3228 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3229 | case '>': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3230 | bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT); |
| 3231 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3232 | case '[': |
| 3233 | case ']': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3234 | l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET); |
| 3235 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3236 | case '\\': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3237 | if (l->buf[l->i] == '\n') { |
| 3238 | l->t.t = BC_LEX_WHITESPACE; |
| 3239 | ++l->i; |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3240 | } else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3241 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3242 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3243 | case '^': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3244 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER); |
| 3245 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3246 | case 'a': |
| 3247 | case 'b': |
| 3248 | case 'c': |
| 3249 | case 'd': |
| 3250 | case 'e': |
| 3251 | case 'f': |
| 3252 | case 'g': |
| 3253 | case 'h': |
| 3254 | case 'i': |
| 3255 | case 'j': |
| 3256 | case 'k': |
| 3257 | case 'l': |
| 3258 | case 'm': |
| 3259 | case 'n': |
| 3260 | case 'o': |
| 3261 | case 'p': |
| 3262 | case 'q': |
| 3263 | case 'r': |
| 3264 | case 's': |
| 3265 | case 't': |
| 3266 | case 'u': |
| 3267 | case 'v': |
| 3268 | case 'w': |
| 3269 | case 'x': |
| 3270 | case 'y': |
| 3271 | case 'z': |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3272 | s = zbc_lex_identifier(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3273 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3274 | case '{': |
| 3275 | case '}': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3276 | l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE); |
| 3277 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3278 | case '|': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3279 | c2 = l->buf[l->i]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3280 | if (c2 == '|') { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3281 | s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||"); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3282 | ERROR_RETURN(if (s) RETURN_STATUS(s);) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3283 | ++l->i; |
| 3284 | l->t.t = BC_LEX_OP_BOOL_OR; |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3285 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3286 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3287 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3288 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3289 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3290 | default: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3291 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3292 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3293 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3294 | } |
| 3295 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3296 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3297 | } |
| 3298 | #endif // ENABLE_BC |
| 3299 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 3300 | #if ENABLE_DC |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3301 | static BC_STATUS zdc_lex_register(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3302 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3303 | if (isspace(l->buf[l->i - 1])) { |
| 3304 | bc_lex_whitespace(l); |
| 3305 | ++l->i; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 3306 | if (!G_exreg) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3307 | RETURN_STATUS(bc_error("extended register")); |
| 3308 | bc_lex_name(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3309 | } |
| 3310 | else { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3311 | bc_vec_pop_all(&l->t.v); |
Denys Vlasenko | e55a572 | 2018-12-06 12:47:17 +0100 | [diff] [blame] | 3312 | bc_vec_push(&l->t.v, &l->buf[l->i - 1]); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 3313 | bc_vec_pushZeroByte(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3314 | l->t.t = BC_LEX_NAME; |
| 3315 | } |
| 3316 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3317 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3318 | } |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 3319 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3320 | # define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 3321 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3322 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3323 | static BC_STATUS zdc_lex_string(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3324 | { |
| 3325 | size_t depth = 1, nls = 0, i = l->i; |
| 3326 | char c; |
| 3327 | |
| 3328 | l->t.t = BC_LEX_STR; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3329 | bc_vec_pop_all(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3330 | |
| 3331 | for (c = l->buf[i]; c != 0 && depth; c = l->buf[++i]) { |
| 3332 | |
| 3333 | depth += (c == '[' && (i == l->i || l->buf[i - 1] != '\\')); |
| 3334 | depth -= (c == ']' && (i == l->i || l->buf[i - 1] != '\\')); |
| 3335 | nls += (c == '\n'); |
| 3336 | |
| 3337 | if (depth) bc_vec_push(&l->t.v, &c); |
| 3338 | } |
| 3339 | |
| 3340 | if (c == '\0') { |
| 3341 | l->i = i; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3342 | RETURN_STATUS(bc_error("string end could not be found")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3343 | } |
| 3344 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 3345 | bc_vec_pushZeroByte(&l->t.v); |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 3346 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
| 3347 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
| 3348 | if (i - l->i > BC_MAX_STRING) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3349 | RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]")); |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 3350 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3351 | |
| 3352 | l->i = i; |
| 3353 | l->line += nls; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3354 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3355 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3356 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3357 | } |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 3358 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3359 | # define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 3360 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3361 | |
Denys Vlasenko | c2265f5 | 2018-12-12 23:10:08 +0100 | [diff] [blame] | 3362 | static BC_STATUS zdc_lex_token(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3363 | { |
| 3364 | BcStatus s = BC_STATUS_SUCCESS; |
| 3365 | char c = l->buf[l->i++], c2; |
| 3366 | size_t i; |
| 3367 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3368 | for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) { |
| 3369 | if (l->t.last == dc_lex_regs[i]) |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3370 | RETURN_STATUS(zdc_lex_register(l)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3371 | } |
| 3372 | |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3373 | if (c >= '%' && c <= '~' |
| 3374 | && (l->t.t = dc_lex_tokens[(c - '%')]) != BC_LEX_INVALID |
| 3375 | ) { |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3376 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3377 | } |
| 3378 | |
| 3379 | // This is the workhorse of the lexer. |
| 3380 | switch (c) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3381 | case '\0': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3382 | l->t.t = BC_LEX_EOF; |
| 3383 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3384 | case '\n': |
| 3385 | case '\t': |
| 3386 | case '\v': |
| 3387 | case '\f': |
| 3388 | case '\r': |
| 3389 | case ' ': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3390 | l->newline = (c == '\n'); |
| 3391 | bc_lex_whitespace(l); |
| 3392 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3393 | case '!': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3394 | c2 = l->buf[l->i]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3395 | if (c2 == '=') |
| 3396 | l->t.t = BC_LEX_OP_REL_NE; |
| 3397 | else if (c2 == '<') |
| 3398 | l->t.t = BC_LEX_OP_REL_LE; |
| 3399 | else if (c2 == '>') |
| 3400 | l->t.t = BC_LEX_OP_REL_GE; |
| 3401 | else |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3402 | RETURN_STATUS(bc_error_bad_character(c)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3403 | ++l->i; |
| 3404 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3405 | case '#': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3406 | bc_lex_lineComment(l); |
| 3407 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3408 | case '.': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3409 | if (isdigit(l->buf[l->i])) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3410 | s = zbc_lex_number(l, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3411 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3412 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3413 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3414 | case '0': |
| 3415 | case '1': |
| 3416 | case '2': |
| 3417 | case '3': |
| 3418 | case '4': |
| 3419 | case '5': |
| 3420 | case '6': |
| 3421 | case '7': |
| 3422 | case '8': |
| 3423 | case '9': |
| 3424 | case 'A': |
| 3425 | case 'B': |
| 3426 | case 'C': |
| 3427 | case 'D': |
| 3428 | case 'E': |
| 3429 | case 'F': |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3430 | s = zbc_lex_number(l, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3431 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3432 | case '[': |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3433 | s = zdc_lex_string(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3434 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3435 | default: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3436 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3437 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3438 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3439 | } |
| 3440 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3441 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3442 | } |
| 3443 | #endif // ENABLE_DC |
| 3444 | |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3445 | static void bc_program_addFunc(char *name, size_t *idx); |
| 3446 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3447 | static void bc_parse_addFunc(BcParse *p, char *name, size_t *idx) |
| 3448 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3449 | bc_program_addFunc(name, idx); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 3450 | p->func = bc_program_func(p->fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3451 | } |
| 3452 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 3453 | static void bc_parse_push(BcParse *p, char i) |
| 3454 | { |
| 3455 | dbg_lex("%s:%d pushing opcode %d", __func__, __LINE__, i); |
| 3456 | bc_vec_pushByte(&p->func->code, i); |
| 3457 | } |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 3458 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3459 | static void bc_parse_pushName(BcParse *p, char *name) |
| 3460 | { |
| 3461 | size_t i = 0, len = strlen(name); |
| 3462 | |
| 3463 | for (; i < len; ++i) bc_parse_push(p, name[i]); |
| 3464 | bc_parse_push(p, BC_PARSE_STREND); |
| 3465 | |
| 3466 | free(name); |
| 3467 | } |
| 3468 | |
| 3469 | static void bc_parse_pushIndex(BcParse *p, size_t idx) |
| 3470 | { |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3471 | size_t mask; |
| 3472 | unsigned amt; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3473 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 3474 | dbg_lex("%s:%d pushing index %d", __func__, __LINE__, idx); |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3475 | mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8); |
| 3476 | amt = sizeof(idx); |
| 3477 | do { |
| 3478 | if (idx & mask) break; |
| 3479 | mask >>= 8; |
| 3480 | amt--; |
| 3481 | } while (amt != 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3482 | |
| 3483 | bc_parse_push(p, amt); |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3484 | |
| 3485 | while (idx != 0) { |
| 3486 | bc_parse_push(p, (unsigned char)idx); |
| 3487 | idx >>= 8; |
| 3488 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3489 | } |
| 3490 | |
| 3491 | static void bc_parse_number(BcParse *p, BcInst *prev, size_t *nexs) |
| 3492 | { |
| 3493 | char *num = xstrdup(p->l.t.v.v); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3494 | size_t idx = G.prog.consts.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3495 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3496 | bc_vec_push(&G.prog.consts, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3497 | |
| 3498 | bc_parse_push(p, BC_INST_NUM); |
| 3499 | bc_parse_pushIndex(p, idx); |
| 3500 | |
| 3501 | ++(*nexs); |
| 3502 | (*prev) = BC_INST_NUM; |
| 3503 | } |
| 3504 | |
Denys Vlasenko | e755e30 | 2018-12-13 22:25:28 +0100 | [diff] [blame] | 3505 | IF_BC(static BC_STATUS zbc_parse_parse(BcParse *p);) |
| 3506 | IF_DC(static BC_STATUS zdc_parse_parse(BcParse *p);) |
Denys Vlasenko | c0ef23c | 2018-12-12 23:03:10 +0100 | [diff] [blame] | 3507 | |
| 3508 | static BC_STATUS zcommon_parse(BcParse *p) |
| 3509 | { |
| 3510 | if (IS_BC) { |
| 3511 | IF_BC(RETURN_STATUS(zbc_parse_parse(p));) |
| 3512 | } |
| 3513 | IF_DC(RETURN_STATUS(zdc_parse_parse(p));) |
| 3514 | } |
| 3515 | |
Denys Vlasenko | f86e960 | 2018-12-14 17:01:56 +0100 | [diff] [blame] | 3516 | static BC_STATUS zbc_parse_text_init(BcParse *p, const char *text) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3517 | { |
| 3518 | BcStatus s; |
| 3519 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 3520 | p->func = bc_program_func(p->fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3521 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3522 | if (!text[0] && !BC_PARSE_CAN_EXEC(p)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3523 | p->l.t.t = BC_LEX_INVALID; |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3524 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | c0ef23c | 2018-12-12 23:03:10 +0100 | [diff] [blame] | 3525 | ERROR_RETURN(s =) zcommon_parse(p); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3526 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3527 | if (!BC_PARSE_CAN_EXEC(p)) |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3528 | RETURN_STATUS(bc_error("file is not executable")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3529 | } |
| 3530 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 3531 | RETURN_STATUS(zbc_lex_text_init(&p->l, text)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3532 | } |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3533 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | f86e960 | 2018-12-14 17:01:56 +0100 | [diff] [blame] | 3534 | # define zbc_parse_text_init(...) (zbc_parse_text_init(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3535 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3536 | |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3537 | // Called when parsing or execution detects a failure, |
| 3538 | // resets execution structures. |
| 3539 | static void bc_program_reset(void) |
| 3540 | { |
| 3541 | BcFunc *f; |
| 3542 | BcInstPtr *ip; |
| 3543 | |
| 3544 | bc_vec_npop(&G.prog.stack, G.prog.stack.len - 1); |
| 3545 | bc_vec_pop_all(&G.prog.results); |
| 3546 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 3547 | f = bc_program_func(0); |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3548 | ip = bc_vec_top(&G.prog.stack); |
| 3549 | ip->idx = f->code.len; |
| 3550 | } |
| 3551 | |
Denys Vlasenko | e55a572 | 2018-12-06 12:47:17 +0100 | [diff] [blame] | 3552 | #define bc_parse_updateFunc(p, f) \ |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 3553 | ((p)->func = bc_program_func((p)->fidx = (f))) |
Denys Vlasenko | e55a572 | 2018-12-06 12:47:17 +0100 | [diff] [blame] | 3554 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3555 | // Called when zbc/zdc_parse_parse() detects a failure, |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 3556 | // resets parsing structures. |
| 3557 | static void bc_parse_reset(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3558 | { |
| 3559 | if (p->fidx != BC_PROG_MAIN) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3560 | p->func->nparams = 0; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3561 | bc_vec_pop_all(&p->func->code); |
| 3562 | bc_vec_pop_all(&p->func->autos); |
| 3563 | bc_vec_pop_all(&p->func->labels); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3564 | |
| 3565 | bc_parse_updateFunc(p, BC_PROG_MAIN); |
| 3566 | } |
| 3567 | |
| 3568 | p->l.i = p->l.len; |
| 3569 | p->l.t.t = BC_LEX_EOF; |
| 3570 | p->auto_part = (p->nbraces = 0); |
| 3571 | |
| 3572 | bc_vec_npop(&p->flags, p->flags.len - 1); |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3573 | bc_vec_pop_all(&p->exits); |
| 3574 | bc_vec_pop_all(&p->conds); |
| 3575 | bc_vec_pop_all(&p->ops); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3576 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 3577 | bc_program_reset(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3578 | } |
| 3579 | |
| 3580 | static void bc_parse_free(BcParse *p) |
| 3581 | { |
| 3582 | bc_vec_free(&p->flags); |
| 3583 | bc_vec_free(&p->exits); |
| 3584 | bc_vec_free(&p->conds); |
| 3585 | bc_vec_free(&p->ops); |
| 3586 | bc_lex_free(&p->l); |
| 3587 | } |
| 3588 | |
Denys Vlasenko | c2265f5 | 2018-12-12 23:10:08 +0100 | [diff] [blame] | 3589 | static void bc_parse_create(BcParse *p, size_t func) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3590 | { |
| 3591 | memset(p, 0, sizeof(BcParse)); |
| 3592 | |
Denys Vlasenko | c2265f5 | 2018-12-12 23:10:08 +0100 | [diff] [blame] | 3593 | bc_lex_init(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3594 | bc_vec_init(&p->flags, sizeof(uint8_t), NULL); |
| 3595 | bc_vec_init(&p->exits, sizeof(BcInstPtr), NULL); |
| 3596 | bc_vec_init(&p->conds, sizeof(size_t), NULL); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 3597 | bc_vec_pushZeroByte(&p->flags); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3598 | bc_vec_init(&p->ops, sizeof(BcLexType), NULL); |
| 3599 | |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 3600 | // p->auto_part = p->nbraces = 0; - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3601 | bc_parse_updateFunc(p, func); |
| 3602 | } |
| 3603 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 3604 | #if ENABLE_BC |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 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 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3616 | static BC_STATUS zbc_parse_else(BcParse *p); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 3617 | static BC_STATUS zbc_parse_stmt(BcParse *p); |
| 3618 | static BC_STATUS zbc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next); |
Denys Vlasenko | 050b0fe | 2018-12-05 22:40:44 +0100 | [diff] [blame] | 3619 | static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 3620 | #if ERRORS_ARE_FATAL |
| 3621 | # define zbc_parse_else(...) (zbc_parse_else(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 3622 | # define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 3623 | # define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 3624 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3625 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3626 | static BC_STATUS zbc_parse_operator(BcParse *p, BcLexType type, size_t start, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3627 | size_t *nexprs, bool next) |
| 3628 | { |
| 3629 | BcStatus s = BC_STATUS_SUCCESS; |
| 3630 | BcLexType t; |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 3631 | char l, r = bc_parse_op_PREC(type - BC_LEX_OP_INC); |
| 3632 | bool left = bc_parse_op_LEFT(type - BC_LEX_OP_INC); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3633 | |
| 3634 | while (p->ops.len > start) { |
| 3635 | |
| 3636 | t = BC_PARSE_TOP_OP(p); |
| 3637 | if (t == BC_LEX_LPAREN) break; |
| 3638 | |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 3639 | l = bc_parse_op_PREC(t - BC_LEX_OP_INC); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3640 | if (l >= r && (l != r || !left)) break; |
| 3641 | |
| 3642 | bc_parse_push(p, BC_PARSE_TOKEN_INST(t)); |
| 3643 | bc_vec_pop(&p->ops); |
| 3644 | *nexprs -= t != BC_LEX_OP_BOOL_NOT && t != BC_LEX_NEG; |
| 3645 | } |
| 3646 | |
| 3647 | bc_vec_push(&p->ops, &type); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3648 | if (next) s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3649 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3650 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3651 | } |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3652 | #if ERRORS_ARE_FATAL |
| 3653 | # define zbc_parse_operator(...) (zbc_parse_operator(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 3654 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3655 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3656 | static BC_STATUS zbc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3657 | { |
| 3658 | BcLexType top; |
| 3659 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3660 | if (p->ops.len <= ops_bgn) |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3661 | RETURN_STATUS(bc_error_bad_expression()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3662 | top = BC_PARSE_TOP_OP(p); |
| 3663 | |
| 3664 | while (top != BC_LEX_LPAREN) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3665 | bc_parse_push(p, BC_PARSE_TOKEN_INST(top)); |
| 3666 | |
| 3667 | bc_vec_pop(&p->ops); |
| 3668 | *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG; |
| 3669 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3670 | if (p->ops.len <= ops_bgn) |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3671 | RETURN_STATUS(bc_error_bad_expression()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3672 | top = BC_PARSE_TOP_OP(p); |
| 3673 | } |
| 3674 | |
| 3675 | bc_vec_pop(&p->ops); |
| 3676 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3677 | RETURN_STATUS(zbc_lex_next(&p->l)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3678 | } |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3679 | #if ERRORS_ARE_FATAL |
| 3680 | # define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 3681 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3682 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3683 | static BC_STATUS zbc_parse_params(BcParse *p, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3684 | { |
| 3685 | BcStatus s; |
| 3686 | bool comma = false; |
| 3687 | size_t nparams; |
| 3688 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 3689 | dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3690 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3691 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3692 | |
| 3693 | for (nparams = 0; p->l.t.t != BC_LEX_RPAREN; ++nparams) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3694 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 3695 | s = zbc_parse_expr(p, flags, bc_parse_next_param); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3696 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3697 | |
| 3698 | comma = p->l.t.t == BC_LEX_COMMA; |
| 3699 | if (comma) { |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3700 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3701 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3702 | } |
| 3703 | } |
| 3704 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3705 | if (comma) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3706 | bc_parse_push(p, BC_INST_CALL); |
| 3707 | bc_parse_pushIndex(p, nparams); |
| 3708 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3709 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3710 | } |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3711 | #if ERRORS_ARE_FATAL |
| 3712 | # define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 3713 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3714 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3715 | static BC_STATUS zbc_parse_call(BcParse *p, char *name, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3716 | { |
| 3717 | BcStatus s; |
| 3718 | BcId entry, *entry_ptr; |
| 3719 | size_t idx; |
| 3720 | |
| 3721 | entry.name = name; |
| 3722 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3723 | s = zbc_parse_params(p, flags); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3724 | if (s) goto err; |
| 3725 | |
| 3726 | if (p->l.t.t != BC_LEX_RPAREN) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3727 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3728 | goto err; |
| 3729 | } |
| 3730 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3731 | idx = bc_map_index(&G.prog.fn_map, &entry); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3732 | |
| 3733 | if (idx == BC_VEC_INVALID_IDX) { |
| 3734 | name = xstrdup(entry.name); |
| 3735 | bc_parse_addFunc(p, name, &idx); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3736 | idx = bc_map_index(&G.prog.fn_map, &entry); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3737 | free(entry.name); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3738 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3739 | free(name); |
| 3740 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3741 | entry_ptr = bc_vec_item(&G.prog.fn_map, idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3742 | bc_parse_pushIndex(p, entry_ptr->idx); |
| 3743 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3744 | RETURN_STATUS(zbc_lex_next(&p->l)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3745 | |
| 3746 | err: |
| 3747 | free(name); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3748 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3749 | } |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3750 | #if ERRORS_ARE_FATAL |
| 3751 | # define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 3752 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3753 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3754 | static BC_STATUS zbc_parse_name(BcParse *p, BcInst *type, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3755 | { |
| 3756 | BcStatus s; |
| 3757 | char *name; |
| 3758 | |
| 3759 | name = xstrdup(p->l.t.v.v); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3760 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3761 | if (s) goto err; |
| 3762 | |
| 3763 | if (p->l.t.t == BC_LEX_LBRACKET) { |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3764 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3765 | if (s) goto err; |
| 3766 | |
| 3767 | if (p->l.t.t == BC_LEX_RBRACKET) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3768 | if (!(flags & BC_PARSE_ARRAY)) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3769 | s = bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3770 | goto err; |
| 3771 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3772 | *type = BC_INST_ARRAY; |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3773 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3774 | *type = BC_INST_ARRAY_ELEM; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3775 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 3776 | s = zbc_parse_expr(p, flags, bc_parse_next_elem); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3777 | if (s) goto err; |
| 3778 | } |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3779 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3780 | if (s) goto err; |
| 3781 | bc_parse_push(p, *type); |
| 3782 | bc_parse_pushName(p, name); |
| 3783 | } |
| 3784 | else if (p->l.t.t == BC_LEX_LPAREN) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3785 | if (flags & BC_PARSE_NOCALL) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3786 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3787 | goto err; |
| 3788 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3789 | *type = BC_INST_CALL; |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3790 | s = zbc_parse_call(p, name, flags); |
| 3791 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3792 | *type = BC_INST_VAR; |
| 3793 | bc_parse_push(p, BC_INST_VAR); |
| 3794 | bc_parse_pushName(p, name); |
| 3795 | } |
| 3796 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3797 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3798 | |
| 3799 | err: |
| 3800 | free(name); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3801 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3802 | } |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3803 | #if ERRORS_ARE_FATAL |
| 3804 | # define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 3805 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3806 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 3807 | static BC_STATUS zbc_parse_read(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3808 | { |
| 3809 | BcStatus s; |
| 3810 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3811 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 3812 | if (s) RETURN_STATUS(s); |
| 3813 | if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3814 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3815 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 3816 | if (s) RETURN_STATUS(s); |
| 3817 | if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3818 | |
| 3819 | bc_parse_push(p, BC_INST_READ); |
| 3820 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 3821 | RETURN_STATUS(zbc_lex_next(&p->l)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3822 | } |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 3823 | #if ERRORS_ARE_FATAL |
| 3824 | # define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 3825 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3826 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3827 | static BC_STATUS zbc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3828 | BcInst *prev) |
| 3829 | { |
| 3830 | BcStatus s; |
| 3831 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3832 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3833 | if (s) RETURN_STATUS(s); |
| 3834 | if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3835 | |
| 3836 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; |
| 3837 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3838 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3839 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3840 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 3841 | s = zbc_parse_expr(p, flags, bc_parse_next_rel); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3842 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3843 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3844 | if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3845 | |
| 3846 | *prev = (type == BC_LEX_KEY_LENGTH) ? BC_INST_LENGTH : BC_INST_SQRT; |
| 3847 | bc_parse_push(p, *prev); |
| 3848 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3849 | RETURN_STATUS(zbc_lex_next(&p->l)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3850 | } |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3851 | #if ERRORS_ARE_FATAL |
| 3852 | # define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 3853 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3854 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3855 | static BC_STATUS zbc_parse_scale(BcParse *p, BcInst *type, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3856 | { |
| 3857 | BcStatus s; |
| 3858 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3859 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3860 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3861 | |
| 3862 | if (p->l.t.t != BC_LEX_LPAREN) { |
| 3863 | *type = BC_INST_SCALE; |
| 3864 | bc_parse_push(p, BC_INST_SCALE); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3865 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3866 | } |
| 3867 | |
| 3868 | *type = BC_INST_SCALE_FUNC; |
| 3869 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); |
| 3870 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3871 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3872 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3873 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 3874 | s = zbc_parse_expr(p, flags, bc_parse_next_rel); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3875 | if (s) RETURN_STATUS(s); |
| 3876 | if (p->l.t.t != BC_LEX_RPAREN) |
| 3877 | RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3878 | bc_parse_push(p, BC_INST_SCALE_FUNC); |
| 3879 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3880 | RETURN_STATUS(zbc_lex_next(&p->l)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3881 | } |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3882 | #if ERRORS_ARE_FATAL |
| 3883 | # define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 3884 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3885 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3886 | static BC_STATUS zbc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3887 | size_t *nexprs, uint8_t flags) |
| 3888 | { |
| 3889 | BcStatus s; |
| 3890 | BcLexType type; |
| 3891 | char inst; |
| 3892 | BcInst etype = *prev; |
| 3893 | |
| 3894 | if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM || |
| 3895 | etype == BC_INST_SCALE || etype == BC_INST_LAST || |
| 3896 | etype == BC_INST_IBASE || etype == BC_INST_OBASE) |
| 3897 | { |
| 3898 | *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC); |
| 3899 | bc_parse_push(p, inst); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3900 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3901 | } |
| 3902 | else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3903 | *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC); |
| 3904 | *paren_expr = true; |
| 3905 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3906 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3907 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3908 | type = p->l.t.t; |
| 3909 | |
| 3910 | // Because we parse the next part of the expression |
| 3911 | // right here, we need to increment this. |
| 3912 | *nexprs = *nexprs + 1; |
| 3913 | |
| 3914 | switch (type) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3915 | case BC_LEX_NAME: |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3916 | s = zbc_parse_name(p, prev, flags | BC_PARSE_NOCALL); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3917 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3918 | case BC_LEX_KEY_IBASE: |
| 3919 | case BC_LEX_KEY_LAST: |
| 3920 | case BC_LEX_KEY_OBASE: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3921 | bc_parse_push(p, type - BC_LEX_KEY_IBASE + BC_INST_IBASE); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3922 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3923 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3924 | case BC_LEX_KEY_SCALE: |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3925 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3926 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3927 | if (p->l.t.t == BC_LEX_LPAREN) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3928 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3929 | else |
| 3930 | bc_parse_push(p, BC_INST_SCALE); |
| 3931 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3932 | default: |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3933 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3934 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3935 | } |
| 3936 | |
| 3937 | if (!s) bc_parse_push(p, inst); |
| 3938 | } |
| 3939 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3940 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3941 | } |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3942 | #if ERRORS_ARE_FATAL |
| 3943 | # define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 3944 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3945 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 3946 | static BC_STATUS zbc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3947 | bool rparen, size_t *nexprs) |
| 3948 | { |
| 3949 | BcStatus s; |
| 3950 | BcLexType type; |
| 3951 | BcInst etype = *prev; |
| 3952 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3953 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 3954 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3955 | |
| 3956 | type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST || |
| 3957 | (etype >= BC_INST_NUM && etype <= BC_INST_SQRT) ? |
| 3958 | BC_LEX_OP_MINUS : |
| 3959 | BC_LEX_NEG; |
| 3960 | *prev = BC_PARSE_TOKEN_INST(type); |
| 3961 | |
| 3962 | // We can just push onto the op stack because this is the largest |
| 3963 | // precedence operator that gets pushed. Inc/dec does not. |
| 3964 | if (type != BC_LEX_OP_MINUS) |
| 3965 | bc_vec_push(&p->ops, &type); |
| 3966 | else |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3967 | s = zbc_parse_operator(p, type, ops_bgn, nexprs, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3968 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 3969 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3970 | } |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 3971 | #if ERRORS_ARE_FATAL |
| 3972 | # define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 3973 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3974 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 3975 | static BC_STATUS zbc_parse_string(BcParse *p, char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3976 | { |
| 3977 | char *str = xstrdup(p->l.t.v.v); |
| 3978 | |
| 3979 | bc_parse_push(p, BC_INST_STR); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3980 | bc_parse_pushIndex(p, G.prog.strs.len); |
| 3981 | bc_vec_push(&G.prog.strs, &str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3982 | bc_parse_push(p, inst); |
| 3983 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 3984 | RETURN_STATUS(zbc_lex_next(&p->l)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3985 | } |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 3986 | #if ERRORS_ARE_FATAL |
| 3987 | # define zbc_parse_string(...) (zbc_parse_string(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 3988 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3989 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 3990 | static BC_STATUS zbc_parse_print(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3991 | { |
| 3992 | BcStatus s; |
| 3993 | BcLexType type; |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 3994 | bool comma; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3995 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3996 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 3997 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3998 | |
| 3999 | type = p->l.t.t; |
| 4000 | |
| 4001 | if (type == BC_LEX_SCOLON || type == BC_LEX_NLINE) |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4002 | RETURN_STATUS(bc_error("bad print statement")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4003 | |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4004 | comma = false; |
| 4005 | while (type != BC_LEX_SCOLON && type != BC_LEX_NLINE) { |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4006 | if (type == BC_LEX_STR) { |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4007 | s = zbc_parse_string(p, BC_INST_PRINT_POP); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4008 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4009 | } else { |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4010 | s = zbc_parse_expr(p, 0, bc_parse_next_print); |
| 4011 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4012 | bc_parse_push(p, BC_INST_PRINT_POP); |
| 4013 | } |
| 4014 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4015 | comma = p->l.t.t == BC_LEX_COMMA; |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4016 | if (comma) { |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4017 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4018 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4019 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4020 | type = p->l.t.t; |
| 4021 | } |
| 4022 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4023 | if (comma) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4024 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4025 | RETURN_STATUS(zbc_lex_next(&p->l)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4026 | } |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4027 | #if ERRORS_ARE_FATAL |
| 4028 | # define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 4029 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4030 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4031 | static BC_STATUS zbc_parse_return(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4032 | { |
| 4033 | BcStatus s; |
| 4034 | BcLexType t; |
| 4035 | bool paren; |
| 4036 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4037 | if (!BC_PARSE_FUNC(p)) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4038 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4039 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4040 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4041 | |
| 4042 | t = p->l.t.t; |
| 4043 | paren = t == BC_LEX_LPAREN; |
| 4044 | |
| 4045 | if (t == BC_LEX_NLINE || t == BC_LEX_SCOLON) |
| 4046 | bc_parse_push(p, BC_INST_RET0); |
| 4047 | else { |
Denys Vlasenko | 050b0fe | 2018-12-05 22:40:44 +0100 | [diff] [blame] | 4048 | s = bc_parse_expr_empty_ok(p, 0, bc_parse_next_expr); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4049 | if (s == BC_STATUS_PARSE_EMPTY_EXP) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4050 | bc_parse_push(p, BC_INST_RET0); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4051 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4052 | } |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4053 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4054 | |
| 4055 | if (!paren || p->l.t.last != BC_LEX_RPAREN) { |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 4056 | s = bc_POSIX_requires("parentheses around return expressions"); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4057 | ERROR_RETURN(if (s) RETURN_STATUS(s);) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4058 | } |
| 4059 | |
| 4060 | bc_parse_push(p, BC_INST_RET); |
| 4061 | } |
| 4062 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4063 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4064 | } |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4065 | #if ERRORS_ARE_FATAL |
| 4066 | # define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 4067 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4068 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4069 | static BC_STATUS zbc_parse_endBody(BcParse *p, bool brace) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4070 | { |
| 4071 | BcStatus s = BC_STATUS_SUCCESS; |
| 4072 | |
| 4073 | if (p->flags.len <= 1 || (brace && p->nbraces == 0)) |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4074 | RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4075 | |
| 4076 | if (brace) { |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4077 | if (p->l.t.t != BC_LEX_RBRACE) |
| 4078 | RETURN_STATUS(bc_error_bad_token()); |
| 4079 | if (!p->nbraces) |
| 4080 | RETURN_STATUS(bc_error_bad_token()); |
| 4081 | --p->nbraces; |
| 4082 | s = zbc_lex_next(&p->l); |
| 4083 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4084 | } |
| 4085 | |
| 4086 | if (BC_PARSE_IF(p)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4087 | uint8_t *flag_ptr; |
| 4088 | |
| 4089 | while (p->l.t.t == BC_LEX_NLINE) { |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4090 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4091 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4092 | } |
| 4093 | |
| 4094 | bc_vec_pop(&p->flags); |
| 4095 | |
| 4096 | flag_ptr = BC_PARSE_TOP_FLAG_PTR(p); |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame^] | 4097 | dbg_lex("%s:%d setting BC_PARSE_FLAG_IF_END bit", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4098 | *flag_ptr = (*flag_ptr | BC_PARSE_FLAG_IF_END); |
| 4099 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4100 | if (p->l.t.t == BC_LEX_KEY_ELSE) |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4101 | s = zbc_parse_else(p); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4102 | } |
| 4103 | else if (BC_PARSE_ELSE(p)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4104 | BcInstPtr *ip; |
| 4105 | size_t *label; |
| 4106 | |
| 4107 | bc_vec_pop(&p->flags); |
| 4108 | |
| 4109 | ip = bc_vec_top(&p->exits); |
| 4110 | label = bc_vec_item(&p->func->labels, ip->idx); |
| 4111 | *label = p->func->code.len; |
| 4112 | |
| 4113 | bc_vec_pop(&p->exits); |
| 4114 | } |
| 4115 | else if (BC_PARSE_FUNC_INNER(p)) { |
| 4116 | bc_parse_push(p, BC_INST_RET0); |
| 4117 | bc_parse_updateFunc(p, BC_PROG_MAIN); |
| 4118 | bc_vec_pop(&p->flags); |
| 4119 | } |
| 4120 | else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4121 | BcInstPtr *ip = bc_vec_top(&p->exits); |
| 4122 | size_t *label = bc_vec_top(&p->conds); |
| 4123 | |
| 4124 | bc_parse_push(p, BC_INST_JUMP); |
| 4125 | bc_parse_pushIndex(p, *label); |
| 4126 | |
| 4127 | label = bc_vec_item(&p->func->labels, ip->idx); |
| 4128 | *label = p->func->code.len; |
| 4129 | |
| 4130 | bc_vec_pop(&p->flags); |
| 4131 | bc_vec_pop(&p->exits); |
| 4132 | bc_vec_pop(&p->conds); |
| 4133 | } |
| 4134 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4135 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4136 | } |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4137 | #if ERRORS_ARE_FATAL |
| 4138 | # define zbc_parse_endBody(...) (zbc_parse_endBody(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 4139 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4140 | |
| 4141 | static void bc_parse_startBody(BcParse *p, uint8_t flags) |
| 4142 | { |
| 4143 | uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p); |
| 4144 | flags |= (*flag_ptr & (BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_LOOP)); |
| 4145 | flags |= BC_PARSE_FLAG_BODY; |
| 4146 | bc_vec_push(&p->flags, &flags); |
| 4147 | } |
| 4148 | |
| 4149 | static void bc_parse_noElse(BcParse *p) |
| 4150 | { |
| 4151 | BcInstPtr *ip; |
| 4152 | size_t *label; |
| 4153 | uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p); |
| 4154 | |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame^] | 4155 | dbg_lex("%s:%d clearing BC_PARSE_FLAG_IF_END bit", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4156 | *flag_ptr = (*flag_ptr & ~(BC_PARSE_FLAG_IF_END)); |
| 4157 | |
| 4158 | ip = bc_vec_top(&p->exits); |
| 4159 | label = bc_vec_item(&p->func->labels, ip->idx); |
| 4160 | *label = p->func->code.len; |
| 4161 | |
| 4162 | bc_vec_pop(&p->exits); |
| 4163 | } |
| 4164 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4165 | static BC_STATUS zbc_parse_if(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4166 | { |
| 4167 | BcStatus s; |
| 4168 | BcInstPtr ip; |
| 4169 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4170 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4171 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4172 | if (s) RETURN_STATUS(s); |
| 4173 | if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4174 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4175 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4176 | if (s) RETURN_STATUS(s); |
| 4177 | s = zbc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel); |
| 4178 | if (s) RETURN_STATUS(s); |
| 4179 | if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4180 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4181 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4182 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4183 | bc_parse_push(p, BC_INST_JUMP_ZERO); |
| 4184 | |
| 4185 | ip.idx = p->func->labels.len; |
| 4186 | ip.func = ip.len = 0; |
| 4187 | |
| 4188 | bc_parse_pushIndex(p, ip.idx); |
| 4189 | bc_vec_push(&p->exits, &ip); |
| 4190 | bc_vec_push(&p->func->labels, &ip.idx); |
| 4191 | bc_parse_startBody(p, BC_PARSE_FLAG_IF); |
| 4192 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4193 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4194 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4195 | } |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4196 | #if ERRORS_ARE_FATAL |
| 4197 | # define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 4198 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4199 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4200 | #undef zbc_parse_else |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4201 | static BC_STATUS zbc_parse_else(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4202 | { |
| 4203 | BcInstPtr ip; |
| 4204 | |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame^] | 4205 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4206 | if (!BC_PARSE_IF_END(p)) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4207 | |
| 4208 | ip.idx = p->func->labels.len; |
| 4209 | ip.func = ip.len = 0; |
| 4210 | |
| 4211 | bc_parse_push(p, BC_INST_JUMP); |
| 4212 | bc_parse_pushIndex(p, ip.idx); |
| 4213 | |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame^] | 4214 | dbg_lex("%s:%d calling bc_parse_noElse()", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4215 | bc_parse_noElse(p); |
| 4216 | |
| 4217 | bc_vec_push(&p->exits, &ip); |
| 4218 | bc_vec_push(&p->func->labels, &ip.idx); |
| 4219 | bc_parse_startBody(p, BC_PARSE_FLAG_ELSE); |
| 4220 | |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame^] | 4221 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4222 | RETURN_STATUS(zbc_lex_next(&p->l)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4223 | } |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4224 | #if ERRORS_ARE_FATAL |
| 4225 | # define zbc_parse_else(...) (zbc_parse_else(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 4226 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4227 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4228 | static BC_STATUS zbc_parse_while(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4229 | { |
| 4230 | BcStatus s; |
| 4231 | BcInstPtr ip; |
| 4232 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4233 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4234 | if (s) RETURN_STATUS(s); |
| 4235 | if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4236 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4237 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4238 | |
| 4239 | ip.idx = p->func->labels.len; |
| 4240 | |
| 4241 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4242 | bc_vec_push(&p->conds, &ip.idx); |
| 4243 | |
| 4244 | ip.idx = p->func->labels.len; |
| 4245 | ip.func = 1; |
| 4246 | ip.len = 0; |
| 4247 | |
| 4248 | bc_vec_push(&p->exits, &ip); |
| 4249 | bc_vec_push(&p->func->labels, &ip.idx); |
| 4250 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4251 | s = zbc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel); |
| 4252 | if (s) RETURN_STATUS(s); |
| 4253 | if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4254 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4255 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4256 | |
| 4257 | bc_parse_push(p, BC_INST_JUMP_ZERO); |
| 4258 | bc_parse_pushIndex(p, ip.idx); |
| 4259 | bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER); |
| 4260 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4261 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4262 | } |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4263 | #if ERRORS_ARE_FATAL |
| 4264 | # define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 4265 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4266 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4267 | static BC_STATUS zbc_parse_for(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4268 | { |
| 4269 | BcStatus s; |
| 4270 | BcInstPtr ip; |
| 4271 | size_t cond_idx, exit_idx, body_idx, update_idx; |
| 4272 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4273 | dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4274 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4275 | if (s) RETURN_STATUS(s); |
| 4276 | if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4277 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4278 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4279 | |
| 4280 | if (p->l.t.t != BC_LEX_SCOLON) |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4281 | s = zbc_parse_expr(p, 0, bc_parse_next_for); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4282 | else |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 4283 | s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4284 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4285 | if (s) RETURN_STATUS(s); |
| 4286 | if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4287 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4288 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4289 | |
| 4290 | cond_idx = p->func->labels.len; |
| 4291 | update_idx = cond_idx + 1; |
| 4292 | body_idx = update_idx + 1; |
| 4293 | exit_idx = body_idx + 1; |
| 4294 | |
| 4295 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4296 | |
| 4297 | if (p->l.t.t != BC_LEX_SCOLON) |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4298 | s = zbc_parse_expr(p, BC_PARSE_REL, bc_parse_next_for); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4299 | else |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 4300 | s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4301 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4302 | if (s) RETURN_STATUS(s); |
| 4303 | if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4304 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4305 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4306 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4307 | |
| 4308 | bc_parse_push(p, BC_INST_JUMP_ZERO); |
| 4309 | bc_parse_pushIndex(p, exit_idx); |
| 4310 | bc_parse_push(p, BC_INST_JUMP); |
| 4311 | bc_parse_pushIndex(p, body_idx); |
| 4312 | |
| 4313 | ip.idx = p->func->labels.len; |
| 4314 | |
| 4315 | bc_vec_push(&p->conds, &update_idx); |
| 4316 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4317 | |
| 4318 | if (p->l.t.t != BC_LEX_RPAREN) |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4319 | s = zbc_parse_expr(p, 0, bc_parse_next_rel); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4320 | else |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 4321 | s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4322 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4323 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4324 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4325 | if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4326 | bc_parse_push(p, BC_INST_JUMP); |
| 4327 | bc_parse_pushIndex(p, cond_idx); |
| 4328 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4329 | |
| 4330 | ip.idx = exit_idx; |
| 4331 | ip.func = 1; |
| 4332 | ip.len = 0; |
| 4333 | |
| 4334 | bc_vec_push(&p->exits, &ip); |
| 4335 | bc_vec_push(&p->func->labels, &ip.idx); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4336 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4337 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4338 | bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER); |
| 4339 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4340 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4341 | } |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4342 | #if ERRORS_ARE_FATAL |
| 4343 | # define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 4344 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4345 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4346 | static BC_STATUS zbc_parse_loopExit(BcParse *p, BcLexType type) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4347 | { |
| 4348 | BcStatus s; |
| 4349 | size_t i; |
| 4350 | BcInstPtr *ip; |
| 4351 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4352 | if (!BC_PARSE_LOOP(p)) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4353 | |
| 4354 | if (type == BC_LEX_KEY_BREAK) { |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4355 | if (p->exits.len == 0) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4356 | |
| 4357 | i = p->exits.len - 1; |
| 4358 | ip = bc_vec_item(&p->exits, i); |
| 4359 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4360 | while (!ip->func && i < p->exits.len) |
| 4361 | ip = bc_vec_item(&p->exits, i--); |
| 4362 | if (i >= p->exits.len && !ip->func) |
| 4363 | RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4364 | |
| 4365 | i = ip->idx; |
| 4366 | } |
| 4367 | else |
| 4368 | i = *((size_t *) bc_vec_top(&p->conds)); |
| 4369 | |
| 4370 | bc_parse_push(p, BC_INST_JUMP); |
| 4371 | bc_parse_pushIndex(p, i); |
| 4372 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4373 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4374 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4375 | |
| 4376 | if (p->l.t.t != BC_LEX_SCOLON && p->l.t.t != BC_LEX_NLINE) |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4377 | RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4378 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4379 | RETURN_STATUS(zbc_lex_next(&p->l)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4380 | } |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4381 | #if ERRORS_ARE_FATAL |
| 4382 | # define zbc_parse_loopExit(...) (zbc_parse_loopExit(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 4383 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4384 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4385 | static BC_STATUS zbc_parse_func(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4386 | { |
| 4387 | BcStatus s; |
| 4388 | bool var, comma = false; |
| 4389 | uint8_t flags; |
| 4390 | char *name; |
| 4391 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4392 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4393 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4394 | if (p->l.t.t != BC_LEX_NAME) |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4395 | RETURN_STATUS(bc_error("bad function definition")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4396 | |
| 4397 | name = xstrdup(p->l.t.v.v); |
| 4398 | bc_parse_addFunc(p, name, &p->fidx); |
| 4399 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4400 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4401 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4402 | if (p->l.t.t != BC_LEX_LPAREN) |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4403 | RETURN_STATUS(bc_error("bad function definition")); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4404 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4405 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4406 | |
| 4407 | while (p->l.t.t != BC_LEX_RPAREN) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4408 | if (p->l.t.t != BC_LEX_NAME) |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4409 | RETURN_STATUS(bc_error("bad function definition")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4410 | |
| 4411 | ++p->func->nparams; |
| 4412 | |
| 4413 | name = xstrdup(p->l.t.v.v); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4414 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4415 | if (s) goto err; |
| 4416 | |
| 4417 | var = p->l.t.t != BC_LEX_LBRACKET; |
| 4418 | |
| 4419 | if (!var) { |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4420 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4421 | if (s) goto err; |
| 4422 | |
| 4423 | if (p->l.t.t != BC_LEX_RBRACKET) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4424 | s = bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4425 | goto err; |
| 4426 | } |
| 4427 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4428 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4429 | if (s) goto err; |
| 4430 | } |
| 4431 | |
| 4432 | comma = p->l.t.t == BC_LEX_COMMA; |
| 4433 | if (comma) { |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4434 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4435 | if (s) goto err; |
| 4436 | } |
| 4437 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 4438 | s = zbc_func_insert(p->func, name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4439 | if (s) goto err; |
| 4440 | } |
| 4441 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4442 | if (comma) RETURN_STATUS(bc_error("bad function definition")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4443 | |
| 4444 | flags = BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_BODY; |
| 4445 | bc_parse_startBody(p, flags); |
| 4446 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4447 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4448 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4449 | |
| 4450 | if (p->l.t.t != BC_LEX_LBRACE) |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 4451 | s = bc_POSIX_requires("the left brace be on the same line as the function header"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4452 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4453 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4454 | |
| 4455 | err: |
| 4456 | free(name); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4457 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4458 | } |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4459 | #if ERRORS_ARE_FATAL |
| 4460 | # define zbc_parse_func(...) (zbc_parse_func(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 4461 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4462 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4463 | static BC_STATUS zbc_parse_auto(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4464 | { |
| 4465 | BcStatus s; |
| 4466 | bool comma, var, one; |
| 4467 | char *name; |
| 4468 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4469 | if (!p->auto_part) RETURN_STATUS(bc_error_bad_token()); |
| 4470 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4471 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4472 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4473 | |
| 4474 | p->auto_part = comma = false; |
| 4475 | one = p->l.t.t == BC_LEX_NAME; |
| 4476 | |
| 4477 | while (p->l.t.t == BC_LEX_NAME) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4478 | name = xstrdup(p->l.t.v.v); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4479 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4480 | if (s) goto err; |
| 4481 | |
| 4482 | var = p->l.t.t != BC_LEX_LBRACKET; |
| 4483 | if (!var) { |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4484 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4485 | if (s) goto err; |
| 4486 | |
| 4487 | if (p->l.t.t != BC_LEX_RBRACKET) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4488 | s = bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4489 | goto err; |
| 4490 | } |
| 4491 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4492 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4493 | if (s) goto err; |
| 4494 | } |
| 4495 | |
| 4496 | comma = p->l.t.t == BC_LEX_COMMA; |
| 4497 | if (comma) { |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4498 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4499 | if (s) goto err; |
| 4500 | } |
| 4501 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 4502 | s = zbc_func_insert(p->func, name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4503 | if (s) goto err; |
| 4504 | } |
| 4505 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4506 | if (comma) RETURN_STATUS(bc_error("bad function definition")); |
| 4507 | if (!one) RETURN_STATUS(bc_error("no auto variable found")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4508 | |
| 4509 | if (p->l.t.t != BC_LEX_NLINE && p->l.t.t != BC_LEX_SCOLON) |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4510 | RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4511 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4512 | RETURN_STATUS(zbc_lex_next(&p->l)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4513 | |
| 4514 | err: |
| 4515 | free(name); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4516 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4517 | } |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4518 | #if ERRORS_ARE_FATAL |
| 4519 | # define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 4520 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4521 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4522 | static BC_STATUS zbc_parse_body(BcParse *p, bool brace) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4523 | { |
| 4524 | BcStatus s = BC_STATUS_SUCCESS; |
| 4525 | uint8_t *flag_ptr = bc_vec_top(&p->flags); |
| 4526 | |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame^] | 4527 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4528 | *flag_ptr &= ~(BC_PARSE_FLAG_BODY); |
| 4529 | |
| 4530 | if (*flag_ptr & BC_PARSE_FLAG_FUNC_INNER) { |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame^] | 4531 | dbg_lex("%s:%d BC_PARSE_FLAG_FUNC_INNER", __func__, __LINE__); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4532 | if (!brace) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4533 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4534 | p->auto_part = p->l.t.t != BC_LEX_KEY_AUTO; |
| 4535 | |
| 4536 | if (!p->auto_part) { |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4537 | s = zbc_parse_auto(p); |
| 4538 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4539 | } |
| 4540 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4541 | if (p->l.t.t == BC_LEX_NLINE) s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4542 | } |
| 4543 | else { |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame^] | 4544 | dbg_lex("%s:%d !BC_PARSE_FLAG_FUNC_INNER", __func__, __LINE__); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4545 | s = zbc_parse_stmt(p); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4546 | if (!s && !brace) s = zbc_parse_endBody(p, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4547 | } |
| 4548 | |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame^] | 4549 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4550 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4551 | } |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4552 | #if ERRORS_ARE_FATAL |
| 4553 | # define zbc_parse_body(...) (zbc_parse_body(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 4554 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4555 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4556 | #undef zbc_parse_stmt |
| 4557 | static BC_STATUS zbc_parse_stmt(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4558 | { |
| 4559 | BcStatus s = BC_STATUS_SUCCESS; |
| 4560 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4561 | dbg_lex_enter("%s:%d entered, p->l.t.t:%d", __func__, __LINE__, p->l.t.t); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4562 | switch (p->l.t.t) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4563 | case BC_LEX_NLINE: |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame^] | 4564 | dbg_lex_done("%s:%d done (seen BC_LEX_NLINE)", __func__, __LINE__); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4565 | RETURN_STATUS(zbc_lex_next(&p->l)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4566 | |
| 4567 | case BC_LEX_KEY_ELSE: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4568 | p->auto_part = false; |
| 4569 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4570 | |
| 4571 | case BC_LEX_LBRACE: |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4572 | if (!BC_PARSE_BODY(p)) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4573 | ++p->nbraces; |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4574 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4575 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame^] | 4576 | dbg_lex_done("%s:%d done (returning zbc_parse_body())", __func__, __LINE__); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4577 | RETURN_STATUS(zbc_parse_body(p, true)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4578 | |
| 4579 | case BC_LEX_KEY_AUTO: |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4580 | RETURN_STATUS(zbc_parse_auto(p)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4581 | |
| 4582 | default: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4583 | p->auto_part = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4584 | if (BC_PARSE_IF_END(p)) { |
| 4585 | bc_parse_noElse(p); |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame^] | 4586 | dbg_lex_done("%s:%d done (BC_PARSE_IF_END is true)", __func__, __LINE__); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4587 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4588 | } |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame^] | 4589 | if (BC_PARSE_BODY(p)) { |
| 4590 | dbg_lex_done("%s:%d done (returning zbc_parse_body())", __func__, __LINE__); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4591 | RETURN_STATUS(zbc_parse_body(p, false)); |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame^] | 4592 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4593 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4594 | } |
| 4595 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4596 | dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4597 | switch (p->l.t.t) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4598 | case BC_LEX_OP_INC: |
| 4599 | case BC_LEX_OP_DEC: |
| 4600 | case BC_LEX_OP_MINUS: |
| 4601 | case BC_LEX_OP_BOOL_NOT: |
| 4602 | case BC_LEX_LPAREN: |
| 4603 | case BC_LEX_NAME: |
| 4604 | case BC_LEX_NUMBER: |
| 4605 | case BC_LEX_KEY_IBASE: |
| 4606 | case BC_LEX_KEY_LAST: |
| 4607 | case BC_LEX_KEY_LENGTH: |
| 4608 | case BC_LEX_KEY_OBASE: |
| 4609 | case BC_LEX_KEY_READ: |
| 4610 | case BC_LEX_KEY_SCALE: |
| 4611 | case BC_LEX_KEY_SQRT: |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4612 | s = zbc_parse_expr(p, BC_PARSE_PRINT, bc_parse_next_expr); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4613 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4614 | case BC_LEX_KEY_ELSE: |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4615 | s = zbc_parse_else(p); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4616 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4617 | case BC_LEX_SCOLON: |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4618 | while (!s && p->l.t.t == BC_LEX_SCOLON) s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4619 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4620 | case BC_LEX_RBRACE: |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4621 | s = zbc_parse_endBody(p, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4622 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4623 | case BC_LEX_STR: |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4624 | s = zbc_parse_string(p, BC_INST_PRINT_STR); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4625 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4626 | case BC_LEX_KEY_BREAK: |
| 4627 | case BC_LEX_KEY_CONTINUE: |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4628 | s = zbc_parse_loopExit(p, p->l.t.t); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4629 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4630 | case BC_LEX_KEY_FOR: |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4631 | s = zbc_parse_for(p); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4632 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4633 | case BC_LEX_KEY_HALT: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4634 | bc_parse_push(p, BC_INST_HALT); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4635 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4636 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4637 | case BC_LEX_KEY_IF: |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4638 | s = zbc_parse_if(p); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4639 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4640 | case BC_LEX_KEY_LIMITS: |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 4641 | // "limits" is a compile-time command, |
| 4642 | // the output is produced at _parse time_. |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4643 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4644 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 4645 | printf( |
| 4646 | "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n" |
| 4647 | "BC_DIM_MAX = "BC_MAX_DIM_STR "\n" |
| 4648 | "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n" |
| 4649 | "BC_STRING_MAX = "BC_MAX_STRING_STR"\n" |
| 4650 | "BC_NAME_MAX = "BC_MAX_NAME_STR "\n" |
| 4651 | "BC_NUM_MAX = "BC_MAX_NUM_STR "\n" |
| 4652 | "MAX Exponent = "BC_MAX_EXP_STR "\n" |
| 4653 | "Number of vars = "BC_MAX_VARS_STR "\n" |
| 4654 | ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4655 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4656 | case BC_LEX_KEY_PRINT: |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4657 | s = zbc_parse_print(p); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4658 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4659 | case BC_LEX_KEY_QUIT: |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 4660 | // "quit" is a compile-time command. For example, |
| 4661 | // "if (0 == 1) quit" terminates when parsing the statement, |
| 4662 | // not when it is executed |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 4663 | QUIT_OR_RETURN_TO_MAIN; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4664 | case BC_LEX_KEY_RETURN: |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4665 | s = zbc_parse_return(p); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4666 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4667 | case BC_LEX_KEY_WHILE: |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4668 | s = zbc_parse_while(p); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4669 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4670 | default: |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4671 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4672 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4673 | } |
| 4674 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4675 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4676 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4677 | } |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4678 | #if ERRORS_ARE_FATAL |
| 4679 | # define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 4680 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4681 | |
Denys Vlasenko | c0ef23c | 2018-12-12 23:03:10 +0100 | [diff] [blame] | 4682 | static BC_STATUS zbc_parse_parse(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4683 | { |
| 4684 | BcStatus s; |
| 4685 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4686 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4687 | if (p->l.t.t == BC_LEX_EOF) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4688 | s = p->flags.len > 0 ? bc_error("block end could not be found") : bc_error("end of file"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4689 | else if (p->l.t.t == BC_LEX_KEY_DEFINE) { |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4690 | dbg_lex("%s:%d p->l.t.t:BC_LEX_KEY_DEFINE", __func__, __LINE__); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4691 | if (!BC_PARSE_CAN_EXEC(p)) |
| 4692 | RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4693 | s = zbc_parse_func(p); |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4694 | } else { |
| 4695 | dbg_lex("%s:%d p->l.t.t:%d (not BC_LEX_KEY_DEFINE)", __func__, __LINE__, p->l.t.t); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4696 | s = zbc_parse_stmt(p); |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4697 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4698 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 4699 | if (s || G_interrupt) { |
| 4700 | bc_parse_reset(p); |
| 4701 | s = BC_STATUS_FAILURE; |
| 4702 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4703 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4704 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4705 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4706 | } |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4707 | #if ERRORS_ARE_FATAL |
| 4708 | # define zbc_parse_parse(...) (zbc_parse_parse(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 4709 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4710 | |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 4711 | // This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP |
Denys Vlasenko | 050b0fe | 2018-12-05 22:40:44 +0100 | [diff] [blame] | 4712 | static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4713 | { |
| 4714 | BcStatus s = BC_STATUS_SUCCESS; |
| 4715 | BcInst prev = BC_INST_PRINT; |
| 4716 | BcLexType top, t = p->l.t.t; |
| 4717 | size_t nexprs = 0, ops_bgn = p->ops.len; |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 4718 | unsigned nparens, nrelops; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4719 | bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last; |
| 4720 | |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame^] | 4721 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4722 | paren_first = p->l.t.t == BC_LEX_LPAREN; |
| 4723 | nparens = nrelops = 0; |
| 4724 | paren_expr = rprn = done = get_token = assign = false; |
| 4725 | bin_last = true; |
| 4726 | |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 4727 | for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4728 | switch (t) { |
| 4729 | |
| 4730 | case BC_LEX_OP_INC: |
| 4731 | case BC_LEX_OP_DEC: |
| 4732 | { |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4733 | s = zbc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4734 | rprn = get_token = bin_last = false; |
| 4735 | break; |
| 4736 | } |
| 4737 | |
| 4738 | case BC_LEX_OP_MINUS: |
| 4739 | { |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4740 | s = zbc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4741 | rprn = get_token = false; |
| 4742 | bin_last = prev == BC_INST_MINUS; |
| 4743 | break; |
| 4744 | } |
| 4745 | |
| 4746 | case BC_LEX_OP_ASSIGN_POWER: |
| 4747 | case BC_LEX_OP_ASSIGN_MULTIPLY: |
| 4748 | case BC_LEX_OP_ASSIGN_DIVIDE: |
| 4749 | case BC_LEX_OP_ASSIGN_MODULUS: |
| 4750 | case BC_LEX_OP_ASSIGN_PLUS: |
| 4751 | case BC_LEX_OP_ASSIGN_MINUS: |
| 4752 | case BC_LEX_OP_ASSIGN: |
| 4753 | { |
| 4754 | if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM && |
| 4755 | prev != BC_INST_SCALE && prev != BC_INST_IBASE && |
| 4756 | prev != BC_INST_OBASE && prev != BC_INST_LAST) |
| 4757 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4758 | s = bc_error("bad assignment:" |
| 4759 | " left side must be scale," |
| 4760 | " ibase, obase, last, var," |
| 4761 | " or array element" |
| 4762 | ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4763 | break; |
| 4764 | } |
| 4765 | } |
| 4766 | // Fallthrough. |
| 4767 | case BC_LEX_OP_POWER: |
| 4768 | case BC_LEX_OP_MULTIPLY: |
| 4769 | case BC_LEX_OP_DIVIDE: |
| 4770 | case BC_LEX_OP_MODULUS: |
| 4771 | case BC_LEX_OP_PLUS: |
| 4772 | case BC_LEX_OP_REL_EQ: |
| 4773 | case BC_LEX_OP_REL_LE: |
| 4774 | case BC_LEX_OP_REL_GE: |
| 4775 | case BC_LEX_OP_REL_NE: |
| 4776 | case BC_LEX_OP_REL_LT: |
| 4777 | case BC_LEX_OP_REL_GT: |
| 4778 | case BC_LEX_OP_BOOL_NOT: |
| 4779 | case BC_LEX_OP_BOOL_OR: |
| 4780 | case BC_LEX_OP_BOOL_AND: |
| 4781 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4782 | if (((t == BC_LEX_OP_BOOL_NOT) != bin_last) |
| 4783 | || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT) |
| 4784 | ) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4785 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4786 | } |
| 4787 | |
| 4788 | nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT; |
| 4789 | prev = BC_PARSE_TOKEN_INST(t); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4790 | s = zbc_parse_operator(p, t, ops_bgn, &nexprs, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4791 | rprn = get_token = false; |
| 4792 | bin_last = t != BC_LEX_OP_BOOL_NOT; |
| 4793 | |
| 4794 | break; |
| 4795 | } |
| 4796 | |
| 4797 | case BC_LEX_LPAREN: |
| 4798 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4799 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4800 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4801 | ++nparens; |
| 4802 | paren_expr = rprn = bin_last = false; |
| 4803 | get_token = true; |
| 4804 | bc_vec_push(&p->ops, &t); |
| 4805 | |
| 4806 | break; |
| 4807 | } |
| 4808 | |
| 4809 | case BC_LEX_RPAREN: |
| 4810 | { |
| 4811 | if (bin_last || prev == BC_INST_BOOL_NOT) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4812 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4813 | |
| 4814 | if (nparens == 0) { |
| 4815 | s = BC_STATUS_SUCCESS; |
| 4816 | done = true; |
| 4817 | get_token = false; |
| 4818 | break; |
| 4819 | } |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame^] | 4820 | else if (!paren_expr) { |
| 4821 | dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4822 | return BC_STATUS_PARSE_EMPTY_EXP; |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame^] | 4823 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4824 | |
| 4825 | --nparens; |
| 4826 | paren_expr = rprn = true; |
| 4827 | get_token = bin_last = false; |
| 4828 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4829 | s = zbc_parse_rightParen(p, ops_bgn, &nexprs); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4830 | |
| 4831 | break; |
| 4832 | } |
| 4833 | |
| 4834 | case BC_LEX_NAME: |
| 4835 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4836 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4837 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4838 | paren_expr = true; |
| 4839 | rprn = get_token = bin_last = false; |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4840 | s = zbc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4841 | ++nexprs; |
| 4842 | |
| 4843 | break; |
| 4844 | } |
| 4845 | |
| 4846 | case BC_LEX_NUMBER: |
| 4847 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4848 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4849 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4850 | bc_parse_number(p, &prev, &nexprs); |
| 4851 | paren_expr = get_token = true; |
| 4852 | rprn = bin_last = false; |
| 4853 | |
| 4854 | break; |
| 4855 | } |
| 4856 | |
| 4857 | case BC_LEX_KEY_IBASE: |
| 4858 | case BC_LEX_KEY_LAST: |
| 4859 | case BC_LEX_KEY_OBASE: |
| 4860 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4861 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4862 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4863 | prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE); |
| 4864 | bc_parse_push(p, (char) prev); |
| 4865 | |
| 4866 | paren_expr = get_token = true; |
| 4867 | rprn = bin_last = false; |
| 4868 | ++nexprs; |
| 4869 | |
| 4870 | break; |
| 4871 | } |
| 4872 | |
| 4873 | case BC_LEX_KEY_LENGTH: |
| 4874 | case BC_LEX_KEY_SQRT: |
| 4875 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4876 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4877 | return bc_error_bad_expression(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4878 | s = zbc_parse_builtin(p, t, flags, &prev); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4879 | paren_expr = true; |
| 4880 | rprn = get_token = bin_last = false; |
| 4881 | ++nexprs; |
| 4882 | |
| 4883 | break; |
| 4884 | } |
| 4885 | |
| 4886 | case BC_LEX_KEY_READ: |
| 4887 | { |
| 4888 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4889 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4890 | else if (flags & BC_PARSE_NOREAD) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4891 | s = bc_error_nested_read_call(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4892 | else |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4893 | s = zbc_parse_read(p); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4894 | |
| 4895 | paren_expr = true; |
| 4896 | rprn = get_token = bin_last = false; |
| 4897 | ++nexprs; |
| 4898 | prev = BC_INST_READ; |
| 4899 | |
| 4900 | break; |
| 4901 | } |
| 4902 | |
| 4903 | case BC_LEX_KEY_SCALE: |
| 4904 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4905 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4906 | return bc_error_bad_expression(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4907 | s = zbc_parse_scale(p, &prev, flags); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4908 | paren_expr = true; |
| 4909 | rprn = get_token = bin_last = false; |
| 4910 | ++nexprs; |
| 4911 | prev = BC_INST_SCALE; |
| 4912 | |
| 4913 | break; |
| 4914 | } |
| 4915 | |
| 4916 | default: |
| 4917 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4918 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4919 | break; |
| 4920 | } |
| 4921 | } |
| 4922 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4923 | if (!s && get_token) s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4924 | } |
| 4925 | |
| 4926 | if (s) return s; |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 4927 | if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4928 | |
| 4929 | while (p->ops.len > ops_bgn) { |
| 4930 | |
| 4931 | top = BC_PARSE_TOP_OP(p); |
| 4932 | assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN; |
| 4933 | |
| 4934 | if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4935 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4936 | |
| 4937 | bc_parse_push(p, BC_PARSE_TOKEN_INST(top)); |
| 4938 | |
| 4939 | nexprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG; |
| 4940 | bc_vec_pop(&p->ops); |
| 4941 | } |
| 4942 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4943 | if (prev == BC_INST_BOOL_NOT || nexprs != 1) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4944 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4945 | |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 4946 | // next is BcParseNext, byte array of up to 4 BC_LEX's, packed into 32-bit word |
| 4947 | for (;;) { |
| 4948 | if (t == (next & 0x7f)) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4949 | goto ok; |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 4950 | if (next & 0x80) // last element? |
| 4951 | break; |
| 4952 | next >>= 8; |
| 4953 | } |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4954 | return bc_error_bad_expression(); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4955 | ok: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4956 | |
| 4957 | if (!(flags & BC_PARSE_REL) && nrelops) { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 4958 | s = bc_POSIX_does_not_allow("comparison operators outside if or loops"); |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 4959 | ERROR_RETURN(if (s) return s;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4960 | } |
| 4961 | else if ((flags & BC_PARSE_REL) && nrelops > 1) { |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 4962 | s = bc_POSIX_requires("exactly one comparison operator per condition"); |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 4963 | ERROR_RETURN(if (s) return s;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4964 | } |
| 4965 | |
| 4966 | if (flags & BC_PARSE_PRINT) { |
| 4967 | if (paren_first || !assign) bc_parse_push(p, BC_INST_PRINT); |
| 4968 | bc_parse_push(p, BC_INST_POP); |
| 4969 | } |
| 4970 | |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame^] | 4971 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4972 | return s; |
| 4973 | } |
| 4974 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4975 | #undef zbc_parse_expr |
| 4976 | static BC_STATUS zbc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next) |
Denys Vlasenko | 050b0fe | 2018-12-05 22:40:44 +0100 | [diff] [blame] | 4977 | { |
| 4978 | BcStatus s; |
| 4979 | |
| 4980 | s = bc_parse_expr_empty_ok(p, flags, next); |
| 4981 | if (s == BC_STATUS_PARSE_EMPTY_EXP) |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4982 | RETURN_STATUS(bc_error("empty expression")); |
| 4983 | RETURN_STATUS(s); |
Denys Vlasenko | 050b0fe | 2018-12-05 22:40:44 +0100 | [diff] [blame] | 4984 | } |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4985 | #if ERRORS_ARE_FATAL |
| 4986 | # define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 4987 | #endif |
Denys Vlasenko | 050b0fe | 2018-12-05 22:40:44 +0100 | [diff] [blame] | 4988 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4989 | static BC_STATUS zbc_parse_expression(BcParse *p, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4990 | { |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4991 | RETURN_STATUS(zbc_parse_expr(p, flags, bc_parse_next_read)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4992 | } |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4993 | #if ERRORS_ARE_FATAL |
| 4994 | # define zbc_parse_expression(...) (zbc_parse_expression(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 4995 | #endif |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 4996 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4997 | #endif // ENABLE_BC |
| 4998 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 4999 | #if ENABLE_DC |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 5000 | |
| 5001 | #define DC_PARSE_BUF_LEN ((int) (sizeof(uint32_t) * CHAR_BIT)) |
| 5002 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5003 | static BC_STATUS zdc_parse_register(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5004 | { |
| 5005 | BcStatus s; |
| 5006 | char *name; |
| 5007 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 5008 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5009 | if (s) RETURN_STATUS(s); |
| 5010 | if (p->l.t.t != BC_LEX_NAME) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5011 | |
| 5012 | name = xstrdup(p->l.t.v.v); |
| 5013 | bc_parse_pushName(p, name); |
| 5014 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5015 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5016 | } |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5017 | #if ERRORS_ARE_FATAL |
| 5018 | # define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 5019 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5020 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 5021 | static BC_STATUS zdc_parse_string(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5022 | { |
| 5023 | char *str, *name, b[DC_PARSE_BUF_LEN + 1]; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5024 | size_t idx, len = G.prog.strs.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5025 | |
| 5026 | sprintf(b, "%0*zu", DC_PARSE_BUF_LEN, len); |
| 5027 | name = xstrdup(b); |
| 5028 | |
| 5029 | str = xstrdup(p->l.t.v.v); |
| 5030 | bc_parse_push(p, BC_INST_STR); |
| 5031 | bc_parse_pushIndex(p, len); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5032 | bc_vec_push(&G.prog.strs, &str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5033 | bc_parse_addFunc(p, name, &idx); |
| 5034 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 5035 | RETURN_STATUS(zbc_lex_next(&p->l)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5036 | } |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 5037 | #if ERRORS_ARE_FATAL |
| 5038 | # define zdc_parse_string(...) (zdc_parse_string(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 5039 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5040 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5041 | static BC_STATUS zdc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5042 | { |
| 5043 | BcStatus s; |
| 5044 | |
| 5045 | bc_parse_push(p, inst); |
| 5046 | if (name) { |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5047 | s = zdc_parse_register(p); |
| 5048 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5049 | } |
| 5050 | |
| 5051 | if (store) { |
| 5052 | bc_parse_push(p, BC_INST_SWAP); |
| 5053 | bc_parse_push(p, BC_INST_ASSIGN); |
| 5054 | bc_parse_push(p, BC_INST_POP); |
| 5055 | } |
| 5056 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5057 | RETURN_STATUS(zbc_lex_next(&p->l)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5058 | } |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5059 | #if ERRORS_ARE_FATAL |
| 5060 | # define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 5061 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5062 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5063 | static BC_STATUS zdc_parse_cond(BcParse *p, uint8_t inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5064 | { |
| 5065 | BcStatus s; |
| 5066 | |
| 5067 | bc_parse_push(p, inst); |
| 5068 | bc_parse_push(p, BC_INST_EXEC_COND); |
| 5069 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5070 | s = zdc_parse_register(p); |
| 5071 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5072 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 5073 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5074 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5075 | |
| 5076 | if (p->l.t.t == BC_LEX_ELSE) { |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5077 | s = zdc_parse_register(p); |
| 5078 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 5079 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5080 | } |
| 5081 | else |
| 5082 | bc_parse_push(p, BC_PARSE_STREND); |
| 5083 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5084 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5085 | } |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5086 | #if ERRORS_ARE_FATAL |
| 5087 | # define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 5088 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5089 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5090 | static BC_STATUS zdc_parse_token(BcParse *p, BcLexType t, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5091 | { |
| 5092 | BcStatus s = BC_STATUS_SUCCESS; |
| 5093 | BcInst prev; |
| 5094 | uint8_t inst; |
| 5095 | bool assign, get_token = false; |
| 5096 | |
| 5097 | switch (t) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5098 | case BC_LEX_OP_REL_EQ: |
| 5099 | case BC_LEX_OP_REL_LE: |
| 5100 | case BC_LEX_OP_REL_GE: |
| 5101 | case BC_LEX_OP_REL_NE: |
| 5102 | case BC_LEX_OP_REL_LT: |
| 5103 | case BC_LEX_OP_REL_GT: |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5104 | s = zdc_parse_cond(p, t - BC_LEX_OP_REL_EQ + BC_INST_REL_EQ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5105 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5106 | case BC_LEX_SCOLON: |
| 5107 | case BC_LEX_COLON: |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5108 | s = zdc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5109 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5110 | case BC_LEX_STR: |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 5111 | s = zdc_parse_string(p); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5112 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5113 | case BC_LEX_NEG: |
| 5114 | case BC_LEX_NUMBER: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5115 | if (t == BC_LEX_NEG) { |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 5116 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5117 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5118 | if (p->l.t.t != BC_LEX_NUMBER) |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5119 | RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5120 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5121 | bc_parse_number(p, &prev, &p->nbraces); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5122 | if (t == BC_LEX_NEG) bc_parse_push(p, BC_INST_NEG); |
| 5123 | get_token = true; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5124 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5125 | case BC_LEX_KEY_READ: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5126 | if (flags & BC_PARSE_NOREAD) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5127 | s = bc_error_nested_read_call(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5128 | else |
| 5129 | bc_parse_push(p, BC_INST_READ); |
| 5130 | get_token = true; |
| 5131 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5132 | case BC_LEX_OP_ASSIGN: |
| 5133 | case BC_LEX_STORE_PUSH: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5134 | assign = t == BC_LEX_OP_ASSIGN; |
| 5135 | inst = assign ? BC_INST_VAR : BC_INST_PUSH_TO_VAR; |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5136 | s = zdc_parse_mem(p, inst, true, assign); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5137 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5138 | case BC_LEX_LOAD: |
| 5139 | case BC_LEX_LOAD_POP: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5140 | inst = t == BC_LEX_LOAD_POP ? BC_INST_PUSH_VAR : BC_INST_LOAD; |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5141 | s = zdc_parse_mem(p, inst, true, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5142 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5143 | case BC_LEX_STORE_IBASE: |
| 5144 | case BC_LEX_STORE_SCALE: |
| 5145 | case BC_LEX_STORE_OBASE: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5146 | inst = t - BC_LEX_STORE_IBASE + BC_INST_IBASE; |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5147 | s = zdc_parse_mem(p, inst, false, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5148 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5149 | default: |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5150 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5151 | get_token = true; |
| 5152 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5153 | } |
| 5154 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 5155 | if (!s && get_token) s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5156 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5157 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5158 | } |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5159 | #if ERRORS_ARE_FATAL |
| 5160 | # define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 5161 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5162 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5163 | static BC_STATUS zdc_parse_expr(BcParse *p, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5164 | { |
| 5165 | BcStatus s = BC_STATUS_SUCCESS; |
| 5166 | BcInst inst; |
| 5167 | BcLexType t; |
| 5168 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5169 | if (flags & BC_PARSE_NOCALL) p->nbraces = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5170 | |
| 5171 | for (t = p->l.t.t; !s && t != BC_LEX_EOF; t = p->l.t.t) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5172 | inst = dc_parse_insts[t]; |
| 5173 | |
| 5174 | if (inst != BC_INST_INVALID) { |
| 5175 | bc_parse_push(p, inst); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 5176 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5177 | } else |
| 5178 | s = zdc_parse_token(p, t, flags); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5179 | } |
| 5180 | |
| 5181 | if (!s && p->l.t.t == BC_LEX_EOF && (flags & BC_PARSE_NOCALL)) |
| 5182 | bc_parse_push(p, BC_INST_POP_EXEC); |
| 5183 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5184 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5185 | } |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5186 | #if ERRORS_ARE_FATAL |
| 5187 | # define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 5188 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5189 | |
Denys Vlasenko | c0ef23c | 2018-12-12 23:03:10 +0100 | [diff] [blame] | 5190 | static BC_STATUS zdc_parse_parse(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5191 | { |
| 5192 | BcStatus s; |
| 5193 | |
| 5194 | if (p->l.t.t == BC_LEX_EOF) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5195 | s = bc_error("end of file"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5196 | else |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 5197 | s = zdc_parse_expr(p, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5198 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 5199 | if (s || G_interrupt) { |
| 5200 | bc_parse_reset(p); |
| 5201 | s = BC_STATUS_FAILURE; |
| 5202 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5203 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 5204 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5205 | } |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 5206 | #if ERRORS_ARE_FATAL |
| 5207 | # define zdc_parse_parse(...) (zdc_parse_parse(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 5208 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5209 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5210 | #endif // ENABLE_DC |
| 5211 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 5212 | static BC_STATUS zcommon_parse_expr(BcParse *p, uint8_t flags) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5213 | { |
| 5214 | if (IS_BC) { |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 5215 | IF_BC(RETURN_STATUS(zbc_parse_expression(p, flags));) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5216 | } else { |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 5217 | IF_DC(RETURN_STATUS(zdc_parse_expr(p, flags));) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5218 | } |
| 5219 | } |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 5220 | #if ERRORS_ARE_FATAL |
| 5221 | # define zcommon_parse_expr(...) (zcommon_parse_expr(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 5222 | #endif |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5223 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5224 | static BcVec* bc_program_search(char *id, bool var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5225 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5226 | BcId e, *ptr; |
| 5227 | BcVec *v, *map; |
| 5228 | size_t i; |
| 5229 | BcResultData data; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 5230 | int new; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5231 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5232 | v = var ? &G.prog.vars : &G.prog.arrs; |
| 5233 | map = var ? &G.prog.var_map : &G.prog.arr_map; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5234 | |
| 5235 | e.name = id; |
| 5236 | e.idx = v->len; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 5237 | new = bc_map_insert(map, &e, &i); // 1 if insertion was successful |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5238 | |
| 5239 | if (new) { |
| 5240 | bc_array_init(&data.v, var); |
| 5241 | bc_vec_push(v, &data.v); |
| 5242 | } |
| 5243 | |
| 5244 | ptr = bc_vec_item(map, i); |
| 5245 | if (new) ptr->name = xstrdup(e.name); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5246 | return bc_vec_item(v, ptr->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5247 | } |
| 5248 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5249 | static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5250 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5251 | switch (r->t) { |
| 5252 | |
| 5253 | case BC_RESULT_STR: |
| 5254 | case BC_RESULT_TEMP: |
| 5255 | case BC_RESULT_IBASE: |
| 5256 | case BC_RESULT_SCALE: |
| 5257 | case BC_RESULT_OBASE: |
| 5258 | { |
| 5259 | *num = &r->d.n; |
| 5260 | break; |
| 5261 | } |
| 5262 | |
| 5263 | case BC_RESULT_CONSTANT: |
| 5264 | { |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5265 | BcStatus s; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5266 | char **str = bc_vec_item(&G.prog.consts, r->d.id.idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5267 | size_t base_t, len = strlen(*str); |
| 5268 | BcNum *base; |
| 5269 | |
| 5270 | bc_num_init(&r->d.n, len); |
| 5271 | |
| 5272 | hex = hex && len == 1; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5273 | base = hex ? &G.prog.hexb : &G.prog.ib; |
| 5274 | base_t = hex ? BC_NUM_MAX_IBASE : G.prog.ib_t; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5275 | s = zbc_num_parse(&r->d.n, *str, base, base_t); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5276 | |
| 5277 | if (s) { |
| 5278 | bc_num_free(&r->d.n); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5279 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5280 | } |
| 5281 | |
| 5282 | *num = &r->d.n; |
| 5283 | r->t = BC_RESULT_TEMP; |
| 5284 | |
| 5285 | break; |
| 5286 | } |
| 5287 | |
| 5288 | case BC_RESULT_VAR: |
| 5289 | case BC_RESULT_ARRAY: |
| 5290 | case BC_RESULT_ARRAY_ELEM: |
| 5291 | { |
| 5292 | BcVec *v; |
| 5293 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5294 | v = bc_program_search(r->d.id.name, r->t == BC_RESULT_VAR); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5295 | |
| 5296 | if (r->t == BC_RESULT_ARRAY_ELEM) { |
| 5297 | v = bc_vec_top(v); |
| 5298 | if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1); |
| 5299 | *num = bc_vec_item(v, r->d.id.idx); |
| 5300 | } |
| 5301 | else |
| 5302 | *num = bc_vec_top(v); |
| 5303 | |
| 5304 | break; |
| 5305 | } |
| 5306 | |
| 5307 | case BC_RESULT_LAST: |
| 5308 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5309 | *num = &G.prog.last; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5310 | break; |
| 5311 | } |
| 5312 | |
| 5313 | case BC_RESULT_ONE: |
| 5314 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5315 | *num = &G.prog.one; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5316 | break; |
| 5317 | } |
| 5318 | } |
| 5319 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5320 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5321 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5322 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5323 | # define zbc_program_num(...) (zbc_program_num(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5324 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5325 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5326 | static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5327 | BcResult **r, BcNum **rn, bool assign) |
| 5328 | { |
| 5329 | BcStatus s; |
| 5330 | bool hex; |
| 5331 | BcResultType lt, rt; |
| 5332 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5333 | if (!BC_PROG_STACK(&G.prog.results, 2)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5334 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5335 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5336 | *r = bc_vec_item_rev(&G.prog.results, 0); |
| 5337 | *l = bc_vec_item_rev(&G.prog.results, 1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5338 | |
| 5339 | lt = (*l)->t; |
| 5340 | rt = (*r)->t; |
| 5341 | hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE); |
| 5342 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5343 | s = zbc_program_num(*l, ln, false); |
| 5344 | if (s) RETURN_STATUS(s); |
| 5345 | s = zbc_program_num(*r, rn, hex); |
| 5346 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5347 | |
| 5348 | // We run this again under these conditions in case any vector has been |
| 5349 | // reallocated out from under the BcNums or arrays we had. |
| 5350 | if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5351 | s = zbc_program_num(*l, ln, false); |
| 5352 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5353 | } |
| 5354 | |
| 5355 | if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5356 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5357 | if (!assign && !BC_PROG_NUM((*r), (*ln))) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5358 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5359 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5360 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5361 | } |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 5362 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5363 | # define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 5364 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5365 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5366 | static void bc_program_binOpRetire(BcResult *r) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5367 | { |
| 5368 | r->t = BC_RESULT_TEMP; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5369 | bc_vec_pop(&G.prog.results); |
| 5370 | bc_vec_pop(&G.prog.results); |
| 5371 | bc_vec_push(&G.prog.results, r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5372 | } |
| 5373 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5374 | static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5375 | { |
| 5376 | BcStatus s; |
| 5377 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5378 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5379 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5380 | *r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5381 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5382 | s = zbc_program_num(*r, n, false); |
| 5383 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5384 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5385 | if (!BC_PROG_NUM((*r), (*n))) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5386 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5387 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5388 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5389 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5390 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5391 | # define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5392 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5393 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5394 | static void bc_program_retire(BcResult *r, BcResultType t) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5395 | { |
| 5396 | r->t = t; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5397 | bc_vec_pop(&G.prog.results); |
| 5398 | bc_vec_push(&G.prog.results, r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5399 | } |
| 5400 | |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5401 | static BC_STATUS zbc_program_op(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5402 | { |
| 5403 | BcStatus s; |
| 5404 | BcResult *opd1, *opd2, res; |
| 5405 | BcNum *n1, *n2 = NULL; |
| 5406 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5407 | s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5408 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5409 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5410 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5411 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 5412 | ERROR_RETURN(s =) zbc_program_ops[inst - BC_INST_POWER](n1, n2, &res.d.n, G.prog.scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5413 | if (s) goto err; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5414 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5415 | |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5416 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5417 | |
| 5418 | err: |
| 5419 | bc_num_free(&res.d.n); |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5420 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5421 | } |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5422 | #if ERRORS_ARE_FATAL |
| 5423 | # define zbc_program_op(...) (zbc_program_op(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 5424 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5425 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 5426 | static BC_STATUS zbc_program_read(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5427 | { |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5428 | const char *sv_file; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5429 | BcStatus s; |
| 5430 | BcParse parse; |
| 5431 | BcVec buf; |
| 5432 | BcInstPtr ip; |
Denys Vlasenko | 69171dc | 2018-12-12 00:29:24 +0100 | [diff] [blame] | 5433 | BcFunc *f; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5434 | |
Denys Vlasenko | 69171dc | 2018-12-12 00:29:24 +0100 | [diff] [blame] | 5435 | if (G.in_read) |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 5436 | RETURN_STATUS(bc_error_nested_read_call()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5437 | |
Denys Vlasenko | 69171dc | 2018-12-12 00:29:24 +0100 | [diff] [blame] | 5438 | f = bc_program_func(BC_PROG_READ); |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 5439 | bc_vec_pop_all(&f->code); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5440 | |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5441 | sv_file = G.prog.file; |
| 5442 | G.prog.file = NULL; |
Denys Vlasenko | 69171dc | 2018-12-12 00:29:24 +0100 | [diff] [blame] | 5443 | G.in_read = 1; |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5444 | |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 5445 | bc_char_vec_init(&buf); |
Denys Vlasenko | 8a89247 | 2018-12-12 22:43:58 +0100 | [diff] [blame] | 5446 | bc_read_line(&buf); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5447 | |
Denys Vlasenko | c2265f5 | 2018-12-12 23:10:08 +0100 | [diff] [blame] | 5448 | bc_parse_create(&parse, BC_PROG_READ); |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5449 | bc_lex_file(&parse.l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5450 | |
Denys Vlasenko | f86e960 | 2018-12-14 17:01:56 +0100 | [diff] [blame] | 5451 | s = zbc_parse_text_init(&parse, buf.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5452 | if (s) goto exec_err; |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 5453 | s = zcommon_parse_expr(&parse, BC_PARSE_NOREAD); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5454 | if (s) goto exec_err; |
| 5455 | |
| 5456 | if (parse.l.t.t != BC_LEX_NLINE && parse.l.t.t != BC_LEX_EOF) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5457 | s = bc_error("bad read() expression"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5458 | goto exec_err; |
| 5459 | } |
| 5460 | |
| 5461 | ip.func = BC_PROG_READ; |
| 5462 | ip.idx = 0; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5463 | ip.len = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5464 | |
| 5465 | // Update this pointer, just in case. |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 5466 | f = bc_program_func(BC_PROG_READ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5467 | |
| 5468 | bc_vec_pushByte(&f->code, BC_INST_POP_EXEC); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5469 | bc_vec_push(&G.prog.stack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5470 | |
| 5471 | exec_err: |
| 5472 | bc_parse_free(&parse); |
Denys Vlasenko | 4dd3652 | 2018-12-11 22:26:38 +0100 | [diff] [blame] | 5473 | //io_err: |
Denys Vlasenko | 69171dc | 2018-12-12 00:29:24 +0100 | [diff] [blame] | 5474 | G.in_read = 0; |
Denys Vlasenko | 4dd3652 | 2018-12-11 22:26:38 +0100 | [diff] [blame] | 5475 | G.prog.file = sv_file; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5476 | bc_vec_free(&buf); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 5477 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5478 | } |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 5479 | #if ERRORS_ARE_FATAL |
| 5480 | # define zbc_program_read(...) (zbc_program_read(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 5481 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5482 | |
| 5483 | static size_t bc_program_index(char *code, size_t *bgn) |
| 5484 | { |
| 5485 | char amt = code[(*bgn)++], i = 0; |
| 5486 | size_t res = 0; |
| 5487 | |
| 5488 | for (; i < amt; ++i, ++(*bgn)) |
| 5489 | res |= (((size_t)((int) code[*bgn]) & UCHAR_MAX) << (i * CHAR_BIT)); |
| 5490 | |
| 5491 | return res; |
| 5492 | } |
| 5493 | |
| 5494 | static char *bc_program_name(char *code, size_t *bgn) |
| 5495 | { |
| 5496 | size_t i; |
| 5497 | char c, *s, *str = code + *bgn, *ptr = strchr(str, BC_PARSE_STREND); |
| 5498 | |
| 5499 | s = xmalloc(ptr - str + 1); |
| 5500 | c = code[(*bgn)++]; |
| 5501 | |
| 5502 | for (i = 0; c != 0 && c != BC_PARSE_STREND; c = code[(*bgn)++], ++i) |
| 5503 | s[i] = c; |
| 5504 | |
| 5505 | s[i] = '\0'; |
| 5506 | |
| 5507 | return s; |
| 5508 | } |
| 5509 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 5510 | static void bc_program_printString(const char *str) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5511 | { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5512 | #if ENABLE_DC |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5513 | if (!str[0]) { |
Denys Vlasenko | 2c6f563 | 2018-12-11 21:21:14 +0100 | [diff] [blame] | 5514 | // Example: echo '[]ap' | dc |
| 5515 | // should print two bytes: 0x00, 0x0A |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5516 | bb_putchar('\0'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5517 | return; |
| 5518 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5519 | #endif |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5520 | while (*str) { |
| 5521 | int c = *str++; |
| 5522 | if (c != '\\' || !*str) |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5523 | bb_putchar(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5524 | else { |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5525 | c = *str++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5526 | switch (c) { |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5527 | case 'a': |
| 5528 | bb_putchar('\a'); |
| 5529 | break; |
| 5530 | case 'b': |
| 5531 | bb_putchar('\b'); |
| 5532 | break; |
| 5533 | case '\\': |
| 5534 | case 'e': |
| 5535 | bb_putchar('\\'); |
| 5536 | break; |
| 5537 | case 'f': |
| 5538 | bb_putchar('\f'); |
| 5539 | break; |
| 5540 | case 'n': |
| 5541 | bb_putchar('\n'); |
| 5542 | G.prog.nchars = SIZE_MAX; |
| 5543 | break; |
| 5544 | case 'r': |
| 5545 | bb_putchar('\r'); |
| 5546 | break; |
| 5547 | case 'q': |
| 5548 | bb_putchar('"'); |
| 5549 | break; |
| 5550 | case 't': |
| 5551 | bb_putchar('\t'); |
| 5552 | break; |
| 5553 | default: |
| 5554 | // Just print the backslash and following character. |
| 5555 | bb_putchar('\\'); |
| 5556 | ++G.prog.nchars; |
| 5557 | bb_putchar(c); |
| 5558 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5559 | } |
| 5560 | } |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5561 | ++G.prog.nchars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5562 | } |
| 5563 | } |
| 5564 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5565 | static void bc_num_printNewline(void) |
| 5566 | { |
| 5567 | if (G.prog.nchars == G.prog.len - 1) { |
| 5568 | bb_putchar('\\'); |
| 5569 | bb_putchar('\n'); |
| 5570 | G.prog.nchars = 0; |
| 5571 | } |
| 5572 | } |
| 5573 | |
| 5574 | #if ENABLE_DC |
| 5575 | static FAST_FUNC void bc_num_printChar(size_t num, size_t width, bool radix) |
| 5576 | { |
| 5577 | (void) radix; |
| 5578 | bb_putchar((char) num); |
| 5579 | G.prog.nchars += width; |
| 5580 | } |
| 5581 | #endif |
| 5582 | |
| 5583 | static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix) |
| 5584 | { |
| 5585 | size_t exp, pow; |
| 5586 | |
| 5587 | bc_num_printNewline(); |
| 5588 | bb_putchar(radix ? '.' : ' '); |
| 5589 | ++G.prog.nchars; |
| 5590 | |
| 5591 | bc_num_printNewline(); |
| 5592 | for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10) |
| 5593 | continue; |
| 5594 | |
| 5595 | for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) { |
| 5596 | size_t dig; |
| 5597 | bc_num_printNewline(); |
| 5598 | dig = num / pow; |
| 5599 | num -= dig * pow; |
| 5600 | bb_putchar(((char) dig) + '0'); |
| 5601 | } |
| 5602 | } |
| 5603 | |
| 5604 | static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix) |
| 5605 | { |
| 5606 | if (radix) { |
| 5607 | bc_num_printNewline(); |
| 5608 | bb_putchar('.'); |
| 5609 | G.prog.nchars += 1; |
| 5610 | } |
| 5611 | |
| 5612 | bc_num_printNewline(); |
| 5613 | bb_putchar(bb_hexdigits_upcase[num]); |
| 5614 | G.prog.nchars += width; |
| 5615 | } |
| 5616 | |
| 5617 | static void bc_num_printDecimal(BcNum *n) |
| 5618 | { |
| 5619 | size_t i, rdx = n->rdx - 1; |
| 5620 | |
| 5621 | if (n->neg) bb_putchar('-'); |
| 5622 | G.prog.nchars += n->neg; |
| 5623 | |
| 5624 | for (i = n->len - 1; i < n->len; --i) |
| 5625 | bc_num_printHex((size_t) n->num[i], 1, i == rdx); |
| 5626 | } |
| 5627 | |
| 5628 | static BC_STATUS zbc_num_printNum(BcNum *n, BcNum *base, size_t width, BcNumDigitOp print) |
| 5629 | { |
| 5630 | BcStatus s; |
| 5631 | BcVec stack; |
| 5632 | BcNum intp, fracp, digit, frac_len; |
| 5633 | unsigned long dig, *ptr; |
| 5634 | size_t i; |
| 5635 | bool radix; |
| 5636 | |
| 5637 | if (n->len == 0) { |
| 5638 | print(0, width, false); |
| 5639 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 5640 | } |
| 5641 | |
| 5642 | bc_vec_init(&stack, sizeof(long), NULL); |
| 5643 | bc_num_init(&intp, n->len); |
| 5644 | bc_num_init(&fracp, n->rdx); |
| 5645 | bc_num_init(&digit, width); |
| 5646 | bc_num_init(&frac_len, BC_NUM_INT(n)); |
| 5647 | bc_num_copy(&intp, n); |
| 5648 | bc_num_one(&frac_len); |
| 5649 | |
| 5650 | bc_num_truncate(&intp, intp.rdx); |
| 5651 | s = zbc_num_sub(n, &intp, &fracp, 0); |
| 5652 | if (s) goto err; |
| 5653 | |
| 5654 | while (intp.len != 0) { |
| 5655 | s = zbc_num_divmod(&intp, base, &intp, &digit, 0); |
| 5656 | if (s) goto err; |
| 5657 | s = zbc_num_ulong(&digit, &dig); |
| 5658 | if (s) goto err; |
| 5659 | bc_vec_push(&stack, &dig); |
| 5660 | } |
| 5661 | |
| 5662 | for (i = 0; i < stack.len; ++i) { |
| 5663 | ptr = bc_vec_item_rev(&stack, i); |
| 5664 | print(*ptr, width, false); |
| 5665 | } |
| 5666 | |
| 5667 | if (!n->rdx) goto err; |
| 5668 | |
| 5669 | for (radix = true; frac_len.len <= n->rdx; radix = false) { |
| 5670 | s = zbc_num_mul(&fracp, base, &fracp, n->rdx); |
| 5671 | if (s) goto err; |
| 5672 | s = zbc_num_ulong(&fracp, &dig); |
| 5673 | if (s) goto err; |
| 5674 | bc_num_ulong2num(&intp, dig); |
| 5675 | s = zbc_num_sub(&fracp, &intp, &fracp, 0); |
| 5676 | if (s) goto err; |
| 5677 | print(dig, width, radix); |
| 5678 | s = zbc_num_mul(&frac_len, base, &frac_len, 0); |
| 5679 | if (s) goto err; |
| 5680 | } |
| 5681 | |
| 5682 | err: |
| 5683 | bc_num_free(&frac_len); |
| 5684 | bc_num_free(&digit); |
| 5685 | bc_num_free(&fracp); |
| 5686 | bc_num_free(&intp); |
| 5687 | bc_vec_free(&stack); |
| 5688 | RETURN_STATUS(s); |
| 5689 | } |
| 5690 | #if ERRORS_ARE_FATAL |
| 5691 | # define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 5692 | #endif |
| 5693 | |
| 5694 | static BC_STATUS zbc_num_printBase(BcNum *n) |
| 5695 | { |
| 5696 | BcStatus s; |
| 5697 | size_t width, i; |
| 5698 | BcNumDigitOp print; |
| 5699 | bool neg = n->neg; |
| 5700 | |
| 5701 | if (neg) { |
| 5702 | bb_putchar('-'); |
| 5703 | G.prog.nchars++; |
| 5704 | } |
| 5705 | |
| 5706 | n->neg = false; |
| 5707 | |
| 5708 | if (G.prog.ob_t <= BC_NUM_MAX_IBASE) { |
| 5709 | width = 1; |
| 5710 | print = bc_num_printHex; |
| 5711 | } |
| 5712 | else { |
| 5713 | for (i = G.prog.ob_t - 1, width = 0; i != 0; i /= 10, ++width) |
| 5714 | continue; |
| 5715 | print = bc_num_printDigits; |
| 5716 | } |
| 5717 | |
| 5718 | s = zbc_num_printNum(n, &G.prog.ob, width, print); |
| 5719 | n->neg = neg; |
| 5720 | |
| 5721 | RETURN_STATUS(s); |
| 5722 | } |
| 5723 | #if ERRORS_ARE_FATAL |
| 5724 | # define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 5725 | #endif |
| 5726 | |
| 5727 | #if ENABLE_DC |
| 5728 | static BC_STATUS zbc_num_stream(BcNum *n, BcNum *base) |
| 5729 | { |
| 5730 | RETURN_STATUS(zbc_num_printNum(n, base, 1, bc_num_printChar)); |
| 5731 | } |
| 5732 | #if ERRORS_ARE_FATAL |
| 5733 | # define zbc_num_stream(...) (zbc_num_stream(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 5734 | #endif |
| 5735 | #endif |
| 5736 | |
| 5737 | static BC_STATUS zbc_num_print(BcNum *n, bool newline) |
| 5738 | { |
| 5739 | BcStatus s = BC_STATUS_SUCCESS; |
| 5740 | |
| 5741 | bc_num_printNewline(); |
| 5742 | |
| 5743 | if (n->len == 0) { |
| 5744 | bb_putchar('0'); |
| 5745 | ++G.prog.nchars; |
| 5746 | } |
| 5747 | else if (G.prog.ob_t == 10) |
| 5748 | bc_num_printDecimal(n); |
| 5749 | else |
| 5750 | s = zbc_num_printBase(n); |
| 5751 | |
| 5752 | if (newline) { |
| 5753 | bb_putchar('\n'); |
| 5754 | G.prog.nchars = 0; |
| 5755 | } |
| 5756 | |
| 5757 | RETURN_STATUS(s); |
| 5758 | } |
| 5759 | #if ERRORS_ARE_FATAL |
| 5760 | # define zbc_num_print(...) (zbc_num_print(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 5761 | #endif |
| 5762 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5763 | static BC_STATUS zbc_program_print(char inst, size_t idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5764 | { |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5765 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5766 | BcResult *r; |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5767 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5768 | bool pop = inst != BC_INST_PRINT; |
| 5769 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5770 | if (!BC_PROG_STACK(&G.prog.results, idx + 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5771 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5772 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5773 | r = bc_vec_item_rev(&G.prog.results, idx); |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5774 | num = NULL; // is this NULL necessary? |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5775 | s = zbc_program_num(r, &num, false); |
| 5776 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5777 | |
| 5778 | if (BC_PROG_NUM(r, num)) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5779 | s = zbc_num_print(num, !pop); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5780 | if (!s) bc_num_copy(&G.prog.last, num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5781 | } |
| 5782 | else { |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5783 | char *str; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5784 | |
| 5785 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx; |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 5786 | str = *bc_program_str(idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5787 | |
| 5788 | if (inst == BC_INST_PRINT_STR) { |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5789 | for (;;) { |
| 5790 | char c = *str++; |
| 5791 | if (c == '\0') break; |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5792 | bb_putchar(c); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5793 | ++G.prog.nchars; |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5794 | if (c == '\n') G.prog.nchars = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5795 | } |
| 5796 | } |
| 5797 | else { |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 5798 | bc_program_printString(str); |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5799 | if (inst == BC_INST_PRINT) bb_putchar('\n'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5800 | } |
| 5801 | } |
| 5802 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5803 | if (!s && pop) bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5804 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5805 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5806 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5807 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5808 | # define zbc_program_print(...) (zbc_program_print(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5809 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5810 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5811 | static BC_STATUS zbc_program_negate(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5812 | { |
| 5813 | BcStatus s; |
| 5814 | BcResult res, *ptr; |
| 5815 | BcNum *num = NULL; |
| 5816 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5817 | s = zbc_program_prep(&ptr, &num); |
| 5818 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5819 | |
| 5820 | bc_num_init(&res.d.n, num->len); |
| 5821 | bc_num_copy(&res.d.n, num); |
| 5822 | if (res.d.n.len) res.d.n.neg = !res.d.n.neg; |
| 5823 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5824 | bc_program_retire(&res, BC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5825 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5826 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5827 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5828 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5829 | # define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5830 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5831 | |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 5832 | static BC_STATUS zbc_program_logical(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5833 | { |
| 5834 | BcStatus s; |
| 5835 | BcResult *opd1, *opd2, res; |
| 5836 | BcNum *n1, *n2; |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5837 | ssize_t cond; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5838 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5839 | s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 5840 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5841 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5842 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5843 | |
| 5844 | if (inst == BC_INST_BOOL_AND) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5845 | cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5846 | else if (inst == BC_INST_BOOL_OR) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5847 | cond = bc_num_cmp(n1, &G.prog.zero) || bc_num_cmp(n2, &G.prog.zero); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5848 | else { |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5849 | cond = bc_num_cmp(n1, n2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5850 | switch (inst) { |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5851 | case BC_INST_REL_EQ: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5852 | cond = (cond == 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5853 | break; |
| 5854 | case BC_INST_REL_LE: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5855 | cond = (cond <= 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5856 | break; |
| 5857 | case BC_INST_REL_GE: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5858 | cond = (cond >= 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5859 | break; |
| 5860 | case BC_INST_REL_LT: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5861 | cond = (cond < 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5862 | break; |
| 5863 | case BC_INST_REL_GT: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5864 | cond = (cond > 0); |
| 5865 | break; |
| 5866 | default: // = case BC_INST_REL_NE: |
| 5867 | //cond = (cond != 0); - not needed |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5868 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5869 | } |
| 5870 | } |
| 5871 | |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5872 | if (cond) bc_num_one(&res.d.n); |
| 5873 | //else bc_num_zero(&res.d.n); - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5874 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5875 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5876 | |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 5877 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5878 | } |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 5879 | #if ERRORS_ARE_FATAL |
| 5880 | # define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 5881 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5882 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5883 | #if ENABLE_DC |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5884 | static BC_STATUS zbc_program_assignStr(BcResult *r, BcVec *v, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5885 | bool push) |
| 5886 | { |
| 5887 | BcNum n2; |
| 5888 | BcResult res; |
| 5889 | |
| 5890 | memset(&n2, 0, sizeof(BcNum)); |
| 5891 | n2.rdx = res.d.id.idx = r->d.id.idx; |
| 5892 | res.t = BC_RESULT_STR; |
| 5893 | |
| 5894 | if (!push) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5895 | if (!BC_PROG_STACK(&G.prog.results, 2)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5896 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5897 | bc_vec_pop(v); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5898 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5899 | } |
| 5900 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5901 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5902 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5903 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5904 | bc_vec_push(v, &n2); |
| 5905 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5906 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5907 | } |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 5908 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5909 | # define zbc_program_assignStr(...) (zbc_program_assignStr(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 5910 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5911 | #endif // ENABLE_DC |
| 5912 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5913 | static BC_STATUS zbc_program_copyToVar(char *name, bool var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5914 | { |
| 5915 | BcStatus s; |
| 5916 | BcResult *ptr, r; |
| 5917 | BcVec *v; |
| 5918 | BcNum *n; |
| 5919 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5920 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5921 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5922 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5923 | ptr = bc_vec_top(&G.prog.results); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5924 | if ((ptr->t == BC_RESULT_ARRAY) != !var) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5925 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5926 | v = bc_program_search(name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5927 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5928 | #if ENABLE_DC |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5929 | if (ptr->t == BC_RESULT_STR && !var) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5930 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
| 5931 | if (ptr->t == BC_RESULT_STR) |
| 5932 | RETURN_STATUS(zbc_program_assignStr(ptr, v, true)); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5933 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5934 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5935 | s = zbc_program_num(ptr, &n, false); |
| 5936 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5937 | |
| 5938 | // Do this once more to make sure that pointers were not invalidated. |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5939 | v = bc_program_search(name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5940 | |
| 5941 | if (var) { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5942 | bc_num_init_DEF_SIZE(&r.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5943 | bc_num_copy(&r.d.n, n); |
| 5944 | } |
| 5945 | else { |
| 5946 | bc_array_init(&r.d.v, true); |
| 5947 | bc_array_copy(&r.d.v, (BcVec *) n); |
| 5948 | } |
| 5949 | |
| 5950 | bc_vec_push(v, &r.d); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5951 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5952 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5953 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5954 | } |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 5955 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5956 | # define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 5957 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5958 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5959 | static BC_STATUS zbc_program_assign(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5960 | { |
| 5961 | BcStatus s; |
| 5962 | BcResult *left, *right, res; |
| 5963 | BcNum *l = NULL, *r = NULL; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5964 | bool assign = inst == BC_INST_ASSIGN, ib, sc; |
| 5965 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5966 | s = zbc_program_binOpPrep(&left, &l, &right, &r, assign); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5967 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5968 | |
| 5969 | ib = left->t == BC_RESULT_IBASE; |
| 5970 | sc = left->t == BC_RESULT_SCALE; |
| 5971 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5972 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5973 | |
| 5974 | if (right->t == BC_RESULT_STR) { |
| 5975 | |
| 5976 | BcVec *v; |
| 5977 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5978 | if (left->t != BC_RESULT_VAR) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5979 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5980 | v = bc_program_search(left->d.id.name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5981 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5982 | RETURN_STATUS(zbc_program_assignStr(right, v, false)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5983 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5984 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5985 | |
| 5986 | if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP) |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5987 | RETURN_STATUS(bc_error("bad assignment:" |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5988 | " left side must be scale," |
| 5989 | " ibase, obase, last, var," |
| 5990 | " or array element" |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5991 | )); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5992 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5993 | #if ENABLE_BC |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5994 | if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5995 | RETURN_STATUS(bc_error("divide by zero")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5996 | |
| 5997 | if (assign) |
| 5998 | bc_num_copy(l, r); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5999 | else { |
| 6000 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 6001 | ERROR_RETURN(s =) zbc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6002 | } |
| 6003 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6004 | #else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6005 | bc_num_copy(l, r); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6006 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6007 | |
| 6008 | if (ib || sc || left->t == BC_RESULT_OBASE) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6009 | static const char *const msg[] = { |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 6010 | "bad ibase; must be [2,16]", //BC_RESULT_IBASE |
| 6011 | "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE |
| 6012 | NULL, //can't happen //BC_RESULT_LAST |
| 6013 | NULL, //can't happen //BC_RESULT_CONSTANT |
| 6014 | NULL, //can't happen //BC_RESULT_ONE |
| 6015 | "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6016 | }; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6017 | size_t *ptr; |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 6018 | unsigned long val, max; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6019 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6020 | s = zbc_num_ulong(l, &val); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6021 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6022 | s = left->t - BC_RESULT_IBASE; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6023 | if (sc) { |
| 6024 | max = BC_MAX_SCALE; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6025 | ptr = &G.prog.scale; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6026 | } |
| 6027 | else { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6028 | if (val < BC_NUM_MIN_BASE) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6029 | RETURN_STATUS(bc_error(msg[s])); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6030 | max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6031 | ptr = ib ? &G.prog.ib_t : &G.prog.ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6032 | } |
| 6033 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6034 | if (val > max) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6035 | RETURN_STATUS(bc_error(msg[s])); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6036 | if (!sc) |
| 6037 | bc_num_copy(ib ? &G.prog.ib : &G.prog.ob, l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6038 | |
| 6039 | *ptr = (size_t) val; |
| 6040 | s = BC_STATUS_SUCCESS; |
| 6041 | } |
| 6042 | |
| 6043 | bc_num_init(&res.d.n, l->len); |
| 6044 | bc_num_copy(&res.d.n, l); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6045 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6046 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6047 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6048 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6049 | #if ERRORS_ARE_FATAL |
| 6050 | # define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 6051 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6052 | |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6053 | #if !ENABLE_DC |
| 6054 | #define bc_program_pushVar(code, bgn, pop, copy) \ |
| 6055 | bc_program_pushVar(code, bgn) |
| 6056 | // for bc, 'pop' and 'copy' are always false |
| 6057 | #endif |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 6058 | static BC_STATUS bc_program_pushVar(char *code, size_t *bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6059 | bool pop, bool copy) |
| 6060 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6061 | BcResult r; |
| 6062 | char *name = bc_program_name(code, bgn); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6063 | |
| 6064 | r.t = BC_RESULT_VAR; |
| 6065 | r.d.id.name = name; |
| 6066 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6067 | #if ENABLE_DC |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6068 | { |
| 6069 | BcVec *v = bc_program_search(name, true); |
| 6070 | BcNum *num = bc_vec_top(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6071 | |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6072 | if (pop || copy) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6073 | |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6074 | if (!BC_PROG_STACK(v, 2 - copy)) { |
| 6075 | free(name); |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 6076 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6077 | } |
| 6078 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6079 | free(name); |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6080 | name = NULL; |
| 6081 | |
| 6082 | if (!BC_PROG_STR(num)) { |
| 6083 | |
| 6084 | r.t = BC_RESULT_TEMP; |
| 6085 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6086 | bc_num_init_DEF_SIZE(&r.d.n); |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6087 | bc_num_copy(&r.d.n, num); |
| 6088 | } |
| 6089 | else { |
| 6090 | r.t = BC_RESULT_STR; |
| 6091 | r.d.id.idx = num->rdx; |
| 6092 | } |
| 6093 | |
| 6094 | if (!copy) bc_vec_pop(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6095 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6096 | } |
| 6097 | #endif // ENABLE_DC |
| 6098 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6099 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6100 | |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 6101 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6102 | } |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 6103 | #if ERRORS_ARE_FATAL |
| 6104 | # define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 6105 | #else |
| 6106 | # define zbc_program_pushVar(...) bc_program_pushVar(__VA_ARGS__) |
| 6107 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6108 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6109 | static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6110 | char inst) |
| 6111 | { |
| 6112 | BcStatus s = BC_STATUS_SUCCESS; |
| 6113 | BcResult r; |
| 6114 | BcNum *num; |
| 6115 | |
| 6116 | r.d.id.name = bc_program_name(code, bgn); |
| 6117 | |
| 6118 | if (inst == BC_INST_ARRAY) { |
| 6119 | r.t = BC_RESULT_ARRAY; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6120 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6121 | } |
| 6122 | else { |
| 6123 | |
| 6124 | BcResult *operand; |
| 6125 | unsigned long temp; |
| 6126 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6127 | s = zbc_program_prep(&operand, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6128 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6129 | s = zbc_num_ulong(num, &temp); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6130 | if (s) goto err; |
| 6131 | |
| 6132 | if (temp > BC_MAX_DIM) { |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 6133 | s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6134 | goto err; |
| 6135 | } |
| 6136 | |
| 6137 | r.d.id.idx = (size_t) temp; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6138 | bc_program_retire(&r, BC_RESULT_ARRAY_ELEM); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6139 | } |
| 6140 | |
| 6141 | err: |
| 6142 | if (s) free(r.d.id.name); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6143 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6144 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6145 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6146 | # define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6147 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6148 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6149 | #if ENABLE_BC |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6150 | static BC_STATUS zbc_program_incdec(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6151 | { |
| 6152 | BcStatus s; |
| 6153 | BcResult *ptr, res, copy; |
| 6154 | BcNum *num = NULL; |
| 6155 | char inst2 = inst; |
| 6156 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6157 | s = zbc_program_prep(&ptr, &num); |
| 6158 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6159 | |
| 6160 | if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) { |
| 6161 | copy.t = BC_RESULT_TEMP; |
| 6162 | bc_num_init(©.d.n, num->len); |
| 6163 | bc_num_copy(©.d.n, num); |
| 6164 | } |
| 6165 | |
| 6166 | res.t = BC_RESULT_ONE; |
| 6167 | inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ? |
| 6168 | BC_INST_ASSIGN_PLUS : |
| 6169 | BC_INST_ASSIGN_MINUS; |
| 6170 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6171 | bc_vec_push(&G.prog.results, &res); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6172 | s = zbc_program_assign(inst); |
| 6173 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6174 | |
| 6175 | if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6176 | bc_vec_pop(&G.prog.results); |
| 6177 | bc_vec_push(&G.prog.results, ©); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6178 | } |
| 6179 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6180 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6181 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6182 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6183 | # define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6184 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6185 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6186 | static BC_STATUS zbc_program_call(char *code, size_t *idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6187 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6188 | BcInstPtr ip; |
| 6189 | size_t i, nparams = bc_program_index(code, idx); |
| 6190 | BcFunc *func; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6191 | BcId *a; |
| 6192 | BcResultData param; |
| 6193 | BcResult *arg; |
| 6194 | |
| 6195 | ip.idx = 0; |
| 6196 | ip.func = bc_program_index(code, idx); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6197 | func = bc_program_func(ip.func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6198 | |
Denys Vlasenko | 04a1c76 | 2018-12-03 21:10:57 +0100 | [diff] [blame] | 6199 | if (func->code.len == 0) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6200 | RETURN_STATUS(bc_error("undefined function")); |
Denys Vlasenko | 04a1c76 | 2018-12-03 21:10:57 +0100 | [diff] [blame] | 6201 | } |
| 6202 | if (nparams != func->nparams) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6203 | RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams)); |
Denys Vlasenko | 04a1c76 | 2018-12-03 21:10:57 +0100 | [diff] [blame] | 6204 | } |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6205 | ip.len = G.prog.results.len - nparams; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6206 | |
| 6207 | for (i = 0; i < nparams; ++i) { |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6208 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6209 | |
| 6210 | a = bc_vec_item(&func->autos, nparams - 1 - i); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6211 | arg = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6212 | |
| 6213 | if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6214 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6215 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6216 | s = zbc_program_copyToVar(a->name, a->idx); |
| 6217 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6218 | } |
| 6219 | |
| 6220 | for (; i < func->autos.len; ++i) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6221 | BcVec *v; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6222 | |
| 6223 | a = bc_vec_item(&func->autos, i); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6224 | v = bc_program_search(a->name, a->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6225 | |
| 6226 | if (a->idx) { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6227 | bc_num_init_DEF_SIZE(¶m.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6228 | bc_vec_push(v, ¶m.n); |
| 6229 | } |
| 6230 | else { |
| 6231 | bc_array_init(¶m.v, true); |
| 6232 | bc_vec_push(v, ¶m.v); |
| 6233 | } |
| 6234 | } |
| 6235 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6236 | bc_vec_push(&G.prog.stack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6237 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6238 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6239 | } |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6240 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6241 | # define zbc_program_call(...) (zbc_program_call(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6242 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6243 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6244 | static BC_STATUS zbc_program_return(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6245 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6246 | BcResult res; |
| 6247 | BcFunc *f; |
| 6248 | size_t i; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6249 | BcInstPtr *ip = bc_vec_top(&G.prog.stack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6250 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6251 | if (!BC_PROG_STACK(&G.prog.results, ip->len + inst == BC_INST_RET)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6252 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6253 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6254 | f = bc_program_func(ip->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6255 | res.t = BC_RESULT_TEMP; |
| 6256 | |
| 6257 | if (inst == BC_INST_RET) { |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6258 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6259 | BcNum *num; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6260 | BcResult *operand = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6261 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6262 | s = zbc_program_num(operand, &num, false); |
| 6263 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6264 | bc_num_init(&res.d.n, num->len); |
| 6265 | bc_num_copy(&res.d.n, num); |
| 6266 | } |
| 6267 | else { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6268 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 6269 | //bc_num_zero(&res.d.n); - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6270 | } |
| 6271 | |
| 6272 | // We need to pop arguments as well, so this takes that into account. |
| 6273 | for (i = 0; i < f->autos.len; ++i) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6274 | BcVec *v; |
| 6275 | BcId *a = bc_vec_item(&f->autos, i); |
| 6276 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6277 | v = bc_program_search(a->name, a->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6278 | bc_vec_pop(v); |
| 6279 | } |
| 6280 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6281 | bc_vec_npop(&G.prog.results, G.prog.results.len - ip->len); |
| 6282 | bc_vec_push(&G.prog.results, &res); |
| 6283 | bc_vec_pop(&G.prog.stack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6284 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6285 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6286 | } |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6287 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6288 | # define zbc_program_return(...) (zbc_program_return(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6289 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6290 | #endif // ENABLE_BC |
| 6291 | |
| 6292 | static unsigned long bc_program_scale(BcNum *n) |
| 6293 | { |
| 6294 | return (unsigned long) n->rdx; |
| 6295 | } |
| 6296 | |
| 6297 | static unsigned long bc_program_len(BcNum *n) |
| 6298 | { |
Denys Vlasenko | a7f1a36 | 2018-12-10 12:57:01 +0100 | [diff] [blame] | 6299 | size_t len = n->len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6300 | |
Denys Vlasenko | a7f1a36 | 2018-12-10 12:57:01 +0100 | [diff] [blame] | 6301 | if (n->rdx != len) return len; |
| 6302 | for (;;) { |
| 6303 | if (len == 0) break; |
| 6304 | len--; |
| 6305 | if (n->num[len] != 0) break; |
| 6306 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6307 | return len; |
| 6308 | } |
| 6309 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6310 | static BC_STATUS zbc_program_builtin(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6311 | { |
| 6312 | BcStatus s; |
| 6313 | BcResult *opnd; |
| 6314 | BcNum *num = NULL; |
| 6315 | BcResult res; |
| 6316 | bool len = inst == BC_INST_LENGTH; |
| 6317 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6318 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6319 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6320 | opnd = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6321 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6322 | s = zbc_program_num(opnd, &num, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6323 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6324 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6325 | #if ENABLE_DC |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6326 | if (!BC_PROG_NUM(opnd, num) && !len) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6327 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6328 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6329 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6330 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6331 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 6332 | if (inst == BC_INST_SQRT) s = zbc_num_sqrt(num, &res.d.n, G.prog.scale); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6333 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6334 | else if (len != 0 && opnd->t == BC_RESULT_ARRAY) { |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6335 | bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6336 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6337 | #endif |
| 6338 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6339 | else if (len != 0 && !BC_PROG_NUM(opnd, num)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6340 | char **str; |
| 6341 | size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx; |
| 6342 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6343 | str = bc_program_str(idx); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6344 | bc_num_ulong2num(&res.d.n, strlen(*str)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6345 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6346 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6347 | else { |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 6348 | bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6349 | } |
| 6350 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6351 | bc_program_retire(&res, BC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6352 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6353 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6354 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6355 | #if ERRORS_ARE_FATAL |
| 6356 | # define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 6357 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6358 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6359 | #if ENABLE_DC |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6360 | static BC_STATUS zbc_program_divmod(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6361 | { |
| 6362 | BcStatus s; |
| 6363 | BcResult *opd1, *opd2, res, res2; |
| 6364 | BcNum *n1, *n2 = NULL; |
| 6365 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6366 | s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6367 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6368 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6369 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6370 | bc_num_init(&res2.d.n, n2->len); |
| 6371 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 6372 | s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6373 | if (s) goto err; |
| 6374 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6375 | bc_program_binOpRetire(&res2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6376 | res.t = BC_RESULT_TEMP; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6377 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6378 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6379 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6380 | |
| 6381 | err: |
| 6382 | bc_num_free(&res2.d.n); |
| 6383 | bc_num_free(&res.d.n); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6384 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6385 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6386 | #if ERRORS_ARE_FATAL |
| 6387 | # define zbc_program_divmod(...) (zbc_program_divmod(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 6388 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6389 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6390 | static BC_STATUS zbc_program_modexp(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6391 | { |
| 6392 | BcStatus s; |
| 6393 | BcResult *r1, *r2, *r3, res; |
| 6394 | BcNum *n1, *n2, *n3; |
| 6395 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6396 | if (!BC_PROG_STACK(&G.prog.results, 3)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6397 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6398 | s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6399 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6400 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6401 | r1 = bc_vec_item_rev(&G.prog.results, 2); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6402 | s = zbc_program_num(r1, &n1, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6403 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6404 | if (!BC_PROG_NUM(r1, n1)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6405 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6406 | |
| 6407 | // Make sure that the values have their pointers updated, if necessary. |
| 6408 | if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) { |
| 6409 | |
| 6410 | if (r1->t == r2->t) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6411 | s = zbc_program_num(r2, &n2, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6412 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6413 | } |
| 6414 | |
| 6415 | if (r1->t == r3->t) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6416 | s = zbc_program_num(r3, &n3, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6417 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6418 | } |
| 6419 | } |
| 6420 | |
| 6421 | bc_num_init(&res.d.n, n3->len); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 6422 | s = zbc_num_modexp(n1, n2, n3, &res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6423 | if (s) goto err; |
| 6424 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6425 | bc_vec_pop(&G.prog.results); |
| 6426 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6427 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6428 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6429 | |
| 6430 | err: |
| 6431 | bc_num_free(&res.d.n); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6432 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6433 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6434 | #if ERRORS_ARE_FATAL |
| 6435 | # define zbc_program_modexp(...) (zbc_program_modexp(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 6436 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6437 | |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6438 | static void bc_program_stackLen(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6439 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6440 | BcResult res; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6441 | size_t len = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6442 | |
| 6443 | res.t = BC_RESULT_TEMP; |
| 6444 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6445 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6446 | bc_num_ulong2num(&res.d.n, len); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6447 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6448 | } |
| 6449 | |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6450 | static BC_STATUS zbc_program_asciify(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6451 | { |
| 6452 | BcStatus s; |
| 6453 | BcResult *r, res; |
Denys Vlasenko | 4a024c7 | 2018-12-09 13:21:54 +0100 | [diff] [blame] | 6454 | BcNum *num, n; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6455 | char *str, *str2, c; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6456 | size_t len = G.prog.strs.len, idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6457 | unsigned long val; |
| 6458 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6459 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6460 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6461 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6462 | |
Denys Vlasenko | 4a024c7 | 2018-12-09 13:21:54 +0100 | [diff] [blame] | 6463 | num = NULL; // TODO: is this NULL needed? |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6464 | s = zbc_program_num(r, &num, false); |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6465 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6466 | |
| 6467 | if (BC_PROG_NUM(r, num)) { |
| 6468 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6469 | bc_num_init_DEF_SIZE(&n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6470 | bc_num_copy(&n, num); |
| 6471 | bc_num_truncate(&n, n.rdx); |
| 6472 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 6473 | s = zbc_num_mod(&n, &G.prog.strmb, &n, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6474 | if (s) goto num_err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6475 | s = zbc_num_ulong(&n, &val); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6476 | if (s) goto num_err; |
| 6477 | |
| 6478 | c = (char) val; |
| 6479 | |
| 6480 | bc_num_free(&n); |
| 6481 | } |
| 6482 | else { |
| 6483 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx; |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6484 | str2 = *bc_program_str(idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6485 | c = str2[0]; |
| 6486 | } |
| 6487 | |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6488 | str = xzalloc(2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6489 | str[0] = c; |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6490 | //str[1] = '\0'; - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6491 | |
| 6492 | str2 = xstrdup(str); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6493 | bc_program_addFunc(str2, &idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6494 | |
| 6495 | if (idx != len + BC_PROG_REQ_FUNCS) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6496 | for (idx = 0; idx < G.prog.strs.len; ++idx) { |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6497 | if (strcmp(*bc_program_str(idx), str) == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6498 | len = idx; |
| 6499 | break; |
| 6500 | } |
| 6501 | } |
| 6502 | |
| 6503 | free(str); |
| 6504 | } |
| 6505 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6506 | bc_vec_push(&G.prog.strs, &str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6507 | |
| 6508 | res.t = BC_RESULT_STR; |
| 6509 | res.d.id.idx = len; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6510 | bc_vec_pop(&G.prog.results); |
| 6511 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6512 | |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6513 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6514 | |
| 6515 | num_err: |
| 6516 | bc_num_free(&n); |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6517 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6518 | } |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6519 | #if ERRORS_ARE_FATAL |
| 6520 | # define zbc_program_asciify(...) (zbc_program_asciify(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 6521 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6522 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6523 | static BC_STATUS zbc_program_printStream(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6524 | { |
| 6525 | BcStatus s; |
| 6526 | BcResult *r; |
| 6527 | BcNum *n = NULL; |
| 6528 | size_t idx; |
| 6529 | char *str; |
| 6530 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6531 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6532 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6533 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6534 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6535 | s = zbc_program_num(r, &n, false); |
| 6536 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6537 | |
| 6538 | if (BC_PROG_NUM(r, n)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6539 | s = zbc_num_stream(n, &G.prog.strmb); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6540 | else { |
| 6541 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx; |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6542 | str = *bc_program_str(idx); |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 6543 | printf("%s", str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6544 | } |
| 6545 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6546 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6547 | } |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6548 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6549 | # define zbc_program_printStream(...) (zbc_program_printStream(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6550 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6551 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6552 | static BC_STATUS zbc_program_nquit(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6553 | { |
| 6554 | BcStatus s; |
| 6555 | BcResult *opnd; |
| 6556 | BcNum *num = NULL; |
| 6557 | unsigned long val; |
| 6558 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6559 | s = zbc_program_prep(&opnd, &num); |
| 6560 | if (s) RETURN_STATUS(s); |
| 6561 | s = zbc_num_ulong(num, &val); |
| 6562 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6563 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6564 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6565 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6566 | if (G.prog.stack.len < val) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6567 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 6568 | if (G.prog.stack.len == val) { |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 6569 | QUIT_OR_RETURN_TO_MAIN; |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 6570 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6571 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6572 | bc_vec_npop(&G.prog.stack, val); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6573 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6574 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6575 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6576 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6577 | # define zbc_program_nquit(...) (zbc_program_nquit(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6578 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6579 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6580 | static BC_STATUS zbc_program_execStr(char *code, size_t *bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6581 | bool cond) |
| 6582 | { |
| 6583 | BcStatus s = BC_STATUS_SUCCESS; |
| 6584 | BcResult *r; |
| 6585 | char **str; |
| 6586 | BcFunc *f; |
| 6587 | BcParse prs; |
| 6588 | BcInstPtr ip; |
| 6589 | size_t fidx, sidx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6590 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6591 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6592 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6593 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6594 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6595 | |
| 6596 | if (cond) { |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6597 | BcNum *n = n; // for compiler |
| 6598 | bool exec; |
| 6599 | char *name; |
| 6600 | char *then_name = bc_program_name(code, bgn); |
| 6601 | char *else_name = NULL; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6602 | |
| 6603 | if (code[*bgn] == BC_PARSE_STREND) |
| 6604 | (*bgn) += 1; |
| 6605 | else |
| 6606 | else_name = bc_program_name(code, bgn); |
| 6607 | |
| 6608 | exec = r->d.n.len != 0; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6609 | name = then_name; |
| 6610 | if (!exec && else_name != NULL) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6611 | exec = true; |
| 6612 | name = else_name; |
| 6613 | } |
| 6614 | |
| 6615 | if (exec) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6616 | BcVec *v; |
| 6617 | v = bc_program_search(name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6618 | n = bc_vec_top(v); |
| 6619 | } |
| 6620 | |
| 6621 | free(then_name); |
| 6622 | free(else_name); |
| 6623 | |
| 6624 | if (!exec) goto exit; |
| 6625 | if (!BC_PROG_STR(n)) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6626 | s = bc_error_variable_is_wrong_type(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6627 | goto exit; |
| 6628 | } |
| 6629 | |
| 6630 | sidx = n->rdx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6631 | } else { |
| 6632 | if (r->t == BC_RESULT_STR) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6633 | sidx = r->d.id.idx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6634 | } else if (r->t == BC_RESULT_VAR) { |
| 6635 | BcNum *n; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6636 | s = zbc_program_num(r, &n, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6637 | if (s || !BC_PROG_STR(n)) goto exit; |
| 6638 | sidx = n->rdx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6639 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6640 | goto exit; |
| 6641 | } |
| 6642 | |
| 6643 | fidx = sidx + BC_PROG_REQ_FUNCS; |
| 6644 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6645 | str = bc_program_str(sidx); |
| 6646 | f = bc_program_func(fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6647 | |
| 6648 | if (f->code.len == 0) { |
Denys Vlasenko | c2265f5 | 2018-12-12 23:10:08 +0100 | [diff] [blame] | 6649 | bc_parse_create(&prs, fidx); |
Denys Vlasenko | f86e960 | 2018-12-14 17:01:56 +0100 | [diff] [blame] | 6650 | s = zbc_parse_text_init(&prs, *str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6651 | if (s) goto err; |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 6652 | s = zcommon_parse_expr(&prs, BC_PARSE_NOCALL); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6653 | if (s) goto err; |
| 6654 | |
| 6655 | if (prs.l.t.t != BC_LEX_EOF) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6656 | s = bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6657 | goto err; |
| 6658 | } |
| 6659 | |
| 6660 | bc_parse_free(&prs); |
| 6661 | } |
| 6662 | |
| 6663 | ip.idx = 0; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6664 | ip.len = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6665 | ip.func = fidx; |
| 6666 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6667 | bc_vec_pop(&G.prog.results); |
| 6668 | bc_vec_push(&G.prog.stack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6669 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6670 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6671 | |
| 6672 | err: |
| 6673 | bc_parse_free(&prs); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6674 | f = bc_program_func(fidx); |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 6675 | bc_vec_pop_all(&f->code); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6676 | exit: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6677 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6678 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6679 | } |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6680 | #if ERRORS_ARE_FATAL |
| 6681 | # define zbc_program_execStr(...) (zbc_program_execStr(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 6682 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6683 | #endif // ENABLE_DC |
| 6684 | |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6685 | static void bc_program_pushGlobal(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6686 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6687 | BcResult res; |
| 6688 | unsigned long val; |
| 6689 | |
| 6690 | res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE; |
| 6691 | if (inst == BC_INST_IBASE) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6692 | val = (unsigned long) G.prog.ib_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6693 | else if (inst == BC_INST_SCALE) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6694 | val = (unsigned long) G.prog.scale; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6695 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6696 | val = (unsigned long) G.prog.ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6697 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6698 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6699 | bc_num_ulong2num(&res.d.n, val); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6700 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6701 | } |
| 6702 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6703 | static void bc_program_addFunc(char *name, size_t *idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6704 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6705 | BcId entry, *entry_ptr; |
| 6706 | BcFunc f; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 6707 | int inserted; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6708 | |
| 6709 | entry.name = name; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6710 | entry.idx = G.prog.fns.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6711 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 6712 | inserted = bc_map_insert(&G.prog.fn_map, &entry, idx); |
| 6713 | if (!inserted) free(name); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6714 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6715 | entry_ptr = bc_vec_item(&G.prog.fn_map, *idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6716 | *idx = entry_ptr->idx; |
| 6717 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 6718 | if (!inserted) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6719 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6720 | BcFunc *func = bc_program_func(entry_ptr->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6721 | |
| 6722 | // We need to reset these, so the function can be repopulated. |
| 6723 | func->nparams = 0; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 6724 | bc_vec_pop_all(&func->autos); |
| 6725 | bc_vec_pop_all(&func->code); |
| 6726 | bc_vec_pop_all(&func->labels); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6727 | } |
| 6728 | else { |
| 6729 | bc_func_init(&f); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6730 | bc_vec_push(&G.prog.fns, &f); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6731 | } |
| 6732 | } |
| 6733 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6734 | static BC_STATUS zbc_program_exec(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6735 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6736 | BcResult r, *ptr; |
| 6737 | BcNum *num; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6738 | BcInstPtr *ip = bc_vec_top(&G.prog.stack); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6739 | BcFunc *func = bc_program_func(ip->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6740 | char *code = func->code.v; |
| 6741 | bool cond = false; |
| 6742 | |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6743 | while (ip->idx < func->code.len) { |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6744 | BcStatus s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6745 | char inst = code[(ip->idx)++]; |
| 6746 | |
| 6747 | switch (inst) { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6748 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6749 | case BC_INST_JUMP_ZERO: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6750 | s = zbc_program_prep(&ptr, &num); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6751 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6752 | cond = !bc_num_cmp(num, &G.prog.zero); |
| 6753 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6754 | // Fallthrough. |
| 6755 | case BC_INST_JUMP: { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6756 | size_t *addr; |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6757 | size_t idx = bc_program_index(code, &ip->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6758 | addr = bc_vec_item(&func->labels, idx); |
| 6759 | if (inst == BC_INST_JUMP || cond) ip->idx = *addr; |
| 6760 | break; |
| 6761 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6762 | case BC_INST_CALL: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6763 | s = zbc_program_call(code, &ip->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6764 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6765 | case BC_INST_INC_PRE: |
| 6766 | case BC_INST_DEC_PRE: |
| 6767 | case BC_INST_INC_POST: |
| 6768 | case BC_INST_DEC_POST: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6769 | s = zbc_program_incdec(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6770 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6771 | case BC_INST_HALT: |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 6772 | QUIT_OR_RETURN_TO_MAIN; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6773 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6774 | case BC_INST_RET: |
| 6775 | case BC_INST_RET0: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6776 | s = zbc_program_return(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6777 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6778 | case BC_INST_BOOL_OR: |
| 6779 | case BC_INST_BOOL_AND: |
| 6780 | #endif // ENABLE_BC |
| 6781 | case BC_INST_REL_EQ: |
| 6782 | case BC_INST_REL_LE: |
| 6783 | case BC_INST_REL_GE: |
| 6784 | case BC_INST_REL_NE: |
| 6785 | case BC_INST_REL_LT: |
| 6786 | case BC_INST_REL_GT: |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 6787 | s = zbc_program_logical(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6788 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6789 | case BC_INST_READ: |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 6790 | s = zbc_program_read(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6791 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6792 | case BC_INST_VAR: |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 6793 | s = zbc_program_pushVar(code, &ip->idx, false, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6794 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6795 | case BC_INST_ARRAY_ELEM: |
| 6796 | case BC_INST_ARRAY: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6797 | s = zbc_program_pushArray(code, &ip->idx, inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6798 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6799 | case BC_INST_LAST: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6800 | r.t = BC_RESULT_LAST; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6801 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6802 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6803 | case BC_INST_IBASE: |
| 6804 | case BC_INST_SCALE: |
| 6805 | case BC_INST_OBASE: |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6806 | bc_program_pushGlobal(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6807 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6808 | case BC_INST_SCALE_FUNC: |
| 6809 | case BC_INST_LENGTH: |
| 6810 | case BC_INST_SQRT: |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6811 | s = zbc_program_builtin(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6812 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6813 | case BC_INST_NUM: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6814 | r.t = BC_RESULT_CONSTANT; |
| 6815 | r.d.id.idx = bc_program_index(code, &ip->idx); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6816 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6817 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6818 | case BC_INST_POP: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6819 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6820 | s = bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6821 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6822 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6823 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6824 | case BC_INST_POP_EXEC: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6825 | bc_vec_pop(&G.prog.stack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6826 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6827 | case BC_INST_PRINT: |
| 6828 | case BC_INST_PRINT_POP: |
| 6829 | case BC_INST_PRINT_STR: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6830 | s = zbc_program_print(inst, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6831 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6832 | case BC_INST_STR: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6833 | r.t = BC_RESULT_STR; |
| 6834 | r.d.id.idx = bc_program_index(code, &ip->idx); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6835 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6836 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6837 | case BC_INST_POWER: |
| 6838 | case BC_INST_MULTIPLY: |
| 6839 | case BC_INST_DIVIDE: |
| 6840 | case BC_INST_MODULUS: |
| 6841 | case BC_INST_PLUS: |
| 6842 | case BC_INST_MINUS: |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 6843 | s = zbc_program_op(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6844 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6845 | case BC_INST_BOOL_NOT: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6846 | s = zbc_program_prep(&ptr, &num); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6847 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6848 | bc_num_init_DEF_SIZE(&r.d.n); |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6849 | if (!bc_num_cmp(num, &G.prog.zero)) |
| 6850 | bc_num_one(&r.d.n); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 6851 | //else bc_num_zero(&r.d.n); - already is |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6852 | bc_program_retire(&r, BC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6853 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6854 | case BC_INST_NEG: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6855 | s = zbc_program_negate(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6856 | break; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6857 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6858 | case BC_INST_ASSIGN_POWER: |
| 6859 | case BC_INST_ASSIGN_MULTIPLY: |
| 6860 | case BC_INST_ASSIGN_DIVIDE: |
| 6861 | case BC_INST_ASSIGN_MODULUS: |
| 6862 | case BC_INST_ASSIGN_PLUS: |
| 6863 | case BC_INST_ASSIGN_MINUS: |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6864 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6865 | case BC_INST_ASSIGN: |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6866 | s = zbc_program_assign(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6867 | break; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6868 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6869 | case BC_INST_MODEXP: |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6870 | s = zbc_program_modexp(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6871 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6872 | case BC_INST_DIVMOD: |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6873 | s = zbc_program_divmod(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6874 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6875 | case BC_INST_EXECUTE: |
| 6876 | case BC_INST_EXEC_COND: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6877 | cond = inst == BC_INST_EXEC_COND; |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6878 | s = zbc_program_execStr(code, &ip->idx, cond); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6879 | break; |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6880 | case BC_INST_PRINT_STACK: { |
| 6881 | size_t idx; |
| 6882 | for (idx = 0; idx < G.prog.results.len; ++idx) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6883 | s = zbc_program_print(BC_INST_PRINT, idx); |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6884 | if (s) break; |
| 6885 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6886 | break; |
| 6887 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6888 | case BC_INST_CLEAR_STACK: |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 6889 | bc_vec_pop_all(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6890 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6891 | case BC_INST_STACK_LEN: |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6892 | bc_program_stackLen(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6893 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6894 | case BC_INST_DUPLICATE: |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6895 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6896 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6897 | ptr = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6898 | bc_result_copy(&r, ptr); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6899 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6900 | break; |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6901 | case BC_INST_SWAP: { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6902 | BcResult *ptr2; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6903 | if (!BC_PROG_STACK(&G.prog.results, 2)) |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6904 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6905 | ptr = bc_vec_item_rev(&G.prog.results, 0); |
| 6906 | ptr2 = bc_vec_item_rev(&G.prog.results, 1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6907 | memcpy(&r, ptr, sizeof(BcResult)); |
| 6908 | memcpy(ptr, ptr2, sizeof(BcResult)); |
| 6909 | memcpy(ptr2, &r, sizeof(BcResult)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6910 | break; |
| 6911 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6912 | case BC_INST_ASCIIFY: |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6913 | s = zbc_program_asciify(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6914 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6915 | case BC_INST_PRINT_STREAM: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6916 | s = zbc_program_printStream(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6917 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6918 | case BC_INST_LOAD: |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6919 | case BC_INST_PUSH_VAR: { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6920 | bool copy = inst == BC_INST_LOAD; |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 6921 | s = zbc_program_pushVar(code, &ip->idx, true, copy); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6922 | break; |
| 6923 | } |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6924 | case BC_INST_PUSH_TO_VAR: { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6925 | char *name = bc_program_name(code, &ip->idx); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6926 | s = zbc_program_copyToVar(name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6927 | free(name); |
| 6928 | break; |
| 6929 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6930 | case BC_INST_QUIT: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6931 | if (G.prog.stack.len <= 2) |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 6932 | QUIT_OR_RETURN_TO_MAIN; |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 6933 | bc_vec_npop(&G.prog.stack, 2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6934 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6935 | case BC_INST_NQUIT: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6936 | s = zbc_program_nquit(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6937 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6938 | #endif // ENABLE_DC |
| 6939 | } |
| 6940 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6941 | if (s || G_interrupt) { |
| 6942 | bc_program_reset(); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6943 | RETURN_STATUS(s); |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6944 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6945 | |
| 6946 | // If the stack has changed, pointers may be invalid. |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6947 | ip = bc_vec_top(&G.prog.stack); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6948 | func = bc_program_func(ip->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6949 | code = func->code.v; |
| 6950 | } |
| 6951 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6952 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6953 | } |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6954 | #if ERRORS_ARE_FATAL |
| 6955 | # define zbc_program_exec(...) (zbc_program_exec(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 6956 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6957 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6958 | static unsigned bc_vm_envLen(const char *var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6959 | { |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6960 | char *lenv; |
| 6961 | unsigned len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6962 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6963 | lenv = getenv(var); |
| 6964 | len = BC_NUM_PRINT_WIDTH; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6965 | if (!lenv) return len; |
| 6966 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6967 | len = bb_strtou(lenv, NULL, 10) - 1; |
| 6968 | if (errno || len < 2 || len >= INT_MAX) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6969 | len = BC_NUM_PRINT_WIDTH; |
| 6970 | |
| 6971 | return len; |
| 6972 | } |
| 6973 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6974 | static BC_STATUS zbc_vm_process(const char *text) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6975 | { |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 6976 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6977 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 6978 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
| 6979 | s = zbc_parse_text_init(&G.prs, text); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6980 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6981 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6982 | while (G.prs.l.t.t != BC_LEX_EOF) { |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 6983 | dbg_lex("%s:%d G.prs.l.t.t:%d", __func__, __LINE__, G.prs.l.t.t); |
Denys Vlasenko | c0ef23c | 2018-12-12 23:03:10 +0100 | [diff] [blame] | 6984 | ERROR_RETURN(s =) zcommon_parse(&G.prs); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6985 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6986 | } |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 6987 | dbg_lex("%s:%d G.prs.l.t.t:BC_LEX_EOF", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6988 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6989 | if (BC_PARSE_CAN_EXEC(&G.prs)) { |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6990 | s = zbc_program_exec(); |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 6991 | fflush_and_check(); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 6992 | if (s) |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6993 | bc_program_reset(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6994 | } |
| 6995 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 6996 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6997 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6998 | } |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6999 | #if ERRORS_ARE_FATAL |
| 7000 | # define zbc_vm_process(...) (zbc_vm_process(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 7001 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7002 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 7003 | static BC_STATUS zbc_vm_file(const char *file) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7004 | { |
Denys Vlasenko | 0fe270e | 2018-12-13 19:58:58 +0100 | [diff] [blame] | 7005 | // So far bc/dc have no way to include a file from another file, |
| 7006 | // therefore we know G.prog.file == NULL on entry |
| 7007 | //const char *sv_file; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7008 | char *data; |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7009 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7010 | BcFunc *main_func; |
| 7011 | BcInstPtr *ip; |
| 7012 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 7013 | data = bc_read_file(file); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 7014 | if (!data) RETURN_STATUS(bc_error_fmt("file '%s' is not text", file)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7015 | |
Denys Vlasenko | 0fe270e | 2018-12-13 19:58:58 +0100 | [diff] [blame] | 7016 | //sv_file = G.prog.file; |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7017 | G.prog.file = file; |
| 7018 | bc_lex_file(&G.prs.l); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 7019 | s = zbc_vm_process(data); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7020 | if (s) goto err; |
| 7021 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 7022 | main_func = bc_program_func(BC_PROG_MAIN); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7023 | ip = bc_vec_item(&G.prog.stack, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7024 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 7025 | if (main_func->code.len < ip->idx) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 7026 | s = bc_error_fmt("file '%s' is not executable", file); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7027 | |
| 7028 | err: |
Denys Vlasenko | 0fe270e | 2018-12-13 19:58:58 +0100 | [diff] [blame] | 7029 | //G.prog.file = sv_file; |
| 7030 | G.prog.file = NULL; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7031 | free(data); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 7032 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7033 | } |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 7034 | #if ERRORS_ARE_FATAL |
| 7035 | # define zbc_vm_file(...) (zbc_vm_file(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 7036 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7037 | |
Denys Vlasenko | 19f1107 | 2018-12-12 22:48:19 +0100 | [diff] [blame] | 7038 | static BC_STATUS zbc_vm_stdin(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7039 | { |
Denys Vlasenko | a0c421c | 2018-12-02 20:16:52 +0100 | [diff] [blame] | 7040 | BcStatus s; |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 7041 | BcVec buffer; |
Denys Vlasenko | 4dd3652 | 2018-12-11 22:26:38 +0100 | [diff] [blame] | 7042 | size_t str; |
| 7043 | bool comment; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7044 | |
Denys Vlasenko | 0fe270e | 2018-12-13 19:58:58 +0100 | [diff] [blame] | 7045 | //G.prog.file = NULL; - already is |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7046 | bc_lex_file(&G.prs.l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7047 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 7048 | bc_char_vec_init(&buffer); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7049 | |
| 7050 | // This loop is complex because the vm tries not to send any lines that end |
| 7051 | // with a backslash to the parser. The reason for that is because the parser |
| 7052 | // treats a backslash+newline combo as whitespace, per the bc spec. In that |
| 7053 | // case, and for strings and comments, the parser will expect more stuff. |
Denys Vlasenko | 8a89247 | 2018-12-12 22:43:58 +0100 | [diff] [blame] | 7054 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 4dd3652 | 2018-12-11 22:26:38 +0100 | [diff] [blame] | 7055 | comment = false; |
| 7056 | str = 0; |
Denys Vlasenko | 8a89247 | 2018-12-12 22:43:58 +0100 | [diff] [blame] | 7057 | for (;;) { |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 7058 | size_t prevlen = buffer.len; |
Denys Vlasenko | b7e61e3 | 2018-12-13 18:16:39 +0100 | [diff] [blame] | 7059 | char *string; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7060 | |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 7061 | bc_read_line(&buffer); |
| 7062 | // No more input means EOF |
| 7063 | if (buffer.len <= prevlen + 1) // (we expect +1 for NUL byte) |
Denys Vlasenko | 8a89247 | 2018-12-12 22:43:58 +0100 | [diff] [blame] | 7064 | break; |
Denys Vlasenko | b7e61e3 | 2018-12-13 18:16:39 +0100 | [diff] [blame] | 7065 | |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 7066 | string = buffer.v + prevlen; |
Denys Vlasenko | b7e61e3 | 2018-12-13 18:16:39 +0100 | [diff] [blame] | 7067 | while (*string) { |
| 7068 | char c = *string; |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 7069 | if (string == buffer.v || string[-1] != '\\') { |
Denys Vlasenko | bbcecc4 | 2018-12-13 21:17:43 +0100 | [diff] [blame] | 7070 | if (IS_BC) |
Denys Vlasenko | b7e61e3 | 2018-12-13 18:16:39 +0100 | [diff] [blame] | 7071 | str ^= (c == '"'); |
Denys Vlasenko | bbcecc4 | 2018-12-13 21:17:43 +0100 | [diff] [blame] | 7072 | else { |
Denys Vlasenko | b7e61e3 | 2018-12-13 18:16:39 +0100 | [diff] [blame] | 7073 | if (c == ']') |
| 7074 | str -= 1; |
| 7075 | else if (c == '[') |
| 7076 | str += 1; |
Denys Vlasenko | 766f672 | 2018-12-13 17:23:24 +0100 | [diff] [blame] | 7077 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7078 | } |
Denys Vlasenko | b7e61e3 | 2018-12-13 18:16:39 +0100 | [diff] [blame] | 7079 | string++; |
| 7080 | if (c == '/' && *string == '*') { |
| 7081 | comment = true; |
| 7082 | string++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7083 | continue; |
| 7084 | } |
Denys Vlasenko | b7e61e3 | 2018-12-13 18:16:39 +0100 | [diff] [blame] | 7085 | if (c == '*' && *string == '/') { |
| 7086 | comment = false; |
| 7087 | string++; |
| 7088 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7089 | } |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 7090 | if (str || comment) { |
| 7091 | buffer.len--; // backstep over the trailing NUL byte |
Denys Vlasenko | b7e61e3 | 2018-12-13 18:16:39 +0100 | [diff] [blame] | 7092 | continue; |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 7093 | } |
Denys Vlasenko | b7e61e3 | 2018-12-13 18:16:39 +0100 | [diff] [blame] | 7094 | |
| 7095 | // Check for backslash+newline. |
| 7096 | // we do not check that last char is '\n' - |
| 7097 | // if it is not, then it's EOF, and looping back |
| 7098 | // to bc_read_line() will detect it: |
| 7099 | string -= 2; |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 7100 | if (string >= buffer.v && *string == '\\') { |
| 7101 | buffer.len--; |
Denys Vlasenko | b7e61e3 | 2018-12-13 18:16:39 +0100 | [diff] [blame] | 7102 | continue; |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 7103 | } |
Denys Vlasenko | b7e61e3 | 2018-12-13 18:16:39 +0100 | [diff] [blame] | 7104 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 7105 | s = zbc_vm_process(buffer.v); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7106 | if (s) { |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7107 | if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin) { |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 7108 | // Debug config, non-interactive mode: |
| 7109 | // return all the way back to main. |
| 7110 | // Non-debug builds do not come here, they exit. |
| 7111 | break; |
| 7112 | } |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7113 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7114 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 7115 | bc_vec_pop_all(&buffer); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7116 | } |
| 7117 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 7118 | if (str) { |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7119 | s = bc_error("string end could not be found"); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 7120 | } |
| 7121 | else if (comment) { |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7122 | s = bc_error("comment end could not be found"); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 7123 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7124 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7125 | bc_vec_free(&buffer); |
Denys Vlasenko | 19f1107 | 2018-12-12 22:48:19 +0100 | [diff] [blame] | 7126 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7127 | } |
Denys Vlasenko | 19f1107 | 2018-12-12 22:48:19 +0100 | [diff] [blame] | 7128 | #if ERRORS_ARE_FATAL |
| 7129 | # define zbc_vm_stdin(...) (zbc_vm_stdin(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 7130 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7131 | |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7132 | #if ENABLE_BC |
Denys Vlasenko | 57b6918 | 2018-12-14 00:12:13 +0100 | [diff] [blame] | 7133 | static void bc_vm_info(void) |
| 7134 | { |
| 7135 | printf("%s "BB_VER"\n" |
| 7136 | "Copyright (c) 2018 Gavin D. Howard and contributors\n" |
| 7137 | , applet_name); |
| 7138 | } |
| 7139 | |
| 7140 | static void bc_args(char **argv) |
| 7141 | { |
| 7142 | unsigned opts; |
| 7143 | int i; |
| 7144 | |
| 7145 | GETOPT_RESET(); |
| 7146 | #if ENABLE_FEATURE_BC_LONG_OPTIONS |
| 7147 | opts = option_mask32 |= getopt32long(argv, "wvsqli", |
| 7148 | "warn\0" No_argument "w" |
| 7149 | "version\0" No_argument "v" |
| 7150 | "standard\0" No_argument "s" |
| 7151 | "quiet\0" No_argument "q" |
| 7152 | "mathlib\0" No_argument "l" |
| 7153 | "interactive\0" No_argument "i" |
| 7154 | ); |
| 7155 | #else |
| 7156 | opts = option_mask32 |= getopt32(argv, "wvsqli"); |
| 7157 | #endif |
| 7158 | if (getenv("POSIXLY_CORRECT")) |
| 7159 | option_mask32 |= BC_FLAG_S; |
| 7160 | |
| 7161 | if (opts & BC_FLAG_V) { |
| 7162 | bc_vm_info(); |
| 7163 | exit(0); |
| 7164 | } |
| 7165 | |
| 7166 | for (i = optind; argv[i]; ++i) |
| 7167 | bc_vec_push(&G.files, argv + i); |
| 7168 | } |
| 7169 | |
| 7170 | static void bc_vm_envArgs(void) |
| 7171 | { |
| 7172 | BcVec v; |
| 7173 | char *buf; |
| 7174 | char *env_args = getenv("BC_ENV_ARGS"); |
| 7175 | |
| 7176 | if (!env_args) return; |
| 7177 | |
| 7178 | G.env_args = xstrdup(env_args); |
| 7179 | buf = G.env_args; |
| 7180 | |
| 7181 | bc_vec_init(&v, sizeof(char *), NULL); |
| 7182 | |
| 7183 | while (*(buf = skip_whitespace(buf)) != '\0') { |
| 7184 | bc_vec_push(&v, &buf); |
| 7185 | buf = skip_non_whitespace(buf); |
| 7186 | if (!*buf) |
| 7187 | break; |
| 7188 | *buf++ = '\0'; |
| 7189 | } |
| 7190 | |
| 7191 | // NULL terminate, and pass argv[] so that first arg is argv[1] |
| 7192 | if (sizeof(int) == sizeof(char*)) { |
| 7193 | bc_vec_push(&v, &const_int_0); |
| 7194 | } else { |
| 7195 | static char *const nullptr = NULL; |
| 7196 | bc_vec_push(&v, &nullptr); |
| 7197 | } |
| 7198 | bc_args(((char **)v.v) - 1); |
| 7199 | |
| 7200 | bc_vec_free(&v); |
| 7201 | } |
| 7202 | |
| 7203 | static const char bc_lib[] ALIGN1 = { |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7204 | "scale=20" |
| 7205 | "\n" "define e(x){" |
| 7206 | "\n" "auto b,s,n,r,d,i,p,f,v" |
Denys Vlasenko | 203210e | 2018-12-14 09:53:50 +0100 | [diff] [blame] | 7207 | ////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below |
| 7208 | //^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc: |
| 7209 | //e(-.998896): GNU:.36828580434569428695 |
| 7210 | // above code:.36828580434569428696 |
| 7211 | // actual value:.3682858043456942869594... |
| 7212 | // but for now let's be "GNU compatible" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7213 | "\n" "b=ibase" |
| 7214 | "\n" "ibase=A" |
| 7215 | "\n" "if(x<0){" |
| 7216 | "\n" "n=1" |
| 7217 | "\n" "x=-x" |
| 7218 | "\n" "}" |
| 7219 | "\n" "s=scale" |
| 7220 | "\n" "r=6+s+0.44*x" |
| 7221 | "\n" "scale=scale(x)+1" |
| 7222 | "\n" "while(x>1){" |
| 7223 | "\n" "d+=1" |
| 7224 | "\n" "x/=2" |
| 7225 | "\n" "scale+=1" |
| 7226 | "\n" "}" |
| 7227 | "\n" "scale=r" |
| 7228 | "\n" "r=x+1" |
| 7229 | "\n" "p=x" |
| 7230 | "\n" "f=v=1" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7231 | "\n" "for(i=2;v;++i){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7232 | "\n" "p*=x" |
| 7233 | "\n" "f*=i" |
| 7234 | "\n" "v=p/f" |
| 7235 | "\n" "r+=v" |
| 7236 | "\n" "}" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7237 | "\n" "while(d--)r*=r" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7238 | "\n" "scale=s" |
| 7239 | "\n" "ibase=b" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7240 | "\n" "if(n)return(1/r)" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7241 | "\n" "return(r/1)" |
| 7242 | "\n" "}" |
| 7243 | "\n" "define l(x){" |
| 7244 | "\n" "auto b,s,r,p,a,q,i,v" |
| 7245 | "\n" "b=ibase" |
| 7246 | "\n" "ibase=A" |
| 7247 | "\n" "if(x<=0){" |
| 7248 | "\n" "r=(1-10^scale)/1" |
| 7249 | "\n" "ibase=b" |
| 7250 | "\n" "return(r)" |
| 7251 | "\n" "}" |
| 7252 | "\n" "s=scale" |
| 7253 | "\n" "scale+=6" |
| 7254 | "\n" "p=2" |
| 7255 | "\n" "while(x>=2){" |
| 7256 | "\n" "p*=2" |
| 7257 | "\n" "x=sqrt(x)" |
| 7258 | "\n" "}" |
| 7259 | "\n" "while(x<=0.5){" |
| 7260 | "\n" "p*=2" |
| 7261 | "\n" "x=sqrt(x)" |
| 7262 | "\n" "}" |
| 7263 | "\n" "r=a=(x-1)/(x+1)" |
| 7264 | "\n" "q=a*a" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7265 | "\n" "v=1" |
| 7266 | "\n" "for(i=3;v;i+=2){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7267 | "\n" "a*=q" |
| 7268 | "\n" "v=a/i" |
| 7269 | "\n" "r+=v" |
| 7270 | "\n" "}" |
| 7271 | "\n" "r*=p" |
| 7272 | "\n" "scale=s" |
| 7273 | "\n" "ibase=b" |
| 7274 | "\n" "return(r/1)" |
| 7275 | "\n" "}" |
| 7276 | "\n" "define s(x){" |
Denys Vlasenko | 240d7ee | 2018-12-14 11:27:09 +0100 | [diff] [blame] | 7277 | "\n" "auto b,s,r,a,q,i" |
| 7278 | "\n" "if(x<0)return(-s(-x))" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7279 | "\n" "b=ibase" |
| 7280 | "\n" "ibase=A" |
| 7281 | "\n" "s=scale" |
| 7282 | "\n" "scale=1.1*s+2" |
| 7283 | "\n" "a=a(1)" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7284 | "\n" "scale=0" |
| 7285 | "\n" "q=(x/a+2)/4" |
Denys Vlasenko | 203210e | 2018-12-14 09:53:50 +0100 | [diff] [blame] | 7286 | "\n" "x-=4*q*a" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7287 | "\n" "if(q%2)x=-x" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7288 | "\n" "scale=s+2" |
| 7289 | "\n" "r=a=x" |
| 7290 | "\n" "q=-x*x" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7291 | "\n" "for(i=3;a;i+=2){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7292 | "\n" "a*=q/(i*(i-1))" |
| 7293 | "\n" "r+=a" |
| 7294 | "\n" "}" |
| 7295 | "\n" "scale=s" |
| 7296 | "\n" "ibase=b" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7297 | "\n" "return(r/1)" |
| 7298 | "\n" "}" |
| 7299 | "\n" "define c(x){" |
| 7300 | "\n" "auto b,s" |
| 7301 | "\n" "b=ibase" |
| 7302 | "\n" "ibase=A" |
| 7303 | "\n" "s=scale" |
| 7304 | "\n" "scale*=1.2" |
| 7305 | "\n" "x=s(2*a(1)+x)" |
| 7306 | "\n" "scale=s" |
| 7307 | "\n" "ibase=b" |
| 7308 | "\n" "return(x/1)" |
| 7309 | "\n" "}" |
| 7310 | "\n" "define a(x){" |
| 7311 | "\n" "auto b,s,r,n,a,m,t,f,i,u" |
| 7312 | "\n" "b=ibase" |
| 7313 | "\n" "ibase=A" |
| 7314 | "\n" "n=1" |
| 7315 | "\n" "if(x<0){" |
| 7316 | "\n" "n=-1" |
| 7317 | "\n" "x=-x" |
| 7318 | "\n" "}" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7319 | "\n" "if(scale<65){" |
| 7320 | "\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)" |
| 7321 | "\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7322 | "\n" "}" |
| 7323 | "\n" "s=scale" |
| 7324 | "\n" "if(x>.2){" |
| 7325 | "\n" "scale+=5" |
| 7326 | "\n" "a=a(.2)" |
| 7327 | "\n" "}" |
| 7328 | "\n" "scale=s+3" |
| 7329 | "\n" "while(x>.2){" |
| 7330 | "\n" "m+=1" |
| 7331 | "\n" "x=(x-.2)/(1+.2*x)" |
| 7332 | "\n" "}" |
| 7333 | "\n" "r=u=x" |
| 7334 | "\n" "f=-x*x" |
| 7335 | "\n" "t=1" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7336 | "\n" "for(i=3;t;i+=2){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7337 | "\n" "u*=f" |
| 7338 | "\n" "t=u/i" |
| 7339 | "\n" "r+=t" |
| 7340 | "\n" "}" |
| 7341 | "\n" "scale=s" |
| 7342 | "\n" "ibase=b" |
| 7343 | "\n" "return((m*a+r)/n)" |
| 7344 | "\n" "}" |
| 7345 | "\n" "define j(n,x){" |
| 7346 | "\n" "auto b,s,o,a,i,v,f" |
| 7347 | "\n" "b=ibase" |
| 7348 | "\n" "ibase=A" |
| 7349 | "\n" "s=scale" |
| 7350 | "\n" "scale=0" |
| 7351 | "\n" "n/=1" |
| 7352 | "\n" "if(n<0){" |
| 7353 | "\n" "n=-n" |
Denys Vlasenko | 203210e | 2018-12-14 09:53:50 +0100 | [diff] [blame] | 7354 | "\n" "o=n%2" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7355 | "\n" "}" |
| 7356 | "\n" "a=1" |
| 7357 | "\n" "for(i=2;i<=n;++i)a*=i" |
| 7358 | "\n" "scale=1.5*s" |
| 7359 | "\n" "a=(x^n)/2^n/a" |
| 7360 | "\n" "r=v=1" |
| 7361 | "\n" "f=-x*x/4" |
Denys Vlasenko | c06537d | 2018-12-14 10:10:37 +0100 | [diff] [blame] | 7362 | "\n" "scale+=length(a)-scale(a)" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7363 | "\n" "for(i=1;v;++i){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7364 | "\n" "v=v*f/i/(n+i)" |
| 7365 | "\n" "r+=v" |
| 7366 | "\n" "}" |
| 7367 | "\n" "scale=s" |
| 7368 | "\n" "ibase=b" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7369 | "\n" "if(o)a=-a" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7370 | "\n" "return(a*r/1)" |
| 7371 | "\n" "}" |
| 7372 | }; |
| 7373 | #endif // ENABLE_BC |
| 7374 | |
Denys Vlasenko | 19f1107 | 2018-12-12 22:48:19 +0100 | [diff] [blame] | 7375 | static BC_STATUS zbc_vm_exec(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7376 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7377 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7378 | size_t i; |
| 7379 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7380 | #if ENABLE_BC |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 7381 | if (option_mask32 & BC_FLAG_L) { |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7382 | // We know that internal library is not buggy, |
| 7383 | // thus error checking is normally disabled. |
| 7384 | # define DEBUG_LIB 0 |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7385 | bc_lex_file(&G.prs.l); |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 7386 | s = zbc_vm_process(bc_lib); |
Denys Vlasenko | 19f1107 | 2018-12-12 22:48:19 +0100 | [diff] [blame] | 7387 | if (DEBUG_LIB && s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7388 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7389 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7390 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7391 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7392 | for (i = 0; !s && i < G.files.len; ++i) |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 7393 | s = zbc_vm_file(*((char **) bc_vec_item(&G.files, i))); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7394 | if (ENABLE_FEATURE_CLEAN_UP && s && !G_ttyin) { |
| 7395 | // Debug config, non-interactive mode: |
| 7396 | // return all the way back to main. |
| 7397 | // Non-debug builds do not come here, they exit. |
Denys Vlasenko | 19f1107 | 2018-12-12 22:48:19 +0100 | [diff] [blame] | 7398 | RETURN_STATUS(s); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7399 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7400 | |
Denys Vlasenko | 91cde95 | 2018-12-10 20:56:08 +0100 | [diff] [blame] | 7401 | if (IS_BC || (option_mask32 & BC_FLAG_I)) |
Denys Vlasenko | 19f1107 | 2018-12-12 22:48:19 +0100 | [diff] [blame] | 7402 | s = zbc_vm_stdin(); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7403 | |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7404 | if (!s && !BC_PARSE_CAN_EXEC(&G.prs)) |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 7405 | s = zbc_vm_process(""); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7406 | |
Denys Vlasenko | 19f1107 | 2018-12-12 22:48:19 +0100 | [diff] [blame] | 7407 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7408 | } |
Denys Vlasenko | 19f1107 | 2018-12-12 22:48:19 +0100 | [diff] [blame] | 7409 | #if ERRORS_ARE_FATAL |
| 7410 | # define zbc_vm_exec(...) (zbc_vm_exec(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 7411 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7412 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7413 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 7414 | static void bc_program_free(void) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7415 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7416 | bc_num_free(&G.prog.ib); |
| 7417 | bc_num_free(&G.prog.ob); |
| 7418 | bc_num_free(&G.prog.hexb); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7419 | # if ENABLE_DC |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7420 | bc_num_free(&G.prog.strmb); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7421 | # endif |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7422 | bc_vec_free(&G.prog.fns); |
| 7423 | bc_vec_free(&G.prog.fn_map); |
| 7424 | bc_vec_free(&G.prog.vars); |
| 7425 | bc_vec_free(&G.prog.var_map); |
| 7426 | bc_vec_free(&G.prog.arrs); |
| 7427 | bc_vec_free(&G.prog.arr_map); |
| 7428 | bc_vec_free(&G.prog.strs); |
| 7429 | bc_vec_free(&G.prog.consts); |
| 7430 | bc_vec_free(&G.prog.results); |
| 7431 | bc_vec_free(&G.prog.stack); |
| 7432 | bc_num_free(&G.prog.last); |
| 7433 | bc_num_free(&G.prog.zero); |
| 7434 | bc_num_free(&G.prog.one); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7435 | } |
| 7436 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7437 | static void bc_vm_free(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7438 | { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7439 | bc_vec_free(&G.files); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7440 | bc_program_free(); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7441 | bc_parse_free(&G.prs); |
| 7442 | free(G.env_args); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7443 | } |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7444 | #endif |
| 7445 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7446 | static void bc_program_init(void) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7447 | { |
| 7448 | size_t idx; |
| 7449 | BcInstPtr ip; |
| 7450 | |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7451 | // memset(&G.prog, 0, sizeof(G.prog)); - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7452 | memset(&ip, 0, sizeof(BcInstPtr)); |
| 7453 | |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7454 | // G.prog.nchars = G.prog.scale = 0; - already is |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7455 | bc_num_init_DEF_SIZE(&G.prog.ib); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7456 | bc_num_ten(&G.prog.ib); |
| 7457 | G.prog.ib_t = 10; |
| 7458 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7459 | bc_num_init_DEF_SIZE(&G.prog.ob); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7460 | bc_num_ten(&G.prog.ob); |
| 7461 | G.prog.ob_t = 10; |
| 7462 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7463 | bc_num_init_DEF_SIZE(&G.prog.hexb); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7464 | bc_num_ten(&G.prog.hexb); |
| 7465 | G.prog.hexb.num[0] = 6; |
| 7466 | |
| 7467 | #if ENABLE_DC |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7468 | bc_num_init_DEF_SIZE(&G.prog.strmb); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7469 | bc_num_ulong2num(&G.prog.strmb, UCHAR_MAX + 1); |
| 7470 | #endif |
| 7471 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7472 | bc_num_init_DEF_SIZE(&G.prog.last); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 7473 | //bc_num_zero(&G.prog.last); - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7474 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7475 | bc_num_init_DEF_SIZE(&G.prog.zero); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 7476 | //bc_num_zero(&G.prog.zero); - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7477 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7478 | bc_num_init_DEF_SIZE(&G.prog.one); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7479 | bc_num_one(&G.prog.one); |
| 7480 | |
| 7481 | bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7482 | bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7483 | |
Denys Vlasenko | 1f67e93 | 2018-12-03 00:08:59 +0100 | [diff] [blame] | 7484 | bc_program_addFunc(xstrdup("(main)"), &idx); |
| 7485 | bc_program_addFunc(xstrdup("(read)"), &idx); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7486 | |
| 7487 | bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7488 | bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7489 | |
| 7490 | bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7491 | bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7492 | |
| 7493 | bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free); |
| 7494 | bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free); |
| 7495 | bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free); |
| 7496 | bc_vec_init(&G.prog.stack, sizeof(BcInstPtr), NULL); |
| 7497 | bc_vec_push(&G.prog.stack, &ip); |
| 7498 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7499 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7500 | static int bc_vm_init(const char *env_len) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7501 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7502 | #if ENABLE_FEATURE_EDITING |
| 7503 | G.line_input_state = new_line_input_t(DO_HISTORY); |
| 7504 | #endif |
| 7505 | G.prog.len = bc_vm_envLen(env_len); |
| 7506 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7507 | bc_vec_init(&G.files, sizeof(char *), NULL); |
Denys Vlasenko | 5f263f4 | 2018-12-13 22:49:59 +0100 | [diff] [blame] | 7508 | IF_BC(if (IS_BC) bc_vm_envArgs();) |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7509 | bc_program_init(); |
Denys Vlasenko | c2265f5 | 2018-12-12 23:10:08 +0100 | [diff] [blame] | 7510 | bc_parse_create(&G.prs, BC_PROG_MAIN); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7511 | |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 7512 | //TODO: in GNU bc, the check is (isatty(0) && isatty(1)), |
| 7513 | //-i option unconditionally enables this regardless of isatty(): |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7514 | if (isatty(0)) { |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7515 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7516 | G_ttyin = 1; |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7517 | // With SA_RESTART, most system calls will restart |
| 7518 | // (IOW: they won't fail with EINTR). |
| 7519 | // In particular, this means ^C won't cause |
| 7520 | // stdout to get into "error state" if SIGINT hits |
| 7521 | // within write() syscall. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 7522 | // |
| 7523 | // The downside is that ^C while tty input is taken |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7524 | // will only be handled after [Enter] since read() |
| 7525 | // from stdin is not interrupted by ^C either, |
| 7526 | // it restarts, thus fgetc() does not return on ^C. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 7527 | // (This problem manifests only if line editing is disabled) |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7528 | signal_SA_RESTART_empty_mask(SIGINT, record_signo); |
| 7529 | |
| 7530 | // Without SA_RESTART, this exhibits a bug: |
| 7531 | // "while (1) print 1" and try ^C-ing it. |
| 7532 | // Intermittently, instead of returning to input line, |
| 7533 | // you'll get "output error: Interrupted system call" |
| 7534 | // and exit. |
| 7535 | //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo); |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7536 | #endif |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7537 | return 1; // "tty" |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7538 | } |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7539 | return 0; // "not a tty" |
| 7540 | } |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7541 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7542 | static BcStatus bc_vm_run(void) |
| 7543 | { |
Denys Vlasenko | 19f1107 | 2018-12-12 22:48:19 +0100 | [diff] [blame] | 7544 | BcStatus st = zbc_vm_exec(); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7545 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 7546 | if (G_exiting) // it was actually "halt" or "quit" |
| 7547 | st = EXIT_SUCCESS; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7548 | bc_vm_free(); |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 7549 | # if ENABLE_FEATURE_EDITING |
| 7550 | free_line_input_t(G.line_input_state); |
| 7551 | # endif |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 7552 | FREE_G(); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7553 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7554 | return st; |
| 7555 | } |
| 7556 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7557 | #if ENABLE_BC |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7558 | int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7559 | int bc_main(int argc UNUSED_PARAM, char **argv) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7560 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7561 | int is_tty; |
| 7562 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7563 | INIT_G(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7564 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7565 | is_tty = bc_vm_init("BC_LINE_LENGTH"); |
| 7566 | |
| 7567 | bc_args(argv); |
| 7568 | |
| 7569 | if (is_tty && !(option_mask32 & BC_FLAG_Q)) |
| 7570 | bc_vm_info(); |
| 7571 | |
| 7572 | return bc_vm_run(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7573 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7574 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7575 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7576 | #if ENABLE_DC |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7577 | int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7578 | int dc_main(int argc UNUSED_PARAM, char **argv) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7579 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7580 | int noscript; |
| 7581 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7582 | INIT_G(); |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7583 | |
| 7584 | // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width |
| 7585 | // 1 char wider than bc from the same package. |
| 7586 | // Both default width, and xC_LINE_LENGTH=N are wider: |
| 7587 | // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints: |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 7588 | // |1234\ | |
| 7589 | // |56 | |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7590 | // "echo '123456' | BC_LINE_LENGTH=5 bc" prints: |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 7591 | // |123\ | |
| 7592 | // |456 | |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7593 | // Do the same, or it's a bug? |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7594 | bc_vm_init("DC_LINE_LENGTH"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7595 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7596 | // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs |
| 7597 | noscript = BC_FLAG_I; |
| 7598 | for (;;) { |
| 7599 | int n = getopt(argc, argv, "e:f:x"); |
| 7600 | if (n <= 0) |
| 7601 | break; |
| 7602 | switch (n) { |
| 7603 | case 'e': |
| 7604 | noscript = 0; |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 7605 | n = zbc_vm_process(optarg); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7606 | if (n) return n; |
| 7607 | break; |
| 7608 | case 'f': |
| 7609 | noscript = 0; |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 7610 | n = zbc_vm_file(optarg); |
| 7611 | if (n) return n; |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7612 | break; |
| 7613 | case 'x': |
| 7614 | option_mask32 |= DC_FLAG_X; |
| 7615 | break; |
| 7616 | default: |
| 7617 | bb_show_usage(); |
| 7618 | } |
| 7619 | } |
| 7620 | argv += optind; |
| 7621 | |
| 7622 | while (*argv) { |
| 7623 | noscript = 0; |
| 7624 | bc_vec_push(&G.files, argv++); |
| 7625 | } |
| 7626 | |
| 7627 | option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin |
| 7628 | |
| 7629 | return bc_vm_run(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7630 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7631 | #endif |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 7632 | |
| 7633 | #endif // not DC_SMALL |