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 |
| 13 | //config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html) |
| 14 | //config: for details. |
| 15 | //config: |
| 16 | //config: This bc has four differences to the GNU bc: |
| 17 | //config: |
| 18 | //config: 1) The period (.) can also be used as a shortcut for "last", as in |
| 19 | //config: the BSD bc. |
| 20 | //config: 2) Arrays are copied before being passed as arguments to |
| 21 | //config: functions. This behavior is required by the bc spec. |
| 22 | //config: 3) Arrays can be passed to the builtin "length" function to get |
| 23 | //config: the number of elements currently in the array. The following |
| 24 | //config: example prints "1": |
| 25 | //config: |
| 26 | //config: a[0] = 0 |
| 27 | //config: length(a[]) |
| 28 | //config: |
| 29 | //config: 4) The precedence of the boolean "not" operator (!) is equal to |
| 30 | //config: that of the unary minus (-), or negation, operator. This still |
| 31 | //config: allows POSIX-compliant scripts to work while somewhat |
| 32 | //config: preserving expected behavior (versus C) and making parsing |
| 33 | //config: easier. |
| 34 | //config: |
| 35 | //config: Options: |
| 36 | //config: |
| 37 | //config: -i --interactive force interactive mode |
| 38 | //config: -l --mathlib use predefined math routines: |
| 39 | //config: |
| 40 | //config: s(expr) = sine of expr in radians |
| 41 | //config: c(expr) = cosine of expr in radians |
| 42 | //config: a(expr) = arctangent of expr, returning |
| 43 | //config: radians |
| 44 | //config: l(expr) = natural log of expr |
| 45 | //config: e(expr) = raises e to the power of expr |
| 46 | //config: j(n, x) = Bessel function of integer order |
| 47 | //config: n of x |
| 48 | //config: |
| 49 | //config: -q --quiet don't print version and copyright. |
| 50 | //config: -s --standard error if any non-POSIX extensions are used. |
| 51 | //config: -w --warn warn if any non-POSIX extensions are used. |
| 52 | //config: -v --version print version and copyright and exit. |
| 53 | //config: |
| 54 | //config: Long options are only available if FEATURE_BC_LONG_OPTIONS is |
| 55 | //config: enabled. |
| 56 | //config: |
| 57 | //config:config DC |
| 58 | //config: bool "dc (38 kb; 49 kb when combined with bc)" |
| 59 | //config: default y |
| 60 | //config: help |
| 61 | //config: dc is a reverse-polish notation command-line calculator which |
| 62 | //config: supports unlimited precision arithmetic. See the FreeBSD man page |
| 63 | //config: (https://www.unix.com/man-page/FreeBSD/1/dc/) and GNU dc manual |
| 64 | //config: (https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html) |
| 65 | //config: for details. |
| 66 | //config: |
| 67 | //config: This dc has a few differences from the two above: |
| 68 | //config: |
| 69 | //config: 1) When printing a byte stream (command "P"), this bc follows what |
| 70 | //config: the FreeBSD dc does. |
| 71 | //config: 2) This dc implements the GNU extensions for divmod ("~") and |
| 72 | //config: modular exponentiation ("|"). |
| 73 | //config: 3) This dc implements all FreeBSD extensions, except for "J" and |
| 74 | //config: "M". |
| 75 | //config: 4) Like the FreeBSD dc, this dc supports extended registers. |
| 76 | //config: However, they are implemented differently. When it encounters |
| 77 | //config: whitespace where a register should be, it skips the whitespace. |
| 78 | //config: If the character following is not a lowercase letter, an error |
| 79 | //config: is issued. Otherwise, the register name is parsed by the |
| 80 | //config: following regex: |
| 81 | //config: |
| 82 | //config: [a-z][a-z0-9_]* |
| 83 | //config: |
| 84 | //config: This generally means that register names will be surrounded by |
| 85 | //config: whitespace. |
| 86 | //config: |
| 87 | //config: Examples: |
| 88 | //config: |
| 89 | //config: l idx s temp L index S temp2 < do_thing |
| 90 | //config: |
| 91 | //config: Also note that, like the FreeBSD dc, extended registers are not |
| 92 | //config: allowed unless the "-x" option is given. |
| 93 | //config: |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 94 | //config:config FEATURE_DC_SMALL |
| 95 | //config: bool "Minimal dc implementation (4.2 kb), not using bc code base" |
| 96 | //config: depends on DC && !BC |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 97 | //config: default n |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 98 | //config: |
| 99 | //config:config FEATURE_DC_LIBM |
| 100 | //config: bool "Enable power and exp functions (requires libm)" |
| 101 | //config: default y |
| 102 | //config: depends on FEATURE_DC_SMALL |
| 103 | //config: help |
| 104 | //config: Enable power and exp functions. |
| 105 | //config: NOTE: This will require libm to be present for linking. |
| 106 | //config: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 107 | //config:config FEATURE_BC_SIGNALS |
| 108 | //config: bool "Enable bc/dc signal handling" |
| 109 | //config: default y |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 110 | //config: depends on (BC || DC) && !FEATURE_DC_SMALL |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 111 | //config: help |
| 112 | //config: Enable signal handling for bc and dc. |
| 113 | //config: |
| 114 | //config:config FEATURE_BC_LONG_OPTIONS |
| 115 | //config: bool "Enable bc/dc long options" |
| 116 | //config: default y |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 117 | //config: depends on (BC || DC) && !FEATURE_DC_SMALL |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 118 | //config: help |
| 119 | //config: Enable long options for bc and dc. |
| 120 | |
| 121 | //applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 122 | //applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 123 | |
| 124 | //kbuild:lib-$(CONFIG_BC) += bc.o |
| 125 | //kbuild:lib-$(CONFIG_DC) += bc.o |
| 126 | |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 127 | //See www.gnu.org/software/bc/manual/bc.html |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 128 | //usage:#define bc_trivial_usage |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 129 | //usage: "[-sqliw] FILE..." |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 130 | //usage: |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 131 | //usage:#define bc_full_usage "\n" |
| 132 | //usage: "\nArbitrary precision calculator" |
| 133 | //usage: "\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 134 | ///////: "\n -i Interactive" - has no effect for now |
| 135 | //usage: "\n -q Quiet" |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 136 | //usage: "\n -l Load standard math library" |
| 137 | //usage: "\n -s Be POSIX compatible" |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 138 | //usage: "\n -w Warn if extensions are used" |
| 139 | ///////: "\n -v Version" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 140 | //usage: "\n" |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 141 | //usage: "\n$BC_LINE_LENGTH changes output width" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 142 | //usage: |
| 143 | //usage:#define bc_example_usage |
| 144 | //usage: "3 + 4.129\n" |
| 145 | //usage: "1903 - 2893\n" |
| 146 | //usage: "-129 * 213.28935\n" |
| 147 | //usage: "12 / -1932\n" |
| 148 | //usage: "12 % 12\n" |
| 149 | //usage: "34 ^ 189\n" |
| 150 | //usage: "scale = 13\n" |
| 151 | //usage: "ibase = 2\n" |
| 152 | //usage: "obase = A\n" |
| 153 | //usage: |
| 154 | //usage:#define dc_trivial_usage |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 155 | //usage: IF_NOT_FEATURE_DC_SMALL("[-x] ")"[-eSCRIPT]... [-fFILE]... [FILE]..." |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 156 | //usage: |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 157 | //usage:#define dc_full_usage "\n" |
| 158 | //usage: "\nTiny RPN calculator. Operations:" |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 159 | //usage: "\n+, -, *, /, %, ~, ^," IF_NOT_FEATURE_DC_SMALL(" |,") |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 160 | //usage: "\np - print top of the stack (without popping)" |
| 161 | //usage: "\nf - print entire stack" |
| 162 | //usage: "\nk - pop the value and set the precision" |
| 163 | //usage: "\ni - pop the value and set input radix" |
| 164 | //usage: "\no - pop the value and set output radix" |
| 165 | //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] | 166 | //usage: |
| 167 | //usage:#define dc_example_usage |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 168 | //usage: "$ dc -e'2 2 + p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 169 | //usage: "4\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 170 | //usage: "$ dc -e'8 8 \\* 2 2 + / p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 171 | //usage: "16\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 172 | //usage: "$ dc -e'0 1 & p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 173 | //usage: "0\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 174 | //usage: "$ dc -e'0 1 | p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 175 | //usage: "1\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 176 | //usage: "$ echo '72 9 / 8 * p' | dc\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 177 | //usage: "64\n" |
| 178 | |
| 179 | #include "libbb.h" |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 180 | #include "common_bufsiz.h" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 181 | |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 182 | #if ENABLE_FEATURE_DC_SMALL |
| 183 | # include "dc.c" |
| 184 | #else |
| 185 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 186 | typedef enum BcStatus { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 187 | BC_STATUS_SUCCESS = 0, |
| 188 | BC_STATUS_FAILURE = 1, |
| 189 | BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr() uses this |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 190 | BC_STATUS_EOF = 3, // bc_vm_stdin() uses this |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 191 | } BcStatus; |
| 192 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 193 | #define BC_VEC_INVALID_IDX ((size_t) -1) |
| 194 | #define BC_VEC_START_CAP (1 << 5) |
| 195 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 196 | typedef void (*BcVecFree)(void *) FAST_FUNC; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 197 | |
| 198 | typedef struct BcVec { |
| 199 | char *v; |
| 200 | size_t len; |
| 201 | size_t cap; |
| 202 | size_t size; |
| 203 | BcVecFree dtor; |
| 204 | } BcVec; |
| 205 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 206 | typedef signed char BcDig; |
| 207 | |
| 208 | typedef struct BcNum { |
| 209 | BcDig *restrict num; |
| 210 | size_t rdx; |
| 211 | size_t len; |
| 212 | size_t cap; |
| 213 | bool neg; |
| 214 | } BcNum; |
| 215 | |
Denys Vlasenko | 2fa11b6 | 2018-12-06 12:34:39 +0100 | [diff] [blame] | 216 | #define BC_NUM_MIN_BASE ((unsigned long) 2) |
| 217 | #define BC_NUM_MAX_IBASE ((unsigned long) 16) |
| 218 | // larger value might speed up BIGNUM calculations a bit: |
| 219 | #define BC_NUM_DEF_SIZE (16) |
| 220 | #define BC_NUM_PRINT_WIDTH (69) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 221 | |
Denys Vlasenko | 2fa11b6 | 2018-12-06 12:34:39 +0100 | [diff] [blame] | 222 | #define BC_NUM_KARATSUBA_LEN (32) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 223 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 224 | typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC; |
Denys Vlasenko | 2a8ad48 | 2018-12-08 21:56:37 +0100 | [diff] [blame] | 225 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 226 | typedef BcStatus (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 227 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 228 | static BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) FAST_FUNC; |
| 229 | static BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) FAST_FUNC; |
| 230 | static BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) FAST_FUNC; |
| 231 | static BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) FAST_FUNC; |
| 232 | static BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) FAST_FUNC; |
| 233 | static BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) FAST_FUNC; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 234 | static BcStatus bc_num_sqrt(BcNum *a, BcNum *b, size_t scale); |
| 235 | static BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, |
| 236 | size_t scale); |
| 237 | |
| 238 | typedef enum BcInst { |
| 239 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 240 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 241 | BC_INST_INC_PRE, |
| 242 | BC_INST_DEC_PRE, |
| 243 | BC_INST_INC_POST, |
| 244 | BC_INST_DEC_POST, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 245 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 246 | |
| 247 | BC_INST_NEG, |
| 248 | |
| 249 | BC_INST_POWER, |
| 250 | BC_INST_MULTIPLY, |
| 251 | BC_INST_DIVIDE, |
| 252 | BC_INST_MODULUS, |
| 253 | BC_INST_PLUS, |
| 254 | BC_INST_MINUS, |
| 255 | |
| 256 | BC_INST_REL_EQ, |
| 257 | BC_INST_REL_LE, |
| 258 | BC_INST_REL_GE, |
| 259 | BC_INST_REL_NE, |
| 260 | BC_INST_REL_LT, |
| 261 | BC_INST_REL_GT, |
| 262 | |
| 263 | BC_INST_BOOL_NOT, |
| 264 | BC_INST_BOOL_OR, |
| 265 | BC_INST_BOOL_AND, |
| 266 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 267 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 268 | BC_INST_ASSIGN_POWER, |
| 269 | BC_INST_ASSIGN_MULTIPLY, |
| 270 | BC_INST_ASSIGN_DIVIDE, |
| 271 | BC_INST_ASSIGN_MODULUS, |
| 272 | BC_INST_ASSIGN_PLUS, |
| 273 | BC_INST_ASSIGN_MINUS, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 274 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 275 | BC_INST_ASSIGN, |
| 276 | |
| 277 | BC_INST_NUM, |
| 278 | BC_INST_VAR, |
| 279 | BC_INST_ARRAY_ELEM, |
| 280 | BC_INST_ARRAY, |
| 281 | |
| 282 | BC_INST_SCALE_FUNC, |
| 283 | BC_INST_IBASE, |
| 284 | BC_INST_SCALE, |
| 285 | BC_INST_LAST, |
| 286 | BC_INST_LENGTH, |
| 287 | BC_INST_READ, |
| 288 | BC_INST_OBASE, |
| 289 | BC_INST_SQRT, |
| 290 | |
| 291 | BC_INST_PRINT, |
| 292 | BC_INST_PRINT_POP, |
| 293 | BC_INST_STR, |
| 294 | BC_INST_PRINT_STR, |
| 295 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 296 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 297 | BC_INST_JUMP, |
| 298 | BC_INST_JUMP_ZERO, |
| 299 | |
| 300 | BC_INST_CALL, |
| 301 | |
| 302 | BC_INST_RET, |
| 303 | BC_INST_RET0, |
| 304 | |
| 305 | BC_INST_HALT, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 306 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 307 | |
| 308 | BC_INST_POP, |
| 309 | BC_INST_POP_EXEC, |
| 310 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 311 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 312 | BC_INST_MODEXP, |
| 313 | BC_INST_DIVMOD, |
| 314 | |
| 315 | BC_INST_EXECUTE, |
| 316 | BC_INST_EXEC_COND, |
| 317 | |
| 318 | BC_INST_ASCIIFY, |
| 319 | BC_INST_PRINT_STREAM, |
| 320 | |
| 321 | BC_INST_PRINT_STACK, |
| 322 | BC_INST_CLEAR_STACK, |
| 323 | BC_INST_STACK_LEN, |
| 324 | BC_INST_DUPLICATE, |
| 325 | BC_INST_SWAP, |
| 326 | |
| 327 | BC_INST_LOAD, |
| 328 | BC_INST_PUSH_VAR, |
| 329 | BC_INST_PUSH_TO_VAR, |
| 330 | |
| 331 | BC_INST_QUIT, |
| 332 | BC_INST_NQUIT, |
| 333 | |
| 334 | BC_INST_INVALID = -1, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 335 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 336 | |
| 337 | } BcInst; |
| 338 | |
| 339 | typedef struct BcId { |
| 340 | char *name; |
| 341 | size_t idx; |
| 342 | } BcId; |
| 343 | |
| 344 | typedef struct BcFunc { |
| 345 | BcVec code; |
| 346 | BcVec labels; |
| 347 | size_t nparams; |
| 348 | BcVec autos; |
| 349 | } BcFunc; |
| 350 | |
| 351 | typedef enum BcResultType { |
| 352 | |
| 353 | BC_RESULT_TEMP, |
| 354 | |
| 355 | BC_RESULT_VAR, |
| 356 | BC_RESULT_ARRAY_ELEM, |
| 357 | BC_RESULT_ARRAY, |
| 358 | |
| 359 | BC_RESULT_STR, |
| 360 | |
| 361 | BC_RESULT_IBASE, |
| 362 | BC_RESULT_SCALE, |
| 363 | BC_RESULT_LAST, |
| 364 | |
| 365 | // These are between to calculate ibase, obase, and last from instructions. |
| 366 | BC_RESULT_CONSTANT, |
| 367 | BC_RESULT_ONE, |
| 368 | |
| 369 | BC_RESULT_OBASE, |
| 370 | |
| 371 | } BcResultType; |
| 372 | |
| 373 | typedef union BcResultData { |
| 374 | BcNum n; |
| 375 | BcVec v; |
| 376 | BcId id; |
| 377 | } BcResultData; |
| 378 | |
| 379 | typedef struct BcResult { |
| 380 | BcResultType t; |
| 381 | BcResultData d; |
| 382 | } BcResult; |
| 383 | |
| 384 | typedef struct BcInstPtr { |
| 385 | size_t func; |
| 386 | size_t idx; |
| 387 | size_t len; |
| 388 | } BcInstPtr; |
| 389 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 390 | // BC_LEX_NEG is not used in lexing; it is only for parsing. |
| 391 | typedef enum BcLexType { |
| 392 | |
| 393 | BC_LEX_EOF, |
| 394 | BC_LEX_INVALID, |
| 395 | |
| 396 | BC_LEX_OP_INC, |
| 397 | BC_LEX_OP_DEC, |
| 398 | |
| 399 | BC_LEX_NEG, |
| 400 | |
| 401 | BC_LEX_OP_POWER, |
| 402 | BC_LEX_OP_MULTIPLY, |
| 403 | BC_LEX_OP_DIVIDE, |
| 404 | BC_LEX_OP_MODULUS, |
| 405 | BC_LEX_OP_PLUS, |
| 406 | BC_LEX_OP_MINUS, |
| 407 | |
| 408 | BC_LEX_OP_REL_EQ, |
| 409 | BC_LEX_OP_REL_LE, |
| 410 | BC_LEX_OP_REL_GE, |
| 411 | BC_LEX_OP_REL_NE, |
| 412 | BC_LEX_OP_REL_LT, |
| 413 | BC_LEX_OP_REL_GT, |
| 414 | |
| 415 | BC_LEX_OP_BOOL_NOT, |
| 416 | BC_LEX_OP_BOOL_OR, |
| 417 | BC_LEX_OP_BOOL_AND, |
| 418 | |
| 419 | BC_LEX_OP_ASSIGN_POWER, |
| 420 | BC_LEX_OP_ASSIGN_MULTIPLY, |
| 421 | BC_LEX_OP_ASSIGN_DIVIDE, |
| 422 | BC_LEX_OP_ASSIGN_MODULUS, |
| 423 | BC_LEX_OP_ASSIGN_PLUS, |
| 424 | BC_LEX_OP_ASSIGN_MINUS, |
| 425 | BC_LEX_OP_ASSIGN, |
| 426 | |
| 427 | BC_LEX_NLINE, |
| 428 | BC_LEX_WHITESPACE, |
| 429 | |
| 430 | BC_LEX_LPAREN, |
| 431 | BC_LEX_RPAREN, |
| 432 | |
| 433 | BC_LEX_LBRACKET, |
| 434 | BC_LEX_COMMA, |
| 435 | BC_LEX_RBRACKET, |
| 436 | |
| 437 | BC_LEX_LBRACE, |
| 438 | BC_LEX_SCOLON, |
| 439 | BC_LEX_RBRACE, |
| 440 | |
| 441 | BC_LEX_STR, |
| 442 | BC_LEX_NAME, |
| 443 | BC_LEX_NUMBER, |
| 444 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 445 | BC_LEX_KEY_1st_keyword, |
| 446 | BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 447 | BC_LEX_KEY_BREAK, |
| 448 | BC_LEX_KEY_CONTINUE, |
| 449 | BC_LEX_KEY_DEFINE, |
| 450 | BC_LEX_KEY_ELSE, |
| 451 | BC_LEX_KEY_FOR, |
| 452 | BC_LEX_KEY_HALT, |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 453 | // code uses "type - BC_LEX_KEY_IBASE + BC_INST_IBASE" construct, |
| 454 | BC_LEX_KEY_IBASE, // relative order should match for: BC_INST_IBASE |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 455 | BC_LEX_KEY_IF, |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 456 | BC_LEX_KEY_LAST, // relative order should match for: BC_INST_LAST |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 457 | BC_LEX_KEY_LENGTH, |
| 458 | BC_LEX_KEY_LIMITS, |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 459 | BC_LEX_KEY_OBASE, // relative order should match for: BC_INST_OBASE |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 460 | BC_LEX_KEY_PRINT, |
| 461 | BC_LEX_KEY_QUIT, |
| 462 | BC_LEX_KEY_READ, |
| 463 | BC_LEX_KEY_RETURN, |
| 464 | BC_LEX_KEY_SCALE, |
| 465 | BC_LEX_KEY_SQRT, |
| 466 | BC_LEX_KEY_WHILE, |
| 467 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 468 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 469 | BC_LEX_EQ_NO_REG, |
| 470 | BC_LEX_OP_MODEXP, |
| 471 | BC_LEX_OP_DIVMOD, |
| 472 | |
| 473 | BC_LEX_COLON, |
| 474 | BC_LEX_ELSE, |
| 475 | BC_LEX_EXECUTE, |
| 476 | BC_LEX_PRINT_STACK, |
| 477 | BC_LEX_CLEAR_STACK, |
| 478 | BC_LEX_STACK_LEVEL, |
| 479 | BC_LEX_DUPLICATE, |
| 480 | BC_LEX_SWAP, |
| 481 | BC_LEX_POP, |
| 482 | |
| 483 | BC_LEX_ASCIIFY, |
| 484 | BC_LEX_PRINT_STREAM, |
| 485 | |
| 486 | BC_LEX_STORE_IBASE, |
| 487 | BC_LEX_STORE_SCALE, |
| 488 | BC_LEX_LOAD, |
| 489 | BC_LEX_LOAD_POP, |
| 490 | BC_LEX_STORE_PUSH, |
| 491 | BC_LEX_STORE_OBASE, |
| 492 | BC_LEX_PRINT_POP, |
| 493 | BC_LEX_NQUIT, |
| 494 | BC_LEX_SCALE_FACTOR, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 495 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 496 | } BcLexType; |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 497 | // must match order of BC_LEX_KEY_foo etc above |
| 498 | #if ENABLE_BC |
| 499 | struct BcLexKeyword { |
| 500 | char name8[8]; |
| 501 | }; |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 502 | #define BC_LEX_KW_ENTRY(a, b) \ |
| 503 | { .name8 = a /*, .posix = b */ } |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 504 | static const struct BcLexKeyword bc_lex_kws[20] = { |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 505 | BC_LEX_KW_ENTRY("auto" , 1), // 0 |
| 506 | BC_LEX_KW_ENTRY("break" , 1), // 1 |
| 507 | BC_LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL |
| 508 | BC_LEX_KW_ENTRY("define" , 1), // 3 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 509 | |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 510 | BC_LEX_KW_ENTRY("else" , 0), // 4 |
| 511 | BC_LEX_KW_ENTRY("for" , 1), // 5 |
| 512 | BC_LEX_KW_ENTRY("halt" , 0), // 6 |
| 513 | BC_LEX_KW_ENTRY("ibase" , 1), // 7 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 514 | |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 515 | BC_LEX_KW_ENTRY("if" , 1), // 8 |
| 516 | BC_LEX_KW_ENTRY("last" , 0), // 9 |
| 517 | BC_LEX_KW_ENTRY("length" , 1), // 10 |
| 518 | BC_LEX_KW_ENTRY("limits" , 0), // 11 |
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 | BC_LEX_KW_ENTRY("obase" , 1), // 12 |
| 521 | BC_LEX_KW_ENTRY("print" , 0), // 13 |
| 522 | BC_LEX_KW_ENTRY("quit" , 1), // 14 |
| 523 | BC_LEX_KW_ENTRY("read" , 0), // 15 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 524 | |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 525 | BC_LEX_KW_ENTRY("return" , 1), // 16 |
| 526 | BC_LEX_KW_ENTRY("scale" , 1), // 17 |
| 527 | BC_LEX_KW_ENTRY("sqrt" , 1), // 18 |
| 528 | BC_LEX_KW_ENTRY("while" , 1), // 19 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 529 | }; |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 530 | #undef BC_LEX_KW_ENTRY |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 531 | enum { |
| 532 | POSIX_KWORD_MASK = 0 |
| 533 | | (1 << 0) |
| 534 | | (1 << 1) |
| 535 | | (0 << 2) |
| 536 | | (1 << 3) |
| 537 | \ |
| 538 | | (0 << 4) |
| 539 | | (1 << 5) |
| 540 | | (0 << 6) |
| 541 | | (1 << 7) |
| 542 | \ |
| 543 | | (1 << 8) |
| 544 | | (0 << 9) |
| 545 | | (1 << 10) |
| 546 | | (0 << 11) |
| 547 | \ |
| 548 | | (1 << 12) |
| 549 | | (0 << 13) |
| 550 | | (1 << 14) |
| 551 | | (0 << 15) |
| 552 | \ |
| 553 | | (1 << 16) |
| 554 | | (1 << 17) |
| 555 | | (1 << 18) |
| 556 | | (1 << 19) |
| 557 | }; |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 558 | #define bc_lex_kws_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK) |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 559 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 560 | |
| 561 | struct BcLex; |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 562 | typedef BcStatus (*BcLexNext)(struct BcLex *) FAST_FUNC; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 563 | |
| 564 | typedef struct BcLex { |
| 565 | |
| 566 | const char *buf; |
| 567 | size_t i; |
| 568 | size_t line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 569 | size_t len; |
| 570 | bool newline; |
| 571 | |
| 572 | struct { |
| 573 | BcLexType t; |
| 574 | BcLexType last; |
| 575 | BcVec v; |
| 576 | } t; |
| 577 | |
| 578 | BcLexNext next; |
| 579 | |
| 580 | } BcLex; |
| 581 | |
| 582 | #define BC_PARSE_STREND ((char) UCHAR_MAX) |
| 583 | |
Denys Vlasenko | e55a572 | 2018-12-06 12:47:17 +0100 | [diff] [blame] | 584 | #define BC_PARSE_REL (1 << 0) |
| 585 | #define BC_PARSE_PRINT (1 << 1) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 586 | #define BC_PARSE_NOCALL (1 << 2) |
| 587 | #define BC_PARSE_NOREAD (1 << 3) |
Denys Vlasenko | e55a572 | 2018-12-06 12:47:17 +0100 | [diff] [blame] | 588 | #define BC_PARSE_ARRAY (1 << 4) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 589 | |
| 590 | #define BC_PARSE_TOP_FLAG_PTR(parse) ((uint8_t *) bc_vec_top(&(parse)->flags)) |
| 591 | #define BC_PARSE_TOP_FLAG(parse) (*(BC_PARSE_TOP_FLAG_PTR(parse))) |
| 592 | |
| 593 | #define BC_PARSE_FLAG_FUNC_INNER (1 << 0) |
| 594 | #define BC_PARSE_FUNC_INNER(parse) \ |
| 595 | (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC_INNER) |
| 596 | |
| 597 | #define BC_PARSE_FLAG_FUNC (1 << 1) |
| 598 | #define BC_PARSE_FUNC(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC) |
| 599 | |
| 600 | #define BC_PARSE_FLAG_BODY (1 << 2) |
| 601 | #define BC_PARSE_BODY(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_BODY) |
| 602 | |
| 603 | #define BC_PARSE_FLAG_LOOP (1 << 3) |
| 604 | #define BC_PARSE_LOOP(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP) |
| 605 | |
| 606 | #define BC_PARSE_FLAG_LOOP_INNER (1 << 4) |
| 607 | #define BC_PARSE_LOOP_INNER(parse) \ |
| 608 | (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP_INNER) |
| 609 | |
| 610 | #define BC_PARSE_FLAG_IF (1 << 5) |
| 611 | #define BC_PARSE_IF(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF) |
| 612 | |
| 613 | #define BC_PARSE_FLAG_ELSE (1 << 6) |
| 614 | #define BC_PARSE_ELSE(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_ELSE) |
| 615 | |
| 616 | #define BC_PARSE_FLAG_IF_END (1 << 7) |
| 617 | #define BC_PARSE_IF_END(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF_END) |
| 618 | |
| 619 | #define BC_PARSE_CAN_EXEC(parse) \ |
| 620 | (!(BC_PARSE_TOP_FLAG(parse) & \ |
| 621 | (BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_BODY | \ |
| 622 | BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER | BC_PARSE_FLAG_IF | \ |
| 623 | BC_PARSE_FLAG_ELSE | BC_PARSE_FLAG_IF_END))) |
| 624 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 625 | struct BcParse; |
| 626 | |
| 627 | struct BcProgram; |
| 628 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 629 | typedef BcStatus (*BcParseParse)(struct BcParse *) FAST_FUNC; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 630 | |
| 631 | typedef struct BcParse { |
| 632 | |
| 633 | BcParseParse parse; |
| 634 | |
| 635 | BcLex l; |
| 636 | |
| 637 | BcVec flags; |
| 638 | |
| 639 | BcVec exits; |
| 640 | BcVec conds; |
| 641 | |
| 642 | BcVec ops; |
| 643 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 644 | BcFunc *func; |
| 645 | size_t fidx; |
| 646 | |
| 647 | size_t nbraces; |
| 648 | bool auto_part; |
| 649 | |
| 650 | } BcParse; |
| 651 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 652 | typedef struct BcProgram { |
| 653 | |
| 654 | size_t len; |
| 655 | size_t scale; |
| 656 | |
| 657 | BcNum ib; |
| 658 | size_t ib_t; |
| 659 | BcNum ob; |
| 660 | size_t ob_t; |
| 661 | |
| 662 | BcNum hexb; |
| 663 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 664 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 665 | BcNum strmb; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 666 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 667 | |
| 668 | BcVec results; |
| 669 | BcVec stack; |
| 670 | |
| 671 | BcVec fns; |
| 672 | BcVec fn_map; |
| 673 | |
| 674 | BcVec vars; |
| 675 | BcVec var_map; |
| 676 | |
| 677 | BcVec arrs; |
| 678 | BcVec arr_map; |
| 679 | |
| 680 | BcVec strs; |
| 681 | BcVec consts; |
| 682 | |
| 683 | const char *file; |
| 684 | |
| 685 | BcNum last; |
| 686 | BcNum zero; |
| 687 | BcNum one; |
| 688 | |
| 689 | size_t nchars; |
| 690 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 691 | } BcProgram; |
| 692 | |
| 693 | #define BC_PROG_STACK(s, n) ((s)->len >= ((size_t) n)) |
| 694 | |
| 695 | #define BC_PROG_MAIN (0) |
| 696 | #define BC_PROG_READ (1) |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 697 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 698 | #define BC_PROG_REQ_FUNCS (2) |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 699 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 700 | |
| 701 | #define BC_PROG_STR(n) (!(n)->num && !(n)->cap) |
| 702 | #define BC_PROG_NUM(r, n) \ |
| 703 | ((r)->t != BC_RESULT_ARRAY && (r)->t != BC_RESULT_STR && !BC_PROG_STR(n)) |
| 704 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 705 | #define BC_FLAG_W (1 << 0) |
| 706 | #define BC_FLAG_V (1 << 1) |
| 707 | #define BC_FLAG_S (1 << 2) |
| 708 | #define BC_FLAG_Q (1 << 3) |
| 709 | #define BC_FLAG_L (1 << 4) |
| 710 | #define BC_FLAG_I (1 << 5) |
| 711 | #define DC_FLAG_X (1 << 6) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 712 | |
| 713 | #define BC_MAX(a, b) ((a) > (b) ? (a) : (b)) |
| 714 | #define BC_MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 715 | |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 716 | #define BC_MAX_OBASE ((unsigned) 999) |
| 717 | #define BC_MAX_DIM ((unsigned) INT_MAX) |
| 718 | #define BC_MAX_SCALE ((unsigned) UINT_MAX) |
| 719 | #define BC_MAX_STRING ((unsigned) UINT_MAX - 1) |
| 720 | #define BC_MAX_NUM BC_MAX_STRING |
| 721 | // Unused apart from "limits" message. Just show a "biggish number" there. |
| 722 | //#define BC_MAX_NAME BC_MAX_STRING |
| 723 | //#define BC_MAX_EXP ((unsigned long) LONG_MAX) |
| 724 | //#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1) |
| 725 | #define BC_MAX_NAME_STR "999999999" |
| 726 | #define BC_MAX_EXP_STR "999999999" |
| 727 | #define BC_MAX_VARS_STR "999999999" |
| 728 | |
| 729 | #define BC_MAX_OBASE_STR "999" |
| 730 | |
| 731 | #if INT_MAX == 2147483647 |
| 732 | # define BC_MAX_DIM_STR "2147483647" |
| 733 | #elif INT_MAX == 9223372036854775807 |
| 734 | # define BC_MAX_DIM_STR "9223372036854775807" |
| 735 | #else |
| 736 | # error Strange INT_MAX |
| 737 | #endif |
| 738 | |
| 739 | #if UINT_MAX == 4294967295 |
| 740 | # define BC_MAX_SCALE_STR "4294967295" |
| 741 | # define BC_MAX_STRING_STR "4294967294" |
| 742 | #elif UINT_MAX == 18446744073709551615 |
| 743 | # define BC_MAX_SCALE_STR "18446744073709551615" |
| 744 | # define BC_MAX_STRING_STR "18446744073709551614" |
| 745 | #else |
| 746 | # error Strange UINT_MAX |
| 747 | #endif |
| 748 | #define BC_MAX_NUM_STR BC_MAX_STRING_STR |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 749 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 750 | struct globals { |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 751 | IF_FEATURE_BC_SIGNALS(smallint ttyin;) |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 752 | IF_FEATURE_CLEAN_UP(smallint exiting;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 753 | char sbgn; |
| 754 | char send; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 755 | |
| 756 | BcParse prs; |
| 757 | BcProgram prog; |
| 758 | |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 759 | // For error messages. Can be set to current parsed line, |
| 760 | // or [TODO] to current executing line (can be before last parsed one) |
| 761 | unsigned err_line; |
| 762 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 763 | BcVec files; |
| 764 | |
| 765 | char *env_args; |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 766 | |
| 767 | #if ENABLE_FEATURE_EDITING |
| 768 | line_input_t *line_input_state; |
| 769 | #endif |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 770 | } FIX_ALIASING; |
| 771 | #define G (*ptr_to_globals) |
| 772 | #define INIT_G() do { \ |
| 773 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 774 | } while (0) |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 775 | #define FREE_G() do { \ |
| 776 | FREE_PTR_TO_GLOBALS(); \ |
| 777 | } while (0) |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 778 | #define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S)) |
| 779 | #define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W)) |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 780 | #define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X)) |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 781 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | b9c321d | 2018-12-07 12:41:42 +0100 | [diff] [blame] | 782 | # define G_interrupt bb_got_signal |
| 783 | # define G_ttyin G.ttyin |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 784 | #else |
Denys Vlasenko | b9c321d | 2018-12-07 12:41:42 +0100 | [diff] [blame] | 785 | # define G_interrupt 0 |
| 786 | # define G_ttyin 0 |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 787 | #endif |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 788 | #if ENABLE_FEATURE_CLEAN_UP |
| 789 | # define G_exiting G.exiting |
| 790 | #else |
| 791 | # define G_exiting 0 |
| 792 | #endif |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 793 | #define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b')) |
| 794 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 795 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 796 | |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 797 | // 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] | 798 | // true if the token is valid in an expression, false otherwise. |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 799 | enum { |
| 800 | BC_PARSE_EXPRS_BITS = 0 |
| 801 | + ((uint64_t)((0 << 0)+(0 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (0*8)) |
| 802 | + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (1*8)) |
| 803 | + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (2*8)) |
| 804 | + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(0 << 3)+(0 << 4)+(1 << 5)+(1 << 6)+(0 << 7)) << (3*8)) |
| 805 | + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(1 << 6)+(1 << 7)) << (4*8)) |
| 806 | + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (5*8)) |
| 807 | + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (6*8)) |
| 808 | + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(0 << 3) ) << (7*8)) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 809 | }; |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 810 | static ALWAYS_INLINE long bc_parse_exprs(unsigned i) |
| 811 | { |
| 812 | #if ULONG_MAX > 0xffffffff |
| 813 | // 64-bit version (will not work correctly for 32-bit longs!) |
| 814 | return BC_PARSE_EXPRS_BITS & (1UL << i); |
| 815 | #else |
| 816 | // 32-bit version |
| 817 | unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS; |
| 818 | if (i >= 32) { |
| 819 | m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32); |
| 820 | i &= 31; |
| 821 | } |
| 822 | return m & (1UL << i); |
| 823 | #endif |
| 824 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 825 | |
| 826 | // 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] | 827 | static const uint8_t bc_parse_ops[] = { |
| 828 | #define OP(p,l) ((int)(l) * 0x10 + (p)) |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 829 | OP(0, false), OP( 0, false ), // inc dec |
| 830 | OP(1, false), // neg |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 831 | OP(2, false), |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 832 | OP(3, true ), OP( 3, true ), OP( 3, true ), // pow mul div |
| 833 | OP(4, true ), OP( 4, true ), // mod + - |
| 834 | OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < > |
| 835 | OP(1, false), // not |
| 836 | OP(7, true ), OP( 7, true ), // or and |
| 837 | OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= += |
| 838 | OP(5, false), OP( 5, false ), // -= = |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 839 | #undef OP |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 840 | }; |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 841 | #define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f) |
| 842 | #define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 843 | |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 844 | // Byte array of up to 4 BC_LEX's, packed into 32-bit word |
| 845 | typedef uint32_t BcParseNext; |
| 846 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 847 | // These identify what tokens can come after expressions in certain cases. |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 848 | enum { |
| 849 | #define BC_PARSE_NEXT4(a,b,c,d) ( (a) | ((b)<<8) | ((c)<<16) | ((((d)|0x80)<<24)) ) |
| 850 | #define BC_PARSE_NEXT2(a,b) BC_PARSE_NEXT4(a,b,0xff,0xff) |
| 851 | #define BC_PARSE_NEXT1(a) BC_PARSE_NEXT4(a,0xff,0xff,0xff) |
| 852 | bc_parse_next_expr = BC_PARSE_NEXT4(BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_RBRACE, BC_LEX_EOF), |
| 853 | bc_parse_next_param = BC_PARSE_NEXT2(BC_LEX_RPAREN, BC_LEX_COMMA), |
| 854 | bc_parse_next_print = BC_PARSE_NEXT4(BC_LEX_COMMA, BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_EOF), |
| 855 | bc_parse_next_rel = BC_PARSE_NEXT1(BC_LEX_RPAREN), |
| 856 | bc_parse_next_elem = BC_PARSE_NEXT1(BC_LEX_RBRACKET), |
| 857 | bc_parse_next_for = BC_PARSE_NEXT1(BC_LEX_SCOLON), |
| 858 | bc_parse_next_read = BC_PARSE_NEXT2(BC_LEX_NLINE, BC_LEX_EOF), |
| 859 | #undef BC_PARSE_NEXT4 |
| 860 | #undef BC_PARSE_NEXT2 |
| 861 | #undef BC_PARSE_NEXT1 |
| 862 | }; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 863 | #endif // ENABLE_BC |
| 864 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 865 | #if ENABLE_DC |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 866 | static const //BcLexType - should be this type, but narrower type saves size: |
| 867 | uint8_t |
| 868 | dc_lex_regs[] = { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 869 | BC_LEX_OP_REL_EQ, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_NE, |
| 870 | BC_LEX_OP_REL_LT, BC_LEX_OP_REL_GT, BC_LEX_SCOLON, BC_LEX_COLON, |
| 871 | BC_LEX_ELSE, BC_LEX_LOAD, BC_LEX_LOAD_POP, BC_LEX_OP_ASSIGN, |
| 872 | BC_LEX_STORE_PUSH, |
| 873 | }; |
| 874 | |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 875 | static const //BcLexType - should be this type |
| 876 | uint8_t |
| 877 | dc_lex_tokens[] = { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 878 | BC_LEX_OP_MODULUS, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_LPAREN, |
| 879 | BC_LEX_INVALID, BC_LEX_OP_MULTIPLY, BC_LEX_OP_PLUS, BC_LEX_INVALID, |
| 880 | BC_LEX_OP_MINUS, BC_LEX_INVALID, BC_LEX_OP_DIVIDE, |
| 881 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, |
| 882 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, |
| 883 | BC_LEX_INVALID, BC_LEX_INVALID, |
| 884 | BC_LEX_COLON, BC_LEX_SCOLON, BC_LEX_OP_REL_GT, BC_LEX_OP_REL_EQ, |
| 885 | BC_LEX_OP_REL_LT, BC_LEX_KEY_READ, BC_LEX_INVALID, |
| 886 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, |
| 887 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_EQ_NO_REG, BC_LEX_INVALID, |
| 888 | BC_LEX_KEY_IBASE, BC_LEX_INVALID, BC_LEX_KEY_SCALE, BC_LEX_LOAD_POP, |
| 889 | BC_LEX_INVALID, BC_LEX_OP_BOOL_NOT, BC_LEX_KEY_OBASE, BC_LEX_PRINT_STREAM, |
| 890 | BC_LEX_NQUIT, BC_LEX_POP, BC_LEX_STORE_PUSH, BC_LEX_INVALID, BC_LEX_INVALID, |
| 891 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_SCALE_FACTOR, BC_LEX_INVALID, |
| 892 | BC_LEX_KEY_LENGTH, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, |
| 893 | BC_LEX_OP_POWER, BC_LEX_NEG, BC_LEX_INVALID, |
| 894 | BC_LEX_ASCIIFY, BC_LEX_INVALID, BC_LEX_CLEAR_STACK, BC_LEX_DUPLICATE, |
| 895 | BC_LEX_ELSE, BC_LEX_PRINT_STACK, BC_LEX_INVALID, BC_LEX_INVALID, |
| 896 | BC_LEX_STORE_IBASE, BC_LEX_INVALID, BC_LEX_STORE_SCALE, BC_LEX_LOAD, |
| 897 | BC_LEX_INVALID, BC_LEX_PRINT_POP, BC_LEX_STORE_OBASE, BC_LEX_KEY_PRINT, |
| 898 | BC_LEX_KEY_QUIT, BC_LEX_SWAP, BC_LEX_OP_ASSIGN, BC_LEX_INVALID, |
| 899 | BC_LEX_INVALID, BC_LEX_KEY_SQRT, BC_LEX_INVALID, BC_LEX_EXECUTE, |
| 900 | BC_LEX_INVALID, BC_LEX_STACK_LEVEL, |
| 901 | BC_LEX_LBRACE, BC_LEX_OP_MODEXP, BC_LEX_INVALID, BC_LEX_OP_DIVMOD, |
| 902 | BC_LEX_INVALID |
| 903 | }; |
| 904 | |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 905 | static const //BcInst - should be this type. Using signed narrow type since BC_INST_INVALID is -1 |
| 906 | int8_t |
| 907 | dc_parse_insts[] = { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 908 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE, |
| 909 | BC_INST_INVALID, BC_INST_POWER, BC_INST_MULTIPLY, BC_INST_DIVIDE, |
| 910 | BC_INST_MODULUS, BC_INST_PLUS, BC_INST_MINUS, |
| 911 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 912 | BC_INST_INVALID, BC_INST_INVALID, |
| 913 | BC_INST_BOOL_NOT, BC_INST_INVALID, BC_INST_INVALID, |
| 914 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 915 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 916 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GT, BC_INST_INVALID, |
| 917 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE, |
| 918 | BC_INST_INVALID, BC_INST_INVALID, |
| 919 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 920 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 921 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_IBASE, |
| 922 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_LENGTH, BC_INST_INVALID, |
| 923 | BC_INST_OBASE, BC_INST_PRINT, BC_INST_QUIT, BC_INST_INVALID, |
| 924 | BC_INST_INVALID, BC_INST_SCALE, BC_INST_SQRT, BC_INST_INVALID, |
| 925 | BC_INST_REL_EQ, BC_INST_MODEXP, BC_INST_DIVMOD, BC_INST_INVALID, |
| 926 | BC_INST_INVALID, BC_INST_EXECUTE, BC_INST_PRINT_STACK, BC_INST_CLEAR_STACK, |
| 927 | BC_INST_STACK_LEN, BC_INST_DUPLICATE, BC_INST_SWAP, BC_INST_POP, |
| 928 | BC_INST_ASCIIFY, BC_INST_PRINT_STREAM, BC_INST_INVALID, BC_INST_INVALID, |
| 929 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 930 | BC_INST_PRINT, BC_INST_NQUIT, BC_INST_SCALE_FUNC, |
| 931 | }; |
| 932 | #endif // ENABLE_DC |
| 933 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 934 | static const BcNumBinaryOp bc_program_ops[] = { |
| 935 | bc_num_pow, bc_num_mul, bc_num_div, bc_num_mod, bc_num_add, bc_num_sub, |
| 936 | }; |
| 937 | |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 938 | static void fflush_and_check(void) |
| 939 | { |
| 940 | fflush_all(); |
| 941 | if (ferror(stdout) || ferror(stderr)) |
| 942 | bb_perror_msg_and_die("output error"); |
| 943 | } |
| 944 | |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 945 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 946 | #define QUIT_OR_RETURN_TO_MAIN \ |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 947 | do { \ |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 948 | 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] | 949 | G_exiting = 1; \ |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 950 | return BC_STATUS_FAILURE; \ |
| 951 | } while (0) |
| 952 | #else |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 953 | #define QUIT_OR_RETURN_TO_MAIN quit() |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 954 | #endif |
| 955 | |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 956 | static void quit(void) NORETURN; |
| 957 | static void quit(void) |
| 958 | { |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 959 | if (ferror(stdin)) |
| 960 | bb_perror_msg_and_die("input error"); |
| 961 | fflush_and_check(); |
| 962 | exit(0); |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 963 | } |
| 964 | |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 965 | static void bc_verror_msg(const char *fmt, va_list p) |
| 966 | { |
| 967 | const char *sv = sv; /* for compiler */ |
| 968 | if (G.prog.file) { |
| 969 | sv = applet_name; |
| 970 | applet_name = xasprintf("%s: %s:%u", applet_name, G.prog.file, G.err_line); |
| 971 | } |
| 972 | bb_verror_msg(fmt, p, NULL); |
| 973 | if (G.prog.file) { |
| 974 | free((char*)applet_name); |
| 975 | applet_name = sv; |
| 976 | } |
| 977 | } |
| 978 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 979 | // In configurations where errors abort instead of propagating error |
| 980 | // return code up the call chain, functions returning BC_STATUS |
| 981 | // actually don't return anything, they always succeed and return "void". |
| 982 | // A macro wrapper is provided, which makes this statement work: |
| 983 | // s = zbc_func(...) |
| 984 | // and makes it visible to the compiler that s is always zero, |
| 985 | // allowing compiler to optimize dead code after the statement. |
| 986 | // |
| 987 | // To make code more readable, each such function has a "z" |
| 988 | // ("always returning zero") prefix, i.e. zbc_foo or zdc_foo. |
| 989 | // |
| 990 | #if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 991 | # define ERRORFUNC /*nothing*/ |
| 992 | # define ERROR_RETURN(a) a |
| 993 | # define ERRORS_ARE_FATAL 0 |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 994 | # define BC_STATUS BcStatus |
| 995 | # define RETURN_STATUS(v) return (v) |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 996 | #else |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 997 | # define ERRORFUNC NORETURN |
| 998 | # define ERROR_RETURN(a) /*nothing*/ |
| 999 | # define ERRORS_ARE_FATAL 1 |
| 1000 | # define BC_STATUS void |
| 1001 | # define RETURN_STATUS(v) do { ((void)(v)); return; } while (0) |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1002 | #endif |
| 1003 | |
| 1004 | static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...) |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1005 | { |
| 1006 | va_list p; |
| 1007 | |
| 1008 | va_start(p, fmt); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 1009 | bc_verror_msg(fmt, p); |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1010 | va_end(p); |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 1011 | |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 1012 | if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin) |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1013 | exit(1); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1014 | ERROR_RETURN(return BC_STATUS_FAILURE;) |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1015 | } |
| 1016 | |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1017 | #if ENABLE_BC |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1018 | static NOINLINE int bc_posix_error_fmt(const char *fmt, ...) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1019 | { |
| 1020 | va_list p; |
| 1021 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1022 | // Are non-POSIX constructs totally ok? |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 1023 | if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W))) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1024 | return BC_STATUS_SUCCESS; // yes |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1025 | |
| 1026 | va_start(p, fmt); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 1027 | bc_verror_msg(fmt, p); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1028 | va_end(p); |
| 1029 | |
| 1030 | // Do we treat non-POSIX constructs as errors? |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 1031 | if (!(option_mask32 & BC_FLAG_S)) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1032 | return BC_STATUS_SUCCESS; // no, it's a warning |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 1033 | if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1034 | exit(1); |
| 1035 | return BC_STATUS_FAILURE; |
| 1036 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1037 | #endif |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1038 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1039 | // We use error functions with "return bc_error(FMT[, PARAMS])" idiom. |
| 1040 | // This idiom begs for tail-call optimization, but for it to work, |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1041 | // function must not have caller-cleaned parameters on stack. |
| 1042 | // Unfortunately, vararg function API does exactly that on most arches. |
| 1043 | // 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] | 1044 | static ERRORFUNC int bc_error(const char *msg) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1045 | { |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1046 | ERROR_RETURN(return) bc_error_fmt("%s", msg); |
| 1047 | } |
| 1048 | static ERRORFUNC int bc_error_bad_character(char c) |
| 1049 | { |
| 1050 | ERROR_RETURN(return) bc_error_fmt("bad character '%c'", c); |
| 1051 | } |
| 1052 | static ERRORFUNC int bc_error_bad_expression(void) |
| 1053 | { |
| 1054 | ERROR_RETURN(return) bc_error("bad expression"); |
| 1055 | } |
| 1056 | static ERRORFUNC int bc_error_bad_token(void) |
| 1057 | { |
| 1058 | ERROR_RETURN(return) bc_error("bad token"); |
| 1059 | } |
| 1060 | static ERRORFUNC int bc_error_stack_has_too_few_elements(void) |
| 1061 | { |
| 1062 | ERROR_RETURN(return) bc_error("stack has too few elements"); |
| 1063 | } |
| 1064 | static ERRORFUNC int bc_error_variable_is_wrong_type(void) |
| 1065 | { |
| 1066 | ERROR_RETURN(return) bc_error("variable is wrong type"); |
| 1067 | } |
| 1068 | static ERRORFUNC int bc_error_nested_read_call(void) |
| 1069 | { |
| 1070 | ERROR_RETURN(return) bc_error("read() call inside of a read() call"); |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1071 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1072 | #if ENABLE_BC |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 1073 | static int bc_POSIX_requires(const char *msg) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1074 | { |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 1075 | return bc_posix_error_fmt("POSIX requires %s", msg); |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1076 | } |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 1077 | static int bc_POSIX_does_not_allow(const char *msg) |
| 1078 | { |
| 1079 | return bc_posix_error_fmt("%s%s", "POSIX does not allow ", msg); |
| 1080 | } |
| 1081 | static int bc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg) |
| 1082 | { |
| 1083 | return bc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; the following is bad:", msg); |
| 1084 | } |
| 1085 | static int bc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg) |
| 1086 | { |
| 1087 | return bc_posix_error_fmt("%san empty %s expression in a for loop", "POSIX does not allow ", msg); |
| 1088 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1089 | #endif |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1090 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1091 | static void bc_vec_grow(BcVec *v, size_t n) |
| 1092 | { |
| 1093 | size_t cap = v->cap * 2; |
| 1094 | while (cap < v->len + n) cap *= 2; |
| 1095 | v->v = xrealloc(v->v, v->size * cap); |
| 1096 | v->cap = cap; |
| 1097 | } |
| 1098 | |
| 1099 | static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor) |
| 1100 | { |
| 1101 | v->size = esize; |
| 1102 | v->cap = BC_VEC_START_CAP; |
| 1103 | v->len = 0; |
| 1104 | v->dtor = dtor; |
| 1105 | v->v = xmalloc(esize * BC_VEC_START_CAP); |
| 1106 | } |
| 1107 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1108 | static void bc_char_vec_init(BcVec *v) |
| 1109 | { |
| 1110 | bc_vec_init(v, sizeof(char), NULL); |
| 1111 | } |
| 1112 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1113 | static void bc_vec_expand(BcVec *v, size_t req) |
| 1114 | { |
| 1115 | if (v->cap < req) { |
| 1116 | v->v = xrealloc(v->v, v->size * req); |
| 1117 | v->cap = req; |
| 1118 | } |
| 1119 | } |
| 1120 | |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 1121 | static void bc_vec_pop(BcVec *v) |
| 1122 | { |
| 1123 | v->len--; |
| 1124 | if (v->dtor) |
| 1125 | v->dtor(v->v + (v->size * v->len)); |
| 1126 | } |
Denys Vlasenko | 2fa11b6 | 2018-12-06 12:34:39 +0100 | [diff] [blame] | 1127 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1128 | static void bc_vec_npop(BcVec *v, size_t n) |
| 1129 | { |
| 1130 | if (!v->dtor) |
| 1131 | v->len -= n; |
| 1132 | else { |
| 1133 | size_t len = v->len - n; |
| 1134 | while (v->len > len) v->dtor(v->v + (v->size * --v->len)); |
| 1135 | } |
| 1136 | } |
| 1137 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1138 | static void bc_vec_pop_all(BcVec *v) |
| 1139 | { |
| 1140 | bc_vec_npop(v, v->len); |
| 1141 | } |
| 1142 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1143 | static void bc_vec_push(BcVec *v, const void *data) |
| 1144 | { |
| 1145 | if (v->len + 1 > v->cap) bc_vec_grow(v, 1); |
| 1146 | memmove(v->v + (v->size * v->len), data, v->size); |
| 1147 | v->len += 1; |
| 1148 | } |
| 1149 | |
| 1150 | static void bc_vec_pushByte(BcVec *v, char data) |
| 1151 | { |
| 1152 | bc_vec_push(v, &data); |
| 1153 | } |
| 1154 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1155 | static void bc_vec_pushZeroByte(BcVec *v) |
| 1156 | { |
| 1157 | //bc_vec_pushByte(v, '\0'); |
| 1158 | // better: |
| 1159 | bc_vec_push(v, &const_int_0); |
| 1160 | } |
| 1161 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1162 | static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx) |
| 1163 | { |
| 1164 | if (idx == v->len) |
| 1165 | bc_vec_push(v, data); |
| 1166 | else { |
| 1167 | |
| 1168 | char *ptr; |
| 1169 | |
| 1170 | if (v->len == v->cap) bc_vec_grow(v, 1); |
| 1171 | |
| 1172 | ptr = v->v + v->size * idx; |
| 1173 | |
| 1174 | memmove(ptr + v->size, ptr, v->size * (v->len++ - idx)); |
| 1175 | memmove(ptr, data, v->size); |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | static void bc_vec_string(BcVec *v, size_t len, const char *str) |
| 1180 | { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1181 | bc_vec_pop_all(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1182 | bc_vec_expand(v, len + 1); |
| 1183 | memcpy(v->v, str, len); |
| 1184 | v->len = len; |
| 1185 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1186 | bc_vec_pushZeroByte(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | static void bc_vec_concat(BcVec *v, const char *str) |
| 1190 | { |
Denys Vlasenko | 8b4cf0d | 2018-12-10 15:12:58 +0100 | [diff] [blame] | 1191 | size_t len, slen; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1192 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1193 | if (v->len == 0) bc_vec_pushZeroByte(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1194 | |
Denys Vlasenko | 8b4cf0d | 2018-12-10 15:12:58 +0100 | [diff] [blame] | 1195 | slen = strlen(str); |
| 1196 | len = v->len + slen; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1197 | |
Denys Vlasenko | 8b4cf0d | 2018-12-10 15:12:58 +0100 | [diff] [blame] | 1198 | if (v->cap < len) bc_vec_grow(v, slen); |
Denys Vlasenko | 1ff8862 | 2018-12-06 12:06:16 +0100 | [diff] [blame] | 1199 | strcpy(v->v + v->len - 1, str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1200 | |
| 1201 | v->len = len; |
| 1202 | } |
| 1203 | |
| 1204 | static void *bc_vec_item(const BcVec *v, size_t idx) |
| 1205 | { |
| 1206 | return v->v + v->size * idx; |
| 1207 | } |
| 1208 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 1209 | static char** bc_program_str(size_t idx) |
| 1210 | { |
| 1211 | return bc_vec_item(&G.prog.strs, idx); |
| 1212 | } |
| 1213 | |
| 1214 | static BcFunc* bc_program_func(size_t idx) |
| 1215 | { |
| 1216 | return bc_vec_item(&G.prog.fns, idx); |
| 1217 | } |
| 1218 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1219 | static void *bc_vec_item_rev(const BcVec *v, size_t idx) |
| 1220 | { |
| 1221 | return v->v + v->size * (v->len - idx - 1); |
| 1222 | } |
| 1223 | |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 1224 | static void *bc_vec_top(const BcVec *v) |
| 1225 | { |
| 1226 | return v->v + v->size * (v->len - 1); |
| 1227 | } |
| 1228 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1229 | static FAST_FUNC void bc_vec_free(void *vec) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1230 | { |
| 1231 | BcVec *v = (BcVec *) vec; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1232 | bc_vec_pop_all(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1233 | free(v->v); |
| 1234 | } |
| 1235 | |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 1236 | static int bc_id_cmp(const void *e1, const void *e2) |
| 1237 | { |
| 1238 | return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name); |
| 1239 | } |
| 1240 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1241 | static FAST_FUNC void bc_id_free(void *id) |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 1242 | { |
| 1243 | free(((BcId *) id)->name); |
| 1244 | } |
| 1245 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1246 | static size_t bc_map_find(const BcVec *v, const void *ptr) |
| 1247 | { |
| 1248 | size_t low = 0, high = v->len; |
| 1249 | |
| 1250 | while (low < high) { |
| 1251 | |
| 1252 | size_t mid = (low + high) / 2; |
| 1253 | BcId *id = bc_vec_item(v, mid); |
| 1254 | int result = bc_id_cmp(ptr, id); |
| 1255 | |
| 1256 | if (result == 0) |
| 1257 | return mid; |
| 1258 | else if (result < 0) |
| 1259 | high = mid; |
| 1260 | else |
| 1261 | low = mid + 1; |
| 1262 | } |
| 1263 | |
| 1264 | return low; |
| 1265 | } |
| 1266 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1267 | 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] | 1268 | { |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1269 | size_t n = *i = bc_map_find(v, ptr); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1270 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1271 | if (n == v->len) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1272 | bc_vec_push(v, ptr); |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1273 | else if (!bc_id_cmp(ptr, bc_vec_item(v, n))) |
| 1274 | return 0; // "was not inserted" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1275 | else |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1276 | bc_vec_pushAt(v, ptr, n); |
| 1277 | return 1; // "was inserted" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1278 | } |
| 1279 | |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1280 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1281 | static size_t bc_map_index(const BcVec *v, const void *ptr) |
| 1282 | { |
| 1283 | size_t i = bc_map_find(v, ptr); |
| 1284 | if (i >= v->len) return BC_VEC_INVALID_IDX; |
| 1285 | return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i; |
| 1286 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1287 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1288 | |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1289 | static int push_input_byte(BcVec *vec, char c) |
| 1290 | { |
| 1291 | if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'? |
| 1292 | || c > 0x7e |
| 1293 | ) { |
| 1294 | // Bad chars on this line, ignore entire line |
| 1295 | bc_error_fmt("illegal character 0x%02x", c); |
| 1296 | return 1; |
| 1297 | } |
| 1298 | bc_vec_pushByte(vec, (char)c); |
| 1299 | return 0; |
| 1300 | } |
| 1301 | |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 1302 | static BcStatus bc_read_line(BcVec *vec) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1303 | { |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 1304 | BcStatus s; |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1305 | bool bad_chars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1306 | |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 1307 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 1308 | do { |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1309 | int c; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1310 | |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1311 | bad_chars = 0; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1312 | bc_vec_pop_all(vec); |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1313 | |
| 1314 | fflush_and_check(); |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1315 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1316 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | b9c321d | 2018-12-07 12:41:42 +0100 | [diff] [blame] | 1317 | if (G_interrupt) { // ^C was pressed |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1318 | intr: |
Denys Vlasenko | b9c321d | 2018-12-07 12:41:42 +0100 | [diff] [blame] | 1319 | G_interrupt = 0; |
Denys Vlasenko | ac6ed11 | 2018-12-08 21:39:10 +0100 | [diff] [blame] | 1320 | // GNU bc says "interrupted execution." |
| 1321 | // GNU dc says "Interrupt!" |
| 1322 | fputs("\ninterrupted execution\n", stderr); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1323 | } |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1324 | # if ENABLE_FEATURE_EDITING |
| 1325 | if (G_ttyin) { |
| 1326 | int n, i; |
| 1327 | # define line_buf bb_common_bufsiz1 |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 1328 | n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE); |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1329 | if (n <= 0) { // read errors or EOF, or ^D, or ^C |
| 1330 | if (n == 0) // ^C |
| 1331 | goto intr; |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 1332 | s = BC_STATUS_EOF; |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1333 | break; |
| 1334 | } |
| 1335 | i = 0; |
| 1336 | for (;;) { |
| 1337 | c = line_buf[i++]; |
| 1338 | if (!c) break; |
| 1339 | bad_chars |= push_input_byte(vec, c); |
| 1340 | } |
| 1341 | # undef line_buf |
| 1342 | } else |
| 1343 | # endif |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1344 | #endif |
Denys Vlasenko | ed84935 | 2018-12-06 10:26:13 +0100 | [diff] [blame] | 1345 | { |
Denys Vlasenko | ed84935 | 2018-12-06 10:26:13 +0100 | [diff] [blame] | 1346 | IF_FEATURE_BC_SIGNALS(errno = 0;) |
| 1347 | do { |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1348 | c = fgetc(stdin); |
| 1349 | #if ENABLE_FEATURE_BC_SIGNALS && !ENABLE_FEATURE_EDITING |
| 1350 | // Both conditions appear simultaneously, check both just in case |
Denys Vlasenko | b9c321d | 2018-12-07 12:41:42 +0100 | [diff] [blame] | 1351 | if (errno == EINTR || G_interrupt) { |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1352 | // ^C was pressed |
| 1353 | clearerr(stdin); |
| 1354 | goto intr; |
| 1355 | } |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1356 | #endif |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1357 | if (c == EOF) { |
Denys Vlasenko | ed84935 | 2018-12-06 10:26:13 +0100 | [diff] [blame] | 1358 | if (ferror(stdin)) |
| 1359 | quit(); // this emits error message |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 1360 | s = BC_STATUS_EOF; |
Denys Vlasenko | ed84935 | 2018-12-06 10:26:13 +0100 | [diff] [blame] | 1361 | // Note: EOF does not append '\n', therefore: |
| 1362 | // printf 'print 123\n' | bc - works |
| 1363 | // printf 'print 123' | bc - fails (syntax error) |
| 1364 | break; |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1365 | } |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1366 | bad_chars |= push_input_byte(vec, c); |
| 1367 | } while (c != '\n'); |
Denys Vlasenko | ed84935 | 2018-12-06 10:26:13 +0100 | [diff] [blame] | 1368 | } |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1369 | } while (bad_chars); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1370 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1371 | bc_vec_pushZeroByte(vec); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1372 | |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 1373 | return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1374 | } |
| 1375 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 1376 | static char* bc_read_file(const char *path) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1377 | { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 1378 | char *buf; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 1379 | size_t size = ((size_t) -1); |
| 1380 | size_t i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1381 | |
Denys Vlasenko | 4c9455f | 2018-12-06 15:21:39 +0100 | [diff] [blame] | 1382 | // Never returns NULL (dies on errors) |
| 1383 | buf = xmalloc_xopen_read_close(path, &size); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1384 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 1385 | for (i = 0; i < size; ++i) { |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1386 | char c = buf[i]; |
| 1387 | if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'? |
| 1388 | || c > 0x7e |
| 1389 | ) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 1390 | free(buf); |
| 1391 | buf = NULL; |
| 1392 | break; |
| 1393 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1394 | } |
| 1395 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 1396 | return buf; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1397 | } |
| 1398 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1399 | static void bc_num_setToZero(BcNum *n, size_t scale) |
| 1400 | { |
| 1401 | n->len = 0; |
| 1402 | n->neg = false; |
| 1403 | n->rdx = scale; |
| 1404 | } |
| 1405 | |
| 1406 | static void bc_num_zero(BcNum *n) |
| 1407 | { |
| 1408 | bc_num_setToZero(n, 0); |
| 1409 | } |
| 1410 | |
| 1411 | static void bc_num_one(BcNum *n) |
| 1412 | { |
| 1413 | bc_num_setToZero(n, 0); |
| 1414 | n->len = 1; |
| 1415 | n->num[0] = 1; |
| 1416 | } |
| 1417 | |
| 1418 | static void bc_num_ten(BcNum *n) |
| 1419 | { |
| 1420 | bc_num_setToZero(n, 0); |
| 1421 | n->len = 2; |
| 1422 | n->num[0] = 0; |
| 1423 | n->num[1] = 1; |
| 1424 | } |
| 1425 | |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1426 | // Note: this also sets BcNum to zero |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1427 | static void bc_num_init(BcNum *n, size_t req) |
| 1428 | { |
| 1429 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1430 | //memset(n, 0, sizeof(BcNum)); - cleared by assignments below |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1431 | n->num = xmalloc(req); |
| 1432 | n->cap = req; |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1433 | n->rdx = 0; |
| 1434 | n->len = 0; |
| 1435 | n->neg = false; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1436 | } |
| 1437 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 1438 | static void bc_num_init_DEF_SIZE(BcNum *n) |
| 1439 | { |
| 1440 | bc_num_init(n, BC_NUM_DEF_SIZE); |
| 1441 | } |
| 1442 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1443 | static void bc_num_expand(BcNum *n, size_t req) |
| 1444 | { |
| 1445 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
| 1446 | if (req > n->cap) { |
| 1447 | n->num = xrealloc(n->num, req); |
| 1448 | n->cap = req; |
| 1449 | } |
| 1450 | } |
| 1451 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1452 | static FAST_FUNC void bc_num_free(void *num) |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1453 | { |
| 1454 | free(((BcNum *) num)->num); |
| 1455 | } |
| 1456 | |
| 1457 | static void bc_num_copy(BcNum *d, BcNum *s) |
| 1458 | { |
| 1459 | if (d != s) { |
| 1460 | bc_num_expand(d, s->cap); |
| 1461 | d->len = s->len; |
| 1462 | d->neg = s->neg; |
| 1463 | d->rdx = s->rdx; |
| 1464 | memcpy(d->num, s->num, sizeof(BcDig) * d->len); |
| 1465 | } |
| 1466 | } |
| 1467 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 1468 | static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p) |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1469 | { |
| 1470 | size_t i; |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1471 | unsigned long pow, result; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1472 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 1473 | if (n->neg) RETURN_STATUS(bc_error("negative number")); |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1474 | |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1475 | for (result = 0, pow = 1, i = n->rdx; i < n->len; ++i) { |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1476 | |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1477 | unsigned long prev = result, powprev = pow; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1478 | |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1479 | result += ((unsigned long) n->num[i]) * pow; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1480 | pow *= 10; |
| 1481 | |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1482 | if (result < prev || pow < powprev) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 1483 | RETURN_STATUS(bc_error("overflow")); |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1484 | prev = result; |
| 1485 | powprev = pow; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1486 | } |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1487 | *result_p = result; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1488 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 1489 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1490 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 1491 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 1492 | # define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 1493 | #endif |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1494 | |
| 1495 | static void bc_num_ulong2num(BcNum *n, unsigned long val) |
| 1496 | { |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1497 | BcDig *ptr; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1498 | |
| 1499 | bc_num_zero(n); |
| 1500 | |
| 1501 | if (val == 0) return; |
| 1502 | |
Denys Vlasenko | b696d9e | 2018-12-10 12:22:15 +0100 | [diff] [blame] | 1503 | if (ULONG_MAX == 0xffffffffUL) |
| 1504 | bc_num_expand(n, 10); // 10 digits: 4294967295 |
Denys Vlasenko | c665c18 | 2018-12-10 15:15:42 +0100 | [diff] [blame] | 1505 | if (ULONG_MAX == 0xffffffffffffffffULL) |
Denys Vlasenko | b696d9e | 2018-12-10 12:22:15 +0100 | [diff] [blame] | 1506 | bc_num_expand(n, 20); // 20 digits: 18446744073709551615 |
Denys Vlasenko | c665c18 | 2018-12-10 15:15:42 +0100 | [diff] [blame] | 1507 | BUILD_BUG_ON(ULONG_MAX > 0xffffffffffffffffULL); |
Denys Vlasenko | b696d9e | 2018-12-10 12:22:15 +0100 | [diff] [blame] | 1508 | |
| 1509 | ptr = n->num; |
| 1510 | for (;;) { |
| 1511 | n->len++; |
| 1512 | *ptr++ = val % 10; |
| 1513 | val /= 10; |
| 1514 | if (val == 0) break; |
| 1515 | } |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1516 | } |
| 1517 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1518 | static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1519 | size_t len) |
| 1520 | { |
| 1521 | size_t i, j; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1522 | for (i = 0; i < len; ++i) { |
| 1523 | for (a[i] -= b[i], j = 0; a[i + j] < 0;) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1524 | a[i + j++] += 10; |
| 1525 | a[i + j] -= 1; |
| 1526 | } |
| 1527 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1528 | } |
| 1529 | |
Denys Vlasenko | 2d615fe | 2018-12-07 16:22:45 +0100 | [diff] [blame] | 1530 | #define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg)) |
| 1531 | #define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1) |
| 1532 | #define BC_NUM_INT(n) ((n)->len - (n)->rdx) |
| 1533 | //#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1) |
| 1534 | static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b) |
| 1535 | { |
| 1536 | return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1; |
| 1537 | } |
| 1538 | //#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1) |
| 1539 | static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale) |
| 1540 | { |
| 1541 | return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1; |
| 1542 | } |
| 1543 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1544 | static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len) |
| 1545 | { |
| 1546 | size_t i; |
| 1547 | int c = 0; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1548 | for (i = len - 1; i < len && !(c = a[i] - b[i]); --i); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1549 | return BC_NUM_NEG(i + 1, c < 0); |
| 1550 | } |
| 1551 | |
| 1552 | static ssize_t bc_num_cmp(BcNum *a, BcNum *b) |
| 1553 | { |
| 1554 | size_t i, min, a_int, b_int, diff; |
| 1555 | BcDig *max_num, *min_num; |
| 1556 | bool a_max, neg = false; |
| 1557 | ssize_t cmp; |
| 1558 | |
| 1559 | if (a == b) return 0; |
| 1560 | if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg); |
| 1561 | if (b->len == 0) return BC_NUM_NEG(1, a->neg); |
| 1562 | if (a->neg) { |
| 1563 | if (b->neg) |
| 1564 | neg = true; |
| 1565 | else |
| 1566 | return -1; |
| 1567 | } |
| 1568 | else if (b->neg) |
| 1569 | return 1; |
| 1570 | |
| 1571 | a_int = BC_NUM_INT(a); |
| 1572 | b_int = BC_NUM_INT(b); |
| 1573 | a_int -= b_int; |
| 1574 | a_max = (a->rdx > b->rdx); |
| 1575 | |
| 1576 | if (a_int != 0) return (ssize_t) a_int; |
| 1577 | |
| 1578 | if (a_max) { |
| 1579 | min = b->rdx; |
| 1580 | diff = a->rdx - b->rdx; |
| 1581 | max_num = a->num + diff; |
| 1582 | min_num = b->num; |
| 1583 | } |
| 1584 | else { |
| 1585 | min = a->rdx; |
| 1586 | diff = b->rdx - a->rdx; |
| 1587 | max_num = b->num + diff; |
| 1588 | min_num = a->num; |
| 1589 | } |
| 1590 | |
| 1591 | cmp = bc_num_compare(max_num, min_num, b_int + min); |
| 1592 | if (cmp != 0) return BC_NUM_NEG(cmp, (!a_max) != neg); |
| 1593 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1594 | for (max_num -= diff, i = diff - 1; i < diff; --i) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1595 | if (max_num[i]) return BC_NUM_NEG(1, (!a_max) != neg); |
| 1596 | } |
| 1597 | |
| 1598 | return 0; |
| 1599 | } |
| 1600 | |
| 1601 | static void bc_num_truncate(BcNum *n, size_t places) |
| 1602 | { |
| 1603 | if (places == 0) return; |
| 1604 | |
| 1605 | n->rdx -= places; |
| 1606 | |
| 1607 | if (n->len != 0) { |
| 1608 | n->len -= places; |
| 1609 | memmove(n->num, n->num + places, n->len * sizeof(BcDig)); |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | static void bc_num_extend(BcNum *n, size_t places) |
| 1614 | { |
| 1615 | size_t len = n->len + places; |
| 1616 | |
| 1617 | if (places != 0) { |
| 1618 | |
| 1619 | if (n->cap < len) bc_num_expand(n, len); |
| 1620 | |
| 1621 | memmove(n->num + places, n->num, sizeof(BcDig) * n->len); |
| 1622 | memset(n->num, 0, sizeof(BcDig) * places); |
| 1623 | |
| 1624 | n->len += places; |
| 1625 | n->rdx += places; |
| 1626 | } |
| 1627 | } |
| 1628 | |
| 1629 | static void bc_num_clean(BcNum *n) |
| 1630 | { |
| 1631 | while (n->len > 0 && n->num[n->len - 1] == 0) --n->len; |
| 1632 | if (n->len == 0) |
| 1633 | n->neg = false; |
| 1634 | else if (n->len < n->rdx) |
| 1635 | n->len = n->rdx; |
| 1636 | } |
| 1637 | |
| 1638 | static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2) |
| 1639 | { |
| 1640 | if (n->rdx < scale) |
| 1641 | bc_num_extend(n, scale - n->rdx); |
| 1642 | else |
| 1643 | bc_num_truncate(n, n->rdx - scale); |
| 1644 | |
| 1645 | bc_num_clean(n); |
| 1646 | if (n->len != 0) n->neg = !neg1 != !neg2; |
| 1647 | } |
| 1648 | |
| 1649 | static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a, |
| 1650 | BcNum *restrict b) |
| 1651 | { |
| 1652 | if (idx < n->len) { |
| 1653 | |
| 1654 | b->len = n->len - idx; |
| 1655 | a->len = idx; |
| 1656 | a->rdx = b->rdx = 0; |
| 1657 | |
| 1658 | memcpy(b->num, n->num + idx, b->len * sizeof(BcDig)); |
| 1659 | memcpy(a->num, n->num, idx * sizeof(BcDig)); |
| 1660 | } |
| 1661 | else { |
| 1662 | bc_num_zero(b); |
| 1663 | bc_num_copy(a, n); |
| 1664 | } |
| 1665 | |
| 1666 | bc_num_clean(a); |
| 1667 | bc_num_clean(b); |
| 1668 | } |
| 1669 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 1670 | static BC_STATUS zbc_num_shift(BcNum *n, size_t places) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1671 | { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 1672 | if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 1673 | |
| 1674 | // This check makes sense only if size_t is (much) larger than BC_MAX_NUM. |
| 1675 | if (SIZE_MAX > (BC_MAX_NUM | 0xff)) { |
| 1676 | if (places + n->len > BC_MAX_NUM) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 1677 | 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] | 1678 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1679 | |
| 1680 | if (n->rdx >= places) |
| 1681 | n->rdx -= places; |
| 1682 | else { |
| 1683 | bc_num_extend(n, places - n->rdx); |
| 1684 | n->rdx = 0; |
| 1685 | } |
| 1686 | |
| 1687 | bc_num_clean(n); |
| 1688 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 1689 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1690 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 1691 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 1692 | # define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 1693 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1694 | |
| 1695 | static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) |
| 1696 | { |
| 1697 | BcNum one; |
| 1698 | BcDig num[2]; |
| 1699 | |
| 1700 | one.cap = 2; |
| 1701 | one.num = num; |
| 1702 | bc_num_one(&one); |
| 1703 | |
| 1704 | return bc_num_div(&one, a, b, scale); |
| 1705 | } |
| 1706 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1707 | static FAST_FUNC BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1708 | { |
| 1709 | BcDig *ptr, *ptr_a, *ptr_b, *ptr_c; |
| 1710 | size_t i, max, min_rdx, min_int, diff, a_int, b_int; |
| 1711 | int carry, in; |
| 1712 | |
| 1713 | // Because this function doesn't need to use scale (per the bc spec), |
| 1714 | // I am hijacking it to say whether it's doing an add or a subtract. |
| 1715 | |
| 1716 | if (a->len == 0) { |
| 1717 | bc_num_copy(c, b); |
| 1718 | if (sub && c->len) c->neg = !c->neg; |
| 1719 | return BC_STATUS_SUCCESS; |
| 1720 | } |
| 1721 | else if (b->len == 0) { |
| 1722 | bc_num_copy(c, a); |
| 1723 | return BC_STATUS_SUCCESS; |
| 1724 | } |
| 1725 | |
| 1726 | c->neg = a->neg; |
| 1727 | c->rdx = BC_MAX(a->rdx, b->rdx); |
| 1728 | min_rdx = BC_MIN(a->rdx, b->rdx); |
| 1729 | c->len = 0; |
| 1730 | |
| 1731 | if (a->rdx > b->rdx) { |
| 1732 | diff = a->rdx - b->rdx; |
| 1733 | ptr = a->num; |
| 1734 | ptr_a = a->num + diff; |
| 1735 | ptr_b = b->num; |
| 1736 | } |
| 1737 | else { |
| 1738 | diff = b->rdx - a->rdx; |
| 1739 | ptr = b->num; |
| 1740 | ptr_a = a->num; |
| 1741 | ptr_b = b->num + diff; |
| 1742 | } |
| 1743 | |
| 1744 | for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i]; |
| 1745 | |
| 1746 | ptr_c += diff; |
| 1747 | a_int = BC_NUM_INT(a); |
| 1748 | b_int = BC_NUM_INT(b); |
| 1749 | |
| 1750 | if (a_int > b_int) { |
| 1751 | min_int = b_int; |
| 1752 | max = a_int; |
| 1753 | ptr = ptr_a; |
| 1754 | } |
| 1755 | else { |
| 1756 | min_int = a_int; |
| 1757 | max = b_int; |
| 1758 | ptr = ptr_b; |
| 1759 | } |
| 1760 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1761 | 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] | 1762 | in = ((int) ptr_a[i]) + ((int) ptr_b[i]) + carry; |
| 1763 | carry = in / 10; |
| 1764 | ptr_c[i] = (BcDig)(in % 10); |
| 1765 | } |
| 1766 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1767 | for (; i < max + min_rdx; ++i, ++c->len) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1768 | in = ((int) ptr[i]) + carry; |
| 1769 | carry = in / 10; |
| 1770 | ptr_c[i] = (BcDig)(in % 10); |
| 1771 | } |
| 1772 | |
| 1773 | if (carry != 0) c->num[c->len++] = (BcDig) carry; |
| 1774 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1775 | return BC_STATUS_SUCCESS; // can't make void, see bc_num_binary() |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1776 | } |
| 1777 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1778 | static FAST_FUNC BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1779 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1780 | ssize_t cmp; |
| 1781 | BcNum *minuend, *subtrahend; |
| 1782 | size_t start; |
| 1783 | bool aneg, bneg, neg; |
| 1784 | |
| 1785 | // Because this function doesn't need to use scale (per the bc spec), |
| 1786 | // I am hijacking it to say whether it's doing an add or a subtract. |
| 1787 | |
| 1788 | if (a->len == 0) { |
| 1789 | bc_num_copy(c, b); |
| 1790 | if (sub && c->len) c->neg = !c->neg; |
| 1791 | return BC_STATUS_SUCCESS; |
| 1792 | } |
| 1793 | else if (b->len == 0) { |
| 1794 | bc_num_copy(c, a); |
| 1795 | return BC_STATUS_SUCCESS; |
| 1796 | } |
| 1797 | |
| 1798 | aneg = a->neg; |
| 1799 | bneg = b->neg; |
| 1800 | a->neg = b->neg = false; |
| 1801 | |
| 1802 | cmp = bc_num_cmp(a, b); |
| 1803 | |
| 1804 | a->neg = aneg; |
| 1805 | b->neg = bneg; |
| 1806 | |
| 1807 | if (cmp == 0) { |
| 1808 | bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx)); |
| 1809 | return BC_STATUS_SUCCESS; |
| 1810 | } |
| 1811 | else if (cmp > 0) { |
| 1812 | neg = a->neg; |
| 1813 | minuend = a; |
| 1814 | subtrahend = b; |
| 1815 | } |
| 1816 | else { |
| 1817 | neg = b->neg; |
| 1818 | if (sub) neg = !neg; |
| 1819 | minuend = b; |
| 1820 | subtrahend = a; |
| 1821 | } |
| 1822 | |
| 1823 | bc_num_copy(c, minuend); |
| 1824 | c->neg = neg; |
| 1825 | |
| 1826 | if (c->rdx < subtrahend->rdx) { |
| 1827 | bc_num_extend(c, subtrahend->rdx - c->rdx); |
| 1828 | start = 0; |
| 1829 | } |
| 1830 | else |
| 1831 | start = c->rdx - subtrahend->rdx; |
| 1832 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1833 | bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1834 | |
| 1835 | bc_num_clean(c); |
| 1836 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1837 | return BC_STATUS_SUCCESS; // can't make void, see bc_num_binary() |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1838 | } |
| 1839 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1840 | static FAST_FUNC BcStatus bc_num_k(BcNum *restrict a, BcNum *restrict b, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1841 | BcNum *restrict c) |
| 1842 | { |
| 1843 | BcStatus s; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1844 | 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] | 1845 | BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1846 | bool aone; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1847 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1848 | if (a->len == 0 || b->len == 0) { |
| 1849 | bc_num_zero(c); |
| 1850 | return BC_STATUS_SUCCESS; |
| 1851 | } |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1852 | aone = BC_NUM_ONE(a); |
| 1853 | if (aone || BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1854 | bc_num_copy(c, aone ? b : a); |
| 1855 | return BC_STATUS_SUCCESS; |
| 1856 | } |
| 1857 | |
| 1858 | if (a->len + b->len < BC_NUM_KARATSUBA_LEN || |
| 1859 | a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN) |
| 1860 | { |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1861 | size_t i, j, len; |
Denys Vlasenko | b3cb901 | 2018-12-05 19:05:32 +0100 | [diff] [blame] | 1862 | unsigned carry; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1863 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1864 | bc_num_expand(c, a->len + b->len + 1); |
| 1865 | |
| 1866 | memset(c->num, 0, sizeof(BcDig) * c->cap); |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1867 | c->len = len = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1868 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1869 | for (i = 0; i < b->len; ++i) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1870 | |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1871 | carry = 0; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1872 | for (j = 0; j < a->len; ++j) { |
Denys Vlasenko | b3cb901 | 2018-12-05 19:05:32 +0100 | [diff] [blame] | 1873 | unsigned in = c->num[i + j]; |
| 1874 | in += ((unsigned) a->num[j]) * ((unsigned) b->num[i]) + carry; |
| 1875 | // note: compilers prefer _unsigned_ div/const |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1876 | carry = in / 10; |
| 1877 | c->num[i + j] = (BcDig)(in % 10); |
| 1878 | } |
| 1879 | |
| 1880 | c->num[i + j] += (BcDig) carry; |
| 1881 | len = BC_MAX(len, i + j + !!carry); |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 1882 | |
| 1883 | // a=2^1000000 |
| 1884 | // a*a <- without check below, this will not be interruptible |
| 1885 | if (G_interrupt) return BC_STATUS_FAILURE; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | c->len = len; |
| 1889 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1890 | return BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1891 | } |
| 1892 | |
| 1893 | bc_num_init(&l1, max); |
| 1894 | bc_num_init(&h1, max); |
| 1895 | bc_num_init(&l2, max); |
| 1896 | bc_num_init(&h2, max); |
| 1897 | bc_num_init(&m1, max); |
| 1898 | bc_num_init(&m2, max); |
| 1899 | bc_num_init(&z0, max); |
| 1900 | bc_num_init(&z1, max); |
| 1901 | bc_num_init(&z2, max); |
| 1902 | bc_num_init(&temp, max + max); |
| 1903 | |
| 1904 | bc_num_split(a, max2, &l1, &h1); |
| 1905 | bc_num_split(b, max2, &l2, &h2); |
| 1906 | |
| 1907 | s = bc_num_add(&h1, &l1, &m1, 0); |
| 1908 | if (s) goto err; |
| 1909 | s = bc_num_add(&h2, &l2, &m2, 0); |
| 1910 | if (s) goto err; |
| 1911 | |
| 1912 | s = bc_num_k(&h1, &h2, &z0); |
| 1913 | if (s) goto err; |
| 1914 | s = bc_num_k(&m1, &m2, &z1); |
| 1915 | if (s) goto err; |
| 1916 | s = bc_num_k(&l1, &l2, &z2); |
| 1917 | if (s) goto err; |
| 1918 | |
| 1919 | s = bc_num_sub(&z1, &z0, &temp, 0); |
| 1920 | if (s) goto err; |
| 1921 | s = bc_num_sub(&temp, &z2, &z1, 0); |
| 1922 | if (s) goto err; |
| 1923 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 1924 | s = zbc_num_shift(&z0, max2 * 2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1925 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 1926 | s = zbc_num_shift(&z1, max2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1927 | if (s) goto err; |
| 1928 | s = bc_num_add(&z0, &z1, &temp, 0); |
| 1929 | if (s) goto err; |
| 1930 | s = bc_num_add(&temp, &z2, c, 0); |
| 1931 | |
| 1932 | err: |
| 1933 | bc_num_free(&temp); |
| 1934 | bc_num_free(&z2); |
| 1935 | bc_num_free(&z1); |
| 1936 | bc_num_free(&z0); |
| 1937 | bc_num_free(&m2); |
| 1938 | bc_num_free(&m1); |
| 1939 | bc_num_free(&h2); |
| 1940 | bc_num_free(&l2); |
| 1941 | bc_num_free(&h1); |
| 1942 | bc_num_free(&l1); |
| 1943 | return s; |
| 1944 | } |
| 1945 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1946 | static FAST_FUNC BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1947 | { |
| 1948 | BcStatus s; |
| 1949 | BcNum cpa, cpb; |
| 1950 | size_t maxrdx = BC_MAX(a->rdx, b->rdx); |
| 1951 | |
| 1952 | scale = BC_MAX(scale, a->rdx); |
| 1953 | scale = BC_MAX(scale, b->rdx); |
| 1954 | scale = BC_MIN(a->rdx + b->rdx, scale); |
| 1955 | maxrdx = BC_MAX(maxrdx, scale); |
| 1956 | |
| 1957 | bc_num_init(&cpa, a->len); |
| 1958 | bc_num_init(&cpb, b->len); |
| 1959 | |
| 1960 | bc_num_copy(&cpa, a); |
| 1961 | bc_num_copy(&cpb, b); |
| 1962 | cpa.neg = cpb.neg = false; |
| 1963 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 1964 | s = zbc_num_shift(&cpa, maxrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1965 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 1966 | s = zbc_num_shift(&cpb, maxrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1967 | if (s) goto err; |
| 1968 | s = bc_num_k(&cpa, &cpb, c); |
| 1969 | if (s) goto err; |
| 1970 | |
| 1971 | maxrdx += scale; |
| 1972 | bc_num_expand(c, c->len + maxrdx); |
| 1973 | |
| 1974 | if (c->len < maxrdx) { |
| 1975 | memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig)); |
| 1976 | c->len += maxrdx; |
| 1977 | } |
| 1978 | |
| 1979 | c->rdx = maxrdx; |
| 1980 | bc_num_retireMul(c, scale, a->neg, b->neg); |
| 1981 | |
| 1982 | err: |
| 1983 | bc_num_free(&cpb); |
| 1984 | bc_num_free(&cpa); |
| 1985 | return s; |
| 1986 | } |
| 1987 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1988 | static FAST_FUNC BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1989 | { |
| 1990 | BcStatus s = BC_STATUS_SUCCESS; |
| 1991 | BcDig *n, *p, q; |
| 1992 | size_t len, end, i; |
| 1993 | BcNum cp; |
| 1994 | bool zero = true; |
| 1995 | |
| 1996 | if (b->len == 0) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 1997 | return bc_error("divide by zero"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1998 | else if (a->len == 0) { |
| 1999 | bc_num_setToZero(c, scale); |
| 2000 | return BC_STATUS_SUCCESS; |
| 2001 | } |
| 2002 | else if (BC_NUM_ONE(b)) { |
| 2003 | bc_num_copy(c, a); |
| 2004 | bc_num_retireMul(c, scale, a->neg, b->neg); |
| 2005 | return BC_STATUS_SUCCESS; |
| 2006 | } |
| 2007 | |
| 2008 | bc_num_init(&cp, BC_NUM_MREQ(a, b, scale)); |
| 2009 | bc_num_copy(&cp, a); |
| 2010 | len = b->len; |
| 2011 | |
| 2012 | if (len > cp.len) { |
| 2013 | bc_num_expand(&cp, len + 2); |
| 2014 | bc_num_extend(&cp, len - cp.len); |
| 2015 | } |
| 2016 | |
| 2017 | if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx); |
| 2018 | cp.rdx -= b->rdx; |
| 2019 | if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx); |
| 2020 | |
| 2021 | if (b->rdx == b->len) { |
| 2022 | for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1]; |
| 2023 | len -= i - 1; |
| 2024 | } |
| 2025 | |
| 2026 | if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1); |
| 2027 | |
| 2028 | // We want an extra zero in front to make things simpler. |
| 2029 | cp.num[cp.len++] = 0; |
| 2030 | end = cp.len - len; |
| 2031 | |
| 2032 | bc_num_expand(c, cp.len); |
| 2033 | |
| 2034 | bc_num_zero(c); |
| 2035 | memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig)); |
| 2036 | c->rdx = cp.rdx; |
| 2037 | c->len = cp.len; |
| 2038 | p = b->num; |
| 2039 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2040 | for (i = end - 1; !s && i < end; --i) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2041 | n = cp.num + i; |
| 2042 | 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] | 2043 | bc_num_subArrays(n, p, len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2044 | c->num[i] = q; |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 2045 | // a=2^100000 |
| 2046 | // scale=40000 |
| 2047 | // 1/a <- without check below, this will not be interruptible |
| 2048 | if (G_interrupt) { |
| 2049 | s = BC_STATUS_FAILURE; |
| 2050 | break; |
| 2051 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2052 | } |
| 2053 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2054 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2055 | bc_num_free(&cp); |
| 2056 | |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 2057 | return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2058 | } |
| 2059 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2060 | static FAST_FUNC BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2061 | BcNum *restrict d, size_t scale, size_t ts) |
| 2062 | { |
| 2063 | BcStatus s; |
| 2064 | BcNum temp; |
| 2065 | bool neg; |
| 2066 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2067 | if (b->len == 0) |
| 2068 | return bc_error("divide by zero"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2069 | |
| 2070 | if (a->len == 0) { |
| 2071 | bc_num_setToZero(d, ts); |
| 2072 | return BC_STATUS_SUCCESS; |
| 2073 | } |
| 2074 | |
| 2075 | bc_num_init(&temp, d->cap); |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 2076 | s = bc_num_d(a, b, c, scale); |
| 2077 | if (s) goto err; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2078 | |
| 2079 | if (scale != 0) scale = ts; |
| 2080 | |
| 2081 | s = bc_num_m(c, b, &temp, scale); |
| 2082 | if (s) goto err; |
| 2083 | s = bc_num_sub(a, &temp, d, scale); |
| 2084 | if (s) goto err; |
| 2085 | |
| 2086 | if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx); |
| 2087 | |
| 2088 | neg = d->neg; |
| 2089 | bc_num_retireMul(d, ts, a->neg, b->neg); |
| 2090 | d->neg = neg; |
| 2091 | |
| 2092 | err: |
| 2093 | bc_num_free(&temp); |
| 2094 | return s; |
| 2095 | } |
| 2096 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2097 | static FAST_FUNC BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2098 | { |
| 2099 | BcStatus s; |
| 2100 | BcNum c1; |
| 2101 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 2102 | |
| 2103 | bc_num_init(&c1, len); |
| 2104 | s = bc_num_r(a, b, &c1, c, scale, ts); |
| 2105 | bc_num_free(&c1); |
| 2106 | |
| 2107 | return s; |
| 2108 | } |
| 2109 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2110 | static FAST_FUNC BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2111 | { |
| 2112 | BcStatus s = BC_STATUS_SUCCESS; |
| 2113 | BcNum copy; |
| 2114 | unsigned long pow; |
| 2115 | size_t i, powrdx, resrdx; |
| 2116 | bool neg, zero; |
| 2117 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2118 | if (b->rdx) return bc_error("non integer number"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2119 | |
| 2120 | if (b->len == 0) { |
| 2121 | bc_num_one(c); |
| 2122 | return BC_STATUS_SUCCESS; |
| 2123 | } |
| 2124 | else if (a->len == 0) { |
| 2125 | bc_num_setToZero(c, scale); |
| 2126 | return BC_STATUS_SUCCESS; |
| 2127 | } |
| 2128 | else if (BC_NUM_ONE(b)) { |
| 2129 | if (!b->neg) |
| 2130 | bc_num_copy(c, a); |
| 2131 | else |
| 2132 | s = bc_num_inv(a, c, scale); |
| 2133 | return s; |
| 2134 | } |
| 2135 | |
| 2136 | neg = b->neg; |
| 2137 | b->neg = false; |
| 2138 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 2139 | s = zbc_num_ulong(b, &pow); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2140 | if (s) return s; |
| 2141 | |
| 2142 | bc_num_init(©, a->len); |
| 2143 | bc_num_copy(©, a); |
| 2144 | |
Denys Vlasenko | 2d615fe | 2018-12-07 16:22:45 +0100 | [diff] [blame] | 2145 | if (!neg) { |
| 2146 | if (a->rdx > scale) |
| 2147 | scale = a->rdx; |
| 2148 | if (a->rdx * pow < scale) |
| 2149 | scale = a->rdx * pow; |
| 2150 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2151 | |
| 2152 | b->neg = neg; |
| 2153 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2154 | for (powrdx = a->rdx; !(pow & 1); pow >>= 1) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2155 | powrdx <<= 1; |
| 2156 | s = bc_num_mul(©, ©, ©, powrdx); |
| 2157 | if (s) goto err; |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 2158 | // Not needed: bc_num_mul() has a check for ^C: |
| 2159 | //if (G_interrupt) { |
| 2160 | // s = BC_STATUS_FAILURE; |
| 2161 | // goto err; |
| 2162 | //} |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2163 | } |
| 2164 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2165 | bc_num_copy(c, ©); |
| 2166 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2167 | for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2168 | |
| 2169 | powrdx <<= 1; |
| 2170 | s = bc_num_mul(©, ©, ©, powrdx); |
| 2171 | if (s) goto err; |
| 2172 | |
| 2173 | if (pow & 1) { |
| 2174 | resrdx += powrdx; |
| 2175 | s = bc_num_mul(c, ©, c, resrdx); |
| 2176 | if (s) goto err; |
| 2177 | } |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 2178 | // Not needed: bc_num_mul() has a check for ^C: |
| 2179 | //if (G_interrupt) { |
| 2180 | // s = BC_STATUS_FAILURE; |
| 2181 | // goto err; |
| 2182 | //} |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2183 | } |
| 2184 | |
| 2185 | if (neg) { |
| 2186 | s = bc_num_inv(c, c, scale); |
| 2187 | if (s) goto err; |
| 2188 | } |
| 2189 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2190 | if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale); |
| 2191 | |
| 2192 | // We can't use bc_num_clean() here. |
| 2193 | for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i]; |
| 2194 | if (zero) bc_num_setToZero(c, scale); |
| 2195 | |
| 2196 | err: |
| 2197 | bc_num_free(©); |
| 2198 | return s; |
| 2199 | } |
| 2200 | |
| 2201 | static BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale, |
| 2202 | BcNumBinaryOp op, size_t req) |
| 2203 | { |
| 2204 | BcStatus s; |
| 2205 | BcNum num2, *ptr_a, *ptr_b; |
| 2206 | bool init = false; |
| 2207 | |
| 2208 | if (c == a) { |
| 2209 | ptr_a = &num2; |
| 2210 | memcpy(ptr_a, c, sizeof(BcNum)); |
| 2211 | init = true; |
| 2212 | } |
| 2213 | else |
| 2214 | ptr_a = a; |
| 2215 | |
| 2216 | if (c == b) { |
| 2217 | ptr_b = &num2; |
| 2218 | if (c != a) { |
| 2219 | memcpy(ptr_b, c, sizeof(BcNum)); |
| 2220 | init = true; |
| 2221 | } |
| 2222 | } |
| 2223 | else |
| 2224 | ptr_b = b; |
| 2225 | |
| 2226 | if (init) |
| 2227 | bc_num_init(c, req); |
| 2228 | else |
| 2229 | bc_num_expand(c, req); |
| 2230 | |
| 2231 | s = op(ptr_a, ptr_b, c, scale); |
| 2232 | |
| 2233 | if (init) bc_num_free(&num2); |
| 2234 | |
| 2235 | return s; |
| 2236 | } |
| 2237 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2238 | static void bc_num_printNewline(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2239 | { |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2240 | if (G.prog.nchars == G.prog.len - 1) { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2241 | bb_putchar('\\'); |
| 2242 | bb_putchar('\n'); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2243 | G.prog.nchars = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2244 | } |
| 2245 | } |
| 2246 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2247 | #if ENABLE_DC |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2248 | static FAST_FUNC void bc_num_printChar(size_t num, size_t width, bool radix) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2249 | { |
Denys Vlasenko | 2a8ad48 | 2018-12-08 21:56:37 +0100 | [diff] [blame] | 2250 | (void) radix; |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2251 | bb_putchar((char) num); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2252 | G.prog.nchars += width; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2253 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2254 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2255 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2256 | static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2257 | { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2258 | size_t exp, pow; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2259 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2260 | bc_num_printNewline(); |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2261 | bb_putchar(radix ? '.' : ' '); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2262 | ++G.prog.nchars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2263 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2264 | bc_num_printNewline(); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2265 | for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10) |
| 2266 | continue; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2267 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2268 | for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2269 | size_t dig; |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2270 | bc_num_printNewline(); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2271 | dig = num / pow; |
| 2272 | num -= dig * pow; |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2273 | bb_putchar(((char) dig) + '0'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2274 | } |
| 2275 | } |
| 2276 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2277 | static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2278 | { |
| 2279 | if (radix) { |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2280 | bc_num_printNewline(); |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2281 | bb_putchar('.'); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2282 | G.prog.nchars += 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2283 | } |
| 2284 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2285 | bc_num_printNewline(); |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2286 | bb_putchar(bb_hexdigits_upcase[num]); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2287 | G.prog.nchars += width; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2288 | } |
| 2289 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2290 | static void bc_num_printDecimal(BcNum *n) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2291 | { |
| 2292 | size_t i, rdx = n->rdx - 1; |
| 2293 | |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2294 | if (n->neg) bb_putchar('-'); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2295 | G.prog.nchars += n->neg; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2296 | |
| 2297 | for (i = n->len - 1; i < n->len; --i) |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2298 | bc_num_printHex((size_t) n->num[i], 1, i == rdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2299 | } |
| 2300 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 2301 | static BC_STATUS zbc_num_printNum(BcNum *n, BcNum *base, size_t width, BcNumDigitOp print) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2302 | { |
| 2303 | BcStatus s; |
| 2304 | BcVec stack; |
| 2305 | BcNum intp, fracp, digit, frac_len; |
| 2306 | unsigned long dig, *ptr; |
| 2307 | size_t i; |
| 2308 | bool radix; |
| 2309 | |
| 2310 | if (n->len == 0) { |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2311 | print(0, width, false); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 2312 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2313 | } |
| 2314 | |
| 2315 | bc_vec_init(&stack, sizeof(long), NULL); |
| 2316 | bc_num_init(&intp, n->len); |
| 2317 | bc_num_init(&fracp, n->rdx); |
| 2318 | bc_num_init(&digit, width); |
| 2319 | bc_num_init(&frac_len, BC_NUM_INT(n)); |
| 2320 | bc_num_copy(&intp, n); |
| 2321 | bc_num_one(&frac_len); |
| 2322 | |
| 2323 | bc_num_truncate(&intp, intp.rdx); |
| 2324 | s = bc_num_sub(n, &intp, &fracp, 0); |
| 2325 | if (s) goto err; |
| 2326 | |
| 2327 | while (intp.len != 0) { |
| 2328 | s = bc_num_divmod(&intp, base, &intp, &digit, 0); |
| 2329 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 2330 | s = zbc_num_ulong(&digit, &dig); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2331 | if (s) goto err; |
| 2332 | bc_vec_push(&stack, &dig); |
| 2333 | } |
| 2334 | |
| 2335 | for (i = 0; i < stack.len; ++i) { |
| 2336 | ptr = bc_vec_item_rev(&stack, i); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2337 | print(*ptr, width, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2338 | } |
| 2339 | |
| 2340 | if (!n->rdx) goto err; |
| 2341 | |
| 2342 | for (radix = true; frac_len.len <= n->rdx; radix = false) { |
| 2343 | s = bc_num_mul(&fracp, base, &fracp, n->rdx); |
| 2344 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 2345 | s = zbc_num_ulong(&fracp, &dig); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2346 | if (s) goto err; |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 2347 | bc_num_ulong2num(&intp, dig); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2348 | s = bc_num_sub(&fracp, &intp, &fracp, 0); |
| 2349 | if (s) goto err; |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2350 | print(dig, width, radix); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2351 | s = bc_num_mul(&frac_len, base, &frac_len, 0); |
| 2352 | if (s) goto err; |
| 2353 | } |
| 2354 | |
| 2355 | err: |
| 2356 | bc_num_free(&frac_len); |
| 2357 | bc_num_free(&digit); |
| 2358 | bc_num_free(&fracp); |
| 2359 | bc_num_free(&intp); |
| 2360 | bc_vec_free(&stack); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 2361 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2362 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2363 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 2364 | # define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2365 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2366 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 2367 | static BC_STATUS zbc_num_printBase(BcNum *n) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2368 | { |
| 2369 | BcStatus s; |
| 2370 | size_t width, i; |
| 2371 | BcNumDigitOp print; |
| 2372 | bool neg = n->neg; |
| 2373 | |
Denys Vlasenko | 0f37b32 | 2018-12-08 23:48:53 +0100 | [diff] [blame] | 2374 | if (neg) { |
| 2375 | bb_putchar('-'); |
| 2376 | G.prog.nchars++; |
| 2377 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2378 | |
| 2379 | n->neg = false; |
| 2380 | |
Denys Vlasenko | 0f37b32 | 2018-12-08 23:48:53 +0100 | [diff] [blame] | 2381 | if (G.prog.ob_t <= BC_NUM_MAX_IBASE) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2382 | width = 1; |
| 2383 | print = bc_num_printHex; |
| 2384 | } |
| 2385 | else { |
Denys Vlasenko | 0f37b32 | 2018-12-08 23:48:53 +0100 | [diff] [blame] | 2386 | for (i = G.prog.ob_t - 1, width = 0; i != 0; i /= 10, ++width) |
| 2387 | continue; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2388 | print = bc_num_printDigits; |
| 2389 | } |
| 2390 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 2391 | s = zbc_num_printNum(n, &G.prog.ob, width, print); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2392 | n->neg = neg; |
| 2393 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 2394 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2395 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2396 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 2397 | # define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2398 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2399 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2400 | #if ENABLE_DC |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 2401 | static BC_STATUS zbc_num_stream(BcNum *n, BcNum *base) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2402 | { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 2403 | RETURN_STATUS(zbc_num_printNum(n, base, 1, bc_num_printChar)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2404 | } |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 2405 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 2406 | # define zbc_num_stream(...) (zbc_num_stream(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 2407 | #endif |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2408 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2409 | |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2410 | static bool bc_num_strValid(const char *val, size_t base) |
| 2411 | { |
| 2412 | BcDig b; |
| 2413 | bool radix; |
| 2414 | |
| 2415 | b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A'); |
| 2416 | radix = false; |
| 2417 | for (;;) { |
| 2418 | BcDig c = *val++; |
| 2419 | if (c == '\0') |
| 2420 | break; |
| 2421 | if (c == '.') { |
| 2422 | if (radix) return false; |
| 2423 | radix = true; |
| 2424 | continue; |
| 2425 | } |
| 2426 | if (c < '0' || c >= b || (c > '9' && c < 'A')) |
| 2427 | return false; |
| 2428 | } |
| 2429 | return true; |
| 2430 | } |
| 2431 | |
| 2432 | // Note: n is already "bc_num_zero()"ed, |
| 2433 | // leading zeroes in "val" are removed |
| 2434 | static void bc_num_parseDecimal(BcNum *n, const char *val) |
| 2435 | { |
| 2436 | size_t len, i; |
| 2437 | const char *ptr; |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2438 | |
| 2439 | len = strlen(val); |
| 2440 | if (len == 0) |
| 2441 | return; |
| 2442 | |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2443 | bc_num_expand(n, len); |
| 2444 | |
| 2445 | ptr = strchr(val, '.'); |
| 2446 | |
| 2447 | n->rdx = 0; |
| 2448 | if (ptr != NULL) |
| 2449 | n->rdx = (size_t)((val + len) - (ptr + 1)); |
| 2450 | |
Denys Vlasenko | dafbc2c | 2018-12-10 15:38:52 +0100 | [diff] [blame] | 2451 | for (i = 0; val[i]; ++i) { |
| 2452 | if (val[i] != '0' && val[i] != '.') { |
| 2453 | // Not entirely zero value - convert it, and exit |
| 2454 | i = len - 1; |
| 2455 | for (;;) { |
| 2456 | n->num[n->len] = val[i] - '0'; |
| 2457 | ++n->len; |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2458 | skip_dot: |
Denys Vlasenko | dafbc2c | 2018-12-10 15:38:52 +0100 | [diff] [blame] | 2459 | if ((ssize_t)--i == (ssize_t)-1) break; |
| 2460 | if (val[i] == '.') goto skip_dot; |
| 2461 | } |
| 2462 | break; |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2463 | } |
| 2464 | } |
Denys Vlasenko | dafbc2c | 2018-12-10 15:38:52 +0100 | [diff] [blame] | 2465 | // if this is reached, the value is entirely zero |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2466 | } |
| 2467 | |
| 2468 | // Note: n is already "bc_num_zero()"ed, |
| 2469 | // leading zeroes in "val" are removed |
| 2470 | static void bc_num_parseBase(BcNum *n, const char *val, BcNum *base) |
| 2471 | { |
| 2472 | BcStatus s; |
| 2473 | BcNum temp, mult, result; |
| 2474 | BcDig c = '\0'; |
| 2475 | unsigned long v; |
| 2476 | size_t i, digits; |
| 2477 | |
| 2478 | for (i = 0; ; ++i) { |
| 2479 | if (val[i] == '\0') |
| 2480 | return; |
| 2481 | if (val[i] != '.' && val[i] != '0') |
| 2482 | break; |
| 2483 | } |
| 2484 | |
| 2485 | bc_num_init_DEF_SIZE(&temp); |
| 2486 | bc_num_init_DEF_SIZE(&mult); |
| 2487 | |
| 2488 | for (;;) { |
| 2489 | c = *val++; |
| 2490 | if (c == '\0') goto int_err; |
| 2491 | if (c == '.') break; |
| 2492 | |
| 2493 | v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10); |
| 2494 | |
| 2495 | s = bc_num_mul(n, base, &mult, 0); |
| 2496 | if (s) goto int_err; |
| 2497 | bc_num_ulong2num(&temp, v); |
| 2498 | s = bc_num_add(&mult, &temp, n, 0); |
| 2499 | if (s) goto int_err; |
| 2500 | } |
| 2501 | |
| 2502 | bc_num_init(&result, base->len); |
| 2503 | //bc_num_zero(&result); - already is |
| 2504 | bc_num_one(&mult); |
| 2505 | |
| 2506 | digits = 0; |
| 2507 | for (;;) { |
| 2508 | c = *val++; |
| 2509 | if (c == '\0') break; |
| 2510 | digits++; |
| 2511 | |
| 2512 | v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10); |
| 2513 | |
| 2514 | s = bc_num_mul(&result, base, &result, 0); |
| 2515 | if (s) goto err; |
| 2516 | bc_num_ulong2num(&temp, v); |
| 2517 | s = bc_num_add(&result, &temp, &result, 0); |
| 2518 | if (s) goto err; |
| 2519 | s = bc_num_mul(&mult, base, &mult, 0); |
| 2520 | if (s) goto err; |
| 2521 | } |
| 2522 | |
| 2523 | s = bc_num_div(&result, &mult, &result, digits); |
| 2524 | if (s) goto err; |
| 2525 | s = bc_num_add(n, &result, n, digits); |
| 2526 | if (s) goto err; |
| 2527 | |
| 2528 | if (n->len != 0) { |
| 2529 | if (n->rdx < digits) bc_num_extend(n, digits - n->rdx); |
| 2530 | } else |
| 2531 | bc_num_zero(n); |
| 2532 | |
| 2533 | err: |
| 2534 | bc_num_free(&result); |
| 2535 | int_err: |
| 2536 | bc_num_free(&mult); |
| 2537 | bc_num_free(&temp); |
| 2538 | } |
| 2539 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 2540 | 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] | 2541 | size_t base_t) |
| 2542 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2543 | if (!bc_num_strValid(val, base_t)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 2544 | RETURN_STATUS(bc_error("bad number string")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2545 | |
Denys Vlasenko | 4a024c7 | 2018-12-09 13:21:54 +0100 | [diff] [blame] | 2546 | bc_num_zero(n); |
| 2547 | while (*val == '0') val++; |
| 2548 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2549 | if (base_t == 10) |
| 2550 | bc_num_parseDecimal(n, val); |
| 2551 | else |
| 2552 | bc_num_parseBase(n, val, base); |
| 2553 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 2554 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2555 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2556 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 2557 | # define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2558 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2559 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 2560 | static BC_STATUS zbc_num_print(BcNum *n, bool newline) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2561 | { |
| 2562 | BcStatus s = BC_STATUS_SUCCESS; |
| 2563 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2564 | bc_num_printNewline(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2565 | |
| 2566 | if (n->len == 0) { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2567 | bb_putchar('0'); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2568 | ++G.prog.nchars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2569 | } |
Denys Vlasenko | 0f37b32 | 2018-12-08 23:48:53 +0100 | [diff] [blame] | 2570 | else if (G.prog.ob_t == 10) |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2571 | bc_num_printDecimal(n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2572 | else |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 2573 | s = zbc_num_printBase(n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2574 | |
| 2575 | if (newline) { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2576 | bb_putchar('\n'); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2577 | G.prog.nchars = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2578 | } |
| 2579 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 2580 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2581 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2582 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 2583 | # define zbc_num_print(...) (zbc_num_print(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2584 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2585 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2586 | static FAST_FUNC BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2587 | { |
| 2588 | BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s; |
| 2589 | (void) scale; |
| 2590 | return bc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)); |
| 2591 | } |
| 2592 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2593 | static FAST_FUNC BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2594 | { |
| 2595 | BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a; |
| 2596 | (void) scale; |
| 2597 | return bc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)); |
| 2598 | } |
| 2599 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2600 | static FAST_FUNC BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2601 | { |
| 2602 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 2603 | return bc_num_binary(a, b, c, scale, bc_num_m, req); |
| 2604 | } |
| 2605 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2606 | static FAST_FUNC BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2607 | { |
| 2608 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 2609 | return bc_num_binary(a, b, c, scale, bc_num_d, req); |
| 2610 | } |
| 2611 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2612 | static FAST_FUNC BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2613 | { |
| 2614 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 2615 | return bc_num_binary(a, b, c, scale, bc_num_rem, req); |
| 2616 | } |
| 2617 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2618 | static FAST_FUNC BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2619 | { |
| 2620 | return bc_num_binary(a, b, c, scale, bc_num_p, a->len * b->len + 1); |
| 2621 | } |
| 2622 | |
| 2623 | static BcStatus bc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale) |
| 2624 | { |
| 2625 | BcStatus s; |
| 2626 | BcNum num1, num2, half, f, fprime, *x0, *x1, *temp; |
| 2627 | size_t pow, len, digs, digs1, resrdx, req, times = 0; |
| 2628 | ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX; |
| 2629 | |
| 2630 | req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1; |
| 2631 | bc_num_expand(b, req); |
| 2632 | |
| 2633 | if (a->len == 0) { |
| 2634 | bc_num_setToZero(b, scale); |
| 2635 | return BC_STATUS_SUCCESS; |
| 2636 | } |
| 2637 | else if (a->neg) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2638 | return bc_error("negative number"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2639 | else if (BC_NUM_ONE(a)) { |
| 2640 | bc_num_one(b); |
| 2641 | bc_num_extend(b, scale); |
| 2642 | return BC_STATUS_SUCCESS; |
| 2643 | } |
| 2644 | |
| 2645 | scale = BC_MAX(scale, a->rdx) + 1; |
| 2646 | len = a->len + scale; |
| 2647 | |
| 2648 | bc_num_init(&num1, len); |
| 2649 | bc_num_init(&num2, len); |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 2650 | bc_num_init_DEF_SIZE(&half); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2651 | |
| 2652 | bc_num_one(&half); |
| 2653 | half.num[0] = 5; |
| 2654 | half.rdx = 1; |
| 2655 | |
| 2656 | bc_num_init(&f, len); |
| 2657 | bc_num_init(&fprime, len); |
| 2658 | |
| 2659 | x0 = &num1; |
| 2660 | x1 = &num2; |
| 2661 | |
| 2662 | bc_num_one(x0); |
| 2663 | pow = BC_NUM_INT(a); |
| 2664 | |
| 2665 | if (pow) { |
| 2666 | |
| 2667 | if (pow & 1) |
| 2668 | x0->num[0] = 2; |
| 2669 | else |
| 2670 | x0->num[0] = 6; |
| 2671 | |
| 2672 | pow -= 2 - (pow & 1); |
| 2673 | |
| 2674 | bc_num_extend(x0, pow); |
| 2675 | |
| 2676 | // Make sure to move the radix back. |
| 2677 | x0->rdx -= pow; |
| 2678 | } |
| 2679 | |
| 2680 | x0->rdx = digs = digs1 = 0; |
| 2681 | resrdx = scale + 2; |
| 2682 | len = BC_NUM_INT(x0) + resrdx - 1; |
| 2683 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2684 | while (cmp != 0 || digs < len) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2685 | |
| 2686 | s = bc_num_div(a, x0, &f, resrdx); |
| 2687 | if (s) goto err; |
| 2688 | s = bc_num_add(x0, &f, &fprime, resrdx); |
| 2689 | if (s) goto err; |
| 2690 | s = bc_num_mul(&fprime, &half, x1, resrdx); |
| 2691 | if (s) goto err; |
| 2692 | |
| 2693 | cmp = bc_num_cmp(x1, x0); |
| 2694 | digs = x1->len - (unsigned long long) llabs(cmp); |
| 2695 | |
| 2696 | if (cmp == cmp2 && digs == digs1) |
| 2697 | times += 1; |
| 2698 | else |
| 2699 | times = 0; |
| 2700 | |
| 2701 | resrdx += times > 4; |
| 2702 | |
| 2703 | cmp2 = cmp1; |
| 2704 | cmp1 = cmp; |
| 2705 | digs1 = digs; |
| 2706 | |
| 2707 | temp = x0; |
| 2708 | x0 = x1; |
| 2709 | x1 = temp; |
| 2710 | } |
| 2711 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2712 | bc_num_copy(b, x0); |
| 2713 | scale -= 1; |
| 2714 | if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale); |
| 2715 | |
| 2716 | err: |
| 2717 | bc_num_free(&fprime); |
| 2718 | bc_num_free(&f); |
| 2719 | bc_num_free(&half); |
| 2720 | bc_num_free(&num2); |
| 2721 | bc_num_free(&num1); |
| 2722 | return s; |
| 2723 | } |
| 2724 | |
| 2725 | static BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, |
| 2726 | size_t scale) |
| 2727 | { |
| 2728 | BcStatus s; |
| 2729 | BcNum num2, *ptr_a; |
| 2730 | bool init = false; |
| 2731 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 2732 | |
| 2733 | if (c == a) { |
| 2734 | memcpy(&num2, c, sizeof(BcNum)); |
| 2735 | ptr_a = &num2; |
| 2736 | bc_num_init(c, len); |
| 2737 | init = true; |
| 2738 | } |
| 2739 | else { |
| 2740 | ptr_a = a; |
| 2741 | bc_num_expand(c, len); |
| 2742 | } |
| 2743 | |
| 2744 | s = bc_num_r(ptr_a, b, c, d, scale, ts); |
| 2745 | |
| 2746 | if (init) bc_num_free(&num2); |
| 2747 | |
| 2748 | return s; |
| 2749 | } |
| 2750 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2751 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2752 | static BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) |
| 2753 | { |
| 2754 | BcStatus s; |
| 2755 | BcNum base, exp, two, temp; |
| 2756 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2757 | if (c->len == 0) |
| 2758 | return bc_error("divide by zero"); |
| 2759 | if (a->rdx || b->rdx || c->rdx) |
| 2760 | return bc_error("non integer number"); |
| 2761 | if (b->neg) |
| 2762 | return bc_error("negative number"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2763 | |
| 2764 | bc_num_expand(d, c->len); |
| 2765 | bc_num_init(&base, c->len); |
| 2766 | bc_num_init(&exp, b->len); |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 2767 | bc_num_init_DEF_SIZE(&two); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2768 | bc_num_init(&temp, b->len); |
| 2769 | |
| 2770 | bc_num_one(&two); |
| 2771 | two.num[0] = 2; |
| 2772 | bc_num_one(d); |
| 2773 | |
| 2774 | s = bc_num_rem(a, c, &base, 0); |
| 2775 | if (s) goto err; |
| 2776 | bc_num_copy(&exp, b); |
| 2777 | |
| 2778 | while (exp.len != 0) { |
| 2779 | |
| 2780 | s = bc_num_divmod(&exp, &two, &exp, &temp, 0); |
| 2781 | if (s) goto err; |
| 2782 | |
| 2783 | if (BC_NUM_ONE(&temp)) { |
| 2784 | s = bc_num_mul(d, &base, &temp, 0); |
| 2785 | if (s) goto err; |
| 2786 | s = bc_num_rem(&temp, c, d, 0); |
| 2787 | if (s) goto err; |
| 2788 | } |
| 2789 | |
| 2790 | s = bc_num_mul(&base, &base, &temp, 0); |
| 2791 | if (s) goto err; |
| 2792 | s = bc_num_rem(&temp, c, &base, 0); |
| 2793 | if (s) goto err; |
| 2794 | } |
| 2795 | |
| 2796 | err: |
| 2797 | bc_num_free(&temp); |
| 2798 | bc_num_free(&two); |
| 2799 | bc_num_free(&exp); |
| 2800 | bc_num_free(&base); |
| 2801 | return s; |
| 2802 | } |
| 2803 | #endif // ENABLE_DC |
| 2804 | |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 2805 | #if ENABLE_BC |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 2806 | static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2807 | { |
| 2808 | BcId a; |
| 2809 | size_t i; |
| 2810 | |
| 2811 | for (i = 0; i < f->autos.len; ++i) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2812 | if (strcmp(name, ((BcId *) bc_vec_item(&f->autos, i))->name) == 0) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 2813 | 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] | 2814 | } |
| 2815 | |
| 2816 | a.idx = var; |
| 2817 | a.name = name; |
| 2818 | |
| 2819 | bc_vec_push(&f->autos, &a); |
| 2820 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 2821 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2822 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2823 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 2824 | # define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2825 | #endif |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 2826 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2827 | |
| 2828 | static void bc_func_init(BcFunc *f) |
| 2829 | { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 2830 | bc_char_vec_init(&f->code); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2831 | bc_vec_init(&f->autos, sizeof(BcId), bc_id_free); |
| 2832 | bc_vec_init(&f->labels, sizeof(size_t), NULL); |
| 2833 | f->nparams = 0; |
| 2834 | } |
| 2835 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2836 | static FAST_FUNC void bc_func_free(void *func) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2837 | { |
| 2838 | BcFunc *f = (BcFunc *) func; |
| 2839 | bc_vec_free(&f->code); |
| 2840 | bc_vec_free(&f->autos); |
| 2841 | bc_vec_free(&f->labels); |
| 2842 | } |
| 2843 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2844 | static void bc_array_expand(BcVec *a, size_t len); |
| 2845 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2846 | static void bc_array_init(BcVec *a, bool nums) |
| 2847 | { |
| 2848 | if (nums) |
| 2849 | bc_vec_init(a, sizeof(BcNum), bc_num_free); |
| 2850 | else |
| 2851 | bc_vec_init(a, sizeof(BcVec), bc_vec_free); |
| 2852 | bc_array_expand(a, 1); |
| 2853 | } |
| 2854 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2855 | static void bc_array_expand(BcVec *a, size_t len) |
| 2856 | { |
| 2857 | BcResultData data; |
| 2858 | |
| 2859 | if (a->size == sizeof(BcNum) && a->dtor == bc_num_free) { |
| 2860 | while (len > a->len) { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 2861 | bc_num_init_DEF_SIZE(&data.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2862 | bc_vec_push(a, &data.n); |
| 2863 | } |
| 2864 | } |
| 2865 | else { |
| 2866 | while (len > a->len) { |
| 2867 | bc_array_init(&data.v, true); |
| 2868 | bc_vec_push(a, &data.v); |
| 2869 | } |
| 2870 | } |
| 2871 | } |
| 2872 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2873 | static void bc_array_copy(BcVec *d, const BcVec *s) |
| 2874 | { |
| 2875 | size_t i; |
| 2876 | |
| 2877 | bc_vec_pop_all(d); |
| 2878 | bc_vec_expand(d, s->cap); |
| 2879 | d->len = s->len; |
| 2880 | |
| 2881 | for (i = 0; i < s->len; ++i) { |
| 2882 | BcNum *dnum = bc_vec_item(d, i), *snum = bc_vec_item(s, i); |
| 2883 | bc_num_init(dnum, snum->len); |
| 2884 | bc_num_copy(dnum, snum); |
| 2885 | } |
| 2886 | } |
| 2887 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2888 | static FAST_FUNC void bc_string_free(void *string) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2889 | { |
| 2890 | free(*((char **) string)); |
| 2891 | } |
| 2892 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2893 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2894 | static void bc_result_copy(BcResult *d, BcResult *src) |
| 2895 | { |
| 2896 | d->t = src->t; |
| 2897 | |
| 2898 | switch (d->t) { |
| 2899 | |
| 2900 | case BC_RESULT_TEMP: |
| 2901 | case BC_RESULT_IBASE: |
| 2902 | case BC_RESULT_SCALE: |
| 2903 | case BC_RESULT_OBASE: |
| 2904 | { |
| 2905 | bc_num_init(&d->d.n, src->d.n.len); |
| 2906 | bc_num_copy(&d->d.n, &src->d.n); |
| 2907 | break; |
| 2908 | } |
| 2909 | |
| 2910 | case BC_RESULT_VAR: |
| 2911 | case BC_RESULT_ARRAY: |
| 2912 | case BC_RESULT_ARRAY_ELEM: |
| 2913 | { |
| 2914 | d->d.id.name = xstrdup(src->d.id.name); |
| 2915 | break; |
| 2916 | } |
| 2917 | |
| 2918 | case BC_RESULT_CONSTANT: |
| 2919 | case BC_RESULT_LAST: |
| 2920 | case BC_RESULT_ONE: |
| 2921 | case BC_RESULT_STR: |
| 2922 | { |
| 2923 | memcpy(&d->d.n, &src->d.n, sizeof(BcNum)); |
| 2924 | break; |
| 2925 | } |
| 2926 | } |
| 2927 | } |
| 2928 | #endif // ENABLE_DC |
| 2929 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2930 | static FAST_FUNC void bc_result_free(void *result) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2931 | { |
| 2932 | BcResult *r = (BcResult *) result; |
| 2933 | |
| 2934 | switch (r->t) { |
| 2935 | |
| 2936 | case BC_RESULT_TEMP: |
| 2937 | case BC_RESULT_IBASE: |
| 2938 | case BC_RESULT_SCALE: |
| 2939 | case BC_RESULT_OBASE: |
| 2940 | { |
| 2941 | bc_num_free(&r->d.n); |
| 2942 | break; |
| 2943 | } |
| 2944 | |
| 2945 | case BC_RESULT_VAR: |
| 2946 | case BC_RESULT_ARRAY: |
| 2947 | case BC_RESULT_ARRAY_ELEM: |
| 2948 | { |
| 2949 | free(r->d.id.name); |
| 2950 | break; |
| 2951 | } |
| 2952 | |
| 2953 | default: |
| 2954 | { |
| 2955 | // Do nothing. |
| 2956 | break; |
| 2957 | } |
| 2958 | } |
| 2959 | } |
| 2960 | |
| 2961 | static void bc_lex_lineComment(BcLex *l) |
| 2962 | { |
| 2963 | l->t.t = BC_LEX_WHITESPACE; |
| 2964 | while (l->i < l->len && l->buf[l->i++] != '\n'); |
| 2965 | --l->i; |
| 2966 | } |
| 2967 | |
| 2968 | static void bc_lex_whitespace(BcLex *l) |
| 2969 | { |
| 2970 | char c; |
| 2971 | l->t.t = BC_LEX_WHITESPACE; |
| 2972 | for (c = l->buf[l->i]; c != '\n' && isspace(c); c = l->buf[++l->i]); |
| 2973 | } |
| 2974 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 2975 | static BC_STATUS zbc_lex_number(BcLex *l, char start) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2976 | { |
| 2977 | const char *buf = l->buf + l->i; |
| 2978 | size_t len, hits = 0, bslashes = 0, i = 0, j; |
| 2979 | char c = buf[i]; |
| 2980 | bool last_pt, pt = start == '.'; |
| 2981 | |
| 2982 | last_pt = pt; |
| 2983 | l->t.t = BC_LEX_NUMBER; |
| 2984 | |
| 2985 | while (c != 0 && (isdigit(c) || (c >= 'A' && c <= 'F') || |
| 2986 | (c == '.' && !pt) || (c == '\\' && buf[i + 1] == '\n'))) |
| 2987 | { |
| 2988 | if (c != '\\') { |
| 2989 | last_pt = c == '.'; |
| 2990 | pt = pt || last_pt; |
| 2991 | } |
| 2992 | else { |
| 2993 | ++i; |
| 2994 | bslashes += 1; |
| 2995 | } |
| 2996 | |
| 2997 | c = buf[++i]; |
| 2998 | } |
| 2999 | |
Denys Vlasenko | d5f7703 | 2018-12-04 21:37:56 +0100 | [diff] [blame] | 3000 | len = i + !last_pt - bslashes * 2; |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 3001 | // This check makes sense only if size_t is (much) larger than BC_MAX_NUM. |
| 3002 | if (SIZE_MAX > (BC_MAX_NUM | 0xff)) { |
| 3003 | if (len > BC_MAX_NUM) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 3004 | 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] | 3005 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3006 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3007 | bc_vec_pop_all(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3008 | bc_vec_expand(&l->t.v, len + 1); |
| 3009 | bc_vec_push(&l->t.v, &start); |
| 3010 | |
| 3011 | for (buf -= 1, j = 1; j < len + hits * 2; ++j) { |
| 3012 | |
| 3013 | c = buf[j]; |
| 3014 | |
| 3015 | // If we have hit a backslash, skip it. We don't have |
| 3016 | // to check for a newline because it's guaranteed. |
| 3017 | if (hits < bslashes && c == '\\') { |
| 3018 | ++hits; |
| 3019 | ++j; |
| 3020 | continue; |
| 3021 | } |
| 3022 | |
| 3023 | bc_vec_push(&l->t.v, &c); |
| 3024 | } |
| 3025 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 3026 | bc_vec_pushZeroByte(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3027 | l->i += i; |
| 3028 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 3029 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3030 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 3031 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 3032 | # define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 3033 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3034 | |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 3035 | static void bc_lex_name(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3036 | { |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 3037 | size_t i; |
| 3038 | const char *buf; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3039 | |
| 3040 | l->t.t = BC_LEX_NAME; |
| 3041 | |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 3042 | i = 0; |
| 3043 | buf = l->buf + l->i - 1; |
| 3044 | for (;;) { |
| 3045 | char c = buf[i]; |
| 3046 | if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break; |
| 3047 | i++; |
| 3048 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3049 | |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 3050 | #if 0 // We do not protect against people with gigabyte-long names |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 3051 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
| 3052 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
| 3053 | if (i > BC_MAX_STRING) |
| 3054 | return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]"); |
| 3055 | } |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 3056 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3057 | bc_vec_string(&l->t.v, i, buf); |
| 3058 | |
| 3059 | // Increment the index. We minus 1 because it has already been incremented. |
| 3060 | l->i += i - 1; |
| 3061 | |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 3062 | //return BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3063 | } |
| 3064 | |
| 3065 | static void bc_lex_init(BcLex *l, BcLexNext next) |
| 3066 | { |
| 3067 | l->next = next; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3068 | bc_char_vec_init(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3069 | } |
| 3070 | |
| 3071 | static void bc_lex_free(BcLex *l) |
| 3072 | { |
| 3073 | bc_vec_free(&l->t.v); |
| 3074 | } |
| 3075 | |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 3076 | static void bc_lex_file(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3077 | { |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3078 | G.err_line = l->line = 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3079 | l->newline = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3080 | } |
| 3081 | |
| 3082 | static BcStatus bc_lex_next(BcLex *l) |
| 3083 | { |
| 3084 | BcStatus s; |
| 3085 | |
| 3086 | l->t.last = l->t.t; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3087 | if (l->t.last == BC_LEX_EOF) return bc_error("end of file"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3088 | |
| 3089 | l->line += l->newline; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3090 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3091 | l->t.t = BC_LEX_EOF; |
| 3092 | |
| 3093 | l->newline = (l->i == l->len); |
| 3094 | if (l->newline) return BC_STATUS_SUCCESS; |
| 3095 | |
| 3096 | // Loop until failure or we don't have whitespace. This |
| 3097 | // is so the parser doesn't get inundated with whitespace. |
| 3098 | do { |
| 3099 | s = l->next(l); |
| 3100 | } while (!s && l->t.t == BC_LEX_WHITESPACE); |
| 3101 | |
| 3102 | return s; |
| 3103 | } |
| 3104 | |
| 3105 | static BcStatus bc_lex_text(BcLex *l, const char *text) |
| 3106 | { |
| 3107 | l->buf = text; |
| 3108 | l->i = 0; |
| 3109 | l->len = strlen(text); |
| 3110 | l->t.t = l->t.last = BC_LEX_INVALID; |
| 3111 | return bc_lex_next(l); |
| 3112 | } |
| 3113 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 3114 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3115 | static BcStatus bc_lex_identifier(BcLex *l) |
| 3116 | { |
| 3117 | BcStatus s; |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3118 | unsigned i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3119 | const char *buf = l->buf + l->i - 1; |
| 3120 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3121 | for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) { |
| 3122 | const char *keyword8 = bc_lex_kws[i].name8; |
| 3123 | unsigned j = 0; |
| 3124 | while (buf[j] != '\0' && buf[j] == keyword8[j]) { |
| 3125 | j++; |
| 3126 | if (j == 8) goto match; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3127 | } |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3128 | if (keyword8[j] != '\0') |
| 3129 | continue; |
| 3130 | match: |
| 3131 | // buf starts with keyword bc_lex_kws[i] |
| 3132 | l->t.t = BC_LEX_KEY_1st_keyword + i; |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 3133 | if (!bc_lex_kws_POSIX(i)) { |
Denys Vlasenko | 0d7e46b | 2018-12-05 18:31:19 +0100 | [diff] [blame] | 3134 | s = bc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8); |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3135 | if (s) return s; |
| 3136 | } |
| 3137 | |
| 3138 | // We minus 1 because the index has already been incremented. |
| 3139 | l->i += j - 1; |
| 3140 | return BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3141 | } |
| 3142 | |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 3143 | bc_lex_name(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3144 | |
Denys Vlasenko | 0d7e46b | 2018-12-05 18:31:19 +0100 | [diff] [blame] | 3145 | if (l->t.v.len > 2) { |
| 3146 | // Prevent this: |
| 3147 | // >>> qwe=1 |
| 3148 | // bc: POSIX only allows one character names; the following is bad: 'qwe=1 |
| 3149 | // ' |
| 3150 | unsigned len = strchrnul(buf, '\n') - buf; |
| 3151 | s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf); |
| 3152 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3153 | |
| 3154 | return s; |
| 3155 | } |
| 3156 | |
| 3157 | static BcStatus bc_lex_string(BcLex *l) |
| 3158 | { |
| 3159 | size_t len, nls = 0, i = l->i; |
| 3160 | char c; |
| 3161 | |
| 3162 | l->t.t = BC_LEX_STR; |
| 3163 | |
| 3164 | for (c = l->buf[i]; c != 0 && c != '"'; c = l->buf[++i]) nls += (c == '\n'); |
| 3165 | |
| 3166 | if (c == '\0') { |
| 3167 | l->i = i; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3168 | return bc_error("string end could not be found"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3169 | } |
| 3170 | |
| 3171 | len = i - l->i; |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 3172 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
| 3173 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
| 3174 | if (len > BC_MAX_STRING) |
| 3175 | return bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"); |
| 3176 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3177 | bc_vec_string(&l->t.v, len, l->buf + l->i); |
| 3178 | |
| 3179 | l->i = i + 1; |
| 3180 | l->line += nls; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3181 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3182 | |
| 3183 | return BC_STATUS_SUCCESS; |
| 3184 | } |
| 3185 | |
| 3186 | static void bc_lex_assign(BcLex *l, BcLexType with, BcLexType without) |
| 3187 | { |
| 3188 | if (l->buf[l->i] == '=') { |
| 3189 | ++l->i; |
| 3190 | l->t.t = with; |
| 3191 | } |
| 3192 | else |
| 3193 | l->t.t = without; |
| 3194 | } |
| 3195 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 3196 | static BC_STATUS zbc_lex_comment(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3197 | { |
| 3198 | size_t i, nls = 0; |
| 3199 | const char *buf = l->buf; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3200 | |
| 3201 | l->t.t = BC_LEX_WHITESPACE; |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3202 | i = ++l->i; |
| 3203 | for (;;) { |
| 3204 | char c = buf[i]; |
| 3205 | check_star: |
| 3206 | if (c == '*') { |
| 3207 | c = buf[++i]; |
| 3208 | if (c == '/') |
| 3209 | break; |
| 3210 | goto check_star; |
| 3211 | } |
| 3212 | if (c == '\0') { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3213 | l->i = i; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 3214 | RETURN_STATUS(bc_error("comment end could not be found")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3215 | } |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3216 | nls += (c == '\n'); |
| 3217 | i++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3218 | } |
| 3219 | |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3220 | l->i = i + 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3221 | l->line += nls; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3222 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3223 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 3224 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3225 | } |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 3226 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 3227 | # define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 3228 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3229 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 3230 | static FAST_FUNC BcStatus bc_lex_token(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3231 | { |
| 3232 | BcStatus s = BC_STATUS_SUCCESS; |
| 3233 | char c = l->buf[l->i++], c2; |
| 3234 | |
| 3235 | // This is the workhorse of the lexer. |
| 3236 | switch (c) { |
| 3237 | |
| 3238 | case '\0': |
| 3239 | case '\n': |
| 3240 | { |
| 3241 | l->newline = true; |
| 3242 | l->t.t = !c ? BC_LEX_EOF : BC_LEX_NLINE; |
| 3243 | break; |
| 3244 | } |
| 3245 | |
| 3246 | case '\t': |
| 3247 | case '\v': |
| 3248 | case '\f': |
| 3249 | case '\r': |
| 3250 | case ' ': |
| 3251 | { |
| 3252 | bc_lex_whitespace(l); |
| 3253 | break; |
| 3254 | } |
| 3255 | |
| 3256 | case '!': |
| 3257 | { |
| 3258 | bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT); |
| 3259 | |
| 3260 | if (l->t.t == BC_LEX_OP_BOOL_NOT) { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3261 | s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3262 | if (s) return s; |
| 3263 | } |
| 3264 | |
| 3265 | break; |
| 3266 | } |
| 3267 | |
| 3268 | case '"': |
| 3269 | { |
| 3270 | s = bc_lex_string(l); |
| 3271 | break; |
| 3272 | } |
| 3273 | |
| 3274 | case '#': |
| 3275 | { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3276 | s = bc_POSIX_does_not_allow("'#' script comments"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3277 | if (s) return s; |
| 3278 | |
| 3279 | bc_lex_lineComment(l); |
| 3280 | |
| 3281 | break; |
| 3282 | } |
| 3283 | |
| 3284 | case '%': |
| 3285 | { |
| 3286 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS); |
| 3287 | break; |
| 3288 | } |
| 3289 | |
| 3290 | case '&': |
| 3291 | { |
| 3292 | c2 = l->buf[l->i]; |
| 3293 | if (c2 == '&') { |
| 3294 | |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3295 | s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3296 | if (s) return s; |
| 3297 | |
| 3298 | ++l->i; |
| 3299 | l->t.t = BC_LEX_OP_BOOL_AND; |
| 3300 | } |
| 3301 | else { |
| 3302 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3303 | s = bc_error_bad_character('&'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3304 | } |
| 3305 | |
| 3306 | break; |
| 3307 | } |
| 3308 | |
| 3309 | case '(': |
| 3310 | case ')': |
| 3311 | { |
| 3312 | l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN); |
| 3313 | break; |
| 3314 | } |
| 3315 | |
| 3316 | case '*': |
| 3317 | { |
| 3318 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY); |
| 3319 | break; |
| 3320 | } |
| 3321 | |
| 3322 | case '+': |
| 3323 | { |
| 3324 | c2 = l->buf[l->i]; |
| 3325 | if (c2 == '+') { |
| 3326 | ++l->i; |
| 3327 | l->t.t = BC_LEX_OP_INC; |
| 3328 | } |
| 3329 | else |
| 3330 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS); |
| 3331 | break; |
| 3332 | } |
| 3333 | |
| 3334 | case ',': |
| 3335 | { |
| 3336 | l->t.t = BC_LEX_COMMA; |
| 3337 | break; |
| 3338 | } |
| 3339 | |
| 3340 | case '-': |
| 3341 | { |
| 3342 | c2 = l->buf[l->i]; |
| 3343 | if (c2 == '-') { |
| 3344 | ++l->i; |
| 3345 | l->t.t = BC_LEX_OP_DEC; |
| 3346 | } |
| 3347 | else |
| 3348 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS); |
| 3349 | break; |
| 3350 | } |
| 3351 | |
| 3352 | case '.': |
| 3353 | { |
| 3354 | if (isdigit(l->buf[l->i])) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 3355 | s = zbc_lex_number(l, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3356 | else { |
| 3357 | l->t.t = BC_LEX_KEY_LAST; |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3358 | 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] | 3359 | } |
| 3360 | break; |
| 3361 | } |
| 3362 | |
| 3363 | case '/': |
| 3364 | { |
| 3365 | c2 = l->buf[l->i]; |
| 3366 | if (c2 == '*') |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 3367 | s = zbc_lex_comment(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3368 | else |
| 3369 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE); |
| 3370 | break; |
| 3371 | } |
| 3372 | |
| 3373 | case '0': |
| 3374 | case '1': |
| 3375 | case '2': |
| 3376 | case '3': |
| 3377 | case '4': |
| 3378 | case '5': |
| 3379 | case '6': |
| 3380 | case '7': |
| 3381 | case '8': |
| 3382 | case '9': |
| 3383 | case 'A': |
| 3384 | case 'B': |
| 3385 | case 'C': |
| 3386 | case 'D': |
| 3387 | case 'E': |
| 3388 | case 'F': |
| 3389 | { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 3390 | s = zbc_lex_number(l, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3391 | break; |
| 3392 | } |
| 3393 | |
| 3394 | case ';': |
| 3395 | { |
| 3396 | l->t.t = BC_LEX_SCOLON; |
| 3397 | break; |
| 3398 | } |
| 3399 | |
| 3400 | case '<': |
| 3401 | { |
| 3402 | bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT); |
| 3403 | break; |
| 3404 | } |
| 3405 | |
| 3406 | case '=': |
| 3407 | { |
| 3408 | bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN); |
| 3409 | break; |
| 3410 | } |
| 3411 | |
| 3412 | case '>': |
| 3413 | { |
| 3414 | bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT); |
| 3415 | break; |
| 3416 | } |
| 3417 | |
| 3418 | case '[': |
| 3419 | case ']': |
| 3420 | { |
| 3421 | l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET); |
| 3422 | break; |
| 3423 | } |
| 3424 | |
| 3425 | case '\\': |
| 3426 | { |
| 3427 | if (l->buf[l->i] == '\n') { |
| 3428 | l->t.t = BC_LEX_WHITESPACE; |
| 3429 | ++l->i; |
| 3430 | } |
| 3431 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3432 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3433 | break; |
| 3434 | } |
| 3435 | |
| 3436 | case '^': |
| 3437 | { |
| 3438 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER); |
| 3439 | break; |
| 3440 | } |
| 3441 | |
| 3442 | case 'a': |
| 3443 | case 'b': |
| 3444 | case 'c': |
| 3445 | case 'd': |
| 3446 | case 'e': |
| 3447 | case 'f': |
| 3448 | case 'g': |
| 3449 | case 'h': |
| 3450 | case 'i': |
| 3451 | case 'j': |
| 3452 | case 'k': |
| 3453 | case 'l': |
| 3454 | case 'm': |
| 3455 | case 'n': |
| 3456 | case 'o': |
| 3457 | case 'p': |
| 3458 | case 'q': |
| 3459 | case 'r': |
| 3460 | case 's': |
| 3461 | case 't': |
| 3462 | case 'u': |
| 3463 | case 'v': |
| 3464 | case 'w': |
| 3465 | case 'x': |
| 3466 | case 'y': |
| 3467 | case 'z': |
| 3468 | { |
| 3469 | s = bc_lex_identifier(l); |
| 3470 | break; |
| 3471 | } |
| 3472 | |
| 3473 | case '{': |
| 3474 | case '}': |
| 3475 | { |
| 3476 | l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE); |
| 3477 | break; |
| 3478 | } |
| 3479 | |
| 3480 | case '|': |
| 3481 | { |
| 3482 | c2 = l->buf[l->i]; |
| 3483 | |
| 3484 | if (c2 == '|') { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3485 | s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3486 | if (s) return s; |
| 3487 | |
| 3488 | ++l->i; |
| 3489 | l->t.t = BC_LEX_OP_BOOL_OR; |
| 3490 | } |
| 3491 | else { |
| 3492 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3493 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3494 | } |
| 3495 | |
| 3496 | break; |
| 3497 | } |
| 3498 | |
| 3499 | default: |
| 3500 | { |
| 3501 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3502 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3503 | break; |
| 3504 | } |
| 3505 | } |
| 3506 | |
| 3507 | return s; |
| 3508 | } |
| 3509 | #endif // ENABLE_BC |
| 3510 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 3511 | #if ENABLE_DC |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 3512 | static BC_STATUS zdc_lex_register(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3513 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3514 | if (isspace(l->buf[l->i - 1])) { |
| 3515 | bc_lex_whitespace(l); |
| 3516 | ++l->i; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 3517 | if (!G_exreg) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 3518 | RETURN_STATUS(bc_error("extended register")); |
| 3519 | bc_lex_name(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3520 | } |
| 3521 | else { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3522 | bc_vec_pop_all(&l->t.v); |
Denys Vlasenko | e55a572 | 2018-12-06 12:47:17 +0100 | [diff] [blame] | 3523 | bc_vec_push(&l->t.v, &l->buf[l->i - 1]); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 3524 | bc_vec_pushZeroByte(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3525 | l->t.t = BC_LEX_NAME; |
| 3526 | } |
| 3527 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 3528 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3529 | } |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 3530 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 3531 | # define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 3532 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3533 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 3534 | static BC_STATUS zdc_lex_string(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3535 | { |
| 3536 | size_t depth = 1, nls = 0, i = l->i; |
| 3537 | char c; |
| 3538 | |
| 3539 | l->t.t = BC_LEX_STR; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3540 | bc_vec_pop_all(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3541 | |
| 3542 | for (c = l->buf[i]; c != 0 && depth; c = l->buf[++i]) { |
| 3543 | |
| 3544 | depth += (c == '[' && (i == l->i || l->buf[i - 1] != '\\')); |
| 3545 | depth -= (c == ']' && (i == l->i || l->buf[i - 1] != '\\')); |
| 3546 | nls += (c == '\n'); |
| 3547 | |
| 3548 | if (depth) bc_vec_push(&l->t.v, &c); |
| 3549 | } |
| 3550 | |
| 3551 | if (c == '\0') { |
| 3552 | l->i = i; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 3553 | RETURN_STATUS(bc_error("string end could not be found")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3554 | } |
| 3555 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 3556 | bc_vec_pushZeroByte(&l->t.v); |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 3557 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
| 3558 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
| 3559 | if (i - l->i > BC_MAX_STRING) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 3560 | 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] | 3561 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3562 | |
| 3563 | l->i = i; |
| 3564 | l->line += nls; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3565 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3566 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 3567 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3568 | } |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 3569 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 3570 | # define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 3571 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3572 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 3573 | static FAST_FUNC BcStatus dc_lex_token(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3574 | { |
| 3575 | BcStatus s = BC_STATUS_SUCCESS; |
| 3576 | char c = l->buf[l->i++], c2; |
| 3577 | size_t i; |
| 3578 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3579 | for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) { |
| 3580 | if (l->t.last == dc_lex_regs[i]) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 3581 | return zdc_lex_register(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3582 | } |
| 3583 | |
| 3584 | if (c >= '%' && c <= '~' && |
| 3585 | (l->t.t = dc_lex_tokens[(c - '%')]) != BC_LEX_INVALID) |
| 3586 | { |
| 3587 | return s; |
| 3588 | } |
| 3589 | |
| 3590 | // This is the workhorse of the lexer. |
| 3591 | switch (c) { |
| 3592 | |
| 3593 | case '\0': |
| 3594 | { |
| 3595 | l->t.t = BC_LEX_EOF; |
| 3596 | break; |
| 3597 | } |
| 3598 | |
| 3599 | case '\n': |
| 3600 | case '\t': |
| 3601 | case '\v': |
| 3602 | case '\f': |
| 3603 | case '\r': |
| 3604 | case ' ': |
| 3605 | { |
| 3606 | l->newline = (c == '\n'); |
| 3607 | bc_lex_whitespace(l); |
| 3608 | break; |
| 3609 | } |
| 3610 | |
| 3611 | case '!': |
| 3612 | { |
| 3613 | c2 = l->buf[l->i]; |
| 3614 | |
| 3615 | if (c2 == '=') |
| 3616 | l->t.t = BC_LEX_OP_REL_NE; |
| 3617 | else if (c2 == '<') |
| 3618 | l->t.t = BC_LEX_OP_REL_LE; |
| 3619 | else if (c2 == '>') |
| 3620 | l->t.t = BC_LEX_OP_REL_GE; |
| 3621 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3622 | return bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3623 | |
| 3624 | ++l->i; |
| 3625 | break; |
| 3626 | } |
| 3627 | |
| 3628 | case '#': |
| 3629 | { |
| 3630 | bc_lex_lineComment(l); |
| 3631 | break; |
| 3632 | } |
| 3633 | |
| 3634 | case '.': |
| 3635 | { |
| 3636 | if (isdigit(l->buf[l->i])) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 3637 | s = zbc_lex_number(l, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3638 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3639 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3640 | break; |
| 3641 | } |
| 3642 | |
| 3643 | case '0': |
| 3644 | case '1': |
| 3645 | case '2': |
| 3646 | case '3': |
| 3647 | case '4': |
| 3648 | case '5': |
| 3649 | case '6': |
| 3650 | case '7': |
| 3651 | case '8': |
| 3652 | case '9': |
| 3653 | case 'A': |
| 3654 | case 'B': |
| 3655 | case 'C': |
| 3656 | case 'D': |
| 3657 | case 'E': |
| 3658 | case 'F': |
| 3659 | { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 3660 | s = zbc_lex_number(l, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3661 | break; |
| 3662 | } |
| 3663 | |
| 3664 | case '[': |
| 3665 | { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 3666 | s = zdc_lex_string(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3667 | break; |
| 3668 | } |
| 3669 | |
| 3670 | default: |
| 3671 | { |
| 3672 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3673 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3674 | break; |
| 3675 | } |
| 3676 | } |
| 3677 | |
| 3678 | return s; |
| 3679 | } |
| 3680 | #endif // ENABLE_DC |
| 3681 | |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3682 | static void bc_program_addFunc(char *name, size_t *idx); |
| 3683 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3684 | static void bc_parse_addFunc(BcParse *p, char *name, size_t *idx) |
| 3685 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3686 | bc_program_addFunc(name, idx); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 3687 | p->func = bc_program_func(p->fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3688 | } |
| 3689 | |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 3690 | #define bc_parse_push(p, i) bc_vec_pushByte(&(p)->func->code, (char) (i)) |
| 3691 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3692 | static void bc_parse_pushName(BcParse *p, char *name) |
| 3693 | { |
| 3694 | size_t i = 0, len = strlen(name); |
| 3695 | |
| 3696 | for (; i < len; ++i) bc_parse_push(p, name[i]); |
| 3697 | bc_parse_push(p, BC_PARSE_STREND); |
| 3698 | |
| 3699 | free(name); |
| 3700 | } |
| 3701 | |
| 3702 | static void bc_parse_pushIndex(BcParse *p, size_t idx) |
| 3703 | { |
| 3704 | unsigned char amt, i, nums[sizeof(size_t)]; |
| 3705 | |
| 3706 | for (amt = 0; idx; ++amt) { |
| 3707 | nums[amt] = (char) idx; |
| 3708 | idx = (idx & ((unsigned long) ~(UCHAR_MAX))) >> sizeof(char) * CHAR_BIT; |
| 3709 | } |
| 3710 | |
| 3711 | bc_parse_push(p, amt); |
| 3712 | for (i = 0; i < amt; ++i) bc_parse_push(p, nums[i]); |
| 3713 | } |
| 3714 | |
| 3715 | static void bc_parse_number(BcParse *p, BcInst *prev, size_t *nexs) |
| 3716 | { |
| 3717 | char *num = xstrdup(p->l.t.v.v); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3718 | size_t idx = G.prog.consts.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3719 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3720 | bc_vec_push(&G.prog.consts, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3721 | |
| 3722 | bc_parse_push(p, BC_INST_NUM); |
| 3723 | bc_parse_pushIndex(p, idx); |
| 3724 | |
| 3725 | ++(*nexs); |
| 3726 | (*prev) = BC_INST_NUM; |
| 3727 | } |
| 3728 | |
| 3729 | static BcStatus bc_parse_text(BcParse *p, const char *text) |
| 3730 | { |
| 3731 | BcStatus s; |
| 3732 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 3733 | p->func = bc_program_func(p->fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3734 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3735 | if (!text[0] && !BC_PARSE_CAN_EXEC(p)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3736 | p->l.t.t = BC_LEX_INVALID; |
| 3737 | s = p->parse(p); |
| 3738 | if (s) return s; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3739 | if (!BC_PARSE_CAN_EXEC(p)) |
| 3740 | return bc_error("file is not executable"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3741 | } |
| 3742 | |
| 3743 | return bc_lex_text(&p->l, text); |
| 3744 | } |
| 3745 | |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3746 | // Called when parsing or execution detects a failure, |
| 3747 | // resets execution structures. |
| 3748 | static void bc_program_reset(void) |
| 3749 | { |
| 3750 | BcFunc *f; |
| 3751 | BcInstPtr *ip; |
| 3752 | |
| 3753 | bc_vec_npop(&G.prog.stack, G.prog.stack.len - 1); |
| 3754 | bc_vec_pop_all(&G.prog.results); |
| 3755 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 3756 | f = bc_program_func(0); |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3757 | ip = bc_vec_top(&G.prog.stack); |
| 3758 | ip->idx = f->code.len; |
| 3759 | } |
| 3760 | |
Denys Vlasenko | e55a572 | 2018-12-06 12:47:17 +0100 | [diff] [blame] | 3761 | #define bc_parse_updateFunc(p, f) \ |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 3762 | ((p)->func = bc_program_func((p)->fidx = (f))) |
Denys Vlasenko | e55a572 | 2018-12-06 12:47:17 +0100 | [diff] [blame] | 3763 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 3764 | // Called when bc/dc_parse_parse() detects a failure, |
| 3765 | // resets parsing structures. |
| 3766 | static void bc_parse_reset(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3767 | { |
| 3768 | if (p->fidx != BC_PROG_MAIN) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3769 | p->func->nparams = 0; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3770 | bc_vec_pop_all(&p->func->code); |
| 3771 | bc_vec_pop_all(&p->func->autos); |
| 3772 | bc_vec_pop_all(&p->func->labels); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3773 | |
| 3774 | bc_parse_updateFunc(p, BC_PROG_MAIN); |
| 3775 | } |
| 3776 | |
| 3777 | p->l.i = p->l.len; |
| 3778 | p->l.t.t = BC_LEX_EOF; |
| 3779 | p->auto_part = (p->nbraces = 0); |
| 3780 | |
| 3781 | bc_vec_npop(&p->flags, p->flags.len - 1); |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3782 | bc_vec_pop_all(&p->exits); |
| 3783 | bc_vec_pop_all(&p->conds); |
| 3784 | bc_vec_pop_all(&p->ops); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3785 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 3786 | bc_program_reset(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3787 | } |
| 3788 | |
| 3789 | static void bc_parse_free(BcParse *p) |
| 3790 | { |
| 3791 | bc_vec_free(&p->flags); |
| 3792 | bc_vec_free(&p->exits); |
| 3793 | bc_vec_free(&p->conds); |
| 3794 | bc_vec_free(&p->ops); |
| 3795 | bc_lex_free(&p->l); |
| 3796 | } |
| 3797 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3798 | static void bc_parse_create(BcParse *p, size_t func, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3799 | BcParseParse parse, BcLexNext next) |
| 3800 | { |
| 3801 | memset(p, 0, sizeof(BcParse)); |
| 3802 | |
| 3803 | bc_lex_init(&p->l, next); |
| 3804 | bc_vec_init(&p->flags, sizeof(uint8_t), NULL); |
| 3805 | bc_vec_init(&p->exits, sizeof(BcInstPtr), NULL); |
| 3806 | bc_vec_init(&p->conds, sizeof(size_t), NULL); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 3807 | bc_vec_pushZeroByte(&p->flags); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3808 | bc_vec_init(&p->ops, sizeof(BcLexType), NULL); |
| 3809 | |
| 3810 | p->parse = parse; |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 3811 | // p->auto_part = p->nbraces = 0; - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3812 | bc_parse_updateFunc(p, func); |
| 3813 | } |
| 3814 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 3815 | #if ENABLE_BC |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 3816 | |
| 3817 | #define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops))) |
| 3818 | #define BC_PARSE_LEAF(p, rparen) \ |
| 3819 | (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \ |
| 3820 | (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST) |
| 3821 | |
| 3822 | // We can calculate the conversion between tokens and exprs by subtracting the |
| 3823 | // position of the first operator in the lex enum and adding the position of the |
| 3824 | // first in the expr enum. Note: This only works for binary operators. |
| 3825 | #define BC_PARSE_TOKEN_INST(t) ((char) ((t) -BC_LEX_NEG + BC_INST_NEG)) |
| 3826 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3827 | static BcStatus bc_parse_else(BcParse *p); |
| 3828 | static BcStatus bc_parse_stmt(BcParse *p); |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 3829 | static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next); |
Denys Vlasenko | 050b0fe | 2018-12-05 22:40:44 +0100 | [diff] [blame] | 3830 | 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] | 3831 | |
| 3832 | static BcStatus bc_parse_operator(BcParse *p, BcLexType type, size_t start, |
| 3833 | size_t *nexprs, bool next) |
| 3834 | { |
| 3835 | BcStatus s = BC_STATUS_SUCCESS; |
| 3836 | BcLexType t; |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 3837 | char l, r = bc_parse_op_PREC(type - BC_LEX_OP_INC); |
| 3838 | bool left = bc_parse_op_LEFT(type - BC_LEX_OP_INC); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3839 | |
| 3840 | while (p->ops.len > start) { |
| 3841 | |
| 3842 | t = BC_PARSE_TOP_OP(p); |
| 3843 | if (t == BC_LEX_LPAREN) break; |
| 3844 | |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 3845 | l = bc_parse_op_PREC(t - BC_LEX_OP_INC); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3846 | if (l >= r && (l != r || !left)) break; |
| 3847 | |
| 3848 | bc_parse_push(p, BC_PARSE_TOKEN_INST(t)); |
| 3849 | bc_vec_pop(&p->ops); |
| 3850 | *nexprs -= t != BC_LEX_OP_BOOL_NOT && t != BC_LEX_NEG; |
| 3851 | } |
| 3852 | |
| 3853 | bc_vec_push(&p->ops, &type); |
| 3854 | if (next) s = bc_lex_next(&p->l); |
| 3855 | |
| 3856 | return s; |
| 3857 | } |
| 3858 | |
| 3859 | static BcStatus bc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs) |
| 3860 | { |
| 3861 | BcLexType top; |
| 3862 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3863 | if (p->ops.len <= ops_bgn) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3864 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3865 | top = BC_PARSE_TOP_OP(p); |
| 3866 | |
| 3867 | while (top != BC_LEX_LPAREN) { |
| 3868 | |
| 3869 | bc_parse_push(p, BC_PARSE_TOKEN_INST(top)); |
| 3870 | |
| 3871 | bc_vec_pop(&p->ops); |
| 3872 | *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG; |
| 3873 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3874 | if (p->ops.len <= ops_bgn) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3875 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3876 | top = BC_PARSE_TOP_OP(p); |
| 3877 | } |
| 3878 | |
| 3879 | bc_vec_pop(&p->ops); |
| 3880 | |
| 3881 | return bc_lex_next(&p->l); |
| 3882 | } |
| 3883 | |
| 3884 | static BcStatus bc_parse_params(BcParse *p, uint8_t flags) |
| 3885 | { |
| 3886 | BcStatus s; |
| 3887 | bool comma = false; |
| 3888 | size_t nparams; |
| 3889 | |
| 3890 | s = bc_lex_next(&p->l); |
| 3891 | if (s) return s; |
| 3892 | |
| 3893 | for (nparams = 0; p->l.t.t != BC_LEX_RPAREN; ++nparams) { |
| 3894 | |
| 3895 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; |
| 3896 | s = bc_parse_expr(p, flags, bc_parse_next_param); |
| 3897 | if (s) return s; |
| 3898 | |
| 3899 | comma = p->l.t.t == BC_LEX_COMMA; |
| 3900 | if (comma) { |
| 3901 | s = bc_lex_next(&p->l); |
| 3902 | if (s) return s; |
| 3903 | } |
| 3904 | } |
| 3905 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3906 | if (comma) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3907 | bc_parse_push(p, BC_INST_CALL); |
| 3908 | bc_parse_pushIndex(p, nparams); |
| 3909 | |
| 3910 | return BC_STATUS_SUCCESS; |
| 3911 | } |
| 3912 | |
| 3913 | static BcStatus bc_parse_call(BcParse *p, char *name, uint8_t flags) |
| 3914 | { |
| 3915 | BcStatus s; |
| 3916 | BcId entry, *entry_ptr; |
| 3917 | size_t idx; |
| 3918 | |
| 3919 | entry.name = name; |
| 3920 | |
| 3921 | s = bc_parse_params(p, flags); |
| 3922 | if (s) goto err; |
| 3923 | |
| 3924 | if (p->l.t.t != BC_LEX_RPAREN) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3925 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3926 | goto err; |
| 3927 | } |
| 3928 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3929 | idx = bc_map_index(&G.prog.fn_map, &entry); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3930 | |
| 3931 | if (idx == BC_VEC_INVALID_IDX) { |
| 3932 | name = xstrdup(entry.name); |
| 3933 | bc_parse_addFunc(p, name, &idx); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3934 | idx = bc_map_index(&G.prog.fn_map, &entry); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3935 | free(entry.name); |
| 3936 | } |
| 3937 | else |
| 3938 | free(name); |
| 3939 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3940 | entry_ptr = bc_vec_item(&G.prog.fn_map, idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3941 | bc_parse_pushIndex(p, entry_ptr->idx); |
| 3942 | |
| 3943 | return bc_lex_next(&p->l); |
| 3944 | |
| 3945 | err: |
| 3946 | free(name); |
| 3947 | return s; |
| 3948 | } |
| 3949 | |
| 3950 | static BcStatus bc_parse_name(BcParse *p, BcInst *type, uint8_t flags) |
| 3951 | { |
| 3952 | BcStatus s; |
| 3953 | char *name; |
| 3954 | |
| 3955 | name = xstrdup(p->l.t.v.v); |
| 3956 | s = bc_lex_next(&p->l); |
| 3957 | if (s) goto err; |
| 3958 | |
| 3959 | if (p->l.t.t == BC_LEX_LBRACKET) { |
| 3960 | |
| 3961 | s = bc_lex_next(&p->l); |
| 3962 | if (s) goto err; |
| 3963 | |
| 3964 | if (p->l.t.t == BC_LEX_RBRACKET) { |
| 3965 | |
| 3966 | if (!(flags & BC_PARSE_ARRAY)) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3967 | s = bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3968 | goto err; |
| 3969 | } |
| 3970 | |
| 3971 | *type = BC_INST_ARRAY; |
| 3972 | } |
| 3973 | else { |
| 3974 | |
| 3975 | *type = BC_INST_ARRAY_ELEM; |
| 3976 | |
| 3977 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); |
| 3978 | s = bc_parse_expr(p, flags, bc_parse_next_elem); |
| 3979 | if (s) goto err; |
| 3980 | } |
| 3981 | |
| 3982 | s = bc_lex_next(&p->l); |
| 3983 | if (s) goto err; |
| 3984 | bc_parse_push(p, *type); |
| 3985 | bc_parse_pushName(p, name); |
| 3986 | } |
| 3987 | else if (p->l.t.t == BC_LEX_LPAREN) { |
| 3988 | |
| 3989 | if (flags & BC_PARSE_NOCALL) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3990 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3991 | goto err; |
| 3992 | } |
| 3993 | |
| 3994 | *type = BC_INST_CALL; |
| 3995 | s = bc_parse_call(p, name, flags); |
| 3996 | } |
| 3997 | else { |
| 3998 | *type = BC_INST_VAR; |
| 3999 | bc_parse_push(p, BC_INST_VAR); |
| 4000 | bc_parse_pushName(p, name); |
| 4001 | } |
| 4002 | |
| 4003 | return s; |
| 4004 | |
| 4005 | err: |
| 4006 | free(name); |
| 4007 | return s; |
| 4008 | } |
| 4009 | |
| 4010 | static BcStatus bc_parse_read(BcParse *p) |
| 4011 | { |
| 4012 | BcStatus s; |
| 4013 | |
| 4014 | s = bc_lex_next(&p->l); |
| 4015 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4016 | if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4017 | |
| 4018 | s = bc_lex_next(&p->l); |
| 4019 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4020 | if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4021 | |
| 4022 | bc_parse_push(p, BC_INST_READ); |
| 4023 | |
| 4024 | return bc_lex_next(&p->l); |
| 4025 | } |
| 4026 | |
| 4027 | static BcStatus bc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags, |
| 4028 | BcInst *prev) |
| 4029 | { |
| 4030 | BcStatus s; |
| 4031 | |
| 4032 | s = bc_lex_next(&p->l); |
| 4033 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4034 | if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4035 | |
| 4036 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; |
| 4037 | |
| 4038 | s = bc_lex_next(&p->l); |
| 4039 | if (s) return s; |
| 4040 | |
| 4041 | s = bc_parse_expr(p, flags, bc_parse_next_rel); |
| 4042 | if (s) return s; |
| 4043 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4044 | if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4045 | |
| 4046 | *prev = (type == BC_LEX_KEY_LENGTH) ? BC_INST_LENGTH : BC_INST_SQRT; |
| 4047 | bc_parse_push(p, *prev); |
| 4048 | |
| 4049 | return bc_lex_next(&p->l); |
| 4050 | } |
| 4051 | |
| 4052 | static BcStatus bc_parse_scale(BcParse *p, BcInst *type, uint8_t flags) |
| 4053 | { |
| 4054 | BcStatus s; |
| 4055 | |
| 4056 | s = bc_lex_next(&p->l); |
| 4057 | if (s) return s; |
| 4058 | |
| 4059 | if (p->l.t.t != BC_LEX_LPAREN) { |
| 4060 | *type = BC_INST_SCALE; |
| 4061 | bc_parse_push(p, BC_INST_SCALE); |
| 4062 | return BC_STATUS_SUCCESS; |
| 4063 | } |
| 4064 | |
| 4065 | *type = BC_INST_SCALE_FUNC; |
| 4066 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); |
| 4067 | |
| 4068 | s = bc_lex_next(&p->l); |
| 4069 | if (s) return s; |
| 4070 | |
| 4071 | s = bc_parse_expr(p, flags, bc_parse_next_rel); |
| 4072 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4073 | if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4074 | bc_parse_push(p, BC_INST_SCALE_FUNC); |
| 4075 | |
| 4076 | return bc_lex_next(&p->l); |
| 4077 | } |
| 4078 | |
| 4079 | static BcStatus bc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr, |
| 4080 | size_t *nexprs, uint8_t flags) |
| 4081 | { |
| 4082 | BcStatus s; |
| 4083 | BcLexType type; |
| 4084 | char inst; |
| 4085 | BcInst etype = *prev; |
| 4086 | |
| 4087 | if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM || |
| 4088 | etype == BC_INST_SCALE || etype == BC_INST_LAST || |
| 4089 | etype == BC_INST_IBASE || etype == BC_INST_OBASE) |
| 4090 | { |
| 4091 | *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC); |
| 4092 | bc_parse_push(p, inst); |
| 4093 | s = bc_lex_next(&p->l); |
| 4094 | } |
| 4095 | else { |
| 4096 | |
| 4097 | *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC); |
| 4098 | *paren_expr = true; |
| 4099 | |
| 4100 | s = bc_lex_next(&p->l); |
| 4101 | if (s) return s; |
| 4102 | type = p->l.t.t; |
| 4103 | |
| 4104 | // Because we parse the next part of the expression |
| 4105 | // right here, we need to increment this. |
| 4106 | *nexprs = *nexprs + 1; |
| 4107 | |
| 4108 | switch (type) { |
| 4109 | |
| 4110 | case BC_LEX_NAME: |
| 4111 | { |
| 4112 | s = bc_parse_name(p, prev, flags | BC_PARSE_NOCALL); |
| 4113 | break; |
| 4114 | } |
| 4115 | |
| 4116 | case BC_LEX_KEY_IBASE: |
| 4117 | case BC_LEX_KEY_LAST: |
| 4118 | case BC_LEX_KEY_OBASE: |
| 4119 | { |
| 4120 | bc_parse_push(p, type - BC_LEX_KEY_IBASE + BC_INST_IBASE); |
| 4121 | s = bc_lex_next(&p->l); |
| 4122 | break; |
| 4123 | } |
| 4124 | |
| 4125 | case BC_LEX_KEY_SCALE: |
| 4126 | { |
| 4127 | s = bc_lex_next(&p->l); |
| 4128 | if (s) return s; |
| 4129 | if (p->l.t.t == BC_LEX_LPAREN) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4130 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4131 | else |
| 4132 | bc_parse_push(p, BC_INST_SCALE); |
| 4133 | break; |
| 4134 | } |
| 4135 | |
| 4136 | default: |
| 4137 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4138 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4139 | break; |
| 4140 | } |
| 4141 | } |
| 4142 | |
| 4143 | if (!s) bc_parse_push(p, inst); |
| 4144 | } |
| 4145 | |
| 4146 | return s; |
| 4147 | } |
| 4148 | |
| 4149 | static BcStatus bc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn, |
| 4150 | bool rparen, size_t *nexprs) |
| 4151 | { |
| 4152 | BcStatus s; |
| 4153 | BcLexType type; |
| 4154 | BcInst etype = *prev; |
| 4155 | |
| 4156 | s = bc_lex_next(&p->l); |
| 4157 | if (s) return s; |
| 4158 | |
| 4159 | type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST || |
| 4160 | (etype >= BC_INST_NUM && etype <= BC_INST_SQRT) ? |
| 4161 | BC_LEX_OP_MINUS : |
| 4162 | BC_LEX_NEG; |
| 4163 | *prev = BC_PARSE_TOKEN_INST(type); |
| 4164 | |
| 4165 | // We can just push onto the op stack because this is the largest |
| 4166 | // precedence operator that gets pushed. Inc/dec does not. |
| 4167 | if (type != BC_LEX_OP_MINUS) |
| 4168 | bc_vec_push(&p->ops, &type); |
| 4169 | else |
| 4170 | s = bc_parse_operator(p, type, ops_bgn, nexprs, false); |
| 4171 | |
| 4172 | return s; |
| 4173 | } |
| 4174 | |
| 4175 | static BcStatus bc_parse_string(BcParse *p, char inst) |
| 4176 | { |
| 4177 | char *str = xstrdup(p->l.t.v.v); |
| 4178 | |
| 4179 | bc_parse_push(p, BC_INST_STR); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 4180 | bc_parse_pushIndex(p, G.prog.strs.len); |
| 4181 | bc_vec_push(&G.prog.strs, &str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4182 | bc_parse_push(p, inst); |
| 4183 | |
| 4184 | return bc_lex_next(&p->l); |
| 4185 | } |
| 4186 | |
| 4187 | static BcStatus bc_parse_print(BcParse *p) |
| 4188 | { |
| 4189 | BcStatus s; |
| 4190 | BcLexType type; |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4191 | bool comma; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4192 | |
| 4193 | s = bc_lex_next(&p->l); |
| 4194 | if (s) return s; |
| 4195 | |
| 4196 | type = p->l.t.t; |
| 4197 | |
| 4198 | if (type == BC_LEX_SCOLON || type == BC_LEX_NLINE) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4199 | return bc_error("bad print statement"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4200 | |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4201 | comma = false; |
| 4202 | while (type != BC_LEX_SCOLON && type != BC_LEX_NLINE) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4203 | |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4204 | if (type == BC_LEX_STR) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4205 | s = bc_parse_string(p, BC_INST_PRINT_POP); |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4206 | if (s) return s; |
| 4207 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4208 | s = bc_parse_expr(p, 0, bc_parse_next_print); |
| 4209 | if (s) return s; |
| 4210 | bc_parse_push(p, BC_INST_PRINT_POP); |
| 4211 | } |
| 4212 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4213 | comma = p->l.t.t == BC_LEX_COMMA; |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4214 | if (comma) { |
| 4215 | s = bc_lex_next(&p->l); |
| 4216 | if (s) return s; |
| 4217 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4218 | type = p->l.t.t; |
| 4219 | } |
| 4220 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4221 | if (comma) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4222 | |
| 4223 | return bc_lex_next(&p->l); |
| 4224 | } |
| 4225 | |
| 4226 | static BcStatus bc_parse_return(BcParse *p) |
| 4227 | { |
| 4228 | BcStatus s; |
| 4229 | BcLexType t; |
| 4230 | bool paren; |
| 4231 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4232 | if (!BC_PARSE_FUNC(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4233 | |
| 4234 | s = bc_lex_next(&p->l); |
| 4235 | if (s) return s; |
| 4236 | |
| 4237 | t = p->l.t.t; |
| 4238 | paren = t == BC_LEX_LPAREN; |
| 4239 | |
| 4240 | if (t == BC_LEX_NLINE || t == BC_LEX_SCOLON) |
| 4241 | bc_parse_push(p, BC_INST_RET0); |
| 4242 | else { |
| 4243 | |
Denys Vlasenko | 050b0fe | 2018-12-05 22:40:44 +0100 | [diff] [blame] | 4244 | s = bc_parse_expr_empty_ok(p, 0, bc_parse_next_expr); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4245 | if (s == BC_STATUS_PARSE_EMPTY_EXP) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4246 | bc_parse_push(p, BC_INST_RET0); |
| 4247 | s = bc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4248 | } |
Denys Vlasenko | 452df92 | 2018-12-05 20:28:26 +0100 | [diff] [blame] | 4249 | if (s) return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4250 | |
| 4251 | if (!paren || p->l.t.last != BC_LEX_RPAREN) { |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 4252 | s = bc_POSIX_requires("parentheses around return expressions"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4253 | if (s) return s; |
| 4254 | } |
| 4255 | |
| 4256 | bc_parse_push(p, BC_INST_RET); |
| 4257 | } |
| 4258 | |
| 4259 | return s; |
| 4260 | } |
| 4261 | |
| 4262 | static BcStatus bc_parse_endBody(BcParse *p, bool brace) |
| 4263 | { |
| 4264 | BcStatus s = BC_STATUS_SUCCESS; |
| 4265 | |
| 4266 | if (p->flags.len <= 1 || (brace && p->nbraces == 0)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4267 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4268 | |
| 4269 | if (brace) { |
| 4270 | |
| 4271 | if (p->l.t.t == BC_LEX_RBRACE) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4272 | if (!p->nbraces) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4273 | --p->nbraces; |
| 4274 | s = bc_lex_next(&p->l); |
| 4275 | if (s) return s; |
| 4276 | } |
| 4277 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4278 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4279 | } |
| 4280 | |
| 4281 | if (BC_PARSE_IF(p)) { |
| 4282 | |
| 4283 | uint8_t *flag_ptr; |
| 4284 | |
| 4285 | while (p->l.t.t == BC_LEX_NLINE) { |
| 4286 | s = bc_lex_next(&p->l); |
| 4287 | if (s) return s; |
| 4288 | } |
| 4289 | |
| 4290 | bc_vec_pop(&p->flags); |
| 4291 | |
| 4292 | flag_ptr = BC_PARSE_TOP_FLAG_PTR(p); |
| 4293 | *flag_ptr = (*flag_ptr | BC_PARSE_FLAG_IF_END); |
| 4294 | |
| 4295 | if (p->l.t.t == BC_LEX_KEY_ELSE) s = bc_parse_else(p); |
| 4296 | } |
| 4297 | else if (BC_PARSE_ELSE(p)) { |
| 4298 | |
| 4299 | BcInstPtr *ip; |
| 4300 | size_t *label; |
| 4301 | |
| 4302 | bc_vec_pop(&p->flags); |
| 4303 | |
| 4304 | ip = bc_vec_top(&p->exits); |
| 4305 | label = bc_vec_item(&p->func->labels, ip->idx); |
| 4306 | *label = p->func->code.len; |
| 4307 | |
| 4308 | bc_vec_pop(&p->exits); |
| 4309 | } |
| 4310 | else if (BC_PARSE_FUNC_INNER(p)) { |
| 4311 | bc_parse_push(p, BC_INST_RET0); |
| 4312 | bc_parse_updateFunc(p, BC_PROG_MAIN); |
| 4313 | bc_vec_pop(&p->flags); |
| 4314 | } |
| 4315 | else { |
| 4316 | |
| 4317 | BcInstPtr *ip = bc_vec_top(&p->exits); |
| 4318 | size_t *label = bc_vec_top(&p->conds); |
| 4319 | |
| 4320 | bc_parse_push(p, BC_INST_JUMP); |
| 4321 | bc_parse_pushIndex(p, *label); |
| 4322 | |
| 4323 | label = bc_vec_item(&p->func->labels, ip->idx); |
| 4324 | *label = p->func->code.len; |
| 4325 | |
| 4326 | bc_vec_pop(&p->flags); |
| 4327 | bc_vec_pop(&p->exits); |
| 4328 | bc_vec_pop(&p->conds); |
| 4329 | } |
| 4330 | |
| 4331 | return s; |
| 4332 | } |
| 4333 | |
| 4334 | static void bc_parse_startBody(BcParse *p, uint8_t flags) |
| 4335 | { |
| 4336 | uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p); |
| 4337 | flags |= (*flag_ptr & (BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_LOOP)); |
| 4338 | flags |= BC_PARSE_FLAG_BODY; |
| 4339 | bc_vec_push(&p->flags, &flags); |
| 4340 | } |
| 4341 | |
| 4342 | static void bc_parse_noElse(BcParse *p) |
| 4343 | { |
| 4344 | BcInstPtr *ip; |
| 4345 | size_t *label; |
| 4346 | uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p); |
| 4347 | |
| 4348 | *flag_ptr = (*flag_ptr & ~(BC_PARSE_FLAG_IF_END)); |
| 4349 | |
| 4350 | ip = bc_vec_top(&p->exits); |
| 4351 | label = bc_vec_item(&p->func->labels, ip->idx); |
| 4352 | *label = p->func->code.len; |
| 4353 | |
| 4354 | bc_vec_pop(&p->exits); |
| 4355 | } |
| 4356 | |
| 4357 | static BcStatus bc_parse_if(BcParse *p) |
| 4358 | { |
| 4359 | BcStatus s; |
| 4360 | BcInstPtr ip; |
| 4361 | |
| 4362 | s = bc_lex_next(&p->l); |
| 4363 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4364 | if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4365 | |
| 4366 | s = bc_lex_next(&p->l); |
| 4367 | if (s) return s; |
| 4368 | s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel); |
| 4369 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4370 | if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4371 | |
| 4372 | s = bc_lex_next(&p->l); |
| 4373 | if (s) return s; |
| 4374 | bc_parse_push(p, BC_INST_JUMP_ZERO); |
| 4375 | |
| 4376 | ip.idx = p->func->labels.len; |
| 4377 | ip.func = ip.len = 0; |
| 4378 | |
| 4379 | bc_parse_pushIndex(p, ip.idx); |
| 4380 | bc_vec_push(&p->exits, &ip); |
| 4381 | bc_vec_push(&p->func->labels, &ip.idx); |
| 4382 | bc_parse_startBody(p, BC_PARSE_FLAG_IF); |
| 4383 | |
| 4384 | return BC_STATUS_SUCCESS; |
| 4385 | } |
| 4386 | |
| 4387 | static BcStatus bc_parse_else(BcParse *p) |
| 4388 | { |
| 4389 | BcInstPtr ip; |
| 4390 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4391 | if (!BC_PARSE_IF_END(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4392 | |
| 4393 | ip.idx = p->func->labels.len; |
| 4394 | ip.func = ip.len = 0; |
| 4395 | |
| 4396 | bc_parse_push(p, BC_INST_JUMP); |
| 4397 | bc_parse_pushIndex(p, ip.idx); |
| 4398 | |
| 4399 | bc_parse_noElse(p); |
| 4400 | |
| 4401 | bc_vec_push(&p->exits, &ip); |
| 4402 | bc_vec_push(&p->func->labels, &ip.idx); |
| 4403 | bc_parse_startBody(p, BC_PARSE_FLAG_ELSE); |
| 4404 | |
| 4405 | return bc_lex_next(&p->l); |
| 4406 | } |
| 4407 | |
| 4408 | static BcStatus bc_parse_while(BcParse *p) |
| 4409 | { |
| 4410 | BcStatus s; |
| 4411 | BcInstPtr ip; |
| 4412 | |
| 4413 | s = bc_lex_next(&p->l); |
| 4414 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4415 | if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4416 | s = bc_lex_next(&p->l); |
| 4417 | if (s) return s; |
| 4418 | |
| 4419 | ip.idx = p->func->labels.len; |
| 4420 | |
| 4421 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4422 | bc_vec_push(&p->conds, &ip.idx); |
| 4423 | |
| 4424 | ip.idx = p->func->labels.len; |
| 4425 | ip.func = 1; |
| 4426 | ip.len = 0; |
| 4427 | |
| 4428 | bc_vec_push(&p->exits, &ip); |
| 4429 | bc_vec_push(&p->func->labels, &ip.idx); |
| 4430 | |
| 4431 | s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel); |
| 4432 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4433 | if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4434 | s = bc_lex_next(&p->l); |
| 4435 | if (s) return s; |
| 4436 | |
| 4437 | bc_parse_push(p, BC_INST_JUMP_ZERO); |
| 4438 | bc_parse_pushIndex(p, ip.idx); |
| 4439 | bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER); |
| 4440 | |
| 4441 | return BC_STATUS_SUCCESS; |
| 4442 | } |
| 4443 | |
| 4444 | static BcStatus bc_parse_for(BcParse *p) |
| 4445 | { |
| 4446 | BcStatus s; |
| 4447 | BcInstPtr ip; |
| 4448 | size_t cond_idx, exit_idx, body_idx, update_idx; |
| 4449 | |
| 4450 | s = bc_lex_next(&p->l); |
| 4451 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4452 | if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4453 | s = bc_lex_next(&p->l); |
| 4454 | if (s) return s; |
| 4455 | |
| 4456 | if (p->l.t.t != BC_LEX_SCOLON) |
| 4457 | s = bc_parse_expr(p, 0, bc_parse_next_for); |
| 4458 | else |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 4459 | s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4460 | |
| 4461 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4462 | if (p->l.t.t != BC_LEX_SCOLON) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4463 | s = bc_lex_next(&p->l); |
| 4464 | if (s) return s; |
| 4465 | |
| 4466 | cond_idx = p->func->labels.len; |
| 4467 | update_idx = cond_idx + 1; |
| 4468 | body_idx = update_idx + 1; |
| 4469 | exit_idx = body_idx + 1; |
| 4470 | |
| 4471 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4472 | |
| 4473 | if (p->l.t.t != BC_LEX_SCOLON) |
| 4474 | s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_for); |
| 4475 | else |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 4476 | s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4477 | |
| 4478 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4479 | if (p->l.t.t != BC_LEX_SCOLON) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4480 | |
| 4481 | s = bc_lex_next(&p->l); |
| 4482 | if (s) return s; |
| 4483 | |
| 4484 | bc_parse_push(p, BC_INST_JUMP_ZERO); |
| 4485 | bc_parse_pushIndex(p, exit_idx); |
| 4486 | bc_parse_push(p, BC_INST_JUMP); |
| 4487 | bc_parse_pushIndex(p, body_idx); |
| 4488 | |
| 4489 | ip.idx = p->func->labels.len; |
| 4490 | |
| 4491 | bc_vec_push(&p->conds, &update_idx); |
| 4492 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4493 | |
| 4494 | if (p->l.t.t != BC_LEX_RPAREN) |
| 4495 | s = bc_parse_expr(p, 0, bc_parse_next_rel); |
| 4496 | else |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 4497 | s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4498 | |
| 4499 | if (s) return s; |
| 4500 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4501 | if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4502 | bc_parse_push(p, BC_INST_JUMP); |
| 4503 | bc_parse_pushIndex(p, cond_idx); |
| 4504 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4505 | |
| 4506 | ip.idx = exit_idx; |
| 4507 | ip.func = 1; |
| 4508 | ip.len = 0; |
| 4509 | |
| 4510 | bc_vec_push(&p->exits, &ip); |
| 4511 | bc_vec_push(&p->func->labels, &ip.idx); |
| 4512 | bc_lex_next(&p->l); |
| 4513 | bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER); |
| 4514 | |
| 4515 | return BC_STATUS_SUCCESS; |
| 4516 | } |
| 4517 | |
| 4518 | static BcStatus bc_parse_loopExit(BcParse *p, BcLexType type) |
| 4519 | { |
| 4520 | BcStatus s; |
| 4521 | size_t i; |
| 4522 | BcInstPtr *ip; |
| 4523 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4524 | if (!BC_PARSE_LOOP(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4525 | |
| 4526 | if (type == BC_LEX_KEY_BREAK) { |
| 4527 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4528 | if (p->exits.len == 0) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4529 | |
| 4530 | i = p->exits.len - 1; |
| 4531 | ip = bc_vec_item(&p->exits, i); |
| 4532 | |
| 4533 | while (!ip->func && i < p->exits.len) ip = bc_vec_item(&p->exits, i--); |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4534 | if (i >= p->exits.len && !ip->func) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4535 | |
| 4536 | i = ip->idx; |
| 4537 | } |
| 4538 | else |
| 4539 | i = *((size_t *) bc_vec_top(&p->conds)); |
| 4540 | |
| 4541 | bc_parse_push(p, BC_INST_JUMP); |
| 4542 | bc_parse_pushIndex(p, i); |
| 4543 | |
| 4544 | s = bc_lex_next(&p->l); |
| 4545 | if (s) return s; |
| 4546 | |
| 4547 | if (p->l.t.t != BC_LEX_SCOLON && p->l.t.t != BC_LEX_NLINE) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4548 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4549 | |
| 4550 | return bc_lex_next(&p->l); |
| 4551 | } |
| 4552 | |
| 4553 | static BcStatus bc_parse_func(BcParse *p) |
| 4554 | { |
| 4555 | BcStatus s; |
| 4556 | bool var, comma = false; |
| 4557 | uint8_t flags; |
| 4558 | char *name; |
| 4559 | |
| 4560 | s = bc_lex_next(&p->l); |
| 4561 | if (s) return s; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4562 | if (p->l.t.t != BC_LEX_NAME) |
| 4563 | return bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4564 | |
| 4565 | name = xstrdup(p->l.t.v.v); |
| 4566 | bc_parse_addFunc(p, name, &p->fidx); |
| 4567 | |
| 4568 | s = bc_lex_next(&p->l); |
| 4569 | if (s) return s; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4570 | if (p->l.t.t != BC_LEX_LPAREN) |
| 4571 | return bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4572 | s = bc_lex_next(&p->l); |
| 4573 | if (s) return s; |
| 4574 | |
| 4575 | while (p->l.t.t != BC_LEX_RPAREN) { |
| 4576 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4577 | if (p->l.t.t != BC_LEX_NAME) |
| 4578 | return bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4579 | |
| 4580 | ++p->func->nparams; |
| 4581 | |
| 4582 | name = xstrdup(p->l.t.v.v); |
| 4583 | s = bc_lex_next(&p->l); |
| 4584 | if (s) goto err; |
| 4585 | |
| 4586 | var = p->l.t.t != BC_LEX_LBRACKET; |
| 4587 | |
| 4588 | if (!var) { |
| 4589 | |
| 4590 | s = bc_lex_next(&p->l); |
| 4591 | if (s) goto err; |
| 4592 | |
| 4593 | if (p->l.t.t != BC_LEX_RBRACKET) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4594 | s = bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4595 | goto err; |
| 4596 | } |
| 4597 | |
| 4598 | s = bc_lex_next(&p->l); |
| 4599 | if (s) goto err; |
| 4600 | } |
| 4601 | |
| 4602 | comma = p->l.t.t == BC_LEX_COMMA; |
| 4603 | if (comma) { |
| 4604 | s = bc_lex_next(&p->l); |
| 4605 | if (s) goto err; |
| 4606 | } |
| 4607 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 4608 | s = zbc_func_insert(p->func, name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4609 | if (s) goto err; |
| 4610 | } |
| 4611 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4612 | if (comma) return bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4613 | |
| 4614 | flags = BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_BODY; |
| 4615 | bc_parse_startBody(p, flags); |
| 4616 | |
| 4617 | s = bc_lex_next(&p->l); |
| 4618 | if (s) return s; |
| 4619 | |
| 4620 | if (p->l.t.t != BC_LEX_LBRACE) |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 4621 | 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] | 4622 | |
| 4623 | return s; |
| 4624 | |
| 4625 | err: |
| 4626 | free(name); |
| 4627 | return s; |
| 4628 | } |
| 4629 | |
| 4630 | static BcStatus bc_parse_auto(BcParse *p) |
| 4631 | { |
| 4632 | BcStatus s; |
| 4633 | bool comma, var, one; |
| 4634 | char *name; |
| 4635 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4636 | if (!p->auto_part) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4637 | s = bc_lex_next(&p->l); |
| 4638 | if (s) return s; |
| 4639 | |
| 4640 | p->auto_part = comma = false; |
| 4641 | one = p->l.t.t == BC_LEX_NAME; |
| 4642 | |
| 4643 | while (p->l.t.t == BC_LEX_NAME) { |
| 4644 | |
| 4645 | name = xstrdup(p->l.t.v.v); |
| 4646 | s = bc_lex_next(&p->l); |
| 4647 | if (s) goto err; |
| 4648 | |
| 4649 | var = p->l.t.t != BC_LEX_LBRACKET; |
| 4650 | if (!var) { |
| 4651 | |
| 4652 | s = bc_lex_next(&p->l); |
| 4653 | if (s) goto err; |
| 4654 | |
| 4655 | if (p->l.t.t != BC_LEX_RBRACKET) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4656 | s = bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4657 | goto err; |
| 4658 | } |
| 4659 | |
| 4660 | s = bc_lex_next(&p->l); |
| 4661 | if (s) goto err; |
| 4662 | } |
| 4663 | |
| 4664 | comma = p->l.t.t == BC_LEX_COMMA; |
| 4665 | if (comma) { |
| 4666 | s = bc_lex_next(&p->l); |
| 4667 | if (s) goto err; |
| 4668 | } |
| 4669 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 4670 | s = zbc_func_insert(p->func, name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4671 | if (s) goto err; |
| 4672 | } |
| 4673 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4674 | if (comma) return bc_error("bad function definition"); |
Denys Vlasenko | abbc433 | 2018-12-03 21:46:41 +0100 | [diff] [blame] | 4675 | if (!one) return bc_error("no auto variable found"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4676 | |
| 4677 | if (p->l.t.t != BC_LEX_NLINE && p->l.t.t != BC_LEX_SCOLON) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4678 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4679 | |
| 4680 | return bc_lex_next(&p->l); |
| 4681 | |
| 4682 | err: |
| 4683 | free(name); |
| 4684 | return s; |
| 4685 | } |
| 4686 | |
| 4687 | static BcStatus bc_parse_body(BcParse *p, bool brace) |
| 4688 | { |
| 4689 | BcStatus s = BC_STATUS_SUCCESS; |
| 4690 | uint8_t *flag_ptr = bc_vec_top(&p->flags); |
| 4691 | |
| 4692 | *flag_ptr &= ~(BC_PARSE_FLAG_BODY); |
| 4693 | |
| 4694 | if (*flag_ptr & BC_PARSE_FLAG_FUNC_INNER) { |
| 4695 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4696 | if (!brace) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4697 | p->auto_part = p->l.t.t != BC_LEX_KEY_AUTO; |
| 4698 | |
| 4699 | if (!p->auto_part) { |
| 4700 | s = bc_parse_auto(p); |
| 4701 | if (s) return s; |
| 4702 | } |
| 4703 | |
| 4704 | if (p->l.t.t == BC_LEX_NLINE) s = bc_lex_next(&p->l); |
| 4705 | } |
| 4706 | else { |
| 4707 | s = bc_parse_stmt(p); |
| 4708 | if (!s && !brace) s = bc_parse_endBody(p, false); |
| 4709 | } |
| 4710 | |
| 4711 | return s; |
| 4712 | } |
| 4713 | |
| 4714 | static BcStatus bc_parse_stmt(BcParse *p) |
| 4715 | { |
| 4716 | BcStatus s = BC_STATUS_SUCCESS; |
| 4717 | |
| 4718 | switch (p->l.t.t) { |
| 4719 | |
| 4720 | case BC_LEX_NLINE: |
| 4721 | { |
| 4722 | return bc_lex_next(&p->l); |
| 4723 | } |
| 4724 | |
| 4725 | case BC_LEX_KEY_ELSE: |
| 4726 | { |
| 4727 | p->auto_part = false; |
| 4728 | break; |
| 4729 | } |
| 4730 | |
| 4731 | case BC_LEX_LBRACE: |
| 4732 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4733 | if (!BC_PARSE_BODY(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4734 | |
| 4735 | ++p->nbraces; |
| 4736 | s = bc_lex_next(&p->l); |
| 4737 | if (s) return s; |
| 4738 | |
| 4739 | return bc_parse_body(p, true); |
| 4740 | } |
| 4741 | |
| 4742 | case BC_LEX_KEY_AUTO: |
| 4743 | { |
| 4744 | return bc_parse_auto(p); |
| 4745 | } |
| 4746 | |
| 4747 | default: |
| 4748 | { |
| 4749 | p->auto_part = false; |
| 4750 | |
| 4751 | if (BC_PARSE_IF_END(p)) { |
| 4752 | bc_parse_noElse(p); |
| 4753 | return BC_STATUS_SUCCESS; |
| 4754 | } |
| 4755 | else if (BC_PARSE_BODY(p)) |
| 4756 | return bc_parse_body(p, false); |
| 4757 | |
| 4758 | break; |
| 4759 | } |
| 4760 | } |
| 4761 | |
| 4762 | switch (p->l.t.t) { |
| 4763 | |
| 4764 | case BC_LEX_OP_INC: |
| 4765 | case BC_LEX_OP_DEC: |
| 4766 | case BC_LEX_OP_MINUS: |
| 4767 | case BC_LEX_OP_BOOL_NOT: |
| 4768 | case BC_LEX_LPAREN: |
| 4769 | case BC_LEX_NAME: |
| 4770 | case BC_LEX_NUMBER: |
| 4771 | case BC_LEX_KEY_IBASE: |
| 4772 | case BC_LEX_KEY_LAST: |
| 4773 | case BC_LEX_KEY_LENGTH: |
| 4774 | case BC_LEX_KEY_OBASE: |
| 4775 | case BC_LEX_KEY_READ: |
| 4776 | case BC_LEX_KEY_SCALE: |
| 4777 | case BC_LEX_KEY_SQRT: |
| 4778 | { |
| 4779 | s = bc_parse_expr(p, BC_PARSE_PRINT, bc_parse_next_expr); |
| 4780 | break; |
| 4781 | } |
| 4782 | |
| 4783 | case BC_LEX_KEY_ELSE: |
| 4784 | { |
| 4785 | s = bc_parse_else(p); |
| 4786 | break; |
| 4787 | } |
| 4788 | |
| 4789 | case BC_LEX_SCOLON: |
| 4790 | { |
| 4791 | while (!s && p->l.t.t == BC_LEX_SCOLON) s = bc_lex_next(&p->l); |
| 4792 | break; |
| 4793 | } |
| 4794 | |
| 4795 | case BC_LEX_RBRACE: |
| 4796 | { |
| 4797 | s = bc_parse_endBody(p, true); |
| 4798 | break; |
| 4799 | } |
| 4800 | |
| 4801 | case BC_LEX_STR: |
| 4802 | { |
| 4803 | s = bc_parse_string(p, BC_INST_PRINT_STR); |
| 4804 | break; |
| 4805 | } |
| 4806 | |
| 4807 | case BC_LEX_KEY_BREAK: |
| 4808 | case BC_LEX_KEY_CONTINUE: |
| 4809 | { |
| 4810 | s = bc_parse_loopExit(p, p->l.t.t); |
| 4811 | break; |
| 4812 | } |
| 4813 | |
| 4814 | case BC_LEX_KEY_FOR: |
| 4815 | { |
| 4816 | s = bc_parse_for(p); |
| 4817 | break; |
| 4818 | } |
| 4819 | |
| 4820 | case BC_LEX_KEY_HALT: |
| 4821 | { |
| 4822 | bc_parse_push(p, BC_INST_HALT); |
| 4823 | s = bc_lex_next(&p->l); |
| 4824 | break; |
| 4825 | } |
| 4826 | |
| 4827 | case BC_LEX_KEY_IF: |
| 4828 | { |
| 4829 | s = bc_parse_if(p); |
| 4830 | break; |
| 4831 | } |
| 4832 | |
| 4833 | case BC_LEX_KEY_LIMITS: |
| 4834 | { |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 4835 | // "limits" is a compile-time command, |
| 4836 | // the output is produced at _parse time_. |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4837 | s = bc_lex_next(&p->l); |
| 4838 | if (s) return s; |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 4839 | printf( |
| 4840 | "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n" |
| 4841 | "BC_DIM_MAX = "BC_MAX_DIM_STR "\n" |
| 4842 | "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n" |
| 4843 | "BC_STRING_MAX = "BC_MAX_STRING_STR"\n" |
| 4844 | "BC_NAME_MAX = "BC_MAX_NAME_STR "\n" |
| 4845 | "BC_NUM_MAX = "BC_MAX_NUM_STR "\n" |
| 4846 | "MAX Exponent = "BC_MAX_EXP_STR "\n" |
| 4847 | "Number of vars = "BC_MAX_VARS_STR "\n" |
| 4848 | ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4849 | break; |
| 4850 | } |
| 4851 | |
| 4852 | case BC_LEX_KEY_PRINT: |
| 4853 | { |
| 4854 | s = bc_parse_print(p); |
| 4855 | break; |
| 4856 | } |
| 4857 | |
| 4858 | case BC_LEX_KEY_QUIT: |
| 4859 | { |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 4860 | // "quit" is a compile-time command. For example, |
| 4861 | // "if (0 == 1) quit" terminates when parsing the statement, |
| 4862 | // not when it is executed |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 4863 | QUIT_OR_RETURN_TO_MAIN; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4864 | } |
| 4865 | |
| 4866 | case BC_LEX_KEY_RETURN: |
| 4867 | { |
| 4868 | s = bc_parse_return(p); |
| 4869 | break; |
| 4870 | } |
| 4871 | |
| 4872 | case BC_LEX_KEY_WHILE: |
| 4873 | { |
| 4874 | s = bc_parse_while(p); |
| 4875 | break; |
| 4876 | } |
| 4877 | |
| 4878 | default: |
| 4879 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4880 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4881 | break; |
| 4882 | } |
| 4883 | } |
| 4884 | |
| 4885 | return s; |
| 4886 | } |
| 4887 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 4888 | static FAST_FUNC BcStatus bc_parse_parse(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4889 | { |
| 4890 | BcStatus s; |
| 4891 | |
| 4892 | if (p->l.t.t == BC_LEX_EOF) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4893 | 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] | 4894 | else if (p->l.t.t == BC_LEX_KEY_DEFINE) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4895 | if (!BC_PARSE_CAN_EXEC(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4896 | s = bc_parse_func(p); |
| 4897 | } |
| 4898 | else |
| 4899 | s = bc_parse_stmt(p); |
| 4900 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 4901 | if (s || G_interrupt) { |
| 4902 | bc_parse_reset(p); |
| 4903 | s = BC_STATUS_FAILURE; |
| 4904 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4905 | |
| 4906 | return s; |
| 4907 | } |
| 4908 | |
Denys Vlasenko | 050b0fe | 2018-12-05 22:40:44 +0100 | [diff] [blame] | 4909 | 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] | 4910 | { |
| 4911 | BcStatus s = BC_STATUS_SUCCESS; |
| 4912 | BcInst prev = BC_INST_PRINT; |
| 4913 | BcLexType top, t = p->l.t.t; |
| 4914 | size_t nexprs = 0, ops_bgn = p->ops.len; |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 4915 | unsigned nparens, nrelops; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4916 | bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last; |
| 4917 | |
| 4918 | paren_first = p->l.t.t == BC_LEX_LPAREN; |
| 4919 | nparens = nrelops = 0; |
| 4920 | paren_expr = rprn = done = get_token = assign = false; |
| 4921 | bin_last = true; |
| 4922 | |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 4923 | 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] | 4924 | switch (t) { |
| 4925 | |
| 4926 | case BC_LEX_OP_INC: |
| 4927 | case BC_LEX_OP_DEC: |
| 4928 | { |
| 4929 | s = bc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags); |
| 4930 | rprn = get_token = bin_last = false; |
| 4931 | break; |
| 4932 | } |
| 4933 | |
| 4934 | case BC_LEX_OP_MINUS: |
| 4935 | { |
| 4936 | s = bc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs); |
| 4937 | rprn = get_token = false; |
| 4938 | bin_last = prev == BC_INST_MINUS; |
| 4939 | break; |
| 4940 | } |
| 4941 | |
| 4942 | case BC_LEX_OP_ASSIGN_POWER: |
| 4943 | case BC_LEX_OP_ASSIGN_MULTIPLY: |
| 4944 | case BC_LEX_OP_ASSIGN_DIVIDE: |
| 4945 | case BC_LEX_OP_ASSIGN_MODULUS: |
| 4946 | case BC_LEX_OP_ASSIGN_PLUS: |
| 4947 | case BC_LEX_OP_ASSIGN_MINUS: |
| 4948 | case BC_LEX_OP_ASSIGN: |
| 4949 | { |
| 4950 | if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM && |
| 4951 | prev != BC_INST_SCALE && prev != BC_INST_IBASE && |
| 4952 | prev != BC_INST_OBASE && prev != BC_INST_LAST) |
| 4953 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4954 | s = bc_error("bad assignment:" |
| 4955 | " left side must be scale," |
| 4956 | " ibase, obase, last, var," |
| 4957 | " or array element" |
| 4958 | ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4959 | break; |
| 4960 | } |
| 4961 | } |
| 4962 | // Fallthrough. |
| 4963 | case BC_LEX_OP_POWER: |
| 4964 | case BC_LEX_OP_MULTIPLY: |
| 4965 | case BC_LEX_OP_DIVIDE: |
| 4966 | case BC_LEX_OP_MODULUS: |
| 4967 | case BC_LEX_OP_PLUS: |
| 4968 | case BC_LEX_OP_REL_EQ: |
| 4969 | case BC_LEX_OP_REL_LE: |
| 4970 | case BC_LEX_OP_REL_GE: |
| 4971 | case BC_LEX_OP_REL_NE: |
| 4972 | case BC_LEX_OP_REL_LT: |
| 4973 | case BC_LEX_OP_REL_GT: |
| 4974 | case BC_LEX_OP_BOOL_NOT: |
| 4975 | case BC_LEX_OP_BOOL_OR: |
| 4976 | case BC_LEX_OP_BOOL_AND: |
| 4977 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4978 | if (((t == BC_LEX_OP_BOOL_NOT) != bin_last) |
| 4979 | || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT) |
| 4980 | ) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4981 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4982 | } |
| 4983 | |
| 4984 | nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT; |
| 4985 | prev = BC_PARSE_TOKEN_INST(t); |
| 4986 | s = bc_parse_operator(p, t, ops_bgn, &nexprs, true); |
| 4987 | rprn = get_token = false; |
| 4988 | bin_last = t != BC_LEX_OP_BOOL_NOT; |
| 4989 | |
| 4990 | break; |
| 4991 | } |
| 4992 | |
| 4993 | case BC_LEX_LPAREN: |
| 4994 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4995 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4996 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4997 | ++nparens; |
| 4998 | paren_expr = rprn = bin_last = false; |
| 4999 | get_token = true; |
| 5000 | bc_vec_push(&p->ops, &t); |
| 5001 | |
| 5002 | break; |
| 5003 | } |
| 5004 | |
| 5005 | case BC_LEX_RPAREN: |
| 5006 | { |
| 5007 | if (bin_last || prev == BC_INST_BOOL_NOT) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5008 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5009 | |
| 5010 | if (nparens == 0) { |
| 5011 | s = BC_STATUS_SUCCESS; |
| 5012 | done = true; |
| 5013 | get_token = false; |
| 5014 | break; |
| 5015 | } |
| 5016 | else if (!paren_expr) |
| 5017 | return BC_STATUS_PARSE_EMPTY_EXP; |
| 5018 | |
| 5019 | --nparens; |
| 5020 | paren_expr = rprn = true; |
| 5021 | get_token = bin_last = false; |
| 5022 | |
| 5023 | s = bc_parse_rightParen(p, ops_bgn, &nexprs); |
| 5024 | |
| 5025 | break; |
| 5026 | } |
| 5027 | |
| 5028 | case BC_LEX_NAME: |
| 5029 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5030 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5031 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5032 | paren_expr = true; |
| 5033 | rprn = get_token = bin_last = false; |
| 5034 | s = bc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL); |
| 5035 | ++nexprs; |
| 5036 | |
| 5037 | break; |
| 5038 | } |
| 5039 | |
| 5040 | case BC_LEX_NUMBER: |
| 5041 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5042 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5043 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5044 | bc_parse_number(p, &prev, &nexprs); |
| 5045 | paren_expr = get_token = true; |
| 5046 | rprn = bin_last = false; |
| 5047 | |
| 5048 | break; |
| 5049 | } |
| 5050 | |
| 5051 | case BC_LEX_KEY_IBASE: |
| 5052 | case BC_LEX_KEY_LAST: |
| 5053 | case BC_LEX_KEY_OBASE: |
| 5054 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5055 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5056 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5057 | prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE); |
| 5058 | bc_parse_push(p, (char) prev); |
| 5059 | |
| 5060 | paren_expr = get_token = true; |
| 5061 | rprn = bin_last = false; |
| 5062 | ++nexprs; |
| 5063 | |
| 5064 | break; |
| 5065 | } |
| 5066 | |
| 5067 | case BC_LEX_KEY_LENGTH: |
| 5068 | case BC_LEX_KEY_SQRT: |
| 5069 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5070 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5071 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5072 | s = bc_parse_builtin(p, t, flags, &prev); |
| 5073 | paren_expr = true; |
| 5074 | rprn = get_token = bin_last = false; |
| 5075 | ++nexprs; |
| 5076 | |
| 5077 | break; |
| 5078 | } |
| 5079 | |
| 5080 | case BC_LEX_KEY_READ: |
| 5081 | { |
| 5082 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5083 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5084 | else if (flags & BC_PARSE_NOREAD) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5085 | s = bc_error_nested_read_call(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5086 | else |
| 5087 | s = bc_parse_read(p); |
| 5088 | |
| 5089 | paren_expr = true; |
| 5090 | rprn = get_token = bin_last = false; |
| 5091 | ++nexprs; |
| 5092 | prev = BC_INST_READ; |
| 5093 | |
| 5094 | break; |
| 5095 | } |
| 5096 | |
| 5097 | case BC_LEX_KEY_SCALE: |
| 5098 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5099 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5100 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5101 | s = bc_parse_scale(p, &prev, flags); |
| 5102 | paren_expr = true; |
| 5103 | rprn = get_token = bin_last = false; |
| 5104 | ++nexprs; |
| 5105 | prev = BC_INST_SCALE; |
| 5106 | |
| 5107 | break; |
| 5108 | } |
| 5109 | |
| 5110 | default: |
| 5111 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5112 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5113 | break; |
| 5114 | } |
| 5115 | } |
| 5116 | |
| 5117 | if (!s && get_token) s = bc_lex_next(&p->l); |
| 5118 | } |
| 5119 | |
| 5120 | if (s) return s; |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 5121 | if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5122 | |
| 5123 | while (p->ops.len > ops_bgn) { |
| 5124 | |
| 5125 | top = BC_PARSE_TOP_OP(p); |
| 5126 | assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN; |
| 5127 | |
| 5128 | if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5129 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5130 | |
| 5131 | bc_parse_push(p, BC_PARSE_TOKEN_INST(top)); |
| 5132 | |
| 5133 | nexprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG; |
| 5134 | bc_vec_pop(&p->ops); |
| 5135 | } |
| 5136 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5137 | if (prev == BC_INST_BOOL_NOT || nexprs != 1) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5138 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5139 | |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 5140 | // next is BcParseNext, byte array of up to 4 BC_LEX's, packed into 32-bit word |
| 5141 | for (;;) { |
| 5142 | if (t == (next & 0x7f)) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5143 | goto ok; |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 5144 | if (next & 0x80) // last element? |
| 5145 | break; |
| 5146 | next >>= 8; |
| 5147 | } |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5148 | return bc_error_bad_expression(); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5149 | ok: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5150 | |
| 5151 | if (!(flags & BC_PARSE_REL) && nrelops) { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 5152 | s = bc_POSIX_does_not_allow("comparison operators outside if or loops"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5153 | if (s) return s; |
| 5154 | } |
| 5155 | else if ((flags & BC_PARSE_REL) && nrelops > 1) { |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 5156 | s = bc_POSIX_requires("exactly one comparison operator per condition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5157 | if (s) return s; |
| 5158 | } |
| 5159 | |
| 5160 | if (flags & BC_PARSE_PRINT) { |
| 5161 | if (paren_first || !assign) bc_parse_push(p, BC_INST_PRINT); |
| 5162 | bc_parse_push(p, BC_INST_POP); |
| 5163 | } |
| 5164 | |
| 5165 | return s; |
| 5166 | } |
| 5167 | |
Denys Vlasenko | 050b0fe | 2018-12-05 22:40:44 +0100 | [diff] [blame] | 5168 | static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next) |
| 5169 | { |
| 5170 | BcStatus s; |
| 5171 | |
| 5172 | s = bc_parse_expr_empty_ok(p, flags, next); |
| 5173 | if (s == BC_STATUS_PARSE_EMPTY_EXP) |
| 5174 | return bc_error("empty expression"); |
| 5175 | return s; |
| 5176 | } |
| 5177 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5178 | static void bc_parse_init(BcParse *p, size_t func) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5179 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5180 | bc_parse_create(p, func, bc_parse_parse, bc_lex_token); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5181 | } |
| 5182 | |
| 5183 | static BcStatus bc_parse_expression(BcParse *p, uint8_t flags) |
| 5184 | { |
| 5185 | return bc_parse_expr(p, flags, bc_parse_next_read); |
| 5186 | } |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 5187 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5188 | #endif // ENABLE_BC |
| 5189 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5190 | #if ENABLE_DC |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 5191 | |
| 5192 | #define DC_PARSE_BUF_LEN ((int) (sizeof(uint32_t) * CHAR_BIT)) |
| 5193 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5194 | static BcStatus dc_parse_register(BcParse *p) |
| 5195 | { |
| 5196 | BcStatus s; |
| 5197 | char *name; |
| 5198 | |
| 5199 | s = bc_lex_next(&p->l); |
| 5200 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5201 | if (p->l.t.t != BC_LEX_NAME) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5202 | |
| 5203 | name = xstrdup(p->l.t.v.v); |
| 5204 | bc_parse_pushName(p, name); |
| 5205 | |
| 5206 | return s; |
| 5207 | } |
| 5208 | |
| 5209 | static BcStatus dc_parse_string(BcParse *p) |
| 5210 | { |
| 5211 | char *str, *name, b[DC_PARSE_BUF_LEN + 1]; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5212 | size_t idx, len = G.prog.strs.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5213 | |
| 5214 | sprintf(b, "%0*zu", DC_PARSE_BUF_LEN, len); |
| 5215 | name = xstrdup(b); |
| 5216 | |
| 5217 | str = xstrdup(p->l.t.v.v); |
| 5218 | bc_parse_push(p, BC_INST_STR); |
| 5219 | bc_parse_pushIndex(p, len); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5220 | bc_vec_push(&G.prog.strs, &str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5221 | bc_parse_addFunc(p, name, &idx); |
| 5222 | |
| 5223 | return bc_lex_next(&p->l); |
| 5224 | } |
| 5225 | |
| 5226 | static BcStatus dc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store) |
| 5227 | { |
| 5228 | BcStatus s; |
| 5229 | |
| 5230 | bc_parse_push(p, inst); |
| 5231 | if (name) { |
| 5232 | s = dc_parse_register(p); |
| 5233 | if (s) return s; |
| 5234 | } |
| 5235 | |
| 5236 | if (store) { |
| 5237 | bc_parse_push(p, BC_INST_SWAP); |
| 5238 | bc_parse_push(p, BC_INST_ASSIGN); |
| 5239 | bc_parse_push(p, BC_INST_POP); |
| 5240 | } |
| 5241 | |
| 5242 | return bc_lex_next(&p->l); |
| 5243 | } |
| 5244 | |
| 5245 | static BcStatus dc_parse_cond(BcParse *p, uint8_t inst) |
| 5246 | { |
| 5247 | BcStatus s; |
| 5248 | |
| 5249 | bc_parse_push(p, inst); |
| 5250 | bc_parse_push(p, BC_INST_EXEC_COND); |
| 5251 | |
| 5252 | s = dc_parse_register(p); |
| 5253 | if (s) return s; |
| 5254 | |
| 5255 | s = bc_lex_next(&p->l); |
| 5256 | if (s) return s; |
| 5257 | |
| 5258 | if (p->l.t.t == BC_LEX_ELSE) { |
| 5259 | s = dc_parse_register(p); |
| 5260 | if (s) return s; |
| 5261 | s = bc_lex_next(&p->l); |
| 5262 | } |
| 5263 | else |
| 5264 | bc_parse_push(p, BC_PARSE_STREND); |
| 5265 | |
| 5266 | return s; |
| 5267 | } |
| 5268 | |
| 5269 | static BcStatus dc_parse_token(BcParse *p, BcLexType t, uint8_t flags) |
| 5270 | { |
| 5271 | BcStatus s = BC_STATUS_SUCCESS; |
| 5272 | BcInst prev; |
| 5273 | uint8_t inst; |
| 5274 | bool assign, get_token = false; |
| 5275 | |
| 5276 | switch (t) { |
| 5277 | |
| 5278 | case BC_LEX_OP_REL_EQ: |
| 5279 | case BC_LEX_OP_REL_LE: |
| 5280 | case BC_LEX_OP_REL_GE: |
| 5281 | case BC_LEX_OP_REL_NE: |
| 5282 | case BC_LEX_OP_REL_LT: |
| 5283 | case BC_LEX_OP_REL_GT: |
| 5284 | { |
| 5285 | s = dc_parse_cond(p, t - BC_LEX_OP_REL_EQ + BC_INST_REL_EQ); |
| 5286 | break; |
| 5287 | } |
| 5288 | |
| 5289 | case BC_LEX_SCOLON: |
| 5290 | case BC_LEX_COLON: |
| 5291 | { |
| 5292 | s = dc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON); |
| 5293 | break; |
| 5294 | } |
| 5295 | |
| 5296 | case BC_LEX_STR: |
| 5297 | { |
| 5298 | s = dc_parse_string(p); |
| 5299 | break; |
| 5300 | } |
| 5301 | |
| 5302 | case BC_LEX_NEG: |
| 5303 | case BC_LEX_NUMBER: |
| 5304 | { |
| 5305 | if (t == BC_LEX_NEG) { |
| 5306 | s = bc_lex_next(&p->l); |
| 5307 | if (s) return s; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5308 | if (p->l.t.t != BC_LEX_NUMBER) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5309 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5310 | } |
| 5311 | |
| 5312 | bc_parse_number(p, &prev, &p->nbraces); |
| 5313 | |
| 5314 | if (t == BC_LEX_NEG) bc_parse_push(p, BC_INST_NEG); |
| 5315 | get_token = true; |
| 5316 | |
| 5317 | break; |
| 5318 | } |
| 5319 | |
| 5320 | case BC_LEX_KEY_READ: |
| 5321 | { |
| 5322 | if (flags & BC_PARSE_NOREAD) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5323 | s = bc_error_nested_read_call(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5324 | else |
| 5325 | bc_parse_push(p, BC_INST_READ); |
| 5326 | get_token = true; |
| 5327 | break; |
| 5328 | } |
| 5329 | |
| 5330 | case BC_LEX_OP_ASSIGN: |
| 5331 | case BC_LEX_STORE_PUSH: |
| 5332 | { |
| 5333 | assign = t == BC_LEX_OP_ASSIGN; |
| 5334 | inst = assign ? BC_INST_VAR : BC_INST_PUSH_TO_VAR; |
| 5335 | s = dc_parse_mem(p, inst, true, assign); |
| 5336 | break; |
| 5337 | } |
| 5338 | |
| 5339 | case BC_LEX_LOAD: |
| 5340 | case BC_LEX_LOAD_POP: |
| 5341 | { |
| 5342 | inst = t == BC_LEX_LOAD_POP ? BC_INST_PUSH_VAR : BC_INST_LOAD; |
| 5343 | s = dc_parse_mem(p, inst, true, false); |
| 5344 | break; |
| 5345 | } |
| 5346 | |
| 5347 | case BC_LEX_STORE_IBASE: |
| 5348 | case BC_LEX_STORE_SCALE: |
| 5349 | case BC_LEX_STORE_OBASE: |
| 5350 | { |
| 5351 | inst = t - BC_LEX_STORE_IBASE + BC_INST_IBASE; |
| 5352 | s = dc_parse_mem(p, inst, false, true); |
| 5353 | break; |
| 5354 | } |
| 5355 | |
| 5356 | default: |
| 5357 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5358 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5359 | get_token = true; |
| 5360 | break; |
| 5361 | } |
| 5362 | } |
| 5363 | |
| 5364 | if (!s && get_token) s = bc_lex_next(&p->l); |
| 5365 | |
| 5366 | return s; |
| 5367 | } |
| 5368 | |
| 5369 | static BcStatus dc_parse_expr(BcParse *p, uint8_t flags) |
| 5370 | { |
| 5371 | BcStatus s = BC_STATUS_SUCCESS; |
| 5372 | BcInst inst; |
| 5373 | BcLexType t; |
| 5374 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5375 | if (flags & BC_PARSE_NOCALL) p->nbraces = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5376 | |
| 5377 | for (t = p->l.t.t; !s && t != BC_LEX_EOF; t = p->l.t.t) { |
| 5378 | |
| 5379 | inst = dc_parse_insts[t]; |
| 5380 | |
| 5381 | if (inst != BC_INST_INVALID) { |
| 5382 | bc_parse_push(p, inst); |
| 5383 | s = bc_lex_next(&p->l); |
| 5384 | } |
| 5385 | else |
| 5386 | s = dc_parse_token(p, t, flags); |
| 5387 | } |
| 5388 | |
| 5389 | if (!s && p->l.t.t == BC_LEX_EOF && (flags & BC_PARSE_NOCALL)) |
| 5390 | bc_parse_push(p, BC_INST_POP_EXEC); |
| 5391 | |
| 5392 | return s; |
| 5393 | } |
| 5394 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 5395 | static FAST_FUNC BcStatus dc_parse_parse(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5396 | { |
| 5397 | BcStatus s; |
| 5398 | |
| 5399 | if (p->l.t.t == BC_LEX_EOF) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5400 | s = bc_error("end of file"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5401 | else |
| 5402 | s = dc_parse_expr(p, 0); |
| 5403 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 5404 | if (s || G_interrupt) { |
| 5405 | bc_parse_reset(p); |
| 5406 | s = BC_STATUS_FAILURE; |
| 5407 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5408 | |
| 5409 | return s; |
| 5410 | } |
| 5411 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5412 | static void dc_parse_init(BcParse *p, size_t func) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5413 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5414 | bc_parse_create(p, func, dc_parse_parse, dc_lex_token); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5415 | } |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 5416 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5417 | #endif // ENABLE_DC |
| 5418 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5419 | static void common_parse_init(BcParse *p, size_t func) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5420 | { |
| 5421 | if (IS_BC) { |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 5422 | IF_BC(bc_parse_init(p, func);) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5423 | } else { |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 5424 | IF_DC(dc_parse_init(p, func);) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5425 | } |
| 5426 | } |
| 5427 | |
| 5428 | static BcStatus common_parse_expr(BcParse *p, uint8_t flags) |
| 5429 | { |
| 5430 | if (IS_BC) { |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 5431 | IF_BC(return bc_parse_expression(p, flags);) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5432 | } else { |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 5433 | IF_DC(return dc_parse_expr(p, flags);) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5434 | } |
| 5435 | } |
| 5436 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5437 | static BcVec* bc_program_search(char *id, bool var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5438 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5439 | BcId e, *ptr; |
| 5440 | BcVec *v, *map; |
| 5441 | size_t i; |
| 5442 | BcResultData data; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 5443 | int new; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5444 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5445 | v = var ? &G.prog.vars : &G.prog.arrs; |
| 5446 | map = var ? &G.prog.var_map : &G.prog.arr_map; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5447 | |
| 5448 | e.name = id; |
| 5449 | e.idx = v->len; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 5450 | new = bc_map_insert(map, &e, &i); // 1 if insertion was successful |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5451 | |
| 5452 | if (new) { |
| 5453 | bc_array_init(&data.v, var); |
| 5454 | bc_vec_push(v, &data.v); |
| 5455 | } |
| 5456 | |
| 5457 | ptr = bc_vec_item(map, i); |
| 5458 | if (new) ptr->name = xstrdup(e.name); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5459 | return bc_vec_item(v, ptr->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5460 | } |
| 5461 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5462 | static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5463 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5464 | switch (r->t) { |
| 5465 | |
| 5466 | case BC_RESULT_STR: |
| 5467 | case BC_RESULT_TEMP: |
| 5468 | case BC_RESULT_IBASE: |
| 5469 | case BC_RESULT_SCALE: |
| 5470 | case BC_RESULT_OBASE: |
| 5471 | { |
| 5472 | *num = &r->d.n; |
| 5473 | break; |
| 5474 | } |
| 5475 | |
| 5476 | case BC_RESULT_CONSTANT: |
| 5477 | { |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5478 | BcStatus s; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5479 | char **str = bc_vec_item(&G.prog.consts, r->d.id.idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5480 | size_t base_t, len = strlen(*str); |
| 5481 | BcNum *base; |
| 5482 | |
| 5483 | bc_num_init(&r->d.n, len); |
| 5484 | |
| 5485 | hex = hex && len == 1; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5486 | base = hex ? &G.prog.hexb : &G.prog.ib; |
| 5487 | base_t = hex ? BC_NUM_MAX_IBASE : G.prog.ib_t; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5488 | s = zbc_num_parse(&r->d.n, *str, base, base_t); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5489 | |
| 5490 | if (s) { |
| 5491 | bc_num_free(&r->d.n); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5492 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5493 | } |
| 5494 | |
| 5495 | *num = &r->d.n; |
| 5496 | r->t = BC_RESULT_TEMP; |
| 5497 | |
| 5498 | break; |
| 5499 | } |
| 5500 | |
| 5501 | case BC_RESULT_VAR: |
| 5502 | case BC_RESULT_ARRAY: |
| 5503 | case BC_RESULT_ARRAY_ELEM: |
| 5504 | { |
| 5505 | BcVec *v; |
| 5506 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5507 | 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] | 5508 | |
| 5509 | if (r->t == BC_RESULT_ARRAY_ELEM) { |
| 5510 | v = bc_vec_top(v); |
| 5511 | if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1); |
| 5512 | *num = bc_vec_item(v, r->d.id.idx); |
| 5513 | } |
| 5514 | else |
| 5515 | *num = bc_vec_top(v); |
| 5516 | |
| 5517 | break; |
| 5518 | } |
| 5519 | |
| 5520 | case BC_RESULT_LAST: |
| 5521 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5522 | *num = &G.prog.last; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5523 | break; |
| 5524 | } |
| 5525 | |
| 5526 | case BC_RESULT_ONE: |
| 5527 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5528 | *num = &G.prog.one; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5529 | break; |
| 5530 | } |
| 5531 | } |
| 5532 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5533 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5534 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5535 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5536 | # define zbc_program_num(...) (zbc_program_num(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5537 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5538 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5539 | static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5540 | BcResult **r, BcNum **rn, bool assign) |
| 5541 | { |
| 5542 | BcStatus s; |
| 5543 | bool hex; |
| 5544 | BcResultType lt, rt; |
| 5545 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5546 | if (!BC_PROG_STACK(&G.prog.results, 2)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5547 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5548 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5549 | *r = bc_vec_item_rev(&G.prog.results, 0); |
| 5550 | *l = bc_vec_item_rev(&G.prog.results, 1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5551 | |
| 5552 | lt = (*l)->t; |
| 5553 | rt = (*r)->t; |
| 5554 | hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE); |
| 5555 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5556 | s = zbc_program_num(*l, ln, false); |
| 5557 | if (s) RETURN_STATUS(s); |
| 5558 | s = zbc_program_num(*r, rn, hex); |
| 5559 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5560 | |
| 5561 | // We run this again under these conditions in case any vector has been |
| 5562 | // reallocated out from under the BcNums or arrays we had. |
| 5563 | if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5564 | s = zbc_program_num(*l, ln, false); |
| 5565 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5566 | } |
| 5567 | |
| 5568 | if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5569 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5570 | if (!assign && !BC_PROG_NUM((*r), (*ln))) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5571 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5572 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5573 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5574 | } |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 5575 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5576 | # define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 5577 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5578 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5579 | static void bc_program_binOpRetire(BcResult *r) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5580 | { |
| 5581 | r->t = BC_RESULT_TEMP; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5582 | bc_vec_pop(&G.prog.results); |
| 5583 | bc_vec_pop(&G.prog.results); |
| 5584 | bc_vec_push(&G.prog.results, r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5585 | } |
| 5586 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5587 | static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5588 | { |
| 5589 | BcStatus s; |
| 5590 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5591 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5592 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5593 | *r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5594 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5595 | s = zbc_program_num(*r, n, false); |
| 5596 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5597 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5598 | if (!BC_PROG_NUM((*r), (*n))) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5599 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5600 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5601 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5602 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5603 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5604 | # define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5605 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5606 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5607 | static void bc_program_retire(BcResult *r, BcResultType t) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5608 | { |
| 5609 | r->t = t; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5610 | bc_vec_pop(&G.prog.results); |
| 5611 | bc_vec_push(&G.prog.results, r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5612 | } |
| 5613 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5614 | static BcStatus bc_program_op(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5615 | { |
| 5616 | BcStatus s; |
| 5617 | BcResult *opd1, *opd2, res; |
| 5618 | BcNum *n1, *n2 = NULL; |
| 5619 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5620 | s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5621 | if (s) return s; |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5622 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5623 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5624 | s = bc_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] | 5625 | if (s) goto err; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5626 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5627 | |
| 5628 | return s; |
| 5629 | |
| 5630 | err: |
| 5631 | bc_num_free(&res.d.n); |
| 5632 | return s; |
| 5633 | } |
| 5634 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 5635 | static BcStatus bc_program_read(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5636 | { |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5637 | const char *sv_file; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5638 | BcStatus s; |
| 5639 | BcParse parse; |
| 5640 | BcVec buf; |
| 5641 | BcInstPtr ip; |
| 5642 | size_t i; |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 5643 | BcFunc *f = bc_program_func(BC_PROG_READ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5644 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5645 | for (i = 0; i < G.prog.stack.len; ++i) { |
| 5646 | BcInstPtr *ip_ptr = bc_vec_item(&G.prog.stack, i); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5647 | if (ip_ptr->func == BC_PROG_READ) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5648 | return bc_error_nested_read_call(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5649 | } |
| 5650 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 5651 | bc_vec_pop_all(&f->code); |
| 5652 | bc_char_vec_init(&buf); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5653 | |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5654 | sv_file = G.prog.file; |
| 5655 | G.prog.file = NULL; |
| 5656 | |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 5657 | s = bc_read_line(&buf); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5658 | if (s) goto io_err; |
| 5659 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5660 | common_parse_init(&parse, BC_PROG_READ); |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5661 | bc_lex_file(&parse.l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5662 | |
| 5663 | s = bc_parse_text(&parse, buf.v); |
| 5664 | if (s) goto exec_err; |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5665 | s = common_parse_expr(&parse, BC_PARSE_NOREAD); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5666 | if (s) goto exec_err; |
| 5667 | |
| 5668 | 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] | 5669 | s = bc_error("bad read() expression"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5670 | goto exec_err; |
| 5671 | } |
| 5672 | |
| 5673 | ip.func = BC_PROG_READ; |
| 5674 | ip.idx = 0; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5675 | ip.len = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5676 | |
| 5677 | // Update this pointer, just in case. |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 5678 | f = bc_program_func(BC_PROG_READ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5679 | |
| 5680 | bc_vec_pushByte(&f->code, BC_INST_POP_EXEC); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5681 | bc_vec_push(&G.prog.stack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5682 | |
| 5683 | exec_err: |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5684 | G.prog.file = sv_file; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5685 | bc_parse_free(&parse); |
| 5686 | io_err: |
| 5687 | bc_vec_free(&buf); |
| 5688 | return s; |
| 5689 | } |
| 5690 | |
| 5691 | static size_t bc_program_index(char *code, size_t *bgn) |
| 5692 | { |
| 5693 | char amt = code[(*bgn)++], i = 0; |
| 5694 | size_t res = 0; |
| 5695 | |
| 5696 | for (; i < amt; ++i, ++(*bgn)) |
| 5697 | res |= (((size_t)((int) code[*bgn]) & UCHAR_MAX) << (i * CHAR_BIT)); |
| 5698 | |
| 5699 | return res; |
| 5700 | } |
| 5701 | |
| 5702 | static char *bc_program_name(char *code, size_t *bgn) |
| 5703 | { |
| 5704 | size_t i; |
| 5705 | char c, *s, *str = code + *bgn, *ptr = strchr(str, BC_PARSE_STREND); |
| 5706 | |
| 5707 | s = xmalloc(ptr - str + 1); |
| 5708 | c = code[(*bgn)++]; |
| 5709 | |
| 5710 | for (i = 0; c != 0 && c != BC_PARSE_STREND; c = code[(*bgn)++], ++i) |
| 5711 | s[i] = c; |
| 5712 | |
| 5713 | s[i] = '\0'; |
| 5714 | |
| 5715 | return s; |
| 5716 | } |
| 5717 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 5718 | static void bc_program_printString(const char *str) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5719 | { |
| 5720 | size_t i, len = strlen(str); |
| 5721 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5722 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5723 | if (len == 0) { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5724 | bb_putchar('\0'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5725 | return; |
| 5726 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5727 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5728 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 5729 | for (i = 0; i < len; ++i, ++G.prog.nchars) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5730 | |
| 5731 | int c = str[i]; |
| 5732 | |
| 5733 | if (c != '\\' || i == len - 1) |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5734 | bb_putchar(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5735 | else { |
| 5736 | |
| 5737 | c = str[++i]; |
| 5738 | |
| 5739 | switch (c) { |
| 5740 | |
| 5741 | case 'a': |
| 5742 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5743 | bb_putchar('\a'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5744 | break; |
| 5745 | } |
| 5746 | |
| 5747 | case 'b': |
| 5748 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5749 | bb_putchar('\b'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5750 | break; |
| 5751 | } |
| 5752 | |
| 5753 | case '\\': |
| 5754 | case 'e': |
| 5755 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5756 | bb_putchar('\\'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5757 | break; |
| 5758 | } |
| 5759 | |
| 5760 | case 'f': |
| 5761 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5762 | bb_putchar('\f'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5763 | break; |
| 5764 | } |
| 5765 | |
| 5766 | case 'n': |
| 5767 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5768 | bb_putchar('\n'); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 5769 | G.prog.nchars = SIZE_MAX; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5770 | break; |
| 5771 | } |
| 5772 | |
| 5773 | case 'r': |
| 5774 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5775 | bb_putchar('\r'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5776 | break; |
| 5777 | } |
| 5778 | |
| 5779 | case 'q': |
| 5780 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5781 | bb_putchar('"'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5782 | break; |
| 5783 | } |
| 5784 | |
| 5785 | case 't': |
| 5786 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5787 | bb_putchar('\t'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5788 | break; |
| 5789 | } |
| 5790 | |
| 5791 | default: |
| 5792 | { |
| 5793 | // Just print the backslash and following character. |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5794 | bb_putchar('\\'); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 5795 | ++G.prog.nchars; |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5796 | bb_putchar(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5797 | break; |
| 5798 | } |
| 5799 | } |
| 5800 | } |
| 5801 | } |
| 5802 | } |
| 5803 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5804 | static BC_STATUS zbc_program_print(char inst, size_t idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5805 | { |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5806 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5807 | BcResult *r; |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5808 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5809 | bool pop = inst != BC_INST_PRINT; |
| 5810 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5811 | if (!BC_PROG_STACK(&G.prog.results, idx + 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5812 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5813 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5814 | r = bc_vec_item_rev(&G.prog.results, idx); |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5815 | num = NULL; // is this NULL necessary? |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5816 | s = zbc_program_num(r, &num, false); |
| 5817 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5818 | |
| 5819 | if (BC_PROG_NUM(r, num)) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5820 | s = zbc_num_print(num, !pop); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5821 | if (!s) bc_num_copy(&G.prog.last, num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5822 | } |
| 5823 | else { |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5824 | char *str; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5825 | |
| 5826 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx; |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 5827 | str = *bc_program_str(idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5828 | |
| 5829 | if (inst == BC_INST_PRINT_STR) { |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5830 | for (;;) { |
| 5831 | char c = *str++; |
| 5832 | if (c == '\0') break; |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5833 | bb_putchar(c); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5834 | ++G.prog.nchars; |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5835 | if (c == '\n') G.prog.nchars = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5836 | } |
| 5837 | } |
| 5838 | else { |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 5839 | bc_program_printString(str); |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5840 | if (inst == BC_INST_PRINT) bb_putchar('\n'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5841 | } |
| 5842 | } |
| 5843 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5844 | if (!s && pop) bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5845 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5846 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5847 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5848 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5849 | # define zbc_program_print(...) (zbc_program_print(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5850 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5851 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5852 | static BC_STATUS zbc_program_negate(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5853 | { |
| 5854 | BcStatus s; |
| 5855 | BcResult res, *ptr; |
| 5856 | BcNum *num = NULL; |
| 5857 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5858 | s = zbc_program_prep(&ptr, &num); |
| 5859 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5860 | |
| 5861 | bc_num_init(&res.d.n, num->len); |
| 5862 | bc_num_copy(&res.d.n, num); |
| 5863 | if (res.d.n.len) res.d.n.neg = !res.d.n.neg; |
| 5864 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5865 | bc_program_retire(&res, BC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5866 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5867 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5868 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5869 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5870 | # define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5871 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5872 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5873 | static BcStatus bc_program_logical(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5874 | { |
| 5875 | BcStatus s; |
| 5876 | BcResult *opd1, *opd2, res; |
| 5877 | BcNum *n1, *n2; |
| 5878 | bool cond = 0; |
| 5879 | ssize_t cmp; |
| 5880 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5881 | s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5882 | if (s) return s; |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5883 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5884 | |
| 5885 | if (inst == BC_INST_BOOL_AND) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5886 | 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] | 5887 | else if (inst == BC_INST_BOOL_OR) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5888 | 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] | 5889 | else { |
| 5890 | |
| 5891 | cmp = bc_num_cmp(n1, n2); |
| 5892 | |
| 5893 | switch (inst) { |
| 5894 | |
| 5895 | case BC_INST_REL_EQ: |
| 5896 | { |
| 5897 | cond = cmp == 0; |
| 5898 | break; |
| 5899 | } |
| 5900 | |
| 5901 | case BC_INST_REL_LE: |
| 5902 | { |
| 5903 | cond = cmp <= 0; |
| 5904 | break; |
| 5905 | } |
| 5906 | |
| 5907 | case BC_INST_REL_GE: |
| 5908 | { |
| 5909 | cond = cmp >= 0; |
| 5910 | break; |
| 5911 | } |
| 5912 | |
| 5913 | case BC_INST_REL_NE: |
| 5914 | { |
| 5915 | cond = cmp != 0; |
| 5916 | break; |
| 5917 | } |
| 5918 | |
| 5919 | case BC_INST_REL_LT: |
| 5920 | { |
| 5921 | cond = cmp < 0; |
| 5922 | break; |
| 5923 | } |
| 5924 | |
| 5925 | case BC_INST_REL_GT: |
| 5926 | { |
| 5927 | cond = cmp > 0; |
| 5928 | break; |
| 5929 | } |
| 5930 | } |
| 5931 | } |
| 5932 | |
| 5933 | (cond ? bc_num_one : bc_num_zero)(&res.d.n); |
| 5934 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5935 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5936 | |
| 5937 | return s; |
| 5938 | } |
| 5939 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5940 | #if ENABLE_DC |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5941 | static BC_STATUS zbc_program_assignStr(BcResult *r, BcVec *v, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5942 | bool push) |
| 5943 | { |
| 5944 | BcNum n2; |
| 5945 | BcResult res; |
| 5946 | |
| 5947 | memset(&n2, 0, sizeof(BcNum)); |
| 5948 | n2.rdx = res.d.id.idx = r->d.id.idx; |
| 5949 | res.t = BC_RESULT_STR; |
| 5950 | |
| 5951 | if (!push) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5952 | if (!BC_PROG_STACK(&G.prog.results, 2)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5953 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5954 | bc_vec_pop(v); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5955 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5956 | } |
| 5957 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5958 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5959 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5960 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5961 | bc_vec_push(v, &n2); |
| 5962 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5963 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5964 | } |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 5965 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5966 | # define zbc_program_assignStr(...) (zbc_program_assignStr(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 5967 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5968 | #endif // ENABLE_DC |
| 5969 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5970 | static BC_STATUS zbc_program_copyToVar(char *name, bool var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5971 | { |
| 5972 | BcStatus s; |
| 5973 | BcResult *ptr, r; |
| 5974 | BcVec *v; |
| 5975 | BcNum *n; |
| 5976 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5977 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5978 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5979 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5980 | ptr = bc_vec_top(&G.prog.results); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5981 | if ((ptr->t == BC_RESULT_ARRAY) != !var) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5982 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5983 | v = bc_program_search(name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5984 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5985 | #if ENABLE_DC |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5986 | if (ptr->t == BC_RESULT_STR && !var) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5987 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
| 5988 | if (ptr->t == BC_RESULT_STR) |
| 5989 | RETURN_STATUS(zbc_program_assignStr(ptr, v, true)); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5990 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5991 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 5992 | s = zbc_program_num(ptr, &n, false); |
| 5993 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5994 | |
| 5995 | // Do this once more to make sure that pointers were not invalidated. |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5996 | v = bc_program_search(name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5997 | |
| 5998 | if (var) { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5999 | bc_num_init_DEF_SIZE(&r.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6000 | bc_num_copy(&r.d.n, n); |
| 6001 | } |
| 6002 | else { |
| 6003 | bc_array_init(&r.d.v, true); |
| 6004 | bc_array_copy(&r.d.v, (BcVec *) n); |
| 6005 | } |
| 6006 | |
| 6007 | bc_vec_push(v, &r.d); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6008 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6009 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6010 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6011 | } |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6012 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6013 | # define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6014 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6015 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6016 | static BcStatus bc_program_assign(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6017 | { |
| 6018 | BcStatus s; |
| 6019 | BcResult *left, *right, res; |
| 6020 | BcNum *l = NULL, *r = NULL; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6021 | bool assign = inst == BC_INST_ASSIGN, ib, sc; |
| 6022 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6023 | s = zbc_program_binOpPrep(&left, &l, &right, &r, assign); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6024 | if (s) return s; |
| 6025 | |
| 6026 | ib = left->t == BC_RESULT_IBASE; |
| 6027 | sc = left->t == BC_RESULT_SCALE; |
| 6028 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6029 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6030 | |
| 6031 | if (right->t == BC_RESULT_STR) { |
| 6032 | |
| 6033 | BcVec *v; |
| 6034 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6035 | if (left->t != BC_RESULT_VAR) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6036 | return bc_error_variable_is_wrong_type(); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6037 | v = bc_program_search(left->d.id.name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6038 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6039 | return zbc_program_assignStr(right, v, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6040 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6041 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6042 | |
| 6043 | if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6044 | return bc_error("bad assignment:" |
| 6045 | " left side must be scale," |
| 6046 | " ibase, obase, last, var," |
| 6047 | " or array element" |
| 6048 | ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6049 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6050 | #if ENABLE_BC |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6051 | if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero)) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6052 | return bc_error("divide by zero"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6053 | |
| 6054 | if (assign) |
| 6055 | bc_num_copy(l, r); |
| 6056 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6057 | s = bc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6058 | |
| 6059 | if (s) return s; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6060 | #else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6061 | bc_num_copy(l, r); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6062 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6063 | |
| 6064 | if (ib || sc || left->t == BC_RESULT_OBASE) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6065 | static const char *const msg[] = { |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 6066 | "bad ibase; must be [2,16]", //BC_RESULT_IBASE |
| 6067 | "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE |
| 6068 | NULL, //can't happen //BC_RESULT_LAST |
| 6069 | NULL, //can't happen //BC_RESULT_CONSTANT |
| 6070 | NULL, //can't happen //BC_RESULT_ONE |
| 6071 | "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6072 | }; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6073 | size_t *ptr; |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 6074 | unsigned long val, max; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6075 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6076 | s = zbc_num_ulong(l, &val); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6077 | if (s) |
| 6078 | return s; |
| 6079 | s = left->t - BC_RESULT_IBASE; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6080 | if (sc) { |
| 6081 | max = BC_MAX_SCALE; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6082 | ptr = &G.prog.scale; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6083 | } |
| 6084 | else { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6085 | if (val < BC_NUM_MIN_BASE) |
| 6086 | return bc_error(msg[s]); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6087 | max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6088 | ptr = ib ? &G.prog.ib_t : &G.prog.ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6089 | } |
| 6090 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6091 | if (val > max) |
| 6092 | return bc_error(msg[s]); |
| 6093 | if (!sc) |
| 6094 | bc_num_copy(ib ? &G.prog.ib : &G.prog.ob, l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6095 | |
| 6096 | *ptr = (size_t) val; |
| 6097 | s = BC_STATUS_SUCCESS; |
| 6098 | } |
| 6099 | |
| 6100 | bc_num_init(&res.d.n, l->len); |
| 6101 | bc_num_copy(&res.d.n, l); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6102 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6103 | |
| 6104 | return s; |
| 6105 | } |
| 6106 | |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6107 | #if !ENABLE_DC |
| 6108 | #define bc_program_pushVar(code, bgn, pop, copy) \ |
| 6109 | bc_program_pushVar(code, bgn) |
| 6110 | // for bc, 'pop' and 'copy' are always false |
| 6111 | #endif |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6112 | static BcStatus bc_program_pushVar(char *code, size_t *bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6113 | bool pop, bool copy) |
| 6114 | { |
| 6115 | BcStatus s = BC_STATUS_SUCCESS; |
| 6116 | BcResult r; |
| 6117 | char *name = bc_program_name(code, bgn); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6118 | |
| 6119 | r.t = BC_RESULT_VAR; |
| 6120 | r.d.id.name = name; |
| 6121 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6122 | #if ENABLE_DC |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6123 | { |
| 6124 | BcVec *v = bc_program_search(name, true); |
| 6125 | BcNum *num = bc_vec_top(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6126 | |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6127 | if (pop || copy) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6128 | |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6129 | if (!BC_PROG_STACK(v, 2 - copy)) { |
| 6130 | free(name); |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6131 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6132 | } |
| 6133 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6134 | free(name); |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6135 | name = NULL; |
| 6136 | |
| 6137 | if (!BC_PROG_STR(num)) { |
| 6138 | |
| 6139 | r.t = BC_RESULT_TEMP; |
| 6140 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6141 | bc_num_init_DEF_SIZE(&r.d.n); |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6142 | bc_num_copy(&r.d.n, num); |
| 6143 | } |
| 6144 | else { |
| 6145 | r.t = BC_RESULT_STR; |
| 6146 | r.d.id.idx = num->rdx; |
| 6147 | } |
| 6148 | |
| 6149 | if (!copy) bc_vec_pop(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6150 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6151 | } |
| 6152 | #endif // ENABLE_DC |
| 6153 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6154 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6155 | |
| 6156 | return s; |
| 6157 | } |
| 6158 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6159 | static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6160 | char inst) |
| 6161 | { |
| 6162 | BcStatus s = BC_STATUS_SUCCESS; |
| 6163 | BcResult r; |
| 6164 | BcNum *num; |
| 6165 | |
| 6166 | r.d.id.name = bc_program_name(code, bgn); |
| 6167 | |
| 6168 | if (inst == BC_INST_ARRAY) { |
| 6169 | r.t = BC_RESULT_ARRAY; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6170 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6171 | } |
| 6172 | else { |
| 6173 | |
| 6174 | BcResult *operand; |
| 6175 | unsigned long temp; |
| 6176 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6177 | s = zbc_program_prep(&operand, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6178 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6179 | s = zbc_num_ulong(num, &temp); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6180 | if (s) goto err; |
| 6181 | |
| 6182 | if (temp > BC_MAX_DIM) { |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 6183 | 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] | 6184 | goto err; |
| 6185 | } |
| 6186 | |
| 6187 | r.d.id.idx = (size_t) temp; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6188 | bc_program_retire(&r, BC_RESULT_ARRAY_ELEM); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6189 | } |
| 6190 | |
| 6191 | err: |
| 6192 | if (s) free(r.d.id.name); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6193 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6194 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6195 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6196 | # define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6197 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6198 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6199 | #if ENABLE_BC |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6200 | static BC_STATUS zbc_program_incdec(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6201 | { |
| 6202 | BcStatus s; |
| 6203 | BcResult *ptr, res, copy; |
| 6204 | BcNum *num = NULL; |
| 6205 | char inst2 = inst; |
| 6206 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6207 | s = zbc_program_prep(&ptr, &num); |
| 6208 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6209 | |
| 6210 | if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) { |
| 6211 | copy.t = BC_RESULT_TEMP; |
| 6212 | bc_num_init(©.d.n, num->len); |
| 6213 | bc_num_copy(©.d.n, num); |
| 6214 | } |
| 6215 | |
| 6216 | res.t = BC_RESULT_ONE; |
| 6217 | inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ? |
| 6218 | BC_INST_ASSIGN_PLUS : |
| 6219 | BC_INST_ASSIGN_MINUS; |
| 6220 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6221 | bc_vec_push(&G.prog.results, &res); |
| 6222 | bc_program_assign(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6223 | |
| 6224 | if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6225 | bc_vec_pop(&G.prog.results); |
| 6226 | bc_vec_push(&G.prog.results, ©); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6227 | } |
| 6228 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6229 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6230 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6231 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6232 | # define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6233 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6234 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6235 | static BC_STATUS zbc_program_call(char *code, size_t *idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6236 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6237 | BcInstPtr ip; |
| 6238 | size_t i, nparams = bc_program_index(code, idx); |
| 6239 | BcFunc *func; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6240 | BcId *a; |
| 6241 | BcResultData param; |
| 6242 | BcResult *arg; |
| 6243 | |
| 6244 | ip.idx = 0; |
| 6245 | ip.func = bc_program_index(code, idx); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6246 | func = bc_program_func(ip.func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6247 | |
Denys Vlasenko | 04a1c76 | 2018-12-03 21:10:57 +0100 | [diff] [blame] | 6248 | if (func->code.len == 0) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6249 | RETURN_STATUS(bc_error("undefined function")); |
Denys Vlasenko | 04a1c76 | 2018-12-03 21:10:57 +0100 | [diff] [blame] | 6250 | } |
| 6251 | if (nparams != func->nparams) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6252 | 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] | 6253 | } |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6254 | ip.len = G.prog.results.len - nparams; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6255 | |
| 6256 | for (i = 0; i < nparams; ++i) { |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6257 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6258 | |
| 6259 | a = bc_vec_item(&func->autos, nparams - 1 - i); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6260 | arg = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6261 | |
| 6262 | 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^] | 6263 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6264 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6265 | s = zbc_program_copyToVar(a->name, a->idx); |
| 6266 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6267 | } |
| 6268 | |
| 6269 | for (; i < func->autos.len; ++i) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6270 | BcVec *v; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6271 | |
| 6272 | a = bc_vec_item(&func->autos, i); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6273 | v = bc_program_search(a->name, a->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6274 | |
| 6275 | if (a->idx) { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6276 | bc_num_init_DEF_SIZE(¶m.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6277 | bc_vec_push(v, ¶m.n); |
| 6278 | } |
| 6279 | else { |
| 6280 | bc_array_init(¶m.v, true); |
| 6281 | bc_vec_push(v, ¶m.v); |
| 6282 | } |
| 6283 | } |
| 6284 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6285 | bc_vec_push(&G.prog.stack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6286 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6287 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6288 | } |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6289 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6290 | # define zbc_program_call(...) (zbc_program_call(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6291 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6292 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6293 | static BC_STATUS zbc_program_return(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6294 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6295 | BcResult res; |
| 6296 | BcFunc *f; |
| 6297 | size_t i; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6298 | BcInstPtr *ip = bc_vec_top(&G.prog.stack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6299 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6300 | 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^] | 6301 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6302 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6303 | f = bc_program_func(ip->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6304 | res.t = BC_RESULT_TEMP; |
| 6305 | |
| 6306 | if (inst == BC_INST_RET) { |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6307 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6308 | BcNum *num; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6309 | BcResult *operand = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6310 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6311 | s = zbc_program_num(operand, &num, false); |
| 6312 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6313 | bc_num_init(&res.d.n, num->len); |
| 6314 | bc_num_copy(&res.d.n, num); |
| 6315 | } |
| 6316 | else { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6317 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 6318 | //bc_num_zero(&res.d.n); - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6319 | } |
| 6320 | |
| 6321 | // We need to pop arguments as well, so this takes that into account. |
| 6322 | for (i = 0; i < f->autos.len; ++i) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6323 | BcVec *v; |
| 6324 | BcId *a = bc_vec_item(&f->autos, i); |
| 6325 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6326 | v = bc_program_search(a->name, a->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6327 | bc_vec_pop(v); |
| 6328 | } |
| 6329 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6330 | bc_vec_npop(&G.prog.results, G.prog.results.len - ip->len); |
| 6331 | bc_vec_push(&G.prog.results, &res); |
| 6332 | bc_vec_pop(&G.prog.stack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6333 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6334 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6335 | } |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6336 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6337 | # define zbc_program_return(...) (zbc_program_return(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6338 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6339 | #endif // ENABLE_BC |
| 6340 | |
| 6341 | static unsigned long bc_program_scale(BcNum *n) |
| 6342 | { |
| 6343 | return (unsigned long) n->rdx; |
| 6344 | } |
| 6345 | |
| 6346 | static unsigned long bc_program_len(BcNum *n) |
| 6347 | { |
Denys Vlasenko | a7f1a36 | 2018-12-10 12:57:01 +0100 | [diff] [blame] | 6348 | size_t len = n->len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6349 | |
Denys Vlasenko | a7f1a36 | 2018-12-10 12:57:01 +0100 | [diff] [blame] | 6350 | if (n->rdx != len) return len; |
| 6351 | for (;;) { |
| 6352 | if (len == 0) break; |
| 6353 | len--; |
| 6354 | if (n->num[len] != 0) break; |
| 6355 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6356 | return len; |
| 6357 | } |
| 6358 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6359 | static BcStatus bc_program_builtin(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6360 | { |
| 6361 | BcStatus s; |
| 6362 | BcResult *opnd; |
| 6363 | BcNum *num = NULL; |
| 6364 | BcResult res; |
| 6365 | bool len = inst == BC_INST_LENGTH; |
| 6366 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6367 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6368 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6369 | opnd = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6370 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6371 | s = zbc_program_num(opnd, &num, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6372 | if (s) return s; |
| 6373 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6374 | #if ENABLE_DC |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6375 | if (!BC_PROG_NUM(opnd, num) && !len) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6376 | return bc_error_variable_is_wrong_type(); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6377 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6378 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6379 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6380 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6381 | if (inst == BC_INST_SQRT) s = bc_num_sqrt(num, &res.d.n, G.prog.scale); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6382 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6383 | else if (len != 0 && opnd->t == BC_RESULT_ARRAY) { |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6384 | bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6385 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6386 | #endif |
| 6387 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6388 | else if (len != 0 && !BC_PROG_NUM(opnd, num)) { |
| 6389 | |
| 6390 | char **str; |
| 6391 | size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx; |
| 6392 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6393 | str = bc_program_str(idx); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6394 | bc_num_ulong2num(&res.d.n, strlen(*str)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6395 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6396 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6397 | else { |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 6398 | 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] | 6399 | } |
| 6400 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6401 | bc_program_retire(&res, BC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6402 | |
| 6403 | return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6404 | } |
| 6405 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6406 | #if ENABLE_DC |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6407 | static BcStatus bc_program_divmod(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6408 | { |
| 6409 | BcStatus s; |
| 6410 | BcResult *opd1, *opd2, res, res2; |
| 6411 | BcNum *n1, *n2 = NULL; |
| 6412 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6413 | s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6414 | if (s) return s; |
| 6415 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6416 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6417 | bc_num_init(&res2.d.n, n2->len); |
| 6418 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6419 | s = bc_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] | 6420 | if (s) goto err; |
| 6421 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6422 | bc_program_binOpRetire(&res2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6423 | res.t = BC_RESULT_TEMP; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6424 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6425 | |
| 6426 | return s; |
| 6427 | |
| 6428 | err: |
| 6429 | bc_num_free(&res2.d.n); |
| 6430 | bc_num_free(&res.d.n); |
| 6431 | return s; |
| 6432 | } |
| 6433 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6434 | static BcStatus bc_program_modexp(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6435 | { |
| 6436 | BcStatus s; |
| 6437 | BcResult *r1, *r2, *r3, res; |
| 6438 | BcNum *n1, *n2, *n3; |
| 6439 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6440 | if (!BC_PROG_STACK(&G.prog.results, 3)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6441 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6442 | s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6443 | if (s) return s; |
| 6444 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6445 | r1 = bc_vec_item_rev(&G.prog.results, 2); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6446 | s = zbc_program_num(r1, &n1, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6447 | if (s) return s; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6448 | if (!BC_PROG_NUM(r1, n1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6449 | return bc_error_variable_is_wrong_type(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6450 | |
| 6451 | // Make sure that the values have their pointers updated, if necessary. |
| 6452 | if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) { |
| 6453 | |
| 6454 | if (r1->t == r2->t) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6455 | s = zbc_program_num(r2, &n2, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6456 | if (s) return s; |
| 6457 | } |
| 6458 | |
| 6459 | if (r1->t == r3->t) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6460 | s = zbc_program_num(r3, &n3, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6461 | if (s) return s; |
| 6462 | } |
| 6463 | } |
| 6464 | |
| 6465 | bc_num_init(&res.d.n, n3->len); |
| 6466 | s = bc_num_modexp(n1, n2, n3, &res.d.n); |
| 6467 | if (s) goto err; |
| 6468 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6469 | bc_vec_pop(&G.prog.results); |
| 6470 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6471 | |
| 6472 | return s; |
| 6473 | |
| 6474 | err: |
| 6475 | bc_num_free(&res.d.n); |
| 6476 | return s; |
| 6477 | } |
| 6478 | |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6479 | static void bc_program_stackLen(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6480 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6481 | BcResult res; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6482 | size_t len = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6483 | |
| 6484 | res.t = BC_RESULT_TEMP; |
| 6485 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6486 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6487 | bc_num_ulong2num(&res.d.n, len); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6488 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6489 | } |
| 6490 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6491 | static BcStatus bc_program_asciify(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6492 | { |
| 6493 | BcStatus s; |
| 6494 | BcResult *r, res; |
Denys Vlasenko | 4a024c7 | 2018-12-09 13:21:54 +0100 | [diff] [blame] | 6495 | BcNum *num, n; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6496 | char *str, *str2, c; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6497 | size_t len = G.prog.strs.len, idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6498 | unsigned long val; |
| 6499 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6500 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6501 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6502 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6503 | |
Denys Vlasenko | 4a024c7 | 2018-12-09 13:21:54 +0100 | [diff] [blame] | 6504 | num = NULL; // TODO: is this NULL needed? |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6505 | s = zbc_program_num(r, &num, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6506 | if (s) return s; |
| 6507 | |
| 6508 | if (BC_PROG_NUM(r, num)) { |
| 6509 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6510 | bc_num_init_DEF_SIZE(&n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6511 | bc_num_copy(&n, num); |
| 6512 | bc_num_truncate(&n, n.rdx); |
| 6513 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6514 | s = bc_num_mod(&n, &G.prog.strmb, &n, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6515 | if (s) goto num_err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6516 | s = zbc_num_ulong(&n, &val); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6517 | if (s) goto num_err; |
| 6518 | |
| 6519 | c = (char) val; |
| 6520 | |
| 6521 | bc_num_free(&n); |
| 6522 | } |
| 6523 | else { |
| 6524 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx; |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6525 | str2 = *bc_program_str(idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6526 | c = str2[0]; |
| 6527 | } |
| 6528 | |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6529 | str = xzalloc(2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6530 | str[0] = c; |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6531 | //str[1] = '\0'; - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6532 | |
| 6533 | str2 = xstrdup(str); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6534 | bc_program_addFunc(str2, &idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6535 | |
| 6536 | if (idx != len + BC_PROG_REQ_FUNCS) { |
| 6537 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6538 | for (idx = 0; idx < G.prog.strs.len; ++idx) { |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6539 | if (strcmp(*bc_program_str(idx), str) == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6540 | len = idx; |
| 6541 | break; |
| 6542 | } |
| 6543 | } |
| 6544 | |
| 6545 | free(str); |
| 6546 | } |
| 6547 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6548 | bc_vec_push(&G.prog.strs, &str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6549 | |
| 6550 | res.t = BC_RESULT_STR; |
| 6551 | res.d.id.idx = len; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6552 | bc_vec_pop(&G.prog.results); |
| 6553 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6554 | |
| 6555 | return BC_STATUS_SUCCESS; |
| 6556 | |
| 6557 | num_err: |
| 6558 | bc_num_free(&n); |
| 6559 | return s; |
| 6560 | } |
| 6561 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6562 | static BC_STATUS zbc_program_printStream(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6563 | { |
| 6564 | BcStatus s; |
| 6565 | BcResult *r; |
| 6566 | BcNum *n = NULL; |
| 6567 | size_t idx; |
| 6568 | char *str; |
| 6569 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6570 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6571 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6572 | r = bc_vec_top(&G.prog.results); |
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 | s = zbc_program_num(r, &n, false); |
| 6575 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6576 | |
| 6577 | if (BC_PROG_NUM(r, n)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6578 | s = zbc_num_stream(n, &G.prog.strmb); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6579 | else { |
| 6580 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx; |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6581 | str = *bc_program_str(idx); |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 6582 | printf("%s", str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6583 | } |
| 6584 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6585 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6586 | } |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6587 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6588 | # define zbc_program_printStream(...) (zbc_program_printStream(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6589 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6590 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6591 | static BC_STATUS zbc_program_nquit(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6592 | { |
| 6593 | BcStatus s; |
| 6594 | BcResult *opnd; |
| 6595 | BcNum *num = NULL; |
| 6596 | unsigned long val; |
| 6597 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6598 | s = zbc_program_prep(&opnd, &num); |
| 6599 | if (s) RETURN_STATUS(s); |
| 6600 | s = zbc_num_ulong(num, &val); |
| 6601 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6602 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6603 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6604 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6605 | if (G.prog.stack.len < val) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6606 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 6607 | if (G.prog.stack.len == val) { |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 6608 | QUIT_OR_RETURN_TO_MAIN; |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 6609 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6610 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6611 | bc_vec_npop(&G.prog.stack, val); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6612 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6613 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6614 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6615 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6616 | # define zbc_program_nquit(...) (zbc_program_nquit(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6617 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6618 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6619 | static BcStatus bc_program_execStr(char *code, size_t *bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6620 | bool cond) |
| 6621 | { |
| 6622 | BcStatus s = BC_STATUS_SUCCESS; |
| 6623 | BcResult *r; |
| 6624 | char **str; |
| 6625 | BcFunc *f; |
| 6626 | BcParse prs; |
| 6627 | BcInstPtr ip; |
| 6628 | size_t fidx, sidx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6629 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6630 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6631 | return bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6632 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6633 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6634 | |
| 6635 | if (cond) { |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6636 | BcNum *n = n; // for compiler |
| 6637 | bool exec; |
| 6638 | char *name; |
| 6639 | char *then_name = bc_program_name(code, bgn); |
| 6640 | char *else_name = NULL; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6641 | |
| 6642 | if (code[*bgn] == BC_PARSE_STREND) |
| 6643 | (*bgn) += 1; |
| 6644 | else |
| 6645 | else_name = bc_program_name(code, bgn); |
| 6646 | |
| 6647 | exec = r->d.n.len != 0; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6648 | name = then_name; |
| 6649 | if (!exec && else_name != NULL) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6650 | exec = true; |
| 6651 | name = else_name; |
| 6652 | } |
| 6653 | |
| 6654 | if (exec) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6655 | BcVec *v; |
| 6656 | v = bc_program_search(name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6657 | n = bc_vec_top(v); |
| 6658 | } |
| 6659 | |
| 6660 | free(then_name); |
| 6661 | free(else_name); |
| 6662 | |
| 6663 | if (!exec) goto exit; |
| 6664 | if (!BC_PROG_STR(n)) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6665 | s = bc_error_variable_is_wrong_type(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6666 | goto exit; |
| 6667 | } |
| 6668 | |
| 6669 | sidx = n->rdx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6670 | } else { |
| 6671 | if (r->t == BC_RESULT_STR) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6672 | sidx = r->d.id.idx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6673 | } else if (r->t == BC_RESULT_VAR) { |
| 6674 | BcNum *n; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6675 | s = zbc_program_num(r, &n, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6676 | if (s || !BC_PROG_STR(n)) goto exit; |
| 6677 | sidx = n->rdx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6678 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6679 | goto exit; |
| 6680 | } |
| 6681 | |
| 6682 | fidx = sidx + BC_PROG_REQ_FUNCS; |
| 6683 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6684 | str = bc_program_str(sidx); |
| 6685 | f = bc_program_func(fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6686 | |
| 6687 | if (f->code.len == 0) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6688 | common_parse_init(&prs, fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6689 | s = bc_parse_text(&prs, *str); |
| 6690 | if (s) goto err; |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 6691 | s = common_parse_expr(&prs, BC_PARSE_NOCALL); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6692 | if (s) goto err; |
| 6693 | |
| 6694 | if (prs.l.t.t != BC_LEX_EOF) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6695 | s = bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6696 | goto err; |
| 6697 | } |
| 6698 | |
| 6699 | bc_parse_free(&prs); |
| 6700 | } |
| 6701 | |
| 6702 | ip.idx = 0; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6703 | ip.len = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6704 | ip.func = fidx; |
| 6705 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6706 | bc_vec_pop(&G.prog.results); |
| 6707 | bc_vec_push(&G.prog.stack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6708 | |
| 6709 | return BC_STATUS_SUCCESS; |
| 6710 | |
| 6711 | err: |
| 6712 | bc_parse_free(&prs); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6713 | f = bc_program_func(fidx); |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 6714 | bc_vec_pop_all(&f->code); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6715 | exit: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6716 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6717 | return s; |
| 6718 | } |
| 6719 | #endif // ENABLE_DC |
| 6720 | |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6721 | static void bc_program_pushGlobal(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6722 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6723 | BcResult res; |
| 6724 | unsigned long val; |
| 6725 | |
| 6726 | res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE; |
| 6727 | if (inst == BC_INST_IBASE) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6728 | val = (unsigned long) G.prog.ib_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6729 | else if (inst == BC_INST_SCALE) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6730 | val = (unsigned long) G.prog.scale; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6731 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6732 | val = (unsigned long) G.prog.ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6733 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6734 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6735 | bc_num_ulong2num(&res.d.n, val); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6736 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6737 | } |
| 6738 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6739 | static void bc_program_addFunc(char *name, size_t *idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6740 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6741 | BcId entry, *entry_ptr; |
| 6742 | BcFunc f; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 6743 | int inserted; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6744 | |
| 6745 | entry.name = name; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6746 | entry.idx = G.prog.fns.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6747 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 6748 | inserted = bc_map_insert(&G.prog.fn_map, &entry, idx); |
| 6749 | if (!inserted) free(name); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6750 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6751 | entry_ptr = bc_vec_item(&G.prog.fn_map, *idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6752 | *idx = entry_ptr->idx; |
| 6753 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 6754 | if (!inserted) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6755 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6756 | BcFunc *func = bc_program_func(entry_ptr->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6757 | |
| 6758 | // We need to reset these, so the function can be repopulated. |
| 6759 | func->nparams = 0; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 6760 | bc_vec_pop_all(&func->autos); |
| 6761 | bc_vec_pop_all(&func->code); |
| 6762 | bc_vec_pop_all(&func->labels); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6763 | } |
| 6764 | else { |
| 6765 | bc_func_init(&f); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6766 | bc_vec_push(&G.prog.fns, &f); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6767 | } |
| 6768 | } |
| 6769 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6770 | static BcStatus bc_program_exec(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6771 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6772 | BcResult r, *ptr; |
| 6773 | BcNum *num; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6774 | BcInstPtr *ip = bc_vec_top(&G.prog.stack); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6775 | BcFunc *func = bc_program_func(ip->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6776 | char *code = func->code.v; |
| 6777 | bool cond = false; |
| 6778 | |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6779 | while (ip->idx < func->code.len) { |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6780 | BcStatus s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6781 | char inst = code[(ip->idx)++]; |
| 6782 | |
| 6783 | switch (inst) { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6784 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6785 | case BC_INST_JUMP_ZERO: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6786 | s = zbc_program_prep(&ptr, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6787 | if (s) return s; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6788 | cond = !bc_num_cmp(num, &G.prog.zero); |
| 6789 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6790 | // Fallthrough. |
| 6791 | case BC_INST_JUMP: { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6792 | size_t *addr; |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6793 | size_t idx = bc_program_index(code, &ip->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6794 | addr = bc_vec_item(&func->labels, idx); |
| 6795 | if (inst == BC_INST_JUMP || cond) ip->idx = *addr; |
| 6796 | break; |
| 6797 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6798 | case BC_INST_CALL: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6799 | s = zbc_program_call(code, &ip->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6800 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6801 | case BC_INST_INC_PRE: |
| 6802 | case BC_INST_DEC_PRE: |
| 6803 | case BC_INST_INC_POST: |
| 6804 | case BC_INST_DEC_POST: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6805 | s = zbc_program_incdec(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6806 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6807 | case BC_INST_HALT: |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 6808 | QUIT_OR_RETURN_TO_MAIN; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6809 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6810 | case BC_INST_RET: |
| 6811 | case BC_INST_RET0: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6812 | s = zbc_program_return(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6813 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6814 | case BC_INST_BOOL_OR: |
| 6815 | case BC_INST_BOOL_AND: |
| 6816 | #endif // ENABLE_BC |
| 6817 | case BC_INST_REL_EQ: |
| 6818 | case BC_INST_REL_LE: |
| 6819 | case BC_INST_REL_GE: |
| 6820 | case BC_INST_REL_NE: |
| 6821 | case BC_INST_REL_LT: |
| 6822 | case BC_INST_REL_GT: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6823 | s = bc_program_logical(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6824 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6825 | case BC_INST_READ: |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6826 | s = bc_program_read(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6827 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6828 | case BC_INST_VAR: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6829 | s = bc_program_pushVar(code, &ip->idx, false, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6830 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6831 | case BC_INST_ARRAY_ELEM: |
| 6832 | case BC_INST_ARRAY: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6833 | s = zbc_program_pushArray(code, &ip->idx, inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6834 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6835 | case BC_INST_LAST: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6836 | r.t = BC_RESULT_LAST; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6837 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6838 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6839 | case BC_INST_IBASE: |
| 6840 | case BC_INST_SCALE: |
| 6841 | case BC_INST_OBASE: |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6842 | bc_program_pushGlobal(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6843 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6844 | case BC_INST_SCALE_FUNC: |
| 6845 | case BC_INST_LENGTH: |
| 6846 | case BC_INST_SQRT: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6847 | s = bc_program_builtin(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6848 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6849 | case BC_INST_NUM: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6850 | r.t = BC_RESULT_CONSTANT; |
| 6851 | r.d.id.idx = bc_program_index(code, &ip->idx); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6852 | bc_vec_push(&G.prog.results, &r); |
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_POP: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6855 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6856 | s = bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6857 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6858 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6859 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6860 | case BC_INST_POP_EXEC: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6861 | bc_vec_pop(&G.prog.stack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6862 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6863 | case BC_INST_PRINT: |
| 6864 | case BC_INST_PRINT_POP: |
| 6865 | case BC_INST_PRINT_STR: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6866 | s = zbc_program_print(inst, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6867 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6868 | case BC_INST_STR: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6869 | r.t = BC_RESULT_STR; |
| 6870 | r.d.id.idx = bc_program_index(code, &ip->idx); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6871 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6872 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6873 | case BC_INST_POWER: |
| 6874 | case BC_INST_MULTIPLY: |
| 6875 | case BC_INST_DIVIDE: |
| 6876 | case BC_INST_MODULUS: |
| 6877 | case BC_INST_PLUS: |
| 6878 | case BC_INST_MINUS: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6879 | s = bc_program_op(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6880 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6881 | case BC_INST_BOOL_NOT: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6882 | s = zbc_program_prep(&ptr, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6883 | if (s) return s; |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6884 | bc_num_init_DEF_SIZE(&r.d.n); |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6885 | if (!bc_num_cmp(num, &G.prog.zero)) |
| 6886 | bc_num_one(&r.d.n); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 6887 | //else bc_num_zero(&r.d.n); - already is |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6888 | bc_program_retire(&r, BC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6889 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6890 | case BC_INST_NEG: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6891 | s = zbc_program_negate(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6892 | break; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6893 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6894 | case BC_INST_ASSIGN_POWER: |
| 6895 | case BC_INST_ASSIGN_MULTIPLY: |
| 6896 | case BC_INST_ASSIGN_DIVIDE: |
| 6897 | case BC_INST_ASSIGN_MODULUS: |
| 6898 | case BC_INST_ASSIGN_PLUS: |
| 6899 | case BC_INST_ASSIGN_MINUS: |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6900 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6901 | case BC_INST_ASSIGN: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6902 | s = bc_program_assign(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6903 | break; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6904 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6905 | case BC_INST_MODEXP: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6906 | s = bc_program_modexp(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6907 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6908 | case BC_INST_DIVMOD: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6909 | s = bc_program_divmod(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6910 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6911 | case BC_INST_EXECUTE: |
| 6912 | case BC_INST_EXEC_COND: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6913 | cond = inst == BC_INST_EXEC_COND; |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6914 | s = bc_program_execStr(code, &ip->idx, cond); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6915 | break; |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6916 | case BC_INST_PRINT_STACK: { |
| 6917 | size_t idx; |
| 6918 | for (idx = 0; idx < G.prog.results.len; ++idx) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6919 | s = zbc_program_print(BC_INST_PRINT, idx); |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6920 | if (s) break; |
| 6921 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6922 | break; |
| 6923 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6924 | case BC_INST_CLEAR_STACK: |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 6925 | bc_vec_pop_all(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6926 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6927 | case BC_INST_STACK_LEN: |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6928 | bc_program_stackLen(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6929 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6930 | case BC_INST_DUPLICATE: |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6931 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6932 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6933 | ptr = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6934 | bc_result_copy(&r, ptr); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6935 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6936 | break; |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6937 | case BC_INST_SWAP: { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6938 | BcResult *ptr2; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6939 | if (!BC_PROG_STACK(&G.prog.results, 2)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6940 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6941 | ptr = bc_vec_item_rev(&G.prog.results, 0); |
| 6942 | ptr2 = bc_vec_item_rev(&G.prog.results, 1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6943 | memcpy(&r, ptr, sizeof(BcResult)); |
| 6944 | memcpy(ptr, ptr2, sizeof(BcResult)); |
| 6945 | memcpy(ptr2, &r, sizeof(BcResult)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6946 | break; |
| 6947 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6948 | case BC_INST_ASCIIFY: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6949 | s = bc_program_asciify(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6950 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6951 | case BC_INST_PRINT_STREAM: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6952 | s = zbc_program_printStream(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6953 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6954 | case BC_INST_LOAD: |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6955 | case BC_INST_PUSH_VAR: { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6956 | bool copy = inst == BC_INST_LOAD; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6957 | s = bc_program_pushVar(code, &ip->idx, true, copy); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6958 | break; |
| 6959 | } |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6960 | case BC_INST_PUSH_TO_VAR: { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6961 | char *name = bc_program_name(code, &ip->idx); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6962 | s = zbc_program_copyToVar(name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6963 | free(name); |
| 6964 | break; |
| 6965 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6966 | case BC_INST_QUIT: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6967 | if (G.prog.stack.len <= 2) |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 6968 | QUIT_OR_RETURN_TO_MAIN; |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 6969 | bc_vec_npop(&G.prog.stack, 2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6970 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6971 | case BC_INST_NQUIT: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame^] | 6972 | s = zbc_program_nquit(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6973 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6974 | #endif // ENABLE_DC |
| 6975 | } |
| 6976 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6977 | if (s || G_interrupt) { |
| 6978 | bc_program_reset(); |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6979 | return s; |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6980 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6981 | |
| 6982 | // If the stack has changed, pointers may be invalid. |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6983 | ip = bc_vec_top(&G.prog.stack); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6984 | func = bc_program_func(ip->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6985 | code = func->code.v; |
| 6986 | } |
| 6987 | |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6988 | return BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6989 | } |
| 6990 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 6991 | #if ENABLE_BC |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 6992 | static void bc_vm_info(void) |
| 6993 | { |
| 6994 | printf("%s "BB_VER"\n" |
| 6995 | "Copyright (c) 2018 Gavin D. Howard and contributors\n" |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 6996 | , applet_name); |
| 6997 | } |
| 6998 | |
| 6999 | static void bc_args(char **argv) |
| 7000 | { |
| 7001 | unsigned opts; |
| 7002 | int i; |
| 7003 | |
| 7004 | GETOPT_RESET(); |
| 7005 | #if ENABLE_FEATURE_BC_LONG_OPTIONS |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7006 | opts = option_mask32 |= getopt32long(argv, "wvsqli", |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 7007 | "warn\0" No_argument "w" |
| 7008 | "version\0" No_argument "v" |
| 7009 | "standard\0" No_argument "s" |
| 7010 | "quiet\0" No_argument "q" |
| 7011 | "mathlib\0" No_argument "l" |
| 7012 | "interactive\0" No_argument "i" |
| 7013 | ); |
| 7014 | #else |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7015 | opts = option_mask32 |= getopt32(argv, "wvsqli"); |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 7016 | #endif |
| 7017 | if (getenv("POSIXLY_CORRECT")) |
| 7018 | option_mask32 |= BC_FLAG_S; |
| 7019 | |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 7020 | if (opts & BC_FLAG_V) { |
| 7021 | bc_vm_info(); |
| 7022 | exit(0); |
| 7023 | } |
| 7024 | |
| 7025 | for (i = optind; argv[i]; ++i) |
| 7026 | bc_vec_push(&G.files, argv + i); |
| 7027 | } |
| 7028 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7029 | static void bc_vm_envArgs(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7030 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7031 | BcVec v; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7032 | char *buf; |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 7033 | char *env_args = getenv("BC_ENV_ARGS"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7034 | |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7035 | if (!env_args) return; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7036 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7037 | G.env_args = xstrdup(env_args); |
| 7038 | buf = G.env_args; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7039 | |
| 7040 | bc_vec_init(&v, sizeof(char *), NULL); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7041 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7042 | while (*(buf = skip_whitespace(buf)) != '\0') { |
| 7043 | bc_vec_push(&v, &buf); |
| 7044 | buf = skip_non_whitespace(buf); |
| 7045 | if (!*buf) |
| 7046 | break; |
| 7047 | *buf++ = '\0'; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7048 | } |
| 7049 | |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 7050 | // NULL terminate, and pass argv[] so that first arg is argv[1] |
| 7051 | if (sizeof(int) == sizeof(char*)) { |
| 7052 | bc_vec_push(&v, &const_int_0); |
| 7053 | } else { |
| 7054 | static char *const nullptr = NULL; |
| 7055 | bc_vec_push(&v, &nullptr); |
| 7056 | } |
| 7057 | bc_args(((char **)v.v) - 1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7058 | |
| 7059 | bc_vec_free(&v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7060 | } |
| 7061 | #endif // ENABLE_BC |
| 7062 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7063 | static unsigned bc_vm_envLen(const char *var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7064 | { |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7065 | char *lenv; |
| 7066 | unsigned len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7067 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7068 | lenv = getenv(var); |
| 7069 | len = BC_NUM_PRINT_WIDTH; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7070 | if (!lenv) return len; |
| 7071 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7072 | len = bb_strtou(lenv, NULL, 10) - 1; |
| 7073 | if (errno || len < 2 || len >= INT_MAX) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7074 | len = BC_NUM_PRINT_WIDTH; |
| 7075 | |
| 7076 | return len; |
| 7077 | } |
| 7078 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7079 | static BcStatus bc_vm_process(const char *text) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7080 | { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7081 | BcStatus s = bc_parse_text(&G.prs, text); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7082 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7083 | if (s) return s; |
| 7084 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7085 | while (G.prs.l.t.t != BC_LEX_EOF) { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7086 | s = G.prs.parse(&G.prs); |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 7087 | if (s) return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7088 | } |
| 7089 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7090 | if (BC_PARSE_CAN_EXEC(&G.prs)) { |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7091 | s = bc_program_exec(); |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 7092 | fflush_and_check(); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7093 | if (s) |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7094 | bc_program_reset(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7095 | } |
| 7096 | |
| 7097 | return s; |
| 7098 | } |
| 7099 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7100 | static BcStatus bc_vm_file(const char *file) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7101 | { |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7102 | const char *sv_file; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7103 | char *data; |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7104 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7105 | BcFunc *main_func; |
| 7106 | BcInstPtr *ip; |
| 7107 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 7108 | data = bc_read_file(file); |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 7109 | if (!data) return bc_error_fmt("file '%s' is not text", file); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7110 | |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7111 | sv_file = G.prog.file; |
| 7112 | G.prog.file = file; |
| 7113 | bc_lex_file(&G.prs.l); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7114 | s = bc_vm_process(data); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7115 | if (s) goto err; |
| 7116 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 7117 | main_func = bc_program_func(BC_PROG_MAIN); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7118 | ip = bc_vec_item(&G.prog.stack, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7119 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 7120 | if (main_func->code.len < ip->idx) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 7121 | s = bc_error_fmt("file '%s' is not executable", file); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7122 | |
| 7123 | err: |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7124 | G.prog.file = sv_file; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7125 | free(data); |
| 7126 | return s; |
| 7127 | } |
| 7128 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7129 | static BcStatus bc_vm_stdin(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7130 | { |
Denys Vlasenko | a0c421c | 2018-12-02 20:16:52 +0100 | [diff] [blame] | 7131 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7132 | BcVec buf, buffer; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7133 | size_t len, i, str = 0; |
Denys Vlasenko | a0c421c | 2018-12-02 20:16:52 +0100 | [diff] [blame] | 7134 | bool comment = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7135 | |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7136 | G.prog.file = NULL; |
| 7137 | bc_lex_file(&G.prs.l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7138 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 7139 | bc_char_vec_init(&buffer); |
| 7140 | bc_char_vec_init(&buf); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 7141 | bc_vec_pushZeroByte(&buffer); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7142 | |
| 7143 | // This loop is complex because the vm tries not to send any lines that end |
| 7144 | // with a backslash to the parser. The reason for that is because the parser |
| 7145 | // treats a backslash+newline combo as whitespace, per the bc spec. In that |
| 7146 | // case, and for strings and comments, the parser will expect more stuff. |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 7147 | while ((s = bc_read_line(&buf)) == BC_STATUS_SUCCESS) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7148 | |
| 7149 | char *string = buf.v; |
| 7150 | |
| 7151 | len = buf.len - 1; |
| 7152 | |
| 7153 | if (len == 1) { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7154 | if (str && buf.v[0] == G.send) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7155 | str -= 1; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7156 | else if (buf.v[0] == G.sbgn) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7157 | str += 1; |
| 7158 | } |
| 7159 | else if (len > 1 || comment) { |
| 7160 | |
| 7161 | for (i = 0; i < len; ++i) { |
| 7162 | |
Denys Vlasenko | a0c421c | 2018-12-02 20:16:52 +0100 | [diff] [blame] | 7163 | bool notend = len > i + 1; |
| 7164 | char c = string[i]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7165 | |
| 7166 | if (i - 1 > len || string[i - 1] != '\\') { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7167 | if (G.sbgn == G.send) |
| 7168 | str ^= c == G.sbgn; |
| 7169 | else if (c == G.send) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7170 | str -= 1; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7171 | else if (c == G.sbgn) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7172 | str += 1; |
| 7173 | } |
| 7174 | |
| 7175 | if (c == '/' && notend && !comment && string[i + 1] == '*') { |
| 7176 | comment = true; |
| 7177 | break; |
| 7178 | } |
| 7179 | else if (c == '*' && notend && comment && string[i + 1] == '/') |
| 7180 | comment = false; |
| 7181 | } |
| 7182 | |
| 7183 | if (str || comment || string[len - 2] == '\\') { |
| 7184 | bc_vec_concat(&buffer, buf.v); |
| 7185 | continue; |
| 7186 | } |
| 7187 | } |
| 7188 | |
| 7189 | bc_vec_concat(&buffer, buf.v); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7190 | s = bc_vm_process(buffer.v); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7191 | if (s) { |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7192 | if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin) { |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 7193 | // Debug config, non-interactive mode: |
| 7194 | // return all the way back to main. |
| 7195 | // Non-debug builds do not come here, they exit. |
| 7196 | break; |
| 7197 | } |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7198 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7199 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 7200 | bc_vec_pop_all(&buffer); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7201 | } |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 7202 | if (s == BC_STATUS_EOF) // input EOF (^D) is not an error |
| 7203 | s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7204 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 7205 | if (str) { |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7206 | s = bc_error("string end could not be found"); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 7207 | } |
| 7208 | else if (comment) { |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7209 | s = bc_error("comment end could not be found"); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 7210 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7211 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7212 | bc_vec_free(&buf); |
| 7213 | bc_vec_free(&buffer); |
| 7214 | return s; |
| 7215 | } |
| 7216 | |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7217 | #if ENABLE_BC |
| 7218 | static const char bc_lib[] = { |
| 7219 | "scale=20" |
| 7220 | "\n" "define e(x){" |
| 7221 | "\n" "auto b,s,n,r,d,i,p,f,v" |
| 7222 | "\n" "b=ibase" |
| 7223 | "\n" "ibase=A" |
| 7224 | "\n" "if(x<0){" |
| 7225 | "\n" "n=1" |
| 7226 | "\n" "x=-x" |
| 7227 | "\n" "}" |
| 7228 | "\n" "s=scale" |
| 7229 | "\n" "r=6+s+0.44*x" |
| 7230 | "\n" "scale=scale(x)+1" |
| 7231 | "\n" "while(x>1){" |
| 7232 | "\n" "d+=1" |
| 7233 | "\n" "x/=2" |
| 7234 | "\n" "scale+=1" |
| 7235 | "\n" "}" |
| 7236 | "\n" "scale=r" |
| 7237 | "\n" "r=x+1" |
| 7238 | "\n" "p=x" |
| 7239 | "\n" "f=v=1" |
| 7240 | "\n" "for(i=2;v!=0;++i){" |
| 7241 | "\n" "p*=x" |
| 7242 | "\n" "f*=i" |
| 7243 | "\n" "v=p/f" |
| 7244 | "\n" "r+=v" |
| 7245 | "\n" "}" |
| 7246 | "\n" "while((d--)!=0)r*=r" |
| 7247 | "\n" "scale=s" |
| 7248 | "\n" "ibase=b" |
| 7249 | "\n" "if(n!=0)return(1/r)" |
| 7250 | "\n" "return(r/1)" |
| 7251 | "\n" "}" |
| 7252 | "\n" "define l(x){" |
| 7253 | "\n" "auto b,s,r,p,a,q,i,v" |
| 7254 | "\n" "b=ibase" |
| 7255 | "\n" "ibase=A" |
| 7256 | "\n" "if(x<=0){" |
| 7257 | "\n" "r=(1-10^scale)/1" |
| 7258 | "\n" "ibase=b" |
| 7259 | "\n" "return(r)" |
| 7260 | "\n" "}" |
| 7261 | "\n" "s=scale" |
| 7262 | "\n" "scale+=6" |
| 7263 | "\n" "p=2" |
| 7264 | "\n" "while(x>=2){" |
| 7265 | "\n" "p*=2" |
| 7266 | "\n" "x=sqrt(x)" |
| 7267 | "\n" "}" |
| 7268 | "\n" "while(x<=0.5){" |
| 7269 | "\n" "p*=2" |
| 7270 | "\n" "x=sqrt(x)" |
| 7271 | "\n" "}" |
| 7272 | "\n" "r=a=(x-1)/(x+1)" |
| 7273 | "\n" "q=a*a" |
| 7274 | "\n" "v=1" |
| 7275 | "\n" "for(i=3;v!=0;i+=2){" |
| 7276 | "\n" "a*=q" |
| 7277 | "\n" "v=a/i" |
| 7278 | "\n" "r+=v" |
| 7279 | "\n" "}" |
| 7280 | "\n" "r*=p" |
| 7281 | "\n" "scale=s" |
| 7282 | "\n" "ibase=b" |
| 7283 | "\n" "return(r/1)" |
| 7284 | "\n" "}" |
| 7285 | "\n" "define s(x){" |
| 7286 | "\n" "auto b,s,r,n,a,q,i" |
| 7287 | "\n" "b=ibase" |
| 7288 | "\n" "ibase=A" |
| 7289 | "\n" "s=scale" |
| 7290 | "\n" "scale=1.1*s+2" |
| 7291 | "\n" "a=a(1)" |
| 7292 | "\n" "if(x<0){" |
| 7293 | "\n" "n=1" |
| 7294 | "\n" "x=-x" |
| 7295 | "\n" "}" |
| 7296 | "\n" "scale=0" |
| 7297 | "\n" "q=(x/a+2)/4" |
| 7298 | "\n" "x=x-4*q*a" |
| 7299 | "\n" "if(q%2!=0)x=-x" |
| 7300 | "\n" "scale=s+2" |
| 7301 | "\n" "r=a=x" |
| 7302 | "\n" "q=-x*x" |
| 7303 | "\n" "for(i=3;a!=0;i+=2){" |
| 7304 | "\n" "a*=q/(i*(i-1))" |
| 7305 | "\n" "r+=a" |
| 7306 | "\n" "}" |
| 7307 | "\n" "scale=s" |
| 7308 | "\n" "ibase=b" |
| 7309 | "\n" "if(n!=0)return(-r/1)" |
| 7310 | "\n" "return(r/1)" |
| 7311 | "\n" "}" |
| 7312 | "\n" "define c(x){" |
| 7313 | "\n" "auto b,s" |
| 7314 | "\n" "b=ibase" |
| 7315 | "\n" "ibase=A" |
| 7316 | "\n" "s=scale" |
| 7317 | "\n" "scale*=1.2" |
| 7318 | "\n" "x=s(2*a(1)+x)" |
| 7319 | "\n" "scale=s" |
| 7320 | "\n" "ibase=b" |
| 7321 | "\n" "return(x/1)" |
| 7322 | "\n" "}" |
| 7323 | "\n" "define a(x){" |
| 7324 | "\n" "auto b,s,r,n,a,m,t,f,i,u" |
| 7325 | "\n" "b=ibase" |
| 7326 | "\n" "ibase=A" |
| 7327 | "\n" "n=1" |
| 7328 | "\n" "if(x<0){" |
| 7329 | "\n" "n=-1" |
| 7330 | "\n" "x=-x" |
| 7331 | "\n" "}" |
| 7332 | "\n" "if(x==1){" |
| 7333 | "\n" "if(scale<65){" |
| 7334 | "\n" "return(.7853981633974483096156608458198757210492923498437764552437361480/n)" |
| 7335 | "\n" "}" |
| 7336 | "\n" "}" |
| 7337 | "\n" "if(x==.2){" |
| 7338 | "\n" "if(scale<65){" |
| 7339 | "\n" "return(.1973955598498807583700497651947902934475851037878521015176889402/n)" |
| 7340 | "\n" "}" |
| 7341 | "\n" "}" |
| 7342 | "\n" "s=scale" |
| 7343 | "\n" "if(x>.2){" |
| 7344 | "\n" "scale+=5" |
| 7345 | "\n" "a=a(.2)" |
| 7346 | "\n" "}" |
| 7347 | "\n" "scale=s+3" |
| 7348 | "\n" "while(x>.2){" |
| 7349 | "\n" "m+=1" |
| 7350 | "\n" "x=(x-.2)/(1+.2*x)" |
| 7351 | "\n" "}" |
| 7352 | "\n" "r=u=x" |
| 7353 | "\n" "f=-x*x" |
| 7354 | "\n" "t=1" |
| 7355 | "\n" "for(i=3;t!=0;i+=2){" |
| 7356 | "\n" "u*=f" |
| 7357 | "\n" "t=u/i" |
| 7358 | "\n" "r+=t" |
| 7359 | "\n" "}" |
| 7360 | "\n" "scale=s" |
| 7361 | "\n" "ibase=b" |
| 7362 | "\n" "return((m*a+r)/n)" |
| 7363 | "\n" "}" |
| 7364 | "\n" "define j(n,x){" |
| 7365 | "\n" "auto b,s,o,a,i,v,f" |
| 7366 | "\n" "b=ibase" |
| 7367 | "\n" "ibase=A" |
| 7368 | "\n" "s=scale" |
| 7369 | "\n" "scale=0" |
| 7370 | "\n" "n/=1" |
| 7371 | "\n" "if(n<0){" |
| 7372 | "\n" "n=-n" |
| 7373 | "\n" "if(n%2==1)o=1" |
| 7374 | "\n" "}" |
| 7375 | "\n" "a=1" |
| 7376 | "\n" "for(i=2;i<=n;++i)a*=i" |
| 7377 | "\n" "scale=1.5*s" |
| 7378 | "\n" "a=(x^n)/2^n/a" |
| 7379 | "\n" "r=v=1" |
| 7380 | "\n" "f=-x*x/4" |
| 7381 | "\n" "scale=scale+length(a)-scale(a)" |
| 7382 | "\n" "for(i=1;v!=0;++i){" |
| 7383 | "\n" "v=v*f/i/(n+i)" |
| 7384 | "\n" "r+=v" |
| 7385 | "\n" "}" |
| 7386 | "\n" "scale=s" |
| 7387 | "\n" "ibase=b" |
| 7388 | "\n" "if(o!=0)a=-a" |
| 7389 | "\n" "return(a*r/1)" |
| 7390 | "\n" "}" |
| 7391 | }; |
| 7392 | #endif // ENABLE_BC |
| 7393 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7394 | static BcStatus bc_vm_exec(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7395 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7396 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7397 | size_t i; |
| 7398 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7399 | #if ENABLE_BC |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 7400 | if (option_mask32 & BC_FLAG_L) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7401 | |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7402 | // We know that internal library is not buggy, |
| 7403 | // thus error checking is normally disabled. |
| 7404 | # define DEBUG_LIB 0 |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7405 | bc_lex_file(&G.prs.l); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7406 | s = bc_parse_text(&G.prs, bc_lib); |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7407 | if (DEBUG_LIB && s) return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7408 | |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7409 | while (G.prs.l.t.t != BC_LEX_EOF) { |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7410 | s = G.prs.parse(&G.prs); |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7411 | if (DEBUG_LIB && s) return s; |
| 7412 | } |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7413 | s = bc_program_exec(); |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7414 | if (DEBUG_LIB && s) return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7415 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7416 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7417 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7418 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7419 | for (i = 0; !s && i < G.files.len; ++i) |
| 7420 | s = bc_vm_file(*((char **) bc_vec_item(&G.files, i))); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7421 | if (ENABLE_FEATURE_CLEAN_UP && s && !G_ttyin) { |
| 7422 | // Debug config, non-interactive mode: |
| 7423 | // return all the way back to main. |
| 7424 | // Non-debug builds do not come here, they exit. |
| 7425 | return s; |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7426 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7427 | |
Denys Vlasenko | 91cde95 | 2018-12-10 20:56:08 +0100 | [diff] [blame] | 7428 | if (IS_BC || (option_mask32 & BC_FLAG_I)) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7429 | s = bc_vm_stdin(); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7430 | |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7431 | if (!s && !BC_PARSE_CAN_EXEC(&G.prs)) |
| 7432 | s = bc_vm_process(""); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7433 | |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 7434 | return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7435 | } |
| 7436 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7437 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 7438 | static void bc_program_free(void) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7439 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7440 | bc_num_free(&G.prog.ib); |
| 7441 | bc_num_free(&G.prog.ob); |
| 7442 | bc_num_free(&G.prog.hexb); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7443 | # if ENABLE_DC |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7444 | bc_num_free(&G.prog.strmb); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7445 | # endif |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7446 | bc_vec_free(&G.prog.fns); |
| 7447 | bc_vec_free(&G.prog.fn_map); |
| 7448 | bc_vec_free(&G.prog.vars); |
| 7449 | bc_vec_free(&G.prog.var_map); |
| 7450 | bc_vec_free(&G.prog.arrs); |
| 7451 | bc_vec_free(&G.prog.arr_map); |
| 7452 | bc_vec_free(&G.prog.strs); |
| 7453 | bc_vec_free(&G.prog.consts); |
| 7454 | bc_vec_free(&G.prog.results); |
| 7455 | bc_vec_free(&G.prog.stack); |
| 7456 | bc_num_free(&G.prog.last); |
| 7457 | bc_num_free(&G.prog.zero); |
| 7458 | bc_num_free(&G.prog.one); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7459 | } |
| 7460 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7461 | static void bc_vm_free(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7462 | { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7463 | bc_vec_free(&G.files); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7464 | bc_program_free(); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7465 | bc_parse_free(&G.prs); |
| 7466 | free(G.env_args); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7467 | } |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7468 | #endif |
| 7469 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7470 | static void bc_program_init(void) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7471 | { |
| 7472 | size_t idx; |
| 7473 | BcInstPtr ip; |
| 7474 | |
| 7475 | /* memset(&G.prog, 0, sizeof(G.prog)); - already is */ |
| 7476 | memset(&ip, 0, sizeof(BcInstPtr)); |
| 7477 | |
| 7478 | /* G.prog.nchars = G.prog.scale = 0; - already is */ |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7479 | bc_num_init_DEF_SIZE(&G.prog.ib); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7480 | bc_num_ten(&G.prog.ib); |
| 7481 | G.prog.ib_t = 10; |
| 7482 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7483 | bc_num_init_DEF_SIZE(&G.prog.ob); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7484 | bc_num_ten(&G.prog.ob); |
| 7485 | G.prog.ob_t = 10; |
| 7486 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7487 | bc_num_init_DEF_SIZE(&G.prog.hexb); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7488 | bc_num_ten(&G.prog.hexb); |
| 7489 | G.prog.hexb.num[0] = 6; |
| 7490 | |
| 7491 | #if ENABLE_DC |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7492 | bc_num_init_DEF_SIZE(&G.prog.strmb); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7493 | bc_num_ulong2num(&G.prog.strmb, UCHAR_MAX + 1); |
| 7494 | #endif |
| 7495 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7496 | bc_num_init_DEF_SIZE(&G.prog.last); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 7497 | //bc_num_zero(&G.prog.last); - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7498 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7499 | bc_num_init_DEF_SIZE(&G.prog.zero); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 7500 | //bc_num_zero(&G.prog.zero); - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7501 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7502 | bc_num_init_DEF_SIZE(&G.prog.one); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7503 | bc_num_one(&G.prog.one); |
| 7504 | |
| 7505 | bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7506 | bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7507 | |
Denys Vlasenko | 1f67e93 | 2018-12-03 00:08:59 +0100 | [diff] [blame] | 7508 | bc_program_addFunc(xstrdup("(main)"), &idx); |
| 7509 | bc_program_addFunc(xstrdup("(read)"), &idx); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7510 | |
| 7511 | bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7512 | bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7513 | |
| 7514 | bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7515 | bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7516 | |
| 7517 | bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free); |
| 7518 | bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free); |
| 7519 | bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free); |
| 7520 | bc_vec_init(&G.prog.stack, sizeof(BcInstPtr), NULL); |
| 7521 | bc_vec_push(&G.prog.stack, &ip); |
| 7522 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7523 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7524 | static int bc_vm_init(const char *env_len) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7525 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7526 | #if ENABLE_FEATURE_EDITING |
| 7527 | G.line_input_state = new_line_input_t(DO_HISTORY); |
| 7528 | #endif |
| 7529 | G.prog.len = bc_vm_envLen(env_len); |
| 7530 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7531 | bc_vec_init(&G.files, sizeof(char *), NULL); |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7532 | if (IS_BC) |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 7533 | IF_BC(bc_vm_envArgs();) |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7534 | bc_program_init(); |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 7535 | if (IS_BC) { |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 7536 | IF_BC(bc_parse_init(&G.prs, BC_PROG_MAIN);) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 7537 | } else { |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 7538 | IF_DC(dc_parse_init(&G.prs, BC_PROG_MAIN);) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 7539 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7540 | |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7541 | if (isatty(0)) { |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7542 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7543 | G_ttyin = 1; |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7544 | // With SA_RESTART, most system calls will restart |
| 7545 | // (IOW: they won't fail with EINTR). |
| 7546 | // In particular, this means ^C won't cause |
| 7547 | // stdout to get into "error state" if SIGINT hits |
| 7548 | // within write() syscall. |
| 7549 | // The downside is that ^C while line input is taken |
| 7550 | // will only be handled after [Enter] since read() |
| 7551 | // from stdin is not interrupted by ^C either, |
| 7552 | // it restarts, thus fgetc() does not return on ^C. |
| 7553 | signal_SA_RESTART_empty_mask(SIGINT, record_signo); |
| 7554 | |
| 7555 | // Without SA_RESTART, this exhibits a bug: |
| 7556 | // "while (1) print 1" and try ^C-ing it. |
| 7557 | // Intermittently, instead of returning to input line, |
| 7558 | // you'll get "output error: Interrupted system call" |
| 7559 | // and exit. |
| 7560 | //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo); |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7561 | #endif |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7562 | return 1; // "tty" |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7563 | } |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7564 | return 0; // "not a tty" |
| 7565 | } |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7566 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7567 | static BcStatus bc_vm_run(void) |
| 7568 | { |
| 7569 | BcStatus st = bc_vm_exec(); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7570 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 7571 | if (G_exiting) // it was actually "halt" or "quit" |
| 7572 | st = EXIT_SUCCESS; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7573 | bc_vm_free(); |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 7574 | # if ENABLE_FEATURE_EDITING |
| 7575 | free_line_input_t(G.line_input_state); |
| 7576 | # endif |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 7577 | FREE_G(); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7578 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7579 | return st; |
| 7580 | } |
| 7581 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7582 | #if ENABLE_BC |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7583 | int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7584 | int bc_main(int argc UNUSED_PARAM, char **argv) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7585 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7586 | int is_tty; |
| 7587 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7588 | INIT_G(); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7589 | G.sbgn = G.send = '"'; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7590 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7591 | is_tty = bc_vm_init("BC_LINE_LENGTH"); |
| 7592 | |
| 7593 | bc_args(argv); |
| 7594 | |
| 7595 | if (is_tty && !(option_mask32 & BC_FLAG_Q)) |
| 7596 | bc_vm_info(); |
| 7597 | |
| 7598 | return bc_vm_run(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7599 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7600 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7601 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7602 | #if ENABLE_DC |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7603 | int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7604 | int dc_main(int argc UNUSED_PARAM, char **argv) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7605 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7606 | int noscript; |
| 7607 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7608 | INIT_G(); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7609 | G.sbgn = '['; |
| 7610 | G.send = ']'; |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 7611 | /* |
| 7612 | * TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width |
| 7613 | * 1 char wider than bc from the same package. |
| 7614 | * Both default width, and xC_LINE_LENGTH=N are wider: |
| 7615 | * "DC_LINE_LENGTH=5 dc -e'123456 p'" prints: |
| 7616 | * 1234\ |
| 7617 | * 56 |
| 7618 | * "echo '123456' | BC_LINE_LENGTH=5 bc" prints: |
| 7619 | * 123\ |
| 7620 | * 456 |
| 7621 | * Do the same, or it's a bug? |
| 7622 | */ |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7623 | bc_vm_init("DC_LINE_LENGTH"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7624 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7625 | // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs |
| 7626 | noscript = BC_FLAG_I; |
| 7627 | for (;;) { |
| 7628 | int n = getopt(argc, argv, "e:f:x"); |
| 7629 | if (n <= 0) |
| 7630 | break; |
| 7631 | switch (n) { |
| 7632 | case 'e': |
| 7633 | noscript = 0; |
| 7634 | n = bc_vm_process(optarg); |
| 7635 | if (n) return n; |
| 7636 | break; |
| 7637 | case 'f': |
| 7638 | noscript = 0; |
| 7639 | bc_vm_file(optarg); |
| 7640 | break; |
| 7641 | case 'x': |
| 7642 | option_mask32 |= DC_FLAG_X; |
| 7643 | break; |
| 7644 | default: |
| 7645 | bb_show_usage(); |
| 7646 | } |
| 7647 | } |
| 7648 | argv += optind; |
| 7649 | |
| 7650 | while (*argv) { |
| 7651 | noscript = 0; |
| 7652 | bc_vec_push(&G.files, argv++); |
| 7653 | } |
| 7654 | |
| 7655 | option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin |
| 7656 | |
| 7657 | return bc_vm_run(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7658 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7659 | #endif |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 7660 | |
| 7661 | #endif // not DC_SMALL |