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, |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 189 | BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr_empty_ok() 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 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 224 | typedef enum BcInst { |
| 225 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 226 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 227 | BC_INST_INC_PRE, |
| 228 | BC_INST_DEC_PRE, |
| 229 | BC_INST_INC_POST, |
| 230 | BC_INST_DEC_POST, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 231 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 232 | |
| 233 | BC_INST_NEG, |
| 234 | |
| 235 | BC_INST_POWER, |
| 236 | BC_INST_MULTIPLY, |
| 237 | BC_INST_DIVIDE, |
| 238 | BC_INST_MODULUS, |
| 239 | BC_INST_PLUS, |
| 240 | BC_INST_MINUS, |
| 241 | |
| 242 | BC_INST_REL_EQ, |
| 243 | BC_INST_REL_LE, |
| 244 | BC_INST_REL_GE, |
| 245 | BC_INST_REL_NE, |
| 246 | BC_INST_REL_LT, |
| 247 | BC_INST_REL_GT, |
| 248 | |
| 249 | BC_INST_BOOL_NOT, |
| 250 | BC_INST_BOOL_OR, |
| 251 | BC_INST_BOOL_AND, |
| 252 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 253 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 254 | BC_INST_ASSIGN_POWER, |
| 255 | BC_INST_ASSIGN_MULTIPLY, |
| 256 | BC_INST_ASSIGN_DIVIDE, |
| 257 | BC_INST_ASSIGN_MODULUS, |
| 258 | BC_INST_ASSIGN_PLUS, |
| 259 | BC_INST_ASSIGN_MINUS, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 260 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 261 | BC_INST_ASSIGN, |
| 262 | |
| 263 | BC_INST_NUM, |
| 264 | BC_INST_VAR, |
| 265 | BC_INST_ARRAY_ELEM, |
| 266 | BC_INST_ARRAY, |
| 267 | |
| 268 | BC_INST_SCALE_FUNC, |
| 269 | BC_INST_IBASE, |
| 270 | BC_INST_SCALE, |
| 271 | BC_INST_LAST, |
| 272 | BC_INST_LENGTH, |
| 273 | BC_INST_READ, |
| 274 | BC_INST_OBASE, |
| 275 | BC_INST_SQRT, |
| 276 | |
| 277 | BC_INST_PRINT, |
| 278 | BC_INST_PRINT_POP, |
| 279 | BC_INST_STR, |
| 280 | BC_INST_PRINT_STR, |
| 281 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 282 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 283 | BC_INST_JUMP, |
| 284 | BC_INST_JUMP_ZERO, |
| 285 | |
| 286 | BC_INST_CALL, |
| 287 | |
| 288 | BC_INST_RET, |
| 289 | BC_INST_RET0, |
| 290 | |
| 291 | BC_INST_HALT, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 292 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 293 | |
| 294 | BC_INST_POP, |
| 295 | BC_INST_POP_EXEC, |
| 296 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 297 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 298 | BC_INST_MODEXP, |
| 299 | BC_INST_DIVMOD, |
| 300 | |
| 301 | BC_INST_EXECUTE, |
| 302 | BC_INST_EXEC_COND, |
| 303 | |
| 304 | BC_INST_ASCIIFY, |
| 305 | BC_INST_PRINT_STREAM, |
| 306 | |
| 307 | BC_INST_PRINT_STACK, |
| 308 | BC_INST_CLEAR_STACK, |
| 309 | BC_INST_STACK_LEN, |
| 310 | BC_INST_DUPLICATE, |
| 311 | BC_INST_SWAP, |
| 312 | |
| 313 | BC_INST_LOAD, |
| 314 | BC_INST_PUSH_VAR, |
| 315 | BC_INST_PUSH_TO_VAR, |
| 316 | |
| 317 | BC_INST_QUIT, |
| 318 | BC_INST_NQUIT, |
| 319 | |
| 320 | BC_INST_INVALID = -1, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 321 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 322 | |
| 323 | } BcInst; |
| 324 | |
| 325 | typedef struct BcId { |
| 326 | char *name; |
| 327 | size_t idx; |
| 328 | } BcId; |
| 329 | |
| 330 | typedef struct BcFunc { |
| 331 | BcVec code; |
| 332 | BcVec labels; |
| 333 | size_t nparams; |
| 334 | BcVec autos; |
| 335 | } BcFunc; |
| 336 | |
| 337 | typedef enum BcResultType { |
| 338 | |
| 339 | BC_RESULT_TEMP, |
| 340 | |
| 341 | BC_RESULT_VAR, |
| 342 | BC_RESULT_ARRAY_ELEM, |
| 343 | BC_RESULT_ARRAY, |
| 344 | |
| 345 | BC_RESULT_STR, |
| 346 | |
| 347 | BC_RESULT_IBASE, |
| 348 | BC_RESULT_SCALE, |
| 349 | BC_RESULT_LAST, |
| 350 | |
| 351 | // These are between to calculate ibase, obase, and last from instructions. |
| 352 | BC_RESULT_CONSTANT, |
| 353 | BC_RESULT_ONE, |
| 354 | |
| 355 | BC_RESULT_OBASE, |
| 356 | |
| 357 | } BcResultType; |
| 358 | |
| 359 | typedef union BcResultData { |
| 360 | BcNum n; |
| 361 | BcVec v; |
| 362 | BcId id; |
| 363 | } BcResultData; |
| 364 | |
| 365 | typedef struct BcResult { |
| 366 | BcResultType t; |
| 367 | BcResultData d; |
| 368 | } BcResult; |
| 369 | |
| 370 | typedef struct BcInstPtr { |
| 371 | size_t func; |
| 372 | size_t idx; |
| 373 | size_t len; |
| 374 | } BcInstPtr; |
| 375 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 376 | // BC_LEX_NEG is not used in lexing; it is only for parsing. |
| 377 | typedef enum BcLexType { |
| 378 | |
| 379 | BC_LEX_EOF, |
| 380 | BC_LEX_INVALID, |
| 381 | |
| 382 | BC_LEX_OP_INC, |
| 383 | BC_LEX_OP_DEC, |
| 384 | |
| 385 | BC_LEX_NEG, |
| 386 | |
| 387 | BC_LEX_OP_POWER, |
| 388 | BC_LEX_OP_MULTIPLY, |
| 389 | BC_LEX_OP_DIVIDE, |
| 390 | BC_LEX_OP_MODULUS, |
| 391 | BC_LEX_OP_PLUS, |
| 392 | BC_LEX_OP_MINUS, |
| 393 | |
| 394 | BC_LEX_OP_REL_EQ, |
| 395 | BC_LEX_OP_REL_LE, |
| 396 | BC_LEX_OP_REL_GE, |
| 397 | BC_LEX_OP_REL_NE, |
| 398 | BC_LEX_OP_REL_LT, |
| 399 | BC_LEX_OP_REL_GT, |
| 400 | |
| 401 | BC_LEX_OP_BOOL_NOT, |
| 402 | BC_LEX_OP_BOOL_OR, |
| 403 | BC_LEX_OP_BOOL_AND, |
| 404 | |
| 405 | BC_LEX_OP_ASSIGN_POWER, |
| 406 | BC_LEX_OP_ASSIGN_MULTIPLY, |
| 407 | BC_LEX_OP_ASSIGN_DIVIDE, |
| 408 | BC_LEX_OP_ASSIGN_MODULUS, |
| 409 | BC_LEX_OP_ASSIGN_PLUS, |
| 410 | BC_LEX_OP_ASSIGN_MINUS, |
| 411 | BC_LEX_OP_ASSIGN, |
| 412 | |
| 413 | BC_LEX_NLINE, |
| 414 | BC_LEX_WHITESPACE, |
| 415 | |
| 416 | BC_LEX_LPAREN, |
| 417 | BC_LEX_RPAREN, |
| 418 | |
| 419 | BC_LEX_LBRACKET, |
| 420 | BC_LEX_COMMA, |
| 421 | BC_LEX_RBRACKET, |
| 422 | |
| 423 | BC_LEX_LBRACE, |
| 424 | BC_LEX_SCOLON, |
| 425 | BC_LEX_RBRACE, |
| 426 | |
| 427 | BC_LEX_STR, |
| 428 | BC_LEX_NAME, |
| 429 | BC_LEX_NUMBER, |
| 430 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 431 | BC_LEX_KEY_1st_keyword, |
| 432 | BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 433 | BC_LEX_KEY_BREAK, |
| 434 | BC_LEX_KEY_CONTINUE, |
| 435 | BC_LEX_KEY_DEFINE, |
| 436 | BC_LEX_KEY_ELSE, |
| 437 | BC_LEX_KEY_FOR, |
| 438 | BC_LEX_KEY_HALT, |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 439 | // code uses "type - BC_LEX_KEY_IBASE + BC_INST_IBASE" construct, |
| 440 | BC_LEX_KEY_IBASE, // relative order should match for: BC_INST_IBASE |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 441 | BC_LEX_KEY_IF, |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 442 | BC_LEX_KEY_LAST, // relative order should match for: BC_INST_LAST |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 443 | BC_LEX_KEY_LENGTH, |
| 444 | BC_LEX_KEY_LIMITS, |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 445 | BC_LEX_KEY_OBASE, // relative order should match for: BC_INST_OBASE |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 446 | BC_LEX_KEY_PRINT, |
| 447 | BC_LEX_KEY_QUIT, |
| 448 | BC_LEX_KEY_READ, |
| 449 | BC_LEX_KEY_RETURN, |
| 450 | BC_LEX_KEY_SCALE, |
| 451 | BC_LEX_KEY_SQRT, |
| 452 | BC_LEX_KEY_WHILE, |
| 453 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 454 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 455 | BC_LEX_EQ_NO_REG, |
| 456 | BC_LEX_OP_MODEXP, |
| 457 | BC_LEX_OP_DIVMOD, |
| 458 | |
| 459 | BC_LEX_COLON, |
| 460 | BC_LEX_ELSE, |
| 461 | BC_LEX_EXECUTE, |
| 462 | BC_LEX_PRINT_STACK, |
| 463 | BC_LEX_CLEAR_STACK, |
| 464 | BC_LEX_STACK_LEVEL, |
| 465 | BC_LEX_DUPLICATE, |
| 466 | BC_LEX_SWAP, |
| 467 | BC_LEX_POP, |
| 468 | |
| 469 | BC_LEX_ASCIIFY, |
| 470 | BC_LEX_PRINT_STREAM, |
| 471 | |
| 472 | BC_LEX_STORE_IBASE, |
| 473 | BC_LEX_STORE_SCALE, |
| 474 | BC_LEX_LOAD, |
| 475 | BC_LEX_LOAD_POP, |
| 476 | BC_LEX_STORE_PUSH, |
| 477 | BC_LEX_STORE_OBASE, |
| 478 | BC_LEX_PRINT_POP, |
| 479 | BC_LEX_NQUIT, |
| 480 | BC_LEX_SCALE_FACTOR, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 481 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 482 | } BcLexType; |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 483 | // must match order of BC_LEX_KEY_foo etc above |
| 484 | #if ENABLE_BC |
| 485 | struct BcLexKeyword { |
| 486 | char name8[8]; |
| 487 | }; |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 488 | #define BC_LEX_KW_ENTRY(a, b) \ |
| 489 | { .name8 = a /*, .posix = b */ } |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 490 | static const struct BcLexKeyword bc_lex_kws[20] = { |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 491 | BC_LEX_KW_ENTRY("auto" , 1), // 0 |
| 492 | BC_LEX_KW_ENTRY("break" , 1), // 1 |
| 493 | BC_LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL |
| 494 | BC_LEX_KW_ENTRY("define" , 1), // 3 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 495 | |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 496 | BC_LEX_KW_ENTRY("else" , 0), // 4 |
| 497 | BC_LEX_KW_ENTRY("for" , 1), // 5 |
| 498 | BC_LEX_KW_ENTRY("halt" , 0), // 6 |
| 499 | BC_LEX_KW_ENTRY("ibase" , 1), // 7 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 500 | |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 501 | BC_LEX_KW_ENTRY("if" , 1), // 8 |
| 502 | BC_LEX_KW_ENTRY("last" , 0), // 9 |
| 503 | BC_LEX_KW_ENTRY("length" , 1), // 10 |
| 504 | BC_LEX_KW_ENTRY("limits" , 0), // 11 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 505 | |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 506 | BC_LEX_KW_ENTRY("obase" , 1), // 12 |
| 507 | BC_LEX_KW_ENTRY("print" , 0), // 13 |
| 508 | BC_LEX_KW_ENTRY("quit" , 1), // 14 |
| 509 | BC_LEX_KW_ENTRY("read" , 0), // 15 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 510 | |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 511 | BC_LEX_KW_ENTRY("return" , 1), // 16 |
| 512 | BC_LEX_KW_ENTRY("scale" , 1), // 17 |
| 513 | BC_LEX_KW_ENTRY("sqrt" , 1), // 18 |
| 514 | BC_LEX_KW_ENTRY("while" , 1), // 19 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 515 | }; |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 516 | #undef BC_LEX_KW_ENTRY |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 517 | enum { |
| 518 | POSIX_KWORD_MASK = 0 |
| 519 | | (1 << 0) |
| 520 | | (1 << 1) |
| 521 | | (0 << 2) |
| 522 | | (1 << 3) |
| 523 | \ |
| 524 | | (0 << 4) |
| 525 | | (1 << 5) |
| 526 | | (0 << 6) |
| 527 | | (1 << 7) |
| 528 | \ |
| 529 | | (1 << 8) |
| 530 | | (0 << 9) |
| 531 | | (1 << 10) |
| 532 | | (0 << 11) |
| 533 | \ |
| 534 | | (1 << 12) |
| 535 | | (0 << 13) |
| 536 | | (1 << 14) |
| 537 | | (0 << 15) |
| 538 | \ |
| 539 | | (1 << 16) |
| 540 | | (1 << 17) |
| 541 | | (1 << 18) |
| 542 | | (1 << 19) |
| 543 | }; |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 544 | #define bc_lex_kws_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK) |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 545 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 546 | |
| 547 | struct BcLex; |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 548 | typedef BcStatus (*BcLexNext)(struct BcLex *) FAST_FUNC; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 549 | |
| 550 | typedef struct BcLex { |
| 551 | |
| 552 | const char *buf; |
| 553 | size_t i; |
| 554 | size_t line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 555 | size_t len; |
| 556 | bool newline; |
| 557 | |
| 558 | struct { |
| 559 | BcLexType t; |
| 560 | BcLexType last; |
| 561 | BcVec v; |
| 562 | } t; |
| 563 | |
| 564 | BcLexNext next; |
| 565 | |
| 566 | } BcLex; |
| 567 | |
| 568 | #define BC_PARSE_STREND ((char) UCHAR_MAX) |
| 569 | |
Denys Vlasenko | e55a572 | 2018-12-06 12:47:17 +0100 | [diff] [blame] | 570 | #define BC_PARSE_REL (1 << 0) |
| 571 | #define BC_PARSE_PRINT (1 << 1) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 572 | #define BC_PARSE_NOCALL (1 << 2) |
| 573 | #define BC_PARSE_NOREAD (1 << 3) |
Denys Vlasenko | e55a572 | 2018-12-06 12:47:17 +0100 | [diff] [blame] | 574 | #define BC_PARSE_ARRAY (1 << 4) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 575 | |
| 576 | #define BC_PARSE_TOP_FLAG_PTR(parse) ((uint8_t *) bc_vec_top(&(parse)->flags)) |
| 577 | #define BC_PARSE_TOP_FLAG(parse) (*(BC_PARSE_TOP_FLAG_PTR(parse))) |
| 578 | |
| 579 | #define BC_PARSE_FLAG_FUNC_INNER (1 << 0) |
| 580 | #define BC_PARSE_FUNC_INNER(parse) \ |
| 581 | (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC_INNER) |
| 582 | |
| 583 | #define BC_PARSE_FLAG_FUNC (1 << 1) |
| 584 | #define BC_PARSE_FUNC(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC) |
| 585 | |
| 586 | #define BC_PARSE_FLAG_BODY (1 << 2) |
| 587 | #define BC_PARSE_BODY(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_BODY) |
| 588 | |
| 589 | #define BC_PARSE_FLAG_LOOP (1 << 3) |
| 590 | #define BC_PARSE_LOOP(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP) |
| 591 | |
| 592 | #define BC_PARSE_FLAG_LOOP_INNER (1 << 4) |
| 593 | #define BC_PARSE_LOOP_INNER(parse) \ |
| 594 | (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP_INNER) |
| 595 | |
| 596 | #define BC_PARSE_FLAG_IF (1 << 5) |
| 597 | #define BC_PARSE_IF(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF) |
| 598 | |
| 599 | #define BC_PARSE_FLAG_ELSE (1 << 6) |
| 600 | #define BC_PARSE_ELSE(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_ELSE) |
| 601 | |
| 602 | #define BC_PARSE_FLAG_IF_END (1 << 7) |
| 603 | #define BC_PARSE_IF_END(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF_END) |
| 604 | |
| 605 | #define BC_PARSE_CAN_EXEC(parse) \ |
| 606 | (!(BC_PARSE_TOP_FLAG(parse) & \ |
| 607 | (BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_BODY | \ |
| 608 | BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER | BC_PARSE_FLAG_IF | \ |
| 609 | BC_PARSE_FLAG_ELSE | BC_PARSE_FLAG_IF_END))) |
| 610 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 611 | struct BcParse; |
| 612 | |
| 613 | struct BcProgram; |
| 614 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 615 | typedef BcStatus (*BcParseParse)(struct BcParse *) FAST_FUNC; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 616 | |
| 617 | typedef struct BcParse { |
| 618 | |
| 619 | BcParseParse parse; |
| 620 | |
| 621 | BcLex l; |
| 622 | |
| 623 | BcVec flags; |
| 624 | |
| 625 | BcVec exits; |
| 626 | BcVec conds; |
| 627 | |
| 628 | BcVec ops; |
| 629 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 630 | BcFunc *func; |
| 631 | size_t fidx; |
| 632 | |
| 633 | size_t nbraces; |
| 634 | bool auto_part; |
| 635 | |
| 636 | } BcParse; |
| 637 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 638 | typedef struct BcProgram { |
| 639 | |
| 640 | size_t len; |
| 641 | size_t scale; |
| 642 | |
| 643 | BcNum ib; |
| 644 | size_t ib_t; |
| 645 | BcNum ob; |
| 646 | size_t ob_t; |
| 647 | |
| 648 | BcNum hexb; |
| 649 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 650 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 651 | BcNum strmb; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 652 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 653 | |
| 654 | BcVec results; |
| 655 | BcVec stack; |
| 656 | |
| 657 | BcVec fns; |
| 658 | BcVec fn_map; |
| 659 | |
| 660 | BcVec vars; |
| 661 | BcVec var_map; |
| 662 | |
| 663 | BcVec arrs; |
| 664 | BcVec arr_map; |
| 665 | |
| 666 | BcVec strs; |
| 667 | BcVec consts; |
| 668 | |
| 669 | const char *file; |
| 670 | |
| 671 | BcNum last; |
| 672 | BcNum zero; |
| 673 | BcNum one; |
| 674 | |
| 675 | size_t nchars; |
| 676 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 677 | } BcProgram; |
| 678 | |
| 679 | #define BC_PROG_STACK(s, n) ((s)->len >= ((size_t) n)) |
| 680 | |
| 681 | #define BC_PROG_MAIN (0) |
| 682 | #define BC_PROG_READ (1) |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 683 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 684 | #define BC_PROG_REQ_FUNCS (2) |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 685 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 686 | |
| 687 | #define BC_PROG_STR(n) (!(n)->num && !(n)->cap) |
| 688 | #define BC_PROG_NUM(r, n) \ |
| 689 | ((r)->t != BC_RESULT_ARRAY && (r)->t != BC_RESULT_STR && !BC_PROG_STR(n)) |
| 690 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 691 | #define BC_FLAG_W (1 << 0) |
| 692 | #define BC_FLAG_V (1 << 1) |
| 693 | #define BC_FLAG_S (1 << 2) |
| 694 | #define BC_FLAG_Q (1 << 3) |
| 695 | #define BC_FLAG_L (1 << 4) |
| 696 | #define BC_FLAG_I (1 << 5) |
| 697 | #define DC_FLAG_X (1 << 6) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 698 | |
| 699 | #define BC_MAX(a, b) ((a) > (b) ? (a) : (b)) |
| 700 | #define BC_MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 701 | |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 702 | #define BC_MAX_OBASE ((unsigned) 999) |
| 703 | #define BC_MAX_DIM ((unsigned) INT_MAX) |
| 704 | #define BC_MAX_SCALE ((unsigned) UINT_MAX) |
| 705 | #define BC_MAX_STRING ((unsigned) UINT_MAX - 1) |
| 706 | #define BC_MAX_NUM BC_MAX_STRING |
| 707 | // Unused apart from "limits" message. Just show a "biggish number" there. |
| 708 | //#define BC_MAX_NAME BC_MAX_STRING |
| 709 | //#define BC_MAX_EXP ((unsigned long) LONG_MAX) |
| 710 | //#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1) |
| 711 | #define BC_MAX_NAME_STR "999999999" |
| 712 | #define BC_MAX_EXP_STR "999999999" |
| 713 | #define BC_MAX_VARS_STR "999999999" |
| 714 | |
| 715 | #define BC_MAX_OBASE_STR "999" |
| 716 | |
| 717 | #if INT_MAX == 2147483647 |
| 718 | # define BC_MAX_DIM_STR "2147483647" |
| 719 | #elif INT_MAX == 9223372036854775807 |
| 720 | # define BC_MAX_DIM_STR "9223372036854775807" |
| 721 | #else |
| 722 | # error Strange INT_MAX |
| 723 | #endif |
| 724 | |
| 725 | #if UINT_MAX == 4294967295 |
| 726 | # define BC_MAX_SCALE_STR "4294967295" |
| 727 | # define BC_MAX_STRING_STR "4294967294" |
| 728 | #elif UINT_MAX == 18446744073709551615 |
| 729 | # define BC_MAX_SCALE_STR "18446744073709551615" |
| 730 | # define BC_MAX_STRING_STR "18446744073709551614" |
| 731 | #else |
| 732 | # error Strange UINT_MAX |
| 733 | #endif |
| 734 | #define BC_MAX_NUM_STR BC_MAX_STRING_STR |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 735 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 736 | struct globals { |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 737 | IF_FEATURE_BC_SIGNALS(smallint ttyin;) |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 738 | IF_FEATURE_CLEAN_UP(smallint exiting;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 739 | char sbgn; |
| 740 | char send; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 741 | |
| 742 | BcParse prs; |
| 743 | BcProgram prog; |
| 744 | |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 745 | // For error messages. Can be set to current parsed line, |
| 746 | // or [TODO] to current executing line (can be before last parsed one) |
| 747 | unsigned err_line; |
| 748 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 749 | BcVec files; |
| 750 | |
| 751 | char *env_args; |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 752 | |
| 753 | #if ENABLE_FEATURE_EDITING |
| 754 | line_input_t *line_input_state; |
| 755 | #endif |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 756 | } FIX_ALIASING; |
| 757 | #define G (*ptr_to_globals) |
| 758 | #define INIT_G() do { \ |
| 759 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 760 | } while (0) |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 761 | #define FREE_G() do { \ |
| 762 | FREE_PTR_TO_GLOBALS(); \ |
| 763 | } while (0) |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 764 | #define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S)) |
| 765 | #define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W)) |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 766 | #define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X)) |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 767 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | b9c321d | 2018-12-07 12:41:42 +0100 | [diff] [blame] | 768 | # define G_interrupt bb_got_signal |
| 769 | # define G_ttyin G.ttyin |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 770 | #else |
Denys Vlasenko | b9c321d | 2018-12-07 12:41:42 +0100 | [diff] [blame] | 771 | # define G_interrupt 0 |
| 772 | # define G_ttyin 0 |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 773 | #endif |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 774 | #if ENABLE_FEATURE_CLEAN_UP |
| 775 | # define G_exiting G.exiting |
| 776 | #else |
| 777 | # define G_exiting 0 |
| 778 | #endif |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 779 | #define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b')) |
| 780 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 781 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 782 | |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 783 | // 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] | 784 | // true if the token is valid in an expression, false otherwise. |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 785 | enum { |
| 786 | BC_PARSE_EXPRS_BITS = 0 |
| 787 | + ((uint64_t)((0 << 0)+(0 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (0*8)) |
| 788 | + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (1*8)) |
| 789 | + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (2*8)) |
| 790 | + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(0 << 3)+(0 << 4)+(1 << 5)+(1 << 6)+(0 << 7)) << (3*8)) |
| 791 | + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(1 << 6)+(1 << 7)) << (4*8)) |
| 792 | + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (5*8)) |
| 793 | + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (6*8)) |
| 794 | + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(0 << 3) ) << (7*8)) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 795 | }; |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 796 | static ALWAYS_INLINE long bc_parse_exprs(unsigned i) |
| 797 | { |
| 798 | #if ULONG_MAX > 0xffffffff |
| 799 | // 64-bit version (will not work correctly for 32-bit longs!) |
| 800 | return BC_PARSE_EXPRS_BITS & (1UL << i); |
| 801 | #else |
| 802 | // 32-bit version |
| 803 | unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS; |
| 804 | if (i >= 32) { |
| 805 | m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32); |
| 806 | i &= 31; |
| 807 | } |
| 808 | return m & (1UL << i); |
| 809 | #endif |
| 810 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 811 | |
| 812 | // 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] | 813 | static const uint8_t bc_parse_ops[] = { |
| 814 | #define OP(p,l) ((int)(l) * 0x10 + (p)) |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 815 | OP(0, false), OP( 0, false ), // inc dec |
| 816 | OP(1, false), // neg |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 817 | OP(2, false), |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 818 | OP(3, true ), OP( 3, true ), OP( 3, true ), // pow mul div |
| 819 | OP(4, true ), OP( 4, true ), // mod + - |
| 820 | OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < > |
| 821 | OP(1, false), // not |
| 822 | OP(7, true ), OP( 7, true ), // or and |
| 823 | OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= += |
| 824 | OP(5, false), OP( 5, false ), // -= = |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 825 | #undef OP |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 826 | }; |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 827 | #define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f) |
| 828 | #define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 829 | |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 830 | // Byte array of up to 4 BC_LEX's, packed into 32-bit word |
| 831 | typedef uint32_t BcParseNext; |
| 832 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 833 | // These identify what tokens can come after expressions in certain cases. |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 834 | enum { |
| 835 | #define BC_PARSE_NEXT4(a,b,c,d) ( (a) | ((b)<<8) | ((c)<<16) | ((((d)|0x80)<<24)) ) |
| 836 | #define BC_PARSE_NEXT2(a,b) BC_PARSE_NEXT4(a,b,0xff,0xff) |
| 837 | #define BC_PARSE_NEXT1(a) BC_PARSE_NEXT4(a,0xff,0xff,0xff) |
| 838 | bc_parse_next_expr = BC_PARSE_NEXT4(BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_RBRACE, BC_LEX_EOF), |
| 839 | bc_parse_next_param = BC_PARSE_NEXT2(BC_LEX_RPAREN, BC_LEX_COMMA), |
| 840 | bc_parse_next_print = BC_PARSE_NEXT4(BC_LEX_COMMA, BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_EOF), |
| 841 | bc_parse_next_rel = BC_PARSE_NEXT1(BC_LEX_RPAREN), |
| 842 | bc_parse_next_elem = BC_PARSE_NEXT1(BC_LEX_RBRACKET), |
| 843 | bc_parse_next_for = BC_PARSE_NEXT1(BC_LEX_SCOLON), |
| 844 | bc_parse_next_read = BC_PARSE_NEXT2(BC_LEX_NLINE, BC_LEX_EOF), |
| 845 | #undef BC_PARSE_NEXT4 |
| 846 | #undef BC_PARSE_NEXT2 |
| 847 | #undef BC_PARSE_NEXT1 |
| 848 | }; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 849 | #endif // ENABLE_BC |
| 850 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 851 | #if ENABLE_DC |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 852 | static const //BcLexType - should be this type, but narrower type saves size: |
| 853 | uint8_t |
| 854 | dc_lex_regs[] = { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 855 | BC_LEX_OP_REL_EQ, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_NE, |
| 856 | BC_LEX_OP_REL_LT, BC_LEX_OP_REL_GT, BC_LEX_SCOLON, BC_LEX_COLON, |
| 857 | BC_LEX_ELSE, BC_LEX_LOAD, BC_LEX_LOAD_POP, BC_LEX_OP_ASSIGN, |
| 858 | BC_LEX_STORE_PUSH, |
| 859 | }; |
| 860 | |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 861 | static const //BcLexType - should be this type |
| 862 | uint8_t |
| 863 | dc_lex_tokens[] = { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 864 | BC_LEX_OP_MODULUS, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_LPAREN, |
| 865 | BC_LEX_INVALID, BC_LEX_OP_MULTIPLY, BC_LEX_OP_PLUS, BC_LEX_INVALID, |
| 866 | BC_LEX_OP_MINUS, BC_LEX_INVALID, BC_LEX_OP_DIVIDE, |
| 867 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, |
| 868 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, |
| 869 | BC_LEX_INVALID, BC_LEX_INVALID, |
| 870 | BC_LEX_COLON, BC_LEX_SCOLON, BC_LEX_OP_REL_GT, BC_LEX_OP_REL_EQ, |
| 871 | BC_LEX_OP_REL_LT, BC_LEX_KEY_READ, BC_LEX_INVALID, |
| 872 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, |
| 873 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_EQ_NO_REG, BC_LEX_INVALID, |
| 874 | BC_LEX_KEY_IBASE, BC_LEX_INVALID, BC_LEX_KEY_SCALE, BC_LEX_LOAD_POP, |
| 875 | BC_LEX_INVALID, BC_LEX_OP_BOOL_NOT, BC_LEX_KEY_OBASE, BC_LEX_PRINT_STREAM, |
| 876 | BC_LEX_NQUIT, BC_LEX_POP, BC_LEX_STORE_PUSH, BC_LEX_INVALID, BC_LEX_INVALID, |
| 877 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_SCALE_FACTOR, BC_LEX_INVALID, |
| 878 | BC_LEX_KEY_LENGTH, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, |
| 879 | BC_LEX_OP_POWER, BC_LEX_NEG, BC_LEX_INVALID, |
| 880 | BC_LEX_ASCIIFY, BC_LEX_INVALID, BC_LEX_CLEAR_STACK, BC_LEX_DUPLICATE, |
| 881 | BC_LEX_ELSE, BC_LEX_PRINT_STACK, BC_LEX_INVALID, BC_LEX_INVALID, |
| 882 | BC_LEX_STORE_IBASE, BC_LEX_INVALID, BC_LEX_STORE_SCALE, BC_LEX_LOAD, |
| 883 | BC_LEX_INVALID, BC_LEX_PRINT_POP, BC_LEX_STORE_OBASE, BC_LEX_KEY_PRINT, |
| 884 | BC_LEX_KEY_QUIT, BC_LEX_SWAP, BC_LEX_OP_ASSIGN, BC_LEX_INVALID, |
| 885 | BC_LEX_INVALID, BC_LEX_KEY_SQRT, BC_LEX_INVALID, BC_LEX_EXECUTE, |
| 886 | BC_LEX_INVALID, BC_LEX_STACK_LEVEL, |
| 887 | BC_LEX_LBRACE, BC_LEX_OP_MODEXP, BC_LEX_INVALID, BC_LEX_OP_DIVMOD, |
| 888 | BC_LEX_INVALID |
| 889 | }; |
| 890 | |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 891 | static const //BcInst - should be this type. Using signed narrow type since BC_INST_INVALID is -1 |
| 892 | int8_t |
| 893 | dc_parse_insts[] = { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 894 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE, |
| 895 | BC_INST_INVALID, BC_INST_POWER, BC_INST_MULTIPLY, BC_INST_DIVIDE, |
| 896 | BC_INST_MODULUS, BC_INST_PLUS, BC_INST_MINUS, |
| 897 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 898 | BC_INST_INVALID, BC_INST_INVALID, |
| 899 | BC_INST_BOOL_NOT, BC_INST_INVALID, BC_INST_INVALID, |
| 900 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 901 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 902 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GT, BC_INST_INVALID, |
| 903 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE, |
| 904 | BC_INST_INVALID, BC_INST_INVALID, |
| 905 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 906 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 907 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_IBASE, |
| 908 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_LENGTH, BC_INST_INVALID, |
| 909 | BC_INST_OBASE, BC_INST_PRINT, BC_INST_QUIT, BC_INST_INVALID, |
| 910 | BC_INST_INVALID, BC_INST_SCALE, BC_INST_SQRT, BC_INST_INVALID, |
| 911 | BC_INST_REL_EQ, BC_INST_MODEXP, BC_INST_DIVMOD, BC_INST_INVALID, |
| 912 | BC_INST_INVALID, BC_INST_EXECUTE, BC_INST_PRINT_STACK, BC_INST_CLEAR_STACK, |
| 913 | BC_INST_STACK_LEN, BC_INST_DUPLICATE, BC_INST_SWAP, BC_INST_POP, |
| 914 | BC_INST_ASCIIFY, BC_INST_PRINT_STREAM, BC_INST_INVALID, BC_INST_INVALID, |
| 915 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 916 | BC_INST_PRINT, BC_INST_NQUIT, BC_INST_SCALE_FUNC, |
| 917 | }; |
| 918 | #endif // ENABLE_DC |
| 919 | |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 920 | // In configurations where errors abort instead of propagating error |
| 921 | // return code up the call chain, functions returning BC_STATUS |
| 922 | // actually don't return anything, they always succeed and return "void". |
| 923 | // A macro wrapper is provided, which makes this statement work: |
| 924 | // s = zbc_func(...) |
| 925 | // and makes it visible to the compiler that s is always zero, |
| 926 | // allowing compiler to optimize dead code after the statement. |
| 927 | // |
| 928 | // To make code more readable, each such function has a "z" |
| 929 | // ("always returning zero") prefix, i.e. zbc_foo or zdc_foo. |
| 930 | // |
| 931 | #if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 932 | # define ERRORS_ARE_FATAL 0 |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 933 | # define ERRORFUNC /*nothing*/ |
| 934 | # define ERROR_RETURN(a) a |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 935 | # define BC_STATUS BcStatus |
| 936 | # define RETURN_STATUS(v) return (v) |
| 937 | #else |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 938 | # define ERRORS_ARE_FATAL 1 |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 939 | # define ERRORFUNC NORETURN |
| 940 | # define ERROR_RETURN(a) /*nothing*/ |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 941 | # define BC_STATUS void |
| 942 | # define RETURN_STATUS(v) do { ((void)(v)); return; } while (0) |
| 943 | #endif |
| 944 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 945 | #define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg)) |
| 946 | #define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1) |
| 947 | #define BC_NUM_INT(n) ((n)->len - (n)->rdx) |
| 948 | //#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1) |
| 949 | static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b) |
| 950 | { |
| 951 | return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1; |
| 952 | } |
| 953 | //#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1) |
| 954 | static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale) |
| 955 | { |
| 956 | return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1; |
| 957 | } |
| 958 | |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 959 | typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC; |
| 960 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 961 | typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC; |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 962 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 963 | static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale, |
| 964 | BcNumBinaryOp op, size_t req); |
| 965 | static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 966 | static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 967 | static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 968 | static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 969 | static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 970 | static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 971 | |
| 972 | static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 973 | { |
| 974 | BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s; |
| 975 | (void) scale; |
| 976 | RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b))); |
| 977 | } |
| 978 | |
| 979 | static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 980 | { |
| 981 | BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a; |
| 982 | (void) scale; |
| 983 | RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b))); |
| 984 | } |
| 985 | |
| 986 | static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 987 | { |
| 988 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 989 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req)); |
| 990 | } |
| 991 | |
| 992 | static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 993 | { |
| 994 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 995 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req)); |
| 996 | } |
| 997 | |
| 998 | static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 999 | { |
| 1000 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 1001 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req)); |
| 1002 | } |
| 1003 | |
| 1004 | static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 1005 | { |
| 1006 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1)); |
| 1007 | } |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 1008 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1009 | static const BcNumBinaryOp zbc_program_ops[] = { |
| 1010 | zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1011 | }; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1012 | #if ERRORS_ARE_FATAL |
| 1013 | # define zbc_num_add(...) (zbc_num_add(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 1014 | # define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 1015 | # define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 1016 | # define zbc_num_div(...) (zbc_num_div(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 1017 | # define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 1018 | # define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 1019 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1020 | |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 1021 | static void fflush_and_check(void) |
| 1022 | { |
| 1023 | fflush_all(); |
| 1024 | if (ferror(stdout) || ferror(stderr)) |
| 1025 | bb_perror_msg_and_die("output error"); |
| 1026 | } |
| 1027 | |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 1028 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 1029 | #define QUIT_OR_RETURN_TO_MAIN \ |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 1030 | do { \ |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 1031 | 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] | 1032 | G_exiting = 1; \ |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 1033 | return BC_STATUS_FAILURE; \ |
| 1034 | } while (0) |
| 1035 | #else |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 1036 | #define QUIT_OR_RETURN_TO_MAIN quit() |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 1037 | #endif |
| 1038 | |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 1039 | static void quit(void) NORETURN; |
| 1040 | static void quit(void) |
| 1041 | { |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 1042 | if (ferror(stdin)) |
| 1043 | bb_perror_msg_and_die("input error"); |
| 1044 | fflush_and_check(); |
| 1045 | exit(0); |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 1046 | } |
| 1047 | |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 1048 | static void bc_verror_msg(const char *fmt, va_list p) |
| 1049 | { |
| 1050 | const char *sv = sv; /* for compiler */ |
| 1051 | if (G.prog.file) { |
| 1052 | sv = applet_name; |
| 1053 | applet_name = xasprintf("%s: %s:%u", applet_name, G.prog.file, G.err_line); |
| 1054 | } |
| 1055 | bb_verror_msg(fmt, p, NULL); |
| 1056 | if (G.prog.file) { |
| 1057 | free((char*)applet_name); |
| 1058 | applet_name = sv; |
| 1059 | } |
| 1060 | } |
| 1061 | |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1062 | static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...) |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1063 | { |
| 1064 | va_list p; |
| 1065 | |
| 1066 | va_start(p, fmt); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 1067 | bc_verror_msg(fmt, p); |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1068 | va_end(p); |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 1069 | |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 1070 | if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin) |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1071 | exit(1); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1072 | ERROR_RETURN(return BC_STATUS_FAILURE;) |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1073 | } |
| 1074 | |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1075 | #if ENABLE_BC |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1076 | static NOINLINE int bc_posix_error_fmt(const char *fmt, ...) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1077 | { |
| 1078 | va_list p; |
| 1079 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1080 | // Are non-POSIX constructs totally ok? |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 1081 | if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W))) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1082 | return BC_STATUS_SUCCESS; // yes |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1083 | |
| 1084 | va_start(p, fmt); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 1085 | bc_verror_msg(fmt, p); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1086 | va_end(p); |
| 1087 | |
| 1088 | // Do we treat non-POSIX constructs as errors? |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 1089 | if (!(option_mask32 & BC_FLAG_S)) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1090 | return BC_STATUS_SUCCESS; // no, it's a warning |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 1091 | if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1092 | exit(1); |
| 1093 | return BC_STATUS_FAILURE; |
| 1094 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1095 | #endif |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1096 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1097 | // We use error functions with "return bc_error(FMT[, PARAMS])" idiom. |
| 1098 | // This idiom begs for tail-call optimization, but for it to work, |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1099 | // function must not have caller-cleaned parameters on stack. |
| 1100 | // Unfortunately, vararg function API does exactly that on most arches. |
| 1101 | // 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] | 1102 | static ERRORFUNC int bc_error(const char *msg) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1103 | { |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1104 | ERROR_RETURN(return) bc_error_fmt("%s", msg); |
| 1105 | } |
| 1106 | static ERRORFUNC int bc_error_bad_character(char c) |
| 1107 | { |
| 1108 | ERROR_RETURN(return) bc_error_fmt("bad character '%c'", c); |
| 1109 | } |
| 1110 | static ERRORFUNC int bc_error_bad_expression(void) |
| 1111 | { |
| 1112 | ERROR_RETURN(return) bc_error("bad expression"); |
| 1113 | } |
| 1114 | static ERRORFUNC int bc_error_bad_token(void) |
| 1115 | { |
| 1116 | ERROR_RETURN(return) bc_error("bad token"); |
| 1117 | } |
| 1118 | static ERRORFUNC int bc_error_stack_has_too_few_elements(void) |
| 1119 | { |
| 1120 | ERROR_RETURN(return) bc_error("stack has too few elements"); |
| 1121 | } |
| 1122 | static ERRORFUNC int bc_error_variable_is_wrong_type(void) |
| 1123 | { |
| 1124 | ERROR_RETURN(return) bc_error("variable is wrong type"); |
| 1125 | } |
| 1126 | static ERRORFUNC int bc_error_nested_read_call(void) |
| 1127 | { |
| 1128 | ERROR_RETURN(return) bc_error("read() call inside of a read() call"); |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1129 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1130 | #if ENABLE_BC |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 1131 | static int bc_POSIX_requires(const char *msg) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1132 | { |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 1133 | return bc_posix_error_fmt("POSIX requires %s", msg); |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1134 | } |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 1135 | static int bc_POSIX_does_not_allow(const char *msg) |
| 1136 | { |
| 1137 | return bc_posix_error_fmt("%s%s", "POSIX does not allow ", msg); |
| 1138 | } |
| 1139 | static int bc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg) |
| 1140 | { |
| 1141 | return bc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; the following is bad:", msg); |
| 1142 | } |
| 1143 | static int bc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg) |
| 1144 | { |
| 1145 | return bc_posix_error_fmt("%san empty %s expression in a for loop", "POSIX does not allow ", msg); |
| 1146 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1147 | #endif |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1148 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1149 | static void bc_vec_grow(BcVec *v, size_t n) |
| 1150 | { |
| 1151 | size_t cap = v->cap * 2; |
| 1152 | while (cap < v->len + n) cap *= 2; |
| 1153 | v->v = xrealloc(v->v, v->size * cap); |
| 1154 | v->cap = cap; |
| 1155 | } |
| 1156 | |
| 1157 | static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor) |
| 1158 | { |
| 1159 | v->size = esize; |
| 1160 | v->cap = BC_VEC_START_CAP; |
| 1161 | v->len = 0; |
| 1162 | v->dtor = dtor; |
| 1163 | v->v = xmalloc(esize * BC_VEC_START_CAP); |
| 1164 | } |
| 1165 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1166 | static void bc_char_vec_init(BcVec *v) |
| 1167 | { |
| 1168 | bc_vec_init(v, sizeof(char), NULL); |
| 1169 | } |
| 1170 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1171 | static void bc_vec_expand(BcVec *v, size_t req) |
| 1172 | { |
| 1173 | if (v->cap < req) { |
| 1174 | v->v = xrealloc(v->v, v->size * req); |
| 1175 | v->cap = req; |
| 1176 | } |
| 1177 | } |
| 1178 | |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 1179 | static void bc_vec_pop(BcVec *v) |
| 1180 | { |
| 1181 | v->len--; |
| 1182 | if (v->dtor) |
| 1183 | v->dtor(v->v + (v->size * v->len)); |
| 1184 | } |
Denys Vlasenko | 2fa11b6 | 2018-12-06 12:34:39 +0100 | [diff] [blame] | 1185 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1186 | static void bc_vec_npop(BcVec *v, size_t n) |
| 1187 | { |
| 1188 | if (!v->dtor) |
| 1189 | v->len -= n; |
| 1190 | else { |
| 1191 | size_t len = v->len - n; |
| 1192 | while (v->len > len) v->dtor(v->v + (v->size * --v->len)); |
| 1193 | } |
| 1194 | } |
| 1195 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1196 | static void bc_vec_pop_all(BcVec *v) |
| 1197 | { |
| 1198 | bc_vec_npop(v, v->len); |
| 1199 | } |
| 1200 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1201 | static void bc_vec_push(BcVec *v, const void *data) |
| 1202 | { |
| 1203 | if (v->len + 1 > v->cap) bc_vec_grow(v, 1); |
| 1204 | memmove(v->v + (v->size * v->len), data, v->size); |
| 1205 | v->len += 1; |
| 1206 | } |
| 1207 | |
| 1208 | static void bc_vec_pushByte(BcVec *v, char data) |
| 1209 | { |
| 1210 | bc_vec_push(v, &data); |
| 1211 | } |
| 1212 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1213 | static void bc_vec_pushZeroByte(BcVec *v) |
| 1214 | { |
| 1215 | //bc_vec_pushByte(v, '\0'); |
| 1216 | // better: |
| 1217 | bc_vec_push(v, &const_int_0); |
| 1218 | } |
| 1219 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1220 | static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx) |
| 1221 | { |
| 1222 | if (idx == v->len) |
| 1223 | bc_vec_push(v, data); |
| 1224 | else { |
| 1225 | |
| 1226 | char *ptr; |
| 1227 | |
| 1228 | if (v->len == v->cap) bc_vec_grow(v, 1); |
| 1229 | |
| 1230 | ptr = v->v + v->size * idx; |
| 1231 | |
| 1232 | memmove(ptr + v->size, ptr, v->size * (v->len++ - idx)); |
| 1233 | memmove(ptr, data, v->size); |
| 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | static void bc_vec_string(BcVec *v, size_t len, const char *str) |
| 1238 | { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1239 | bc_vec_pop_all(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1240 | bc_vec_expand(v, len + 1); |
| 1241 | memcpy(v->v, str, len); |
| 1242 | v->len = len; |
| 1243 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1244 | bc_vec_pushZeroByte(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | static void bc_vec_concat(BcVec *v, const char *str) |
| 1248 | { |
Denys Vlasenko | 8b4cf0d | 2018-12-10 15:12:58 +0100 | [diff] [blame] | 1249 | size_t len, slen; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1250 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1251 | if (v->len == 0) bc_vec_pushZeroByte(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1252 | |
Denys Vlasenko | 8b4cf0d | 2018-12-10 15:12:58 +0100 | [diff] [blame] | 1253 | slen = strlen(str); |
| 1254 | len = v->len + slen; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1255 | |
Denys Vlasenko | 8b4cf0d | 2018-12-10 15:12:58 +0100 | [diff] [blame] | 1256 | if (v->cap < len) bc_vec_grow(v, slen); |
Denys Vlasenko | 1ff8862 | 2018-12-06 12:06:16 +0100 | [diff] [blame] | 1257 | strcpy(v->v + v->len - 1, str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1258 | |
| 1259 | v->len = len; |
| 1260 | } |
| 1261 | |
| 1262 | static void *bc_vec_item(const BcVec *v, size_t idx) |
| 1263 | { |
| 1264 | return v->v + v->size * idx; |
| 1265 | } |
| 1266 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 1267 | static char** bc_program_str(size_t idx) |
| 1268 | { |
| 1269 | return bc_vec_item(&G.prog.strs, idx); |
| 1270 | } |
| 1271 | |
| 1272 | static BcFunc* bc_program_func(size_t idx) |
| 1273 | { |
| 1274 | return bc_vec_item(&G.prog.fns, idx); |
| 1275 | } |
| 1276 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1277 | static void *bc_vec_item_rev(const BcVec *v, size_t idx) |
| 1278 | { |
| 1279 | return v->v + v->size * (v->len - idx - 1); |
| 1280 | } |
| 1281 | |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 1282 | static void *bc_vec_top(const BcVec *v) |
| 1283 | { |
| 1284 | return v->v + v->size * (v->len - 1); |
| 1285 | } |
| 1286 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1287 | static FAST_FUNC void bc_vec_free(void *vec) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1288 | { |
| 1289 | BcVec *v = (BcVec *) vec; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1290 | bc_vec_pop_all(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1291 | free(v->v); |
| 1292 | } |
| 1293 | |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 1294 | static int bc_id_cmp(const void *e1, const void *e2) |
| 1295 | { |
| 1296 | return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name); |
| 1297 | } |
| 1298 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1299 | static FAST_FUNC void bc_id_free(void *id) |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 1300 | { |
| 1301 | free(((BcId *) id)->name); |
| 1302 | } |
| 1303 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1304 | static size_t bc_map_find(const BcVec *v, const void *ptr) |
| 1305 | { |
| 1306 | size_t low = 0, high = v->len; |
| 1307 | |
| 1308 | while (low < high) { |
| 1309 | |
| 1310 | size_t mid = (low + high) / 2; |
| 1311 | BcId *id = bc_vec_item(v, mid); |
| 1312 | int result = bc_id_cmp(ptr, id); |
| 1313 | |
| 1314 | if (result == 0) |
| 1315 | return mid; |
| 1316 | else if (result < 0) |
| 1317 | high = mid; |
| 1318 | else |
| 1319 | low = mid + 1; |
| 1320 | } |
| 1321 | |
| 1322 | return low; |
| 1323 | } |
| 1324 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1325 | 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] | 1326 | { |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1327 | size_t n = *i = bc_map_find(v, ptr); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1328 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1329 | if (n == v->len) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1330 | bc_vec_push(v, ptr); |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1331 | else if (!bc_id_cmp(ptr, bc_vec_item(v, n))) |
| 1332 | return 0; // "was not inserted" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1333 | else |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1334 | bc_vec_pushAt(v, ptr, n); |
| 1335 | return 1; // "was inserted" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1336 | } |
| 1337 | |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1338 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1339 | static size_t bc_map_index(const BcVec *v, const void *ptr) |
| 1340 | { |
| 1341 | size_t i = bc_map_find(v, ptr); |
| 1342 | if (i >= v->len) return BC_VEC_INVALID_IDX; |
| 1343 | return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i; |
| 1344 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1345 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1346 | |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1347 | static int push_input_byte(BcVec *vec, char c) |
| 1348 | { |
| 1349 | if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'? |
| 1350 | || c > 0x7e |
| 1351 | ) { |
| 1352 | // Bad chars on this line, ignore entire line |
| 1353 | bc_error_fmt("illegal character 0x%02x", c); |
| 1354 | return 1; |
| 1355 | } |
| 1356 | bc_vec_pushByte(vec, (char)c); |
| 1357 | return 0; |
| 1358 | } |
| 1359 | |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 1360 | // This is not a "z" function: can also return BC_STATUS_EOF |
Denys Vlasenko | 4dd3652 | 2018-12-11 22:26:38 +0100 | [diff] [blame] | 1361 | // Can return success (0) or BC_STATUS_EOF. |
| 1362 | // Exits with error message if read error is detected. |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 1363 | static BcStatus bc_read_line(BcVec *vec) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1364 | { |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 1365 | BcStatus s; |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1366 | bool bad_chars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1367 | |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 1368 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 1369 | do { |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1370 | int c; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1371 | |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1372 | bad_chars = 0; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1373 | bc_vec_pop_all(vec); |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1374 | |
| 1375 | fflush_and_check(); |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1376 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1377 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | b9c321d | 2018-12-07 12:41:42 +0100 | [diff] [blame] | 1378 | if (G_interrupt) { // ^C was pressed |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1379 | intr: |
Denys Vlasenko | b9c321d | 2018-12-07 12:41:42 +0100 | [diff] [blame] | 1380 | G_interrupt = 0; |
Denys Vlasenko | ac6ed11 | 2018-12-08 21:39:10 +0100 | [diff] [blame] | 1381 | // GNU bc says "interrupted execution." |
| 1382 | // GNU dc says "Interrupt!" |
| 1383 | fputs("\ninterrupted execution\n", stderr); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1384 | } |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1385 | # if ENABLE_FEATURE_EDITING |
| 1386 | if (G_ttyin) { |
| 1387 | int n, i; |
| 1388 | # define line_buf bb_common_bufsiz1 |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 1389 | n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE); |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1390 | if (n <= 0) { // read errors or EOF, or ^D, or ^C |
| 1391 | if (n == 0) // ^C |
| 1392 | goto intr; |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 1393 | s = BC_STATUS_EOF; |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1394 | break; |
| 1395 | } |
| 1396 | i = 0; |
| 1397 | for (;;) { |
| 1398 | c = line_buf[i++]; |
| 1399 | if (!c) break; |
| 1400 | bad_chars |= push_input_byte(vec, c); |
| 1401 | } |
| 1402 | # undef line_buf |
| 1403 | } else |
| 1404 | # endif |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1405 | #endif |
Denys Vlasenko | ed84935 | 2018-12-06 10:26:13 +0100 | [diff] [blame] | 1406 | { |
Denys Vlasenko | ed84935 | 2018-12-06 10:26:13 +0100 | [diff] [blame] | 1407 | IF_FEATURE_BC_SIGNALS(errno = 0;) |
| 1408 | do { |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1409 | c = fgetc(stdin); |
| 1410 | #if ENABLE_FEATURE_BC_SIGNALS && !ENABLE_FEATURE_EDITING |
| 1411 | // Both conditions appear simultaneously, check both just in case |
Denys Vlasenko | b9c321d | 2018-12-07 12:41:42 +0100 | [diff] [blame] | 1412 | if (errno == EINTR || G_interrupt) { |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1413 | // ^C was pressed |
| 1414 | clearerr(stdin); |
| 1415 | goto intr; |
| 1416 | } |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1417 | #endif |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1418 | if (c == EOF) { |
Denys Vlasenko | ed84935 | 2018-12-06 10:26:13 +0100 | [diff] [blame] | 1419 | if (ferror(stdin)) |
| 1420 | quit(); // this emits error message |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 1421 | s = BC_STATUS_EOF; |
Denys Vlasenko | ed84935 | 2018-12-06 10:26:13 +0100 | [diff] [blame] | 1422 | // Note: EOF does not append '\n', therefore: |
| 1423 | // printf 'print 123\n' | bc - works |
| 1424 | // printf 'print 123' | bc - fails (syntax error) |
| 1425 | break; |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1426 | } |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1427 | bad_chars |= push_input_byte(vec, c); |
| 1428 | } while (c != '\n'); |
Denys Vlasenko | ed84935 | 2018-12-06 10:26:13 +0100 | [diff] [blame] | 1429 | } |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1430 | } while (bad_chars); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1431 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1432 | bc_vec_pushZeroByte(vec); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1433 | |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 1434 | return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1435 | } |
| 1436 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 1437 | static char* bc_read_file(const char *path) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1438 | { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 1439 | char *buf; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 1440 | size_t size = ((size_t) -1); |
| 1441 | size_t i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1442 | |
Denys Vlasenko | 4c9455f | 2018-12-06 15:21:39 +0100 | [diff] [blame] | 1443 | // Never returns NULL (dies on errors) |
| 1444 | buf = xmalloc_xopen_read_close(path, &size); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1445 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 1446 | for (i = 0; i < size; ++i) { |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1447 | char c = buf[i]; |
| 1448 | if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'? |
| 1449 | || c > 0x7e |
| 1450 | ) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 1451 | free(buf); |
| 1452 | buf = NULL; |
| 1453 | break; |
| 1454 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1455 | } |
| 1456 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 1457 | return buf; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1458 | } |
| 1459 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1460 | static void bc_num_setToZero(BcNum *n, size_t scale) |
| 1461 | { |
| 1462 | n->len = 0; |
| 1463 | n->neg = false; |
| 1464 | n->rdx = scale; |
| 1465 | } |
| 1466 | |
| 1467 | static void bc_num_zero(BcNum *n) |
| 1468 | { |
| 1469 | bc_num_setToZero(n, 0); |
| 1470 | } |
| 1471 | |
| 1472 | static void bc_num_one(BcNum *n) |
| 1473 | { |
| 1474 | bc_num_setToZero(n, 0); |
| 1475 | n->len = 1; |
| 1476 | n->num[0] = 1; |
| 1477 | } |
| 1478 | |
| 1479 | static void bc_num_ten(BcNum *n) |
| 1480 | { |
| 1481 | bc_num_setToZero(n, 0); |
| 1482 | n->len = 2; |
| 1483 | n->num[0] = 0; |
| 1484 | n->num[1] = 1; |
| 1485 | } |
| 1486 | |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1487 | // Note: this also sets BcNum to zero |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1488 | static void bc_num_init(BcNum *n, size_t req) |
| 1489 | { |
| 1490 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1491 | //memset(n, 0, sizeof(BcNum)); - cleared by assignments below |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1492 | n->num = xmalloc(req); |
| 1493 | n->cap = req; |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1494 | n->rdx = 0; |
| 1495 | n->len = 0; |
| 1496 | n->neg = false; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1497 | } |
| 1498 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 1499 | static void bc_num_init_DEF_SIZE(BcNum *n) |
| 1500 | { |
| 1501 | bc_num_init(n, BC_NUM_DEF_SIZE); |
| 1502 | } |
| 1503 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1504 | static void bc_num_expand(BcNum *n, size_t req) |
| 1505 | { |
| 1506 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
| 1507 | if (req > n->cap) { |
| 1508 | n->num = xrealloc(n->num, req); |
| 1509 | n->cap = req; |
| 1510 | } |
| 1511 | } |
| 1512 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1513 | static FAST_FUNC void bc_num_free(void *num) |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1514 | { |
| 1515 | free(((BcNum *) num)->num); |
| 1516 | } |
| 1517 | |
| 1518 | static void bc_num_copy(BcNum *d, BcNum *s) |
| 1519 | { |
| 1520 | if (d != s) { |
| 1521 | bc_num_expand(d, s->cap); |
| 1522 | d->len = s->len; |
| 1523 | d->neg = s->neg; |
| 1524 | d->rdx = s->rdx; |
| 1525 | memcpy(d->num, s->num, sizeof(BcDig) * d->len); |
| 1526 | } |
| 1527 | } |
| 1528 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1529 | static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p) |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1530 | { |
| 1531 | size_t i; |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1532 | unsigned long pow, result; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1533 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1534 | if (n->neg) RETURN_STATUS(bc_error("negative number")); |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1535 | |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1536 | for (result = 0, pow = 1, i = n->rdx; i < n->len; ++i) { |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1537 | |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1538 | unsigned long prev = result, powprev = pow; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1539 | |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1540 | result += ((unsigned long) n->num[i]) * pow; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1541 | pow *= 10; |
| 1542 | |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1543 | if (result < prev || pow < powprev) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1544 | RETURN_STATUS(bc_error("overflow")); |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1545 | prev = result; |
| 1546 | powprev = pow; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1547 | } |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1548 | *result_p = result; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1549 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1550 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1551 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 1552 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1553 | # define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 1554 | #endif |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1555 | |
| 1556 | static void bc_num_ulong2num(BcNum *n, unsigned long val) |
| 1557 | { |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1558 | BcDig *ptr; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1559 | |
| 1560 | bc_num_zero(n); |
| 1561 | |
| 1562 | if (val == 0) return; |
| 1563 | |
Denys Vlasenko | b696d9e | 2018-12-10 12:22:15 +0100 | [diff] [blame] | 1564 | if (ULONG_MAX == 0xffffffffUL) |
| 1565 | bc_num_expand(n, 10); // 10 digits: 4294967295 |
Denys Vlasenko | c665c18 | 2018-12-10 15:15:42 +0100 | [diff] [blame] | 1566 | if (ULONG_MAX == 0xffffffffffffffffULL) |
Denys Vlasenko | b696d9e | 2018-12-10 12:22:15 +0100 | [diff] [blame] | 1567 | bc_num_expand(n, 20); // 20 digits: 18446744073709551615 |
Denys Vlasenko | c665c18 | 2018-12-10 15:15:42 +0100 | [diff] [blame] | 1568 | BUILD_BUG_ON(ULONG_MAX > 0xffffffffffffffffULL); |
Denys Vlasenko | b696d9e | 2018-12-10 12:22:15 +0100 | [diff] [blame] | 1569 | |
| 1570 | ptr = n->num; |
| 1571 | for (;;) { |
| 1572 | n->len++; |
| 1573 | *ptr++ = val % 10; |
| 1574 | val /= 10; |
| 1575 | if (val == 0) break; |
| 1576 | } |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1577 | } |
| 1578 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1579 | static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1580 | size_t len) |
| 1581 | { |
| 1582 | size_t i, j; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1583 | for (i = 0; i < len; ++i) { |
| 1584 | for (a[i] -= b[i], j = 0; a[i + j] < 0;) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1585 | a[i + j++] += 10; |
| 1586 | a[i + j] -= 1; |
| 1587 | } |
| 1588 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len) |
| 1592 | { |
| 1593 | size_t i; |
| 1594 | int c = 0; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1595 | for (i = len - 1; i < len && !(c = a[i] - b[i]); --i); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1596 | return BC_NUM_NEG(i + 1, c < 0); |
| 1597 | } |
| 1598 | |
| 1599 | static ssize_t bc_num_cmp(BcNum *a, BcNum *b) |
| 1600 | { |
| 1601 | size_t i, min, a_int, b_int, diff; |
| 1602 | BcDig *max_num, *min_num; |
| 1603 | bool a_max, neg = false; |
| 1604 | ssize_t cmp; |
| 1605 | |
| 1606 | if (a == b) return 0; |
| 1607 | if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg); |
| 1608 | if (b->len == 0) return BC_NUM_NEG(1, a->neg); |
| 1609 | if (a->neg) { |
| 1610 | if (b->neg) |
| 1611 | neg = true; |
| 1612 | else |
| 1613 | return -1; |
| 1614 | } |
| 1615 | else if (b->neg) |
| 1616 | return 1; |
| 1617 | |
| 1618 | a_int = BC_NUM_INT(a); |
| 1619 | b_int = BC_NUM_INT(b); |
| 1620 | a_int -= b_int; |
| 1621 | a_max = (a->rdx > b->rdx); |
| 1622 | |
| 1623 | if (a_int != 0) return (ssize_t) a_int; |
| 1624 | |
| 1625 | if (a_max) { |
| 1626 | min = b->rdx; |
| 1627 | diff = a->rdx - b->rdx; |
| 1628 | max_num = a->num + diff; |
| 1629 | min_num = b->num; |
| 1630 | } |
| 1631 | else { |
| 1632 | min = a->rdx; |
| 1633 | diff = b->rdx - a->rdx; |
| 1634 | max_num = b->num + diff; |
| 1635 | min_num = a->num; |
| 1636 | } |
| 1637 | |
| 1638 | cmp = bc_num_compare(max_num, min_num, b_int + min); |
| 1639 | if (cmp != 0) return BC_NUM_NEG(cmp, (!a_max) != neg); |
| 1640 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1641 | for (max_num -= diff, i = diff - 1; i < diff; --i) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1642 | if (max_num[i]) return BC_NUM_NEG(1, (!a_max) != neg); |
| 1643 | } |
| 1644 | |
| 1645 | return 0; |
| 1646 | } |
| 1647 | |
| 1648 | static void bc_num_truncate(BcNum *n, size_t places) |
| 1649 | { |
| 1650 | if (places == 0) return; |
| 1651 | |
| 1652 | n->rdx -= places; |
| 1653 | |
| 1654 | if (n->len != 0) { |
| 1655 | n->len -= places; |
| 1656 | memmove(n->num, n->num + places, n->len * sizeof(BcDig)); |
| 1657 | } |
| 1658 | } |
| 1659 | |
| 1660 | static void bc_num_extend(BcNum *n, size_t places) |
| 1661 | { |
| 1662 | size_t len = n->len + places; |
| 1663 | |
| 1664 | if (places != 0) { |
| 1665 | |
| 1666 | if (n->cap < len) bc_num_expand(n, len); |
| 1667 | |
| 1668 | memmove(n->num + places, n->num, sizeof(BcDig) * n->len); |
| 1669 | memset(n->num, 0, sizeof(BcDig) * places); |
| 1670 | |
| 1671 | n->len += places; |
| 1672 | n->rdx += places; |
| 1673 | } |
| 1674 | } |
| 1675 | |
| 1676 | static void bc_num_clean(BcNum *n) |
| 1677 | { |
| 1678 | while (n->len > 0 && n->num[n->len - 1] == 0) --n->len; |
| 1679 | if (n->len == 0) |
| 1680 | n->neg = false; |
| 1681 | else if (n->len < n->rdx) |
| 1682 | n->len = n->rdx; |
| 1683 | } |
| 1684 | |
| 1685 | static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2) |
| 1686 | { |
| 1687 | if (n->rdx < scale) |
| 1688 | bc_num_extend(n, scale - n->rdx); |
| 1689 | else |
| 1690 | bc_num_truncate(n, n->rdx - scale); |
| 1691 | |
| 1692 | bc_num_clean(n); |
| 1693 | if (n->len != 0) n->neg = !neg1 != !neg2; |
| 1694 | } |
| 1695 | |
| 1696 | static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a, |
| 1697 | BcNum *restrict b) |
| 1698 | { |
| 1699 | if (idx < n->len) { |
| 1700 | |
| 1701 | b->len = n->len - idx; |
| 1702 | a->len = idx; |
| 1703 | a->rdx = b->rdx = 0; |
| 1704 | |
| 1705 | memcpy(b->num, n->num + idx, b->len * sizeof(BcDig)); |
| 1706 | memcpy(a->num, n->num, idx * sizeof(BcDig)); |
| 1707 | } |
| 1708 | else { |
| 1709 | bc_num_zero(b); |
| 1710 | bc_num_copy(a, n); |
| 1711 | } |
| 1712 | |
| 1713 | bc_num_clean(a); |
| 1714 | bc_num_clean(b); |
| 1715 | } |
| 1716 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1717 | static BC_STATUS zbc_num_shift(BcNum *n, size_t places) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1718 | { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1719 | if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 1720 | |
| 1721 | // This check makes sense only if size_t is (much) larger than BC_MAX_NUM. |
| 1722 | if (SIZE_MAX > (BC_MAX_NUM | 0xff)) { |
| 1723 | if (places + n->len > BC_MAX_NUM) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1724 | 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] | 1725 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1726 | |
| 1727 | if (n->rdx >= places) |
| 1728 | n->rdx -= places; |
| 1729 | else { |
| 1730 | bc_num_extend(n, places - n->rdx); |
| 1731 | n->rdx = 0; |
| 1732 | } |
| 1733 | |
| 1734 | bc_num_clean(n); |
| 1735 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1736 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1737 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 1738 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1739 | # define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 1740 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1741 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1742 | static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1743 | { |
| 1744 | BcNum one; |
| 1745 | BcDig num[2]; |
| 1746 | |
| 1747 | one.cap = 2; |
| 1748 | one.num = num; |
| 1749 | bc_num_one(&one); |
| 1750 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1751 | RETURN_STATUS(zbc_num_div(&one, a, b, scale)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1752 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1753 | #if ERRORS_ARE_FATAL |
| 1754 | # define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 1755 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1756 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1757 | static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1758 | { |
| 1759 | BcDig *ptr, *ptr_a, *ptr_b, *ptr_c; |
| 1760 | size_t i, max, min_rdx, min_int, diff, a_int, b_int; |
| 1761 | int carry, in; |
| 1762 | |
| 1763 | // Because this function doesn't need to use scale (per the bc spec), |
| 1764 | // I am hijacking it to say whether it's doing an add or a subtract. |
| 1765 | |
| 1766 | if (a->len == 0) { |
| 1767 | bc_num_copy(c, b); |
| 1768 | if (sub && c->len) c->neg = !c->neg; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1769 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1770 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1771 | if (b->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1772 | bc_num_copy(c, a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1773 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | c->neg = a->neg; |
| 1777 | c->rdx = BC_MAX(a->rdx, b->rdx); |
| 1778 | min_rdx = BC_MIN(a->rdx, b->rdx); |
| 1779 | c->len = 0; |
| 1780 | |
| 1781 | if (a->rdx > b->rdx) { |
| 1782 | diff = a->rdx - b->rdx; |
| 1783 | ptr = a->num; |
| 1784 | ptr_a = a->num + diff; |
| 1785 | ptr_b = b->num; |
| 1786 | } |
| 1787 | else { |
| 1788 | diff = b->rdx - a->rdx; |
| 1789 | ptr = b->num; |
| 1790 | ptr_a = a->num; |
| 1791 | ptr_b = b->num + diff; |
| 1792 | } |
| 1793 | |
| 1794 | for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i]; |
| 1795 | |
| 1796 | ptr_c += diff; |
| 1797 | a_int = BC_NUM_INT(a); |
| 1798 | b_int = BC_NUM_INT(b); |
| 1799 | |
| 1800 | if (a_int > b_int) { |
| 1801 | min_int = b_int; |
| 1802 | max = a_int; |
| 1803 | ptr = ptr_a; |
| 1804 | } |
| 1805 | else { |
| 1806 | min_int = a_int; |
| 1807 | max = b_int; |
| 1808 | ptr = ptr_b; |
| 1809 | } |
| 1810 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1811 | 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] | 1812 | in = ((int) ptr_a[i]) + ((int) ptr_b[i]) + carry; |
| 1813 | carry = in / 10; |
| 1814 | ptr_c[i] = (BcDig)(in % 10); |
| 1815 | } |
| 1816 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1817 | for (; i < max + min_rdx; ++i, ++c->len) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1818 | in = ((int) ptr[i]) + carry; |
| 1819 | carry = in / 10; |
| 1820 | ptr_c[i] = (BcDig)(in % 10); |
| 1821 | } |
| 1822 | |
| 1823 | if (carry != 0) c->num[c->len++] = (BcDig) carry; |
| 1824 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1825 | RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary() |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1826 | } |
| 1827 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1828 | static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1829 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1830 | ssize_t cmp; |
| 1831 | BcNum *minuend, *subtrahend; |
| 1832 | size_t start; |
| 1833 | bool aneg, bneg, neg; |
| 1834 | |
| 1835 | // Because this function doesn't need to use scale (per the bc spec), |
| 1836 | // I am hijacking it to say whether it's doing an add or a subtract. |
| 1837 | |
| 1838 | if (a->len == 0) { |
| 1839 | bc_num_copy(c, b); |
| 1840 | if (sub && c->len) c->neg = !c->neg; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1841 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1842 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1843 | if (b->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1844 | bc_num_copy(c, a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1845 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1846 | } |
| 1847 | |
| 1848 | aneg = a->neg; |
| 1849 | bneg = b->neg; |
| 1850 | a->neg = b->neg = false; |
| 1851 | |
| 1852 | cmp = bc_num_cmp(a, b); |
| 1853 | |
| 1854 | a->neg = aneg; |
| 1855 | b->neg = bneg; |
| 1856 | |
| 1857 | if (cmp == 0) { |
| 1858 | bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx)); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1859 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1860 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1861 | if (cmp > 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1862 | neg = a->neg; |
| 1863 | minuend = a; |
| 1864 | subtrahend = b; |
| 1865 | } |
| 1866 | else { |
| 1867 | neg = b->neg; |
| 1868 | if (sub) neg = !neg; |
| 1869 | minuend = b; |
| 1870 | subtrahend = a; |
| 1871 | } |
| 1872 | |
| 1873 | bc_num_copy(c, minuend); |
| 1874 | c->neg = neg; |
| 1875 | |
| 1876 | if (c->rdx < subtrahend->rdx) { |
| 1877 | bc_num_extend(c, subtrahend->rdx - c->rdx); |
| 1878 | start = 0; |
| 1879 | } |
| 1880 | else |
| 1881 | start = c->rdx - subtrahend->rdx; |
| 1882 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1883 | bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1884 | |
| 1885 | bc_num_clean(c); |
| 1886 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1887 | RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary() |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1888 | } |
| 1889 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1890 | static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1891 | BcNum *restrict c) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1892 | #if ERRORS_ARE_FATAL |
| 1893 | # define zbc_num_k(...) (zbc_num_k(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 1894 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1895 | { |
| 1896 | BcStatus s; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1897 | 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] | 1898 | BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1899 | bool aone; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1900 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1901 | if (a->len == 0 || b->len == 0) { |
| 1902 | bc_num_zero(c); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1903 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1904 | } |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1905 | aone = BC_NUM_ONE(a); |
| 1906 | if (aone || BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1907 | bc_num_copy(c, aone ? b : a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1908 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1909 | } |
| 1910 | |
| 1911 | if (a->len + b->len < BC_NUM_KARATSUBA_LEN || |
| 1912 | a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN) |
| 1913 | { |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1914 | size_t i, j, len; |
Denys Vlasenko | b3cb901 | 2018-12-05 19:05:32 +0100 | [diff] [blame] | 1915 | unsigned carry; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1916 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1917 | bc_num_expand(c, a->len + b->len + 1); |
| 1918 | |
| 1919 | memset(c->num, 0, sizeof(BcDig) * c->cap); |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1920 | c->len = len = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1921 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1922 | for (i = 0; i < b->len; ++i) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1923 | |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1924 | carry = 0; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1925 | for (j = 0; j < a->len; ++j) { |
Denys Vlasenko | b3cb901 | 2018-12-05 19:05:32 +0100 | [diff] [blame] | 1926 | unsigned in = c->num[i + j]; |
| 1927 | in += ((unsigned) a->num[j]) * ((unsigned) b->num[i]) + carry; |
| 1928 | // note: compilers prefer _unsigned_ div/const |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1929 | carry = in / 10; |
| 1930 | c->num[i + j] = (BcDig)(in % 10); |
| 1931 | } |
| 1932 | |
| 1933 | c->num[i + j] += (BcDig) carry; |
| 1934 | len = BC_MAX(len, i + j + !!carry); |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 1935 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1936 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 1937 | // a=2^1000000 |
| 1938 | // a*a <- without check below, this will not be interruptible |
| 1939 | if (G_interrupt) return BC_STATUS_FAILURE; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1940 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | c->len = len; |
| 1944 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1945 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1946 | } |
| 1947 | |
| 1948 | bc_num_init(&l1, max); |
| 1949 | bc_num_init(&h1, max); |
| 1950 | bc_num_init(&l2, max); |
| 1951 | bc_num_init(&h2, max); |
| 1952 | bc_num_init(&m1, max); |
| 1953 | bc_num_init(&m2, max); |
| 1954 | bc_num_init(&z0, max); |
| 1955 | bc_num_init(&z1, max); |
| 1956 | bc_num_init(&z2, max); |
| 1957 | bc_num_init(&temp, max + max); |
| 1958 | |
| 1959 | bc_num_split(a, max2, &l1, &h1); |
| 1960 | bc_num_split(b, max2, &l2, &h2); |
| 1961 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1962 | s = zbc_num_add(&h1, &l1, &m1, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1963 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1964 | s = zbc_num_add(&h2, &l2, &m2, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1965 | if (s) goto err; |
| 1966 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1967 | s = zbc_num_k(&h1, &h2, &z0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1968 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1969 | s = zbc_num_k(&m1, &m2, &z1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1970 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1971 | s = zbc_num_k(&l1, &l2, &z2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1972 | if (s) goto err; |
| 1973 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1974 | s = zbc_num_sub(&z1, &z0, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1975 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1976 | s = zbc_num_sub(&temp, &z2, &z1, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1977 | if (s) goto err; |
| 1978 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1979 | s = zbc_num_shift(&z0, max2 * 2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1980 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1981 | s = zbc_num_shift(&z1, max2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1982 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1983 | s = zbc_num_add(&z0, &z1, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1984 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1985 | s = zbc_num_add(&temp, &z2, c, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1986 | |
| 1987 | err: |
| 1988 | bc_num_free(&temp); |
| 1989 | bc_num_free(&z2); |
| 1990 | bc_num_free(&z1); |
| 1991 | bc_num_free(&z0); |
| 1992 | bc_num_free(&m2); |
| 1993 | bc_num_free(&m1); |
| 1994 | bc_num_free(&h2); |
| 1995 | bc_num_free(&l2); |
| 1996 | bc_num_free(&h1); |
| 1997 | bc_num_free(&l1); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1998 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1999 | } |
| 2000 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2001 | static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2002 | { |
| 2003 | BcStatus s; |
| 2004 | BcNum cpa, cpb; |
| 2005 | size_t maxrdx = BC_MAX(a->rdx, b->rdx); |
| 2006 | |
| 2007 | scale = BC_MAX(scale, a->rdx); |
| 2008 | scale = BC_MAX(scale, b->rdx); |
| 2009 | scale = BC_MIN(a->rdx + b->rdx, scale); |
| 2010 | maxrdx = BC_MAX(maxrdx, scale); |
| 2011 | |
| 2012 | bc_num_init(&cpa, a->len); |
| 2013 | bc_num_init(&cpb, b->len); |
| 2014 | |
| 2015 | bc_num_copy(&cpa, a); |
| 2016 | bc_num_copy(&cpb, b); |
| 2017 | cpa.neg = cpb.neg = false; |
| 2018 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2019 | s = zbc_num_shift(&cpa, maxrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2020 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2021 | s = zbc_num_shift(&cpb, maxrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2022 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2023 | s = zbc_num_k(&cpa, &cpb, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2024 | if (s) goto err; |
| 2025 | |
| 2026 | maxrdx += scale; |
| 2027 | bc_num_expand(c, c->len + maxrdx); |
| 2028 | |
| 2029 | if (c->len < maxrdx) { |
| 2030 | memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig)); |
| 2031 | c->len += maxrdx; |
| 2032 | } |
| 2033 | |
| 2034 | c->rdx = maxrdx; |
| 2035 | bc_num_retireMul(c, scale, a->neg, b->neg); |
| 2036 | |
| 2037 | err: |
| 2038 | bc_num_free(&cpb); |
| 2039 | bc_num_free(&cpa); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2040 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2041 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2042 | #if ERRORS_ARE_FATAL |
| 2043 | # define zbc_num_m(...) (zbc_num_m(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 2044 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2045 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2046 | static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2047 | { |
| 2048 | BcStatus s = BC_STATUS_SUCCESS; |
| 2049 | BcDig *n, *p, q; |
| 2050 | size_t len, end, i; |
| 2051 | BcNum cp; |
| 2052 | bool zero = true; |
| 2053 | |
| 2054 | if (b->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2055 | RETURN_STATUS(bc_error("divide by zero")); |
| 2056 | if (a->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2057 | bc_num_setToZero(c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2058 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2059 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2060 | if (BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2061 | bc_num_copy(c, a); |
| 2062 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2063 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2064 | } |
| 2065 | |
| 2066 | bc_num_init(&cp, BC_NUM_MREQ(a, b, scale)); |
| 2067 | bc_num_copy(&cp, a); |
| 2068 | len = b->len; |
| 2069 | |
| 2070 | if (len > cp.len) { |
| 2071 | bc_num_expand(&cp, len + 2); |
| 2072 | bc_num_extend(&cp, len - cp.len); |
| 2073 | } |
| 2074 | |
| 2075 | if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx); |
| 2076 | cp.rdx -= b->rdx; |
| 2077 | if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx); |
| 2078 | |
| 2079 | if (b->rdx == b->len) { |
| 2080 | for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1]; |
| 2081 | len -= i - 1; |
| 2082 | } |
| 2083 | |
| 2084 | if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1); |
| 2085 | |
| 2086 | // We want an extra zero in front to make things simpler. |
| 2087 | cp.num[cp.len++] = 0; |
| 2088 | end = cp.len - len; |
| 2089 | |
| 2090 | bc_num_expand(c, cp.len); |
| 2091 | |
| 2092 | bc_num_zero(c); |
| 2093 | memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig)); |
| 2094 | c->rdx = cp.rdx; |
| 2095 | c->len = cp.len; |
| 2096 | p = b->num; |
| 2097 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2098 | for (i = end - 1; !s && i < end; --i) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2099 | n = cp.num + i; |
| 2100 | 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] | 2101 | bc_num_subArrays(n, p, len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2102 | c->num[i] = q; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2103 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 2104 | // a=2^100000 |
| 2105 | // scale=40000 |
| 2106 | // 1/a <- without check below, this will not be interruptible |
| 2107 | if (G_interrupt) { |
| 2108 | s = BC_STATUS_FAILURE; |
| 2109 | break; |
| 2110 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2111 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2112 | } |
| 2113 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2114 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2115 | bc_num_free(&cp); |
| 2116 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2117 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2118 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2119 | #if ERRORS_ARE_FATAL |
| 2120 | # define zbc_num_d(...) (zbc_num_d(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 2121 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2122 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2123 | static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2124 | BcNum *restrict d, size_t scale, size_t ts) |
| 2125 | { |
| 2126 | BcStatus s; |
| 2127 | BcNum temp; |
| 2128 | bool neg; |
| 2129 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2130 | if (b->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2131 | RETURN_STATUS(bc_error("divide by zero")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2132 | |
| 2133 | if (a->len == 0) { |
| 2134 | bc_num_setToZero(d, ts); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2135 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2136 | } |
| 2137 | |
| 2138 | bc_num_init(&temp, d->cap); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2139 | s = zbc_num_d(a, b, c, scale); |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 2140 | if (s) goto err; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2141 | |
| 2142 | if (scale != 0) scale = ts; |
| 2143 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2144 | s = zbc_num_m(c, b, &temp, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2145 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2146 | s = zbc_num_sub(a, &temp, d, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2147 | if (s) goto err; |
| 2148 | |
| 2149 | if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx); |
| 2150 | |
| 2151 | neg = d->neg; |
| 2152 | bc_num_retireMul(d, ts, a->neg, b->neg); |
| 2153 | d->neg = neg; |
| 2154 | |
| 2155 | err: |
| 2156 | bc_num_free(&temp); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2157 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2158 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2159 | #if ERRORS_ARE_FATAL |
| 2160 | # define zbc_num_r(...) (zbc_num_r(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 2161 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2162 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2163 | static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2164 | { |
| 2165 | BcStatus s; |
| 2166 | BcNum c1; |
| 2167 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 2168 | |
| 2169 | bc_num_init(&c1, len); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2170 | s = zbc_num_r(a, b, &c1, c, scale, ts); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2171 | bc_num_free(&c1); |
| 2172 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2173 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2174 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2175 | #if ERRORS_ARE_FATAL |
| 2176 | # define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 2177 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2178 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2179 | static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2180 | { |
| 2181 | BcStatus s = BC_STATUS_SUCCESS; |
| 2182 | BcNum copy; |
| 2183 | unsigned long pow; |
| 2184 | size_t i, powrdx, resrdx; |
| 2185 | bool neg, zero; |
| 2186 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2187 | if (b->rdx) RETURN_STATUS(bc_error("non integer number")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2188 | |
| 2189 | if (b->len == 0) { |
| 2190 | bc_num_one(c); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2191 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2192 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2193 | if (a->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2194 | bc_num_setToZero(c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2195 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2196 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2197 | if (BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2198 | if (!b->neg) |
| 2199 | bc_num_copy(c, a); |
| 2200 | else |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2201 | s = zbc_num_inv(a, c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2202 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2203 | } |
| 2204 | |
| 2205 | neg = b->neg; |
| 2206 | b->neg = false; |
| 2207 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2208 | s = zbc_num_ulong(b, &pow); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2209 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2210 | |
| 2211 | bc_num_init(©, a->len); |
| 2212 | bc_num_copy(©, a); |
| 2213 | |
Denys Vlasenko | 2d615fe | 2018-12-07 16:22:45 +0100 | [diff] [blame] | 2214 | if (!neg) { |
| 2215 | if (a->rdx > scale) |
| 2216 | scale = a->rdx; |
| 2217 | if (a->rdx * pow < scale) |
| 2218 | scale = a->rdx * pow; |
| 2219 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2220 | |
| 2221 | b->neg = neg; |
| 2222 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2223 | for (powrdx = a->rdx; !(pow & 1); pow >>= 1) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2224 | powrdx <<= 1; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2225 | s = zbc_num_mul(©, ©, ©, powrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2226 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2227 | // Not needed: zbc_num_mul() has a check for ^C: |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 2228 | //if (G_interrupt) { |
| 2229 | // s = BC_STATUS_FAILURE; |
| 2230 | // goto err; |
| 2231 | //} |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2232 | } |
| 2233 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2234 | bc_num_copy(c, ©); |
| 2235 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2236 | for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2237 | |
| 2238 | powrdx <<= 1; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2239 | s = zbc_num_mul(©, ©, ©, powrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2240 | if (s) goto err; |
| 2241 | |
| 2242 | if (pow & 1) { |
| 2243 | resrdx += powrdx; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2244 | s = zbc_num_mul(c, ©, c, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2245 | if (s) goto err; |
| 2246 | } |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2247 | // Not needed: zbc_num_mul() has a check for ^C: |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 2248 | //if (G_interrupt) { |
| 2249 | // s = BC_STATUS_FAILURE; |
| 2250 | // goto err; |
| 2251 | //} |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2252 | } |
| 2253 | |
| 2254 | if (neg) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2255 | s = zbc_num_inv(c, c, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2256 | if (s) goto err; |
| 2257 | } |
| 2258 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2259 | if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale); |
| 2260 | |
| 2261 | // We can't use bc_num_clean() here. |
| 2262 | for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i]; |
| 2263 | if (zero) bc_num_setToZero(c, scale); |
| 2264 | |
| 2265 | err: |
| 2266 | bc_num_free(©); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2267 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2268 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2269 | #if ERRORS_ARE_FATAL |
| 2270 | # define zbc_num_p(...) (zbc_num_p(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 2271 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2272 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2273 | static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2274 | BcNumBinaryOp op, size_t req) |
| 2275 | { |
| 2276 | BcStatus s; |
| 2277 | BcNum num2, *ptr_a, *ptr_b; |
| 2278 | bool init = false; |
| 2279 | |
| 2280 | if (c == a) { |
| 2281 | ptr_a = &num2; |
| 2282 | memcpy(ptr_a, c, sizeof(BcNum)); |
| 2283 | init = true; |
| 2284 | } |
| 2285 | else |
| 2286 | ptr_a = a; |
| 2287 | |
| 2288 | if (c == b) { |
| 2289 | ptr_b = &num2; |
| 2290 | if (c != a) { |
| 2291 | memcpy(ptr_b, c, sizeof(BcNum)); |
| 2292 | init = true; |
| 2293 | } |
| 2294 | } |
| 2295 | else |
| 2296 | ptr_b = b; |
| 2297 | |
| 2298 | if (init) |
| 2299 | bc_num_init(c, req); |
| 2300 | else |
| 2301 | bc_num_expand(c, req); |
| 2302 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2303 | #if !ERRORS_ARE_FATAL |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2304 | s = op(ptr_a, ptr_b, c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2305 | #else |
| 2306 | op(ptr_a, ptr_b, c, scale); |
| 2307 | s = BC_STATUS_SUCCESS; |
| 2308 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2309 | |
| 2310 | if (init) bc_num_free(&num2); |
| 2311 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2312 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2313 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2314 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2315 | # define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2316 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2317 | |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2318 | static bool bc_num_strValid(const char *val, size_t base) |
| 2319 | { |
| 2320 | BcDig b; |
| 2321 | bool radix; |
| 2322 | |
| 2323 | b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A'); |
| 2324 | radix = false; |
| 2325 | for (;;) { |
| 2326 | BcDig c = *val++; |
| 2327 | if (c == '\0') |
| 2328 | break; |
| 2329 | if (c == '.') { |
| 2330 | if (radix) return false; |
| 2331 | radix = true; |
| 2332 | continue; |
| 2333 | } |
| 2334 | if (c < '0' || c >= b || (c > '9' && c < 'A')) |
| 2335 | return false; |
| 2336 | } |
| 2337 | return true; |
| 2338 | } |
| 2339 | |
| 2340 | // Note: n is already "bc_num_zero()"ed, |
| 2341 | // leading zeroes in "val" are removed |
| 2342 | static void bc_num_parseDecimal(BcNum *n, const char *val) |
| 2343 | { |
| 2344 | size_t len, i; |
| 2345 | const char *ptr; |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2346 | |
| 2347 | len = strlen(val); |
| 2348 | if (len == 0) |
| 2349 | return; |
| 2350 | |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2351 | bc_num_expand(n, len); |
| 2352 | |
| 2353 | ptr = strchr(val, '.'); |
| 2354 | |
| 2355 | n->rdx = 0; |
| 2356 | if (ptr != NULL) |
| 2357 | n->rdx = (size_t)((val + len) - (ptr + 1)); |
| 2358 | |
Denys Vlasenko | dafbc2c | 2018-12-10 15:38:52 +0100 | [diff] [blame] | 2359 | for (i = 0; val[i]; ++i) { |
| 2360 | if (val[i] != '0' && val[i] != '.') { |
| 2361 | // Not entirely zero value - convert it, and exit |
| 2362 | i = len - 1; |
| 2363 | for (;;) { |
| 2364 | n->num[n->len] = val[i] - '0'; |
| 2365 | ++n->len; |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2366 | skip_dot: |
Denys Vlasenko | dafbc2c | 2018-12-10 15:38:52 +0100 | [diff] [blame] | 2367 | if ((ssize_t)--i == (ssize_t)-1) break; |
| 2368 | if (val[i] == '.') goto skip_dot; |
| 2369 | } |
| 2370 | break; |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2371 | } |
| 2372 | } |
Denys Vlasenko | dafbc2c | 2018-12-10 15:38:52 +0100 | [diff] [blame] | 2373 | // if this is reached, the value is entirely zero |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2374 | } |
| 2375 | |
| 2376 | // Note: n is already "bc_num_zero()"ed, |
| 2377 | // leading zeroes in "val" are removed |
| 2378 | static void bc_num_parseBase(BcNum *n, const char *val, BcNum *base) |
| 2379 | { |
| 2380 | BcStatus s; |
| 2381 | BcNum temp, mult, result; |
| 2382 | BcDig c = '\0'; |
| 2383 | unsigned long v; |
| 2384 | size_t i, digits; |
| 2385 | |
| 2386 | for (i = 0; ; ++i) { |
| 2387 | if (val[i] == '\0') |
| 2388 | return; |
| 2389 | if (val[i] != '.' && val[i] != '0') |
| 2390 | break; |
| 2391 | } |
| 2392 | |
| 2393 | bc_num_init_DEF_SIZE(&temp); |
| 2394 | bc_num_init_DEF_SIZE(&mult); |
| 2395 | |
| 2396 | for (;;) { |
| 2397 | c = *val++; |
| 2398 | if (c == '\0') goto int_err; |
| 2399 | if (c == '.') break; |
| 2400 | |
| 2401 | v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10); |
| 2402 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2403 | s = zbc_num_mul(n, base, &mult, 0); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2404 | if (s) goto int_err; |
| 2405 | bc_num_ulong2num(&temp, v); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2406 | s = zbc_num_add(&mult, &temp, n, 0); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2407 | if (s) goto int_err; |
| 2408 | } |
| 2409 | |
| 2410 | bc_num_init(&result, base->len); |
| 2411 | //bc_num_zero(&result); - already is |
| 2412 | bc_num_one(&mult); |
| 2413 | |
| 2414 | digits = 0; |
| 2415 | for (;;) { |
| 2416 | c = *val++; |
| 2417 | if (c == '\0') break; |
| 2418 | digits++; |
| 2419 | |
| 2420 | v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10); |
| 2421 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2422 | s = zbc_num_mul(&result, base, &result, 0); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2423 | if (s) goto err; |
| 2424 | bc_num_ulong2num(&temp, v); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2425 | s = zbc_num_add(&result, &temp, &result, 0); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2426 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2427 | s = zbc_num_mul(&mult, base, &mult, 0); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2428 | if (s) goto err; |
| 2429 | } |
| 2430 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2431 | s = zbc_num_div(&result, &mult, &result, digits); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2432 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2433 | s = zbc_num_add(n, &result, n, digits); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2434 | if (s) goto err; |
| 2435 | |
| 2436 | if (n->len != 0) { |
| 2437 | if (n->rdx < digits) bc_num_extend(n, digits - n->rdx); |
| 2438 | } else |
| 2439 | bc_num_zero(n); |
| 2440 | |
| 2441 | err: |
| 2442 | bc_num_free(&result); |
| 2443 | int_err: |
| 2444 | bc_num_free(&mult); |
| 2445 | bc_num_free(&temp); |
| 2446 | } |
| 2447 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2448 | 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] | 2449 | size_t base_t) |
| 2450 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2451 | if (!bc_num_strValid(val, base_t)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2452 | RETURN_STATUS(bc_error("bad number string")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2453 | |
Denys Vlasenko | 4a024c7 | 2018-12-09 13:21:54 +0100 | [diff] [blame] | 2454 | bc_num_zero(n); |
| 2455 | while (*val == '0') val++; |
| 2456 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2457 | if (base_t == 10) |
| 2458 | bc_num_parseDecimal(n, val); |
| 2459 | else |
| 2460 | bc_num_parseBase(n, val, base); |
| 2461 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2462 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2463 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2464 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2465 | # define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2466 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2467 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2468 | static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2469 | { |
| 2470 | BcStatus s; |
| 2471 | BcNum num1, num2, half, f, fprime, *x0, *x1, *temp; |
| 2472 | size_t pow, len, digs, digs1, resrdx, req, times = 0; |
| 2473 | ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX; |
| 2474 | |
| 2475 | req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1; |
| 2476 | bc_num_expand(b, req); |
| 2477 | |
| 2478 | if (a->len == 0) { |
| 2479 | bc_num_setToZero(b, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2480 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2481 | } |
| 2482 | else if (a->neg) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2483 | RETURN_STATUS(bc_error("negative number")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2484 | else if (BC_NUM_ONE(a)) { |
| 2485 | bc_num_one(b); |
| 2486 | bc_num_extend(b, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2487 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2488 | } |
| 2489 | |
| 2490 | scale = BC_MAX(scale, a->rdx) + 1; |
| 2491 | len = a->len + scale; |
| 2492 | |
| 2493 | bc_num_init(&num1, len); |
| 2494 | bc_num_init(&num2, len); |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 2495 | bc_num_init_DEF_SIZE(&half); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2496 | |
| 2497 | bc_num_one(&half); |
| 2498 | half.num[0] = 5; |
| 2499 | half.rdx = 1; |
| 2500 | |
| 2501 | bc_num_init(&f, len); |
| 2502 | bc_num_init(&fprime, len); |
| 2503 | |
| 2504 | x0 = &num1; |
| 2505 | x1 = &num2; |
| 2506 | |
| 2507 | bc_num_one(x0); |
| 2508 | pow = BC_NUM_INT(a); |
| 2509 | |
| 2510 | if (pow) { |
| 2511 | |
| 2512 | if (pow & 1) |
| 2513 | x0->num[0] = 2; |
| 2514 | else |
| 2515 | x0->num[0] = 6; |
| 2516 | |
| 2517 | pow -= 2 - (pow & 1); |
| 2518 | |
| 2519 | bc_num_extend(x0, pow); |
| 2520 | |
| 2521 | // Make sure to move the radix back. |
| 2522 | x0->rdx -= pow; |
| 2523 | } |
| 2524 | |
| 2525 | x0->rdx = digs = digs1 = 0; |
| 2526 | resrdx = scale + 2; |
| 2527 | len = BC_NUM_INT(x0) + resrdx - 1; |
| 2528 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2529 | while (cmp != 0 || digs < len) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2530 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2531 | s = zbc_num_div(a, x0, &f, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2532 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2533 | s = zbc_num_add(x0, &f, &fprime, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2534 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2535 | s = zbc_num_mul(&fprime, &half, x1, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2536 | if (s) goto err; |
| 2537 | |
| 2538 | cmp = bc_num_cmp(x1, x0); |
| 2539 | digs = x1->len - (unsigned long long) llabs(cmp); |
| 2540 | |
| 2541 | if (cmp == cmp2 && digs == digs1) |
| 2542 | times += 1; |
| 2543 | else |
| 2544 | times = 0; |
| 2545 | |
| 2546 | resrdx += times > 4; |
| 2547 | |
| 2548 | cmp2 = cmp1; |
| 2549 | cmp1 = cmp; |
| 2550 | digs1 = digs; |
| 2551 | |
| 2552 | temp = x0; |
| 2553 | x0 = x1; |
| 2554 | x1 = temp; |
| 2555 | } |
| 2556 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2557 | bc_num_copy(b, x0); |
| 2558 | scale -= 1; |
| 2559 | if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale); |
| 2560 | |
| 2561 | err: |
| 2562 | bc_num_free(&fprime); |
| 2563 | bc_num_free(&f); |
| 2564 | bc_num_free(&half); |
| 2565 | bc_num_free(&num2); |
| 2566 | bc_num_free(&num1); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2567 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2568 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2569 | #if ERRORS_ARE_FATAL |
| 2570 | # define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 2571 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2572 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2573 | static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2574 | size_t scale) |
| 2575 | { |
| 2576 | BcStatus s; |
| 2577 | BcNum num2, *ptr_a; |
| 2578 | bool init = false; |
| 2579 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 2580 | |
| 2581 | if (c == a) { |
| 2582 | memcpy(&num2, c, sizeof(BcNum)); |
| 2583 | ptr_a = &num2; |
| 2584 | bc_num_init(c, len); |
| 2585 | init = true; |
| 2586 | } |
| 2587 | else { |
| 2588 | ptr_a = a; |
| 2589 | bc_num_expand(c, len); |
| 2590 | } |
| 2591 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2592 | s = zbc_num_r(ptr_a, b, c, d, scale, ts); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2593 | |
| 2594 | if (init) bc_num_free(&num2); |
| 2595 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2596 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2597 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2598 | #if ERRORS_ARE_FATAL |
| 2599 | # define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 2600 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2601 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2602 | #if ENABLE_DC |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2603 | static BC_STATUS zbc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2604 | { |
| 2605 | BcStatus s; |
| 2606 | BcNum base, exp, two, temp; |
| 2607 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2608 | if (c->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2609 | RETURN_STATUS(bc_error("divide by zero")); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2610 | if (a->rdx || b->rdx || c->rdx) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2611 | RETURN_STATUS(bc_error("non integer number")); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2612 | if (b->neg) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2613 | RETURN_STATUS(bc_error("negative number")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2614 | |
| 2615 | bc_num_expand(d, c->len); |
| 2616 | bc_num_init(&base, c->len); |
| 2617 | bc_num_init(&exp, b->len); |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 2618 | bc_num_init_DEF_SIZE(&two); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2619 | bc_num_init(&temp, b->len); |
| 2620 | |
| 2621 | bc_num_one(&two); |
| 2622 | two.num[0] = 2; |
| 2623 | bc_num_one(d); |
| 2624 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2625 | s = zbc_num_rem(a, c, &base, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2626 | if (s) goto err; |
| 2627 | bc_num_copy(&exp, b); |
| 2628 | |
| 2629 | while (exp.len != 0) { |
| 2630 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2631 | s = zbc_num_divmod(&exp, &two, &exp, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2632 | if (s) goto err; |
| 2633 | |
| 2634 | if (BC_NUM_ONE(&temp)) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2635 | s = zbc_num_mul(d, &base, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2636 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2637 | s = zbc_num_rem(&temp, c, d, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2638 | if (s) goto err; |
| 2639 | } |
| 2640 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2641 | s = zbc_num_mul(&base, &base, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2642 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2643 | s = zbc_num_rem(&temp, c, &base, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2644 | if (s) goto err; |
| 2645 | } |
| 2646 | |
| 2647 | err: |
| 2648 | bc_num_free(&temp); |
| 2649 | bc_num_free(&two); |
| 2650 | bc_num_free(&exp); |
| 2651 | bc_num_free(&base); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2652 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2653 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2654 | #if ERRORS_ARE_FATAL |
| 2655 | # define zbc_num_modexp(...) (zbc_num_modexp(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 2656 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2657 | #endif // ENABLE_DC |
| 2658 | |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 2659 | #if ENABLE_BC |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2660 | static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2661 | { |
| 2662 | BcId a; |
| 2663 | size_t i; |
| 2664 | |
| 2665 | for (i = 0; i < f->autos.len; ++i) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2666 | if (strcmp(name, ((BcId *) bc_vec_item(&f->autos, i))->name) == 0) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2667 | 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] | 2668 | } |
| 2669 | |
| 2670 | a.idx = var; |
| 2671 | a.name = name; |
| 2672 | |
| 2673 | bc_vec_push(&f->autos, &a); |
| 2674 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2675 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2676 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2677 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2678 | # define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2679 | #endif |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 2680 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2681 | |
| 2682 | static void bc_func_init(BcFunc *f) |
| 2683 | { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 2684 | bc_char_vec_init(&f->code); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2685 | bc_vec_init(&f->autos, sizeof(BcId), bc_id_free); |
| 2686 | bc_vec_init(&f->labels, sizeof(size_t), NULL); |
| 2687 | f->nparams = 0; |
| 2688 | } |
| 2689 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2690 | static FAST_FUNC void bc_func_free(void *func) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2691 | { |
| 2692 | BcFunc *f = (BcFunc *) func; |
| 2693 | bc_vec_free(&f->code); |
| 2694 | bc_vec_free(&f->autos); |
| 2695 | bc_vec_free(&f->labels); |
| 2696 | } |
| 2697 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2698 | static void bc_array_expand(BcVec *a, size_t len); |
| 2699 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2700 | static void bc_array_init(BcVec *a, bool nums) |
| 2701 | { |
| 2702 | if (nums) |
| 2703 | bc_vec_init(a, sizeof(BcNum), bc_num_free); |
| 2704 | else |
| 2705 | bc_vec_init(a, sizeof(BcVec), bc_vec_free); |
| 2706 | bc_array_expand(a, 1); |
| 2707 | } |
| 2708 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2709 | static void bc_array_expand(BcVec *a, size_t len) |
| 2710 | { |
| 2711 | BcResultData data; |
| 2712 | |
| 2713 | if (a->size == sizeof(BcNum) && a->dtor == bc_num_free) { |
| 2714 | while (len > a->len) { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 2715 | bc_num_init_DEF_SIZE(&data.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2716 | bc_vec_push(a, &data.n); |
| 2717 | } |
| 2718 | } |
| 2719 | else { |
| 2720 | while (len > a->len) { |
| 2721 | bc_array_init(&data.v, true); |
| 2722 | bc_vec_push(a, &data.v); |
| 2723 | } |
| 2724 | } |
| 2725 | } |
| 2726 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2727 | static void bc_array_copy(BcVec *d, const BcVec *s) |
| 2728 | { |
| 2729 | size_t i; |
| 2730 | |
| 2731 | bc_vec_pop_all(d); |
| 2732 | bc_vec_expand(d, s->cap); |
| 2733 | d->len = s->len; |
| 2734 | |
| 2735 | for (i = 0; i < s->len; ++i) { |
| 2736 | BcNum *dnum = bc_vec_item(d, i), *snum = bc_vec_item(s, i); |
| 2737 | bc_num_init(dnum, snum->len); |
| 2738 | bc_num_copy(dnum, snum); |
| 2739 | } |
| 2740 | } |
| 2741 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2742 | static FAST_FUNC void bc_string_free(void *string) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2743 | { |
| 2744 | free(*((char **) string)); |
| 2745 | } |
| 2746 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2747 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2748 | static void bc_result_copy(BcResult *d, BcResult *src) |
| 2749 | { |
| 2750 | d->t = src->t; |
| 2751 | |
| 2752 | switch (d->t) { |
| 2753 | |
| 2754 | case BC_RESULT_TEMP: |
| 2755 | case BC_RESULT_IBASE: |
| 2756 | case BC_RESULT_SCALE: |
| 2757 | case BC_RESULT_OBASE: |
| 2758 | { |
| 2759 | bc_num_init(&d->d.n, src->d.n.len); |
| 2760 | bc_num_copy(&d->d.n, &src->d.n); |
| 2761 | break; |
| 2762 | } |
| 2763 | |
| 2764 | case BC_RESULT_VAR: |
| 2765 | case BC_RESULT_ARRAY: |
| 2766 | case BC_RESULT_ARRAY_ELEM: |
| 2767 | { |
| 2768 | d->d.id.name = xstrdup(src->d.id.name); |
| 2769 | break; |
| 2770 | } |
| 2771 | |
| 2772 | case BC_RESULT_CONSTANT: |
| 2773 | case BC_RESULT_LAST: |
| 2774 | case BC_RESULT_ONE: |
| 2775 | case BC_RESULT_STR: |
| 2776 | { |
| 2777 | memcpy(&d->d.n, &src->d.n, sizeof(BcNum)); |
| 2778 | break; |
| 2779 | } |
| 2780 | } |
| 2781 | } |
| 2782 | #endif // ENABLE_DC |
| 2783 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2784 | static FAST_FUNC void bc_result_free(void *result) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2785 | { |
| 2786 | BcResult *r = (BcResult *) result; |
| 2787 | |
| 2788 | switch (r->t) { |
| 2789 | |
| 2790 | case BC_RESULT_TEMP: |
| 2791 | case BC_RESULT_IBASE: |
| 2792 | case BC_RESULT_SCALE: |
| 2793 | case BC_RESULT_OBASE: |
| 2794 | { |
| 2795 | bc_num_free(&r->d.n); |
| 2796 | break; |
| 2797 | } |
| 2798 | |
| 2799 | case BC_RESULT_VAR: |
| 2800 | case BC_RESULT_ARRAY: |
| 2801 | case BC_RESULT_ARRAY_ELEM: |
| 2802 | { |
| 2803 | free(r->d.id.name); |
| 2804 | break; |
| 2805 | } |
| 2806 | |
| 2807 | default: |
| 2808 | { |
| 2809 | // Do nothing. |
| 2810 | break; |
| 2811 | } |
| 2812 | } |
| 2813 | } |
| 2814 | |
| 2815 | static void bc_lex_lineComment(BcLex *l) |
| 2816 | { |
| 2817 | l->t.t = BC_LEX_WHITESPACE; |
| 2818 | while (l->i < l->len && l->buf[l->i++] != '\n'); |
| 2819 | --l->i; |
| 2820 | } |
| 2821 | |
| 2822 | static void bc_lex_whitespace(BcLex *l) |
| 2823 | { |
| 2824 | char c; |
| 2825 | l->t.t = BC_LEX_WHITESPACE; |
| 2826 | for (c = l->buf[l->i]; c != '\n' && isspace(c); c = l->buf[++l->i]); |
| 2827 | } |
| 2828 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2829 | static BC_STATUS zbc_lex_number(BcLex *l, char start) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2830 | { |
| 2831 | const char *buf = l->buf + l->i; |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2832 | size_t len, bslashes, i, ccnt; |
| 2833 | bool pt; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2834 | |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2835 | pt = (start == '.'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2836 | l->t.t = BC_LEX_NUMBER; |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2837 | bslashes = 0; |
| 2838 | ccnt = i = 0; |
| 2839 | for (;;) { |
| 2840 | char c = buf[i]; |
| 2841 | if (c == '\0') |
| 2842 | break; |
| 2843 | if (c == '\\' && buf[i + 1] == '\n') { |
| 2844 | i += 2; |
| 2845 | bslashes++; |
| 2846 | continue; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2847 | } |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2848 | if (!isdigit(c) && (c < 'A' || c > 'F')) { |
| 2849 | if (c != '.') break; |
| 2850 | // if '.' was already seen, stop on second one: |
| 2851 | if (pt) break; |
| 2852 | pt = 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2853 | } |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2854 | // buf[i] is one of "0-9A-F." |
| 2855 | i++; |
| 2856 | if (c != '.') |
| 2857 | ccnt = i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2858 | } |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2859 | //i is buf[i] index of the first not-yet-parsed char |
| 2860 | l->i += i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2861 | |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2862 | //ccnt is the number of chars in the number string, excluding possible |
| 2863 | //trailing "." and possible following trailing "\<newline>"(s). |
| 2864 | len = ccnt - bslashes * 2 + 1; // +1 byte for NUL termination |
| 2865 | |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 2866 | // This check makes sense only if size_t is (much) larger than BC_MAX_NUM. |
| 2867 | if (SIZE_MAX > (BC_MAX_NUM | 0xff)) { |
| 2868 | if (len > BC_MAX_NUM) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2869 | 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] | 2870 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2871 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 2872 | bc_vec_pop_all(&l->t.v); |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2873 | bc_vec_expand(&l->t.v, 1 + len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2874 | bc_vec_push(&l->t.v, &start); |
| 2875 | |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2876 | while (ccnt != 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2877 | // If we have hit a backslash, skip it. We don't have |
| 2878 | // to check for a newline because it's guaranteed. |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2879 | if (*buf == '\\') { |
| 2880 | buf += 2; |
| 2881 | ccnt -= 2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2882 | continue; |
| 2883 | } |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2884 | bc_vec_push(&l->t.v, buf); |
| 2885 | buf++; |
| 2886 | ccnt--; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2887 | } |
| 2888 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 2889 | bc_vec_pushZeroByte(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2890 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2891 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2892 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2893 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2894 | # define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2895 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2896 | |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 2897 | static void bc_lex_name(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2898 | { |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 2899 | size_t i; |
| 2900 | const char *buf; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2901 | |
| 2902 | l->t.t = BC_LEX_NAME; |
| 2903 | |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 2904 | i = 0; |
| 2905 | buf = l->buf + l->i - 1; |
| 2906 | for (;;) { |
| 2907 | char c = buf[i]; |
| 2908 | if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break; |
| 2909 | i++; |
| 2910 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2911 | |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 2912 | #if 0 // We do not protect against people with gigabyte-long names |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 2913 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
| 2914 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
| 2915 | if (i > BC_MAX_STRING) |
| 2916 | return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]"); |
| 2917 | } |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 2918 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2919 | bc_vec_string(&l->t.v, i, buf); |
| 2920 | |
| 2921 | // Increment the index. We minus 1 because it has already been incremented. |
| 2922 | l->i += i - 1; |
| 2923 | |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 2924 | //return BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2925 | } |
| 2926 | |
| 2927 | static void bc_lex_init(BcLex *l, BcLexNext next) |
| 2928 | { |
| 2929 | l->next = next; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 2930 | bc_char_vec_init(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2931 | } |
| 2932 | |
| 2933 | static void bc_lex_free(BcLex *l) |
| 2934 | { |
| 2935 | bc_vec_free(&l->t.v); |
| 2936 | } |
| 2937 | |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 2938 | static void bc_lex_file(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2939 | { |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 2940 | G.err_line = l->line = 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2941 | l->newline = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2942 | } |
| 2943 | |
| 2944 | static BcStatus bc_lex_next(BcLex *l) |
| 2945 | { |
| 2946 | BcStatus s; |
| 2947 | |
| 2948 | l->t.last = l->t.t; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2949 | 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] | 2950 | |
| 2951 | l->line += l->newline; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 2952 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2953 | l->t.t = BC_LEX_EOF; |
| 2954 | |
| 2955 | l->newline = (l->i == l->len); |
| 2956 | if (l->newline) return BC_STATUS_SUCCESS; |
| 2957 | |
| 2958 | // Loop until failure or we don't have whitespace. This |
| 2959 | // is so the parser doesn't get inundated with whitespace. |
| 2960 | do { |
| 2961 | s = l->next(l); |
| 2962 | } while (!s && l->t.t == BC_LEX_WHITESPACE); |
| 2963 | |
| 2964 | return s; |
| 2965 | } |
| 2966 | |
| 2967 | static BcStatus bc_lex_text(BcLex *l, const char *text) |
| 2968 | { |
| 2969 | l->buf = text; |
| 2970 | l->i = 0; |
| 2971 | l->len = strlen(text); |
| 2972 | l->t.t = l->t.last = BC_LEX_INVALID; |
| 2973 | return bc_lex_next(l); |
| 2974 | } |
| 2975 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2976 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2977 | static BcStatus bc_lex_identifier(BcLex *l) |
| 2978 | { |
| 2979 | BcStatus s; |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 2980 | unsigned i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2981 | const char *buf = l->buf + l->i - 1; |
| 2982 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 2983 | for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) { |
| 2984 | const char *keyword8 = bc_lex_kws[i].name8; |
| 2985 | unsigned j = 0; |
| 2986 | while (buf[j] != '\0' && buf[j] == keyword8[j]) { |
| 2987 | j++; |
| 2988 | if (j == 8) goto match; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2989 | } |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 2990 | if (keyword8[j] != '\0') |
| 2991 | continue; |
| 2992 | match: |
| 2993 | // buf starts with keyword bc_lex_kws[i] |
| 2994 | l->t.t = BC_LEX_KEY_1st_keyword + i; |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 2995 | if (!bc_lex_kws_POSIX(i)) { |
Denys Vlasenko | 0d7e46b | 2018-12-05 18:31:19 +0100 | [diff] [blame] | 2996 | 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] | 2997 | if (s) return s; |
| 2998 | } |
| 2999 | |
| 3000 | // We minus 1 because the index has already been incremented. |
| 3001 | l->i += j - 1; |
| 3002 | return BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3003 | } |
| 3004 | |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 3005 | bc_lex_name(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3006 | |
Denys Vlasenko | 0d7e46b | 2018-12-05 18:31:19 +0100 | [diff] [blame] | 3007 | if (l->t.v.len > 2) { |
| 3008 | // Prevent this: |
| 3009 | // >>> qwe=1 |
| 3010 | // bc: POSIX only allows one character names; the following is bad: 'qwe=1 |
| 3011 | // ' |
| 3012 | unsigned len = strchrnul(buf, '\n') - buf; |
| 3013 | s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf); |
| 3014 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3015 | |
| 3016 | return s; |
| 3017 | } |
| 3018 | |
| 3019 | static BcStatus bc_lex_string(BcLex *l) |
| 3020 | { |
| 3021 | size_t len, nls = 0, i = l->i; |
| 3022 | char c; |
| 3023 | |
| 3024 | l->t.t = BC_LEX_STR; |
| 3025 | |
| 3026 | for (c = l->buf[i]; c != 0 && c != '"'; c = l->buf[++i]) nls += (c == '\n'); |
| 3027 | |
| 3028 | if (c == '\0') { |
| 3029 | l->i = i; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3030 | return bc_error("string end could not be found"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3031 | } |
| 3032 | |
| 3033 | len = i - l->i; |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 3034 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
| 3035 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
| 3036 | if (len > BC_MAX_STRING) |
| 3037 | return bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"); |
| 3038 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3039 | bc_vec_string(&l->t.v, len, l->buf + l->i); |
| 3040 | |
| 3041 | l->i = i + 1; |
| 3042 | l->line += nls; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3043 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3044 | |
| 3045 | return BC_STATUS_SUCCESS; |
| 3046 | } |
| 3047 | |
| 3048 | static void bc_lex_assign(BcLex *l, BcLexType with, BcLexType without) |
| 3049 | { |
| 3050 | if (l->buf[l->i] == '=') { |
| 3051 | ++l->i; |
| 3052 | l->t.t = with; |
| 3053 | } |
| 3054 | else |
| 3055 | l->t.t = without; |
| 3056 | } |
| 3057 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3058 | static BC_STATUS zbc_lex_comment(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3059 | { |
| 3060 | size_t i, nls = 0; |
| 3061 | const char *buf = l->buf; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3062 | |
| 3063 | l->t.t = BC_LEX_WHITESPACE; |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3064 | i = ++l->i; |
| 3065 | for (;;) { |
| 3066 | char c = buf[i]; |
| 3067 | check_star: |
| 3068 | if (c == '*') { |
| 3069 | c = buf[++i]; |
| 3070 | if (c == '/') |
| 3071 | break; |
| 3072 | goto check_star; |
| 3073 | } |
| 3074 | if (c == '\0') { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3075 | l->i = i; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3076 | RETURN_STATUS(bc_error("comment end could not be found")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3077 | } |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3078 | nls += (c == '\n'); |
| 3079 | i++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3080 | } |
| 3081 | |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3082 | l->i = i + 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3083 | l->line += nls; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3084 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3085 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3086 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3087 | } |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 3088 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3089 | # define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 3090 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3091 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 3092 | static FAST_FUNC BcStatus bc_lex_token(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3093 | { |
| 3094 | BcStatus s = BC_STATUS_SUCCESS; |
| 3095 | char c = l->buf[l->i++], c2; |
| 3096 | |
| 3097 | // This is the workhorse of the lexer. |
| 3098 | switch (c) { |
| 3099 | |
| 3100 | case '\0': |
| 3101 | case '\n': |
| 3102 | { |
| 3103 | l->newline = true; |
| 3104 | l->t.t = !c ? BC_LEX_EOF : BC_LEX_NLINE; |
| 3105 | break; |
| 3106 | } |
| 3107 | |
| 3108 | case '\t': |
| 3109 | case '\v': |
| 3110 | case '\f': |
| 3111 | case '\r': |
| 3112 | case ' ': |
| 3113 | { |
| 3114 | bc_lex_whitespace(l); |
| 3115 | break; |
| 3116 | } |
| 3117 | |
| 3118 | case '!': |
| 3119 | { |
| 3120 | bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT); |
| 3121 | |
| 3122 | if (l->t.t == BC_LEX_OP_BOOL_NOT) { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3123 | s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3124 | if (s) return s; |
| 3125 | } |
| 3126 | |
| 3127 | break; |
| 3128 | } |
| 3129 | |
| 3130 | case '"': |
| 3131 | { |
| 3132 | s = bc_lex_string(l); |
| 3133 | break; |
| 3134 | } |
| 3135 | |
| 3136 | case '#': |
| 3137 | { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3138 | s = bc_POSIX_does_not_allow("'#' script comments"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3139 | if (s) return s; |
| 3140 | |
| 3141 | bc_lex_lineComment(l); |
| 3142 | |
| 3143 | break; |
| 3144 | } |
| 3145 | |
| 3146 | case '%': |
| 3147 | { |
| 3148 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS); |
| 3149 | break; |
| 3150 | } |
| 3151 | |
| 3152 | case '&': |
| 3153 | { |
| 3154 | c2 = l->buf[l->i]; |
| 3155 | if (c2 == '&') { |
| 3156 | |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3157 | s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3158 | if (s) return s; |
| 3159 | |
| 3160 | ++l->i; |
| 3161 | l->t.t = BC_LEX_OP_BOOL_AND; |
| 3162 | } |
| 3163 | else { |
| 3164 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3165 | s = bc_error_bad_character('&'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3166 | } |
| 3167 | |
| 3168 | break; |
| 3169 | } |
| 3170 | |
| 3171 | case '(': |
| 3172 | case ')': |
| 3173 | { |
| 3174 | l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN); |
| 3175 | break; |
| 3176 | } |
| 3177 | |
| 3178 | case '*': |
| 3179 | { |
| 3180 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY); |
| 3181 | break; |
| 3182 | } |
| 3183 | |
| 3184 | case '+': |
| 3185 | { |
| 3186 | c2 = l->buf[l->i]; |
| 3187 | if (c2 == '+') { |
| 3188 | ++l->i; |
| 3189 | l->t.t = BC_LEX_OP_INC; |
| 3190 | } |
| 3191 | else |
| 3192 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS); |
| 3193 | break; |
| 3194 | } |
| 3195 | |
| 3196 | case ',': |
| 3197 | { |
| 3198 | l->t.t = BC_LEX_COMMA; |
| 3199 | break; |
| 3200 | } |
| 3201 | |
| 3202 | case '-': |
| 3203 | { |
| 3204 | c2 = l->buf[l->i]; |
| 3205 | if (c2 == '-') { |
| 3206 | ++l->i; |
| 3207 | l->t.t = BC_LEX_OP_DEC; |
| 3208 | } |
| 3209 | else |
| 3210 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS); |
| 3211 | break; |
| 3212 | } |
| 3213 | |
| 3214 | case '.': |
| 3215 | { |
| 3216 | if (isdigit(l->buf[l->i])) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3217 | s = zbc_lex_number(l, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3218 | else { |
| 3219 | l->t.t = BC_LEX_KEY_LAST; |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3220 | 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] | 3221 | } |
| 3222 | break; |
| 3223 | } |
| 3224 | |
| 3225 | case '/': |
| 3226 | { |
| 3227 | c2 = l->buf[l->i]; |
| 3228 | if (c2 == '*') |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3229 | s = zbc_lex_comment(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3230 | else |
| 3231 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE); |
| 3232 | break; |
| 3233 | } |
| 3234 | |
| 3235 | case '0': |
| 3236 | case '1': |
| 3237 | case '2': |
| 3238 | case '3': |
| 3239 | case '4': |
| 3240 | case '5': |
| 3241 | case '6': |
| 3242 | case '7': |
| 3243 | case '8': |
| 3244 | case '9': |
| 3245 | case 'A': |
| 3246 | case 'B': |
| 3247 | case 'C': |
| 3248 | case 'D': |
| 3249 | case 'E': |
| 3250 | case 'F': |
| 3251 | { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3252 | s = zbc_lex_number(l, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3253 | break; |
| 3254 | } |
| 3255 | |
| 3256 | case ';': |
| 3257 | { |
| 3258 | l->t.t = BC_LEX_SCOLON; |
| 3259 | break; |
| 3260 | } |
| 3261 | |
| 3262 | case '<': |
| 3263 | { |
| 3264 | bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT); |
| 3265 | break; |
| 3266 | } |
| 3267 | |
| 3268 | case '=': |
| 3269 | { |
| 3270 | bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN); |
| 3271 | break; |
| 3272 | } |
| 3273 | |
| 3274 | case '>': |
| 3275 | { |
| 3276 | bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT); |
| 3277 | break; |
| 3278 | } |
| 3279 | |
| 3280 | case '[': |
| 3281 | case ']': |
| 3282 | { |
| 3283 | l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET); |
| 3284 | break; |
| 3285 | } |
| 3286 | |
| 3287 | case '\\': |
| 3288 | { |
| 3289 | if (l->buf[l->i] == '\n') { |
| 3290 | l->t.t = BC_LEX_WHITESPACE; |
| 3291 | ++l->i; |
| 3292 | } |
| 3293 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3294 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3295 | break; |
| 3296 | } |
| 3297 | |
| 3298 | case '^': |
| 3299 | { |
| 3300 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER); |
| 3301 | break; |
| 3302 | } |
| 3303 | |
| 3304 | case 'a': |
| 3305 | case 'b': |
| 3306 | case 'c': |
| 3307 | case 'd': |
| 3308 | case 'e': |
| 3309 | case 'f': |
| 3310 | case 'g': |
| 3311 | case 'h': |
| 3312 | case 'i': |
| 3313 | case 'j': |
| 3314 | case 'k': |
| 3315 | case 'l': |
| 3316 | case 'm': |
| 3317 | case 'n': |
| 3318 | case 'o': |
| 3319 | case 'p': |
| 3320 | case 'q': |
| 3321 | case 'r': |
| 3322 | case 's': |
| 3323 | case 't': |
| 3324 | case 'u': |
| 3325 | case 'v': |
| 3326 | case 'w': |
| 3327 | case 'x': |
| 3328 | case 'y': |
| 3329 | case 'z': |
| 3330 | { |
| 3331 | s = bc_lex_identifier(l); |
| 3332 | break; |
| 3333 | } |
| 3334 | |
| 3335 | case '{': |
| 3336 | case '}': |
| 3337 | { |
| 3338 | l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE); |
| 3339 | break; |
| 3340 | } |
| 3341 | |
| 3342 | case '|': |
| 3343 | { |
| 3344 | c2 = l->buf[l->i]; |
| 3345 | |
| 3346 | if (c2 == '|') { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3347 | s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3348 | if (s) return s; |
| 3349 | |
| 3350 | ++l->i; |
| 3351 | l->t.t = BC_LEX_OP_BOOL_OR; |
| 3352 | } |
| 3353 | else { |
| 3354 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3355 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3356 | } |
| 3357 | |
| 3358 | break; |
| 3359 | } |
| 3360 | |
| 3361 | default: |
| 3362 | { |
| 3363 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3364 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3365 | break; |
| 3366 | } |
| 3367 | } |
| 3368 | |
| 3369 | return s; |
| 3370 | } |
| 3371 | #endif // ENABLE_BC |
| 3372 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 3373 | #if ENABLE_DC |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3374 | static BC_STATUS zdc_lex_register(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3375 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3376 | if (isspace(l->buf[l->i - 1])) { |
| 3377 | bc_lex_whitespace(l); |
| 3378 | ++l->i; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 3379 | if (!G_exreg) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3380 | RETURN_STATUS(bc_error("extended register")); |
| 3381 | bc_lex_name(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3382 | } |
| 3383 | else { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3384 | bc_vec_pop_all(&l->t.v); |
Denys Vlasenko | e55a572 | 2018-12-06 12:47:17 +0100 | [diff] [blame] | 3385 | bc_vec_push(&l->t.v, &l->buf[l->i - 1]); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 3386 | bc_vec_pushZeroByte(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3387 | l->t.t = BC_LEX_NAME; |
| 3388 | } |
| 3389 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3390 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3391 | } |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 3392 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3393 | # define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 3394 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3395 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3396 | static BC_STATUS zdc_lex_string(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3397 | { |
| 3398 | size_t depth = 1, nls = 0, i = l->i; |
| 3399 | char c; |
| 3400 | |
| 3401 | l->t.t = BC_LEX_STR; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3402 | bc_vec_pop_all(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3403 | |
| 3404 | for (c = l->buf[i]; c != 0 && depth; c = l->buf[++i]) { |
| 3405 | |
| 3406 | depth += (c == '[' && (i == l->i || l->buf[i - 1] != '\\')); |
| 3407 | depth -= (c == ']' && (i == l->i || l->buf[i - 1] != '\\')); |
| 3408 | nls += (c == '\n'); |
| 3409 | |
| 3410 | if (depth) bc_vec_push(&l->t.v, &c); |
| 3411 | } |
| 3412 | |
| 3413 | if (c == '\0') { |
| 3414 | l->i = i; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3415 | RETURN_STATUS(bc_error("string end could not be found")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3416 | } |
| 3417 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 3418 | bc_vec_pushZeroByte(&l->t.v); |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 3419 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
| 3420 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
| 3421 | if (i - l->i > BC_MAX_STRING) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3422 | 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] | 3423 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3424 | |
| 3425 | l->i = i; |
| 3426 | l->line += nls; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3427 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3428 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3429 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3430 | } |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 3431 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3432 | # define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 3433 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3434 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 3435 | static FAST_FUNC BcStatus dc_lex_token(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3436 | { |
| 3437 | BcStatus s = BC_STATUS_SUCCESS; |
| 3438 | char c = l->buf[l->i++], c2; |
| 3439 | size_t i; |
| 3440 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3441 | for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) { |
| 3442 | if (l->t.last == dc_lex_regs[i]) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3443 | return zdc_lex_register(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3444 | } |
| 3445 | |
| 3446 | if (c >= '%' && c <= '~' && |
| 3447 | (l->t.t = dc_lex_tokens[(c - '%')]) != BC_LEX_INVALID) |
| 3448 | { |
| 3449 | return s; |
| 3450 | } |
| 3451 | |
| 3452 | // This is the workhorse of the lexer. |
| 3453 | switch (c) { |
| 3454 | |
| 3455 | case '\0': |
| 3456 | { |
| 3457 | l->t.t = BC_LEX_EOF; |
| 3458 | break; |
| 3459 | } |
| 3460 | |
| 3461 | case '\n': |
| 3462 | case '\t': |
| 3463 | case '\v': |
| 3464 | case '\f': |
| 3465 | case '\r': |
| 3466 | case ' ': |
| 3467 | { |
| 3468 | l->newline = (c == '\n'); |
| 3469 | bc_lex_whitespace(l); |
| 3470 | break; |
| 3471 | } |
| 3472 | |
| 3473 | case '!': |
| 3474 | { |
| 3475 | c2 = l->buf[l->i]; |
| 3476 | |
| 3477 | if (c2 == '=') |
| 3478 | l->t.t = BC_LEX_OP_REL_NE; |
| 3479 | else if (c2 == '<') |
| 3480 | l->t.t = BC_LEX_OP_REL_LE; |
| 3481 | else if (c2 == '>') |
| 3482 | l->t.t = BC_LEX_OP_REL_GE; |
| 3483 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3484 | return bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3485 | |
| 3486 | ++l->i; |
| 3487 | break; |
| 3488 | } |
| 3489 | |
| 3490 | case '#': |
| 3491 | { |
| 3492 | bc_lex_lineComment(l); |
| 3493 | break; |
| 3494 | } |
| 3495 | |
| 3496 | case '.': |
| 3497 | { |
| 3498 | if (isdigit(l->buf[l->i])) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3499 | s = zbc_lex_number(l, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3500 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3501 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3502 | break; |
| 3503 | } |
| 3504 | |
| 3505 | case '0': |
| 3506 | case '1': |
| 3507 | case '2': |
| 3508 | case '3': |
| 3509 | case '4': |
| 3510 | case '5': |
| 3511 | case '6': |
| 3512 | case '7': |
| 3513 | case '8': |
| 3514 | case '9': |
| 3515 | case 'A': |
| 3516 | case 'B': |
| 3517 | case 'C': |
| 3518 | case 'D': |
| 3519 | case 'E': |
| 3520 | case 'F': |
| 3521 | { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3522 | s = zbc_lex_number(l, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3523 | break; |
| 3524 | } |
| 3525 | |
| 3526 | case '[': |
| 3527 | { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3528 | s = zdc_lex_string(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3529 | break; |
| 3530 | } |
| 3531 | |
| 3532 | default: |
| 3533 | { |
| 3534 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3535 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3536 | break; |
| 3537 | } |
| 3538 | } |
| 3539 | |
| 3540 | return s; |
| 3541 | } |
| 3542 | #endif // ENABLE_DC |
| 3543 | |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3544 | static void bc_program_addFunc(char *name, size_t *idx); |
| 3545 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3546 | static void bc_parse_addFunc(BcParse *p, char *name, size_t *idx) |
| 3547 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3548 | bc_program_addFunc(name, idx); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 3549 | p->func = bc_program_func(p->fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3550 | } |
| 3551 | |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 3552 | #define bc_parse_push(p, i) bc_vec_pushByte(&(p)->func->code, (char) (i)) |
| 3553 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3554 | static void bc_parse_pushName(BcParse *p, char *name) |
| 3555 | { |
| 3556 | size_t i = 0, len = strlen(name); |
| 3557 | |
| 3558 | for (; i < len; ++i) bc_parse_push(p, name[i]); |
| 3559 | bc_parse_push(p, BC_PARSE_STREND); |
| 3560 | |
| 3561 | free(name); |
| 3562 | } |
| 3563 | |
| 3564 | static void bc_parse_pushIndex(BcParse *p, size_t idx) |
| 3565 | { |
| 3566 | unsigned char amt, i, nums[sizeof(size_t)]; |
| 3567 | |
| 3568 | for (amt = 0; idx; ++amt) { |
| 3569 | nums[amt] = (char) idx; |
| 3570 | idx = (idx & ((unsigned long) ~(UCHAR_MAX))) >> sizeof(char) * CHAR_BIT; |
| 3571 | } |
| 3572 | |
| 3573 | bc_parse_push(p, amt); |
| 3574 | for (i = 0; i < amt; ++i) bc_parse_push(p, nums[i]); |
| 3575 | } |
| 3576 | |
| 3577 | static void bc_parse_number(BcParse *p, BcInst *prev, size_t *nexs) |
| 3578 | { |
| 3579 | char *num = xstrdup(p->l.t.v.v); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3580 | size_t idx = G.prog.consts.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3581 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3582 | bc_vec_push(&G.prog.consts, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3583 | |
| 3584 | bc_parse_push(p, BC_INST_NUM); |
| 3585 | bc_parse_pushIndex(p, idx); |
| 3586 | |
| 3587 | ++(*nexs); |
| 3588 | (*prev) = BC_INST_NUM; |
| 3589 | } |
| 3590 | |
| 3591 | static BcStatus bc_parse_text(BcParse *p, const char *text) |
| 3592 | { |
| 3593 | BcStatus s; |
| 3594 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 3595 | p->func = bc_program_func(p->fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3596 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3597 | if (!text[0] && !BC_PARSE_CAN_EXEC(p)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3598 | p->l.t.t = BC_LEX_INVALID; |
| 3599 | s = p->parse(p); |
| 3600 | if (s) return s; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3601 | if (!BC_PARSE_CAN_EXEC(p)) |
| 3602 | return bc_error("file is not executable"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3603 | } |
| 3604 | |
| 3605 | return bc_lex_text(&p->l, text); |
| 3606 | } |
| 3607 | |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3608 | // Called when parsing or execution detects a failure, |
| 3609 | // resets execution structures. |
| 3610 | static void bc_program_reset(void) |
| 3611 | { |
| 3612 | BcFunc *f; |
| 3613 | BcInstPtr *ip; |
| 3614 | |
| 3615 | bc_vec_npop(&G.prog.stack, G.prog.stack.len - 1); |
| 3616 | bc_vec_pop_all(&G.prog.results); |
| 3617 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 3618 | f = bc_program_func(0); |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3619 | ip = bc_vec_top(&G.prog.stack); |
| 3620 | ip->idx = f->code.len; |
| 3621 | } |
| 3622 | |
Denys Vlasenko | e55a572 | 2018-12-06 12:47:17 +0100 | [diff] [blame] | 3623 | #define bc_parse_updateFunc(p, f) \ |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 3624 | ((p)->func = bc_program_func((p)->fidx = (f))) |
Denys Vlasenko | e55a572 | 2018-12-06 12:47:17 +0100 | [diff] [blame] | 3625 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 3626 | // Called when bc/dc_parse_parse() detects a failure, |
| 3627 | // resets parsing structures. |
| 3628 | static void bc_parse_reset(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3629 | { |
| 3630 | if (p->fidx != BC_PROG_MAIN) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3631 | p->func->nparams = 0; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3632 | bc_vec_pop_all(&p->func->code); |
| 3633 | bc_vec_pop_all(&p->func->autos); |
| 3634 | bc_vec_pop_all(&p->func->labels); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3635 | |
| 3636 | bc_parse_updateFunc(p, BC_PROG_MAIN); |
| 3637 | } |
| 3638 | |
| 3639 | p->l.i = p->l.len; |
| 3640 | p->l.t.t = BC_LEX_EOF; |
| 3641 | p->auto_part = (p->nbraces = 0); |
| 3642 | |
| 3643 | bc_vec_npop(&p->flags, p->flags.len - 1); |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3644 | bc_vec_pop_all(&p->exits); |
| 3645 | bc_vec_pop_all(&p->conds); |
| 3646 | bc_vec_pop_all(&p->ops); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3647 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 3648 | bc_program_reset(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3649 | } |
| 3650 | |
| 3651 | static void bc_parse_free(BcParse *p) |
| 3652 | { |
| 3653 | bc_vec_free(&p->flags); |
| 3654 | bc_vec_free(&p->exits); |
| 3655 | bc_vec_free(&p->conds); |
| 3656 | bc_vec_free(&p->ops); |
| 3657 | bc_lex_free(&p->l); |
| 3658 | } |
| 3659 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3660 | static void bc_parse_create(BcParse *p, size_t func, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3661 | BcParseParse parse, BcLexNext next) |
| 3662 | { |
| 3663 | memset(p, 0, sizeof(BcParse)); |
| 3664 | |
| 3665 | bc_lex_init(&p->l, next); |
| 3666 | bc_vec_init(&p->flags, sizeof(uint8_t), NULL); |
| 3667 | bc_vec_init(&p->exits, sizeof(BcInstPtr), NULL); |
| 3668 | bc_vec_init(&p->conds, sizeof(size_t), NULL); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 3669 | bc_vec_pushZeroByte(&p->flags); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3670 | bc_vec_init(&p->ops, sizeof(BcLexType), NULL); |
| 3671 | |
| 3672 | p->parse = parse; |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 3673 | // p->auto_part = p->nbraces = 0; - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3674 | bc_parse_updateFunc(p, func); |
| 3675 | } |
| 3676 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 3677 | #if ENABLE_BC |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 3678 | |
| 3679 | #define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops))) |
| 3680 | #define BC_PARSE_LEAF(p, rparen) \ |
| 3681 | (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \ |
| 3682 | (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST) |
| 3683 | |
| 3684 | // We can calculate the conversion between tokens and exprs by subtracting the |
| 3685 | // position of the first operator in the lex enum and adding the position of the |
| 3686 | // first in the expr enum. Note: This only works for binary operators. |
| 3687 | #define BC_PARSE_TOKEN_INST(t) ((char) ((t) -BC_LEX_NEG + BC_INST_NEG)) |
| 3688 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3689 | static BcStatus bc_parse_else(BcParse *p); |
| 3690 | static BcStatus bc_parse_stmt(BcParse *p); |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 3691 | static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next); |
Denys Vlasenko | 050b0fe | 2018-12-05 22:40:44 +0100 | [diff] [blame] | 3692 | 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] | 3693 | |
| 3694 | static BcStatus bc_parse_operator(BcParse *p, BcLexType type, size_t start, |
| 3695 | size_t *nexprs, bool next) |
| 3696 | { |
| 3697 | BcStatus s = BC_STATUS_SUCCESS; |
| 3698 | BcLexType t; |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 3699 | char l, r = bc_parse_op_PREC(type - BC_LEX_OP_INC); |
| 3700 | bool left = bc_parse_op_LEFT(type - BC_LEX_OP_INC); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3701 | |
| 3702 | while (p->ops.len > start) { |
| 3703 | |
| 3704 | t = BC_PARSE_TOP_OP(p); |
| 3705 | if (t == BC_LEX_LPAREN) break; |
| 3706 | |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 3707 | l = bc_parse_op_PREC(t - BC_LEX_OP_INC); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3708 | if (l >= r && (l != r || !left)) break; |
| 3709 | |
| 3710 | bc_parse_push(p, BC_PARSE_TOKEN_INST(t)); |
| 3711 | bc_vec_pop(&p->ops); |
| 3712 | *nexprs -= t != BC_LEX_OP_BOOL_NOT && t != BC_LEX_NEG; |
| 3713 | } |
| 3714 | |
| 3715 | bc_vec_push(&p->ops, &type); |
| 3716 | if (next) s = bc_lex_next(&p->l); |
| 3717 | |
| 3718 | return s; |
| 3719 | } |
| 3720 | |
| 3721 | static BcStatus bc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs) |
| 3722 | { |
| 3723 | BcLexType top; |
| 3724 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3725 | if (p->ops.len <= ops_bgn) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3726 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3727 | top = BC_PARSE_TOP_OP(p); |
| 3728 | |
| 3729 | while (top != BC_LEX_LPAREN) { |
| 3730 | |
| 3731 | bc_parse_push(p, BC_PARSE_TOKEN_INST(top)); |
| 3732 | |
| 3733 | bc_vec_pop(&p->ops); |
| 3734 | *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG; |
| 3735 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3736 | if (p->ops.len <= ops_bgn) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3737 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3738 | top = BC_PARSE_TOP_OP(p); |
| 3739 | } |
| 3740 | |
| 3741 | bc_vec_pop(&p->ops); |
| 3742 | |
| 3743 | return bc_lex_next(&p->l); |
| 3744 | } |
| 3745 | |
| 3746 | static BcStatus bc_parse_params(BcParse *p, uint8_t flags) |
| 3747 | { |
| 3748 | BcStatus s; |
| 3749 | bool comma = false; |
| 3750 | size_t nparams; |
| 3751 | |
| 3752 | s = bc_lex_next(&p->l); |
| 3753 | if (s) return s; |
| 3754 | |
| 3755 | for (nparams = 0; p->l.t.t != BC_LEX_RPAREN; ++nparams) { |
| 3756 | |
| 3757 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; |
| 3758 | s = bc_parse_expr(p, flags, bc_parse_next_param); |
| 3759 | if (s) return s; |
| 3760 | |
| 3761 | comma = p->l.t.t == BC_LEX_COMMA; |
| 3762 | if (comma) { |
| 3763 | s = bc_lex_next(&p->l); |
| 3764 | if (s) return s; |
| 3765 | } |
| 3766 | } |
| 3767 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3768 | if (comma) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3769 | bc_parse_push(p, BC_INST_CALL); |
| 3770 | bc_parse_pushIndex(p, nparams); |
| 3771 | |
| 3772 | return BC_STATUS_SUCCESS; |
| 3773 | } |
| 3774 | |
| 3775 | static BcStatus bc_parse_call(BcParse *p, char *name, uint8_t flags) |
| 3776 | { |
| 3777 | BcStatus s; |
| 3778 | BcId entry, *entry_ptr; |
| 3779 | size_t idx; |
| 3780 | |
| 3781 | entry.name = name; |
| 3782 | |
| 3783 | s = bc_parse_params(p, flags); |
| 3784 | if (s) goto err; |
| 3785 | |
| 3786 | if (p->l.t.t != BC_LEX_RPAREN) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3787 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3788 | goto err; |
| 3789 | } |
| 3790 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3791 | idx = bc_map_index(&G.prog.fn_map, &entry); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3792 | |
| 3793 | if (idx == BC_VEC_INVALID_IDX) { |
| 3794 | name = xstrdup(entry.name); |
| 3795 | bc_parse_addFunc(p, name, &idx); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3796 | idx = bc_map_index(&G.prog.fn_map, &entry); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3797 | free(entry.name); |
| 3798 | } |
| 3799 | else |
| 3800 | free(name); |
| 3801 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3802 | entry_ptr = bc_vec_item(&G.prog.fn_map, idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3803 | bc_parse_pushIndex(p, entry_ptr->idx); |
| 3804 | |
| 3805 | return bc_lex_next(&p->l); |
| 3806 | |
| 3807 | err: |
| 3808 | free(name); |
| 3809 | return s; |
| 3810 | } |
| 3811 | |
| 3812 | static BcStatus bc_parse_name(BcParse *p, BcInst *type, uint8_t flags) |
| 3813 | { |
| 3814 | BcStatus s; |
| 3815 | char *name; |
| 3816 | |
| 3817 | name = xstrdup(p->l.t.v.v); |
| 3818 | s = bc_lex_next(&p->l); |
| 3819 | if (s) goto err; |
| 3820 | |
| 3821 | if (p->l.t.t == BC_LEX_LBRACKET) { |
| 3822 | |
| 3823 | s = bc_lex_next(&p->l); |
| 3824 | if (s) goto err; |
| 3825 | |
| 3826 | if (p->l.t.t == BC_LEX_RBRACKET) { |
| 3827 | |
| 3828 | if (!(flags & BC_PARSE_ARRAY)) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3829 | s = bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3830 | goto err; |
| 3831 | } |
| 3832 | |
| 3833 | *type = BC_INST_ARRAY; |
| 3834 | } |
| 3835 | else { |
| 3836 | |
| 3837 | *type = BC_INST_ARRAY_ELEM; |
| 3838 | |
| 3839 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); |
| 3840 | s = bc_parse_expr(p, flags, bc_parse_next_elem); |
| 3841 | if (s) goto err; |
| 3842 | } |
| 3843 | |
| 3844 | s = bc_lex_next(&p->l); |
| 3845 | if (s) goto err; |
| 3846 | bc_parse_push(p, *type); |
| 3847 | bc_parse_pushName(p, name); |
| 3848 | } |
| 3849 | else if (p->l.t.t == BC_LEX_LPAREN) { |
| 3850 | |
| 3851 | if (flags & BC_PARSE_NOCALL) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3852 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3853 | goto err; |
| 3854 | } |
| 3855 | |
| 3856 | *type = BC_INST_CALL; |
| 3857 | s = bc_parse_call(p, name, flags); |
| 3858 | } |
| 3859 | else { |
| 3860 | *type = BC_INST_VAR; |
| 3861 | bc_parse_push(p, BC_INST_VAR); |
| 3862 | bc_parse_pushName(p, name); |
| 3863 | } |
| 3864 | |
| 3865 | return s; |
| 3866 | |
| 3867 | err: |
| 3868 | free(name); |
| 3869 | return s; |
| 3870 | } |
| 3871 | |
| 3872 | static BcStatus bc_parse_read(BcParse *p) |
| 3873 | { |
| 3874 | BcStatus s; |
| 3875 | |
| 3876 | s = bc_lex_next(&p->l); |
| 3877 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3878 | 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] | 3879 | |
| 3880 | s = bc_lex_next(&p->l); |
| 3881 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3882 | 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] | 3883 | |
| 3884 | bc_parse_push(p, BC_INST_READ); |
| 3885 | |
| 3886 | return bc_lex_next(&p->l); |
| 3887 | } |
| 3888 | |
| 3889 | static BcStatus bc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags, |
| 3890 | BcInst *prev) |
| 3891 | { |
| 3892 | BcStatus s; |
| 3893 | |
| 3894 | s = bc_lex_next(&p->l); |
| 3895 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3896 | 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] | 3897 | |
| 3898 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; |
| 3899 | |
| 3900 | s = bc_lex_next(&p->l); |
| 3901 | if (s) return s; |
| 3902 | |
| 3903 | s = bc_parse_expr(p, flags, bc_parse_next_rel); |
| 3904 | if (s) return s; |
| 3905 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3906 | 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] | 3907 | |
| 3908 | *prev = (type == BC_LEX_KEY_LENGTH) ? BC_INST_LENGTH : BC_INST_SQRT; |
| 3909 | bc_parse_push(p, *prev); |
| 3910 | |
| 3911 | return bc_lex_next(&p->l); |
| 3912 | } |
| 3913 | |
| 3914 | static BcStatus bc_parse_scale(BcParse *p, BcInst *type, uint8_t flags) |
| 3915 | { |
| 3916 | BcStatus s; |
| 3917 | |
| 3918 | s = bc_lex_next(&p->l); |
| 3919 | if (s) return s; |
| 3920 | |
| 3921 | if (p->l.t.t != BC_LEX_LPAREN) { |
| 3922 | *type = BC_INST_SCALE; |
| 3923 | bc_parse_push(p, BC_INST_SCALE); |
| 3924 | return BC_STATUS_SUCCESS; |
| 3925 | } |
| 3926 | |
| 3927 | *type = BC_INST_SCALE_FUNC; |
| 3928 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); |
| 3929 | |
| 3930 | s = bc_lex_next(&p->l); |
| 3931 | if (s) return s; |
| 3932 | |
| 3933 | s = bc_parse_expr(p, flags, bc_parse_next_rel); |
| 3934 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3935 | 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] | 3936 | bc_parse_push(p, BC_INST_SCALE_FUNC); |
| 3937 | |
| 3938 | return bc_lex_next(&p->l); |
| 3939 | } |
| 3940 | |
| 3941 | static BcStatus bc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr, |
| 3942 | size_t *nexprs, uint8_t flags) |
| 3943 | { |
| 3944 | BcStatus s; |
| 3945 | BcLexType type; |
| 3946 | char inst; |
| 3947 | BcInst etype = *prev; |
| 3948 | |
| 3949 | if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM || |
| 3950 | etype == BC_INST_SCALE || etype == BC_INST_LAST || |
| 3951 | etype == BC_INST_IBASE || etype == BC_INST_OBASE) |
| 3952 | { |
| 3953 | *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC); |
| 3954 | bc_parse_push(p, inst); |
| 3955 | s = bc_lex_next(&p->l); |
| 3956 | } |
| 3957 | else { |
| 3958 | |
| 3959 | *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC); |
| 3960 | *paren_expr = true; |
| 3961 | |
| 3962 | s = bc_lex_next(&p->l); |
| 3963 | if (s) return s; |
| 3964 | type = p->l.t.t; |
| 3965 | |
| 3966 | // Because we parse the next part of the expression |
| 3967 | // right here, we need to increment this. |
| 3968 | *nexprs = *nexprs + 1; |
| 3969 | |
| 3970 | switch (type) { |
| 3971 | |
| 3972 | case BC_LEX_NAME: |
| 3973 | { |
| 3974 | s = bc_parse_name(p, prev, flags | BC_PARSE_NOCALL); |
| 3975 | break; |
| 3976 | } |
| 3977 | |
| 3978 | case BC_LEX_KEY_IBASE: |
| 3979 | case BC_LEX_KEY_LAST: |
| 3980 | case BC_LEX_KEY_OBASE: |
| 3981 | { |
| 3982 | bc_parse_push(p, type - BC_LEX_KEY_IBASE + BC_INST_IBASE); |
| 3983 | s = bc_lex_next(&p->l); |
| 3984 | break; |
| 3985 | } |
| 3986 | |
| 3987 | case BC_LEX_KEY_SCALE: |
| 3988 | { |
| 3989 | s = bc_lex_next(&p->l); |
| 3990 | if (s) return s; |
| 3991 | if (p->l.t.t == BC_LEX_LPAREN) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3992 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3993 | else |
| 3994 | bc_parse_push(p, BC_INST_SCALE); |
| 3995 | break; |
| 3996 | } |
| 3997 | |
| 3998 | default: |
| 3999 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4000 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4001 | break; |
| 4002 | } |
| 4003 | } |
| 4004 | |
| 4005 | if (!s) bc_parse_push(p, inst); |
| 4006 | } |
| 4007 | |
| 4008 | return s; |
| 4009 | } |
| 4010 | |
| 4011 | static BcStatus bc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn, |
| 4012 | bool rparen, size_t *nexprs) |
| 4013 | { |
| 4014 | BcStatus s; |
| 4015 | BcLexType type; |
| 4016 | BcInst etype = *prev; |
| 4017 | |
| 4018 | s = bc_lex_next(&p->l); |
| 4019 | if (s) return s; |
| 4020 | |
| 4021 | type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST || |
| 4022 | (etype >= BC_INST_NUM && etype <= BC_INST_SQRT) ? |
| 4023 | BC_LEX_OP_MINUS : |
| 4024 | BC_LEX_NEG; |
| 4025 | *prev = BC_PARSE_TOKEN_INST(type); |
| 4026 | |
| 4027 | // We can just push onto the op stack because this is the largest |
| 4028 | // precedence operator that gets pushed. Inc/dec does not. |
| 4029 | if (type != BC_LEX_OP_MINUS) |
| 4030 | bc_vec_push(&p->ops, &type); |
| 4031 | else |
| 4032 | s = bc_parse_operator(p, type, ops_bgn, nexprs, false); |
| 4033 | |
| 4034 | return s; |
| 4035 | } |
| 4036 | |
| 4037 | static BcStatus bc_parse_string(BcParse *p, char inst) |
| 4038 | { |
| 4039 | char *str = xstrdup(p->l.t.v.v); |
| 4040 | |
| 4041 | bc_parse_push(p, BC_INST_STR); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 4042 | bc_parse_pushIndex(p, G.prog.strs.len); |
| 4043 | bc_vec_push(&G.prog.strs, &str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4044 | bc_parse_push(p, inst); |
| 4045 | |
| 4046 | return bc_lex_next(&p->l); |
| 4047 | } |
| 4048 | |
| 4049 | static BcStatus bc_parse_print(BcParse *p) |
| 4050 | { |
| 4051 | BcStatus s; |
| 4052 | BcLexType type; |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4053 | bool comma; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4054 | |
| 4055 | s = bc_lex_next(&p->l); |
| 4056 | if (s) return s; |
| 4057 | |
| 4058 | type = p->l.t.t; |
| 4059 | |
| 4060 | if (type == BC_LEX_SCOLON || type == BC_LEX_NLINE) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4061 | return bc_error("bad print statement"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4062 | |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4063 | comma = false; |
| 4064 | while (type != BC_LEX_SCOLON && type != BC_LEX_NLINE) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4065 | |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4066 | if (type == BC_LEX_STR) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4067 | s = bc_parse_string(p, BC_INST_PRINT_POP); |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4068 | if (s) return s; |
| 4069 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4070 | s = bc_parse_expr(p, 0, bc_parse_next_print); |
| 4071 | if (s) return s; |
| 4072 | bc_parse_push(p, BC_INST_PRINT_POP); |
| 4073 | } |
| 4074 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4075 | comma = p->l.t.t == BC_LEX_COMMA; |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4076 | if (comma) { |
| 4077 | s = bc_lex_next(&p->l); |
| 4078 | if (s) return s; |
| 4079 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4080 | type = p->l.t.t; |
| 4081 | } |
| 4082 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4083 | if (comma) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4084 | |
| 4085 | return bc_lex_next(&p->l); |
| 4086 | } |
| 4087 | |
| 4088 | static BcStatus bc_parse_return(BcParse *p) |
| 4089 | { |
| 4090 | BcStatus s; |
| 4091 | BcLexType t; |
| 4092 | bool paren; |
| 4093 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4094 | if (!BC_PARSE_FUNC(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4095 | |
| 4096 | s = bc_lex_next(&p->l); |
| 4097 | if (s) return s; |
| 4098 | |
| 4099 | t = p->l.t.t; |
| 4100 | paren = t == BC_LEX_LPAREN; |
| 4101 | |
| 4102 | if (t == BC_LEX_NLINE || t == BC_LEX_SCOLON) |
| 4103 | bc_parse_push(p, BC_INST_RET0); |
| 4104 | else { |
| 4105 | |
Denys Vlasenko | 050b0fe | 2018-12-05 22:40:44 +0100 | [diff] [blame] | 4106 | s = bc_parse_expr_empty_ok(p, 0, bc_parse_next_expr); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4107 | if (s == BC_STATUS_PARSE_EMPTY_EXP) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4108 | bc_parse_push(p, BC_INST_RET0); |
| 4109 | s = bc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4110 | } |
Denys Vlasenko | 452df92 | 2018-12-05 20:28:26 +0100 | [diff] [blame] | 4111 | if (s) return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4112 | |
| 4113 | if (!paren || p->l.t.last != BC_LEX_RPAREN) { |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 4114 | s = bc_POSIX_requires("parentheses around return expressions"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4115 | if (s) return s; |
| 4116 | } |
| 4117 | |
| 4118 | bc_parse_push(p, BC_INST_RET); |
| 4119 | } |
| 4120 | |
| 4121 | return s; |
| 4122 | } |
| 4123 | |
| 4124 | static BcStatus bc_parse_endBody(BcParse *p, bool brace) |
| 4125 | { |
| 4126 | BcStatus s = BC_STATUS_SUCCESS; |
| 4127 | |
| 4128 | if (p->flags.len <= 1 || (brace && p->nbraces == 0)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4129 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4130 | |
| 4131 | if (brace) { |
| 4132 | |
| 4133 | if (p->l.t.t == BC_LEX_RBRACE) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4134 | if (!p->nbraces) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4135 | --p->nbraces; |
| 4136 | s = bc_lex_next(&p->l); |
| 4137 | if (s) return s; |
| 4138 | } |
| 4139 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4140 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4141 | } |
| 4142 | |
| 4143 | if (BC_PARSE_IF(p)) { |
| 4144 | |
| 4145 | uint8_t *flag_ptr; |
| 4146 | |
| 4147 | while (p->l.t.t == BC_LEX_NLINE) { |
| 4148 | s = bc_lex_next(&p->l); |
| 4149 | if (s) return s; |
| 4150 | } |
| 4151 | |
| 4152 | bc_vec_pop(&p->flags); |
| 4153 | |
| 4154 | flag_ptr = BC_PARSE_TOP_FLAG_PTR(p); |
| 4155 | *flag_ptr = (*flag_ptr | BC_PARSE_FLAG_IF_END); |
| 4156 | |
| 4157 | if (p->l.t.t == BC_LEX_KEY_ELSE) s = bc_parse_else(p); |
| 4158 | } |
| 4159 | else if (BC_PARSE_ELSE(p)) { |
| 4160 | |
| 4161 | BcInstPtr *ip; |
| 4162 | size_t *label; |
| 4163 | |
| 4164 | bc_vec_pop(&p->flags); |
| 4165 | |
| 4166 | ip = bc_vec_top(&p->exits); |
| 4167 | label = bc_vec_item(&p->func->labels, ip->idx); |
| 4168 | *label = p->func->code.len; |
| 4169 | |
| 4170 | bc_vec_pop(&p->exits); |
| 4171 | } |
| 4172 | else if (BC_PARSE_FUNC_INNER(p)) { |
| 4173 | bc_parse_push(p, BC_INST_RET0); |
| 4174 | bc_parse_updateFunc(p, BC_PROG_MAIN); |
| 4175 | bc_vec_pop(&p->flags); |
| 4176 | } |
| 4177 | else { |
| 4178 | |
| 4179 | BcInstPtr *ip = bc_vec_top(&p->exits); |
| 4180 | size_t *label = bc_vec_top(&p->conds); |
| 4181 | |
| 4182 | bc_parse_push(p, BC_INST_JUMP); |
| 4183 | bc_parse_pushIndex(p, *label); |
| 4184 | |
| 4185 | label = bc_vec_item(&p->func->labels, ip->idx); |
| 4186 | *label = p->func->code.len; |
| 4187 | |
| 4188 | bc_vec_pop(&p->flags); |
| 4189 | bc_vec_pop(&p->exits); |
| 4190 | bc_vec_pop(&p->conds); |
| 4191 | } |
| 4192 | |
| 4193 | return s; |
| 4194 | } |
| 4195 | |
| 4196 | static void bc_parse_startBody(BcParse *p, uint8_t flags) |
| 4197 | { |
| 4198 | uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p); |
| 4199 | flags |= (*flag_ptr & (BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_LOOP)); |
| 4200 | flags |= BC_PARSE_FLAG_BODY; |
| 4201 | bc_vec_push(&p->flags, &flags); |
| 4202 | } |
| 4203 | |
| 4204 | static void bc_parse_noElse(BcParse *p) |
| 4205 | { |
| 4206 | BcInstPtr *ip; |
| 4207 | size_t *label; |
| 4208 | uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p); |
| 4209 | |
| 4210 | *flag_ptr = (*flag_ptr & ~(BC_PARSE_FLAG_IF_END)); |
| 4211 | |
| 4212 | ip = bc_vec_top(&p->exits); |
| 4213 | label = bc_vec_item(&p->func->labels, ip->idx); |
| 4214 | *label = p->func->code.len; |
| 4215 | |
| 4216 | bc_vec_pop(&p->exits); |
| 4217 | } |
| 4218 | |
| 4219 | static BcStatus bc_parse_if(BcParse *p) |
| 4220 | { |
| 4221 | BcStatus s; |
| 4222 | BcInstPtr ip; |
| 4223 | |
| 4224 | s = bc_lex_next(&p->l); |
| 4225 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4226 | 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] | 4227 | |
| 4228 | s = bc_lex_next(&p->l); |
| 4229 | if (s) return s; |
| 4230 | s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel); |
| 4231 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4232 | 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] | 4233 | |
| 4234 | s = bc_lex_next(&p->l); |
| 4235 | if (s) return s; |
| 4236 | bc_parse_push(p, BC_INST_JUMP_ZERO); |
| 4237 | |
| 4238 | ip.idx = p->func->labels.len; |
| 4239 | ip.func = ip.len = 0; |
| 4240 | |
| 4241 | bc_parse_pushIndex(p, ip.idx); |
| 4242 | bc_vec_push(&p->exits, &ip); |
| 4243 | bc_vec_push(&p->func->labels, &ip.idx); |
| 4244 | bc_parse_startBody(p, BC_PARSE_FLAG_IF); |
| 4245 | |
| 4246 | return BC_STATUS_SUCCESS; |
| 4247 | } |
| 4248 | |
| 4249 | static BcStatus bc_parse_else(BcParse *p) |
| 4250 | { |
| 4251 | BcInstPtr ip; |
| 4252 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4253 | if (!BC_PARSE_IF_END(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4254 | |
| 4255 | ip.idx = p->func->labels.len; |
| 4256 | ip.func = ip.len = 0; |
| 4257 | |
| 4258 | bc_parse_push(p, BC_INST_JUMP); |
| 4259 | bc_parse_pushIndex(p, ip.idx); |
| 4260 | |
| 4261 | bc_parse_noElse(p); |
| 4262 | |
| 4263 | bc_vec_push(&p->exits, &ip); |
| 4264 | bc_vec_push(&p->func->labels, &ip.idx); |
| 4265 | bc_parse_startBody(p, BC_PARSE_FLAG_ELSE); |
| 4266 | |
| 4267 | return bc_lex_next(&p->l); |
| 4268 | } |
| 4269 | |
| 4270 | static BcStatus bc_parse_while(BcParse *p) |
| 4271 | { |
| 4272 | BcStatus s; |
| 4273 | BcInstPtr ip; |
| 4274 | |
| 4275 | s = bc_lex_next(&p->l); |
| 4276 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4277 | 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] | 4278 | s = bc_lex_next(&p->l); |
| 4279 | if (s) return s; |
| 4280 | |
| 4281 | ip.idx = p->func->labels.len; |
| 4282 | |
| 4283 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4284 | bc_vec_push(&p->conds, &ip.idx); |
| 4285 | |
| 4286 | ip.idx = p->func->labels.len; |
| 4287 | ip.func = 1; |
| 4288 | ip.len = 0; |
| 4289 | |
| 4290 | bc_vec_push(&p->exits, &ip); |
| 4291 | bc_vec_push(&p->func->labels, &ip.idx); |
| 4292 | |
| 4293 | s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel); |
| 4294 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4295 | 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] | 4296 | s = bc_lex_next(&p->l); |
| 4297 | if (s) return s; |
| 4298 | |
| 4299 | bc_parse_push(p, BC_INST_JUMP_ZERO); |
| 4300 | bc_parse_pushIndex(p, ip.idx); |
| 4301 | bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER); |
| 4302 | |
| 4303 | return BC_STATUS_SUCCESS; |
| 4304 | } |
| 4305 | |
| 4306 | static BcStatus bc_parse_for(BcParse *p) |
| 4307 | { |
| 4308 | BcStatus s; |
| 4309 | BcInstPtr ip; |
| 4310 | size_t cond_idx, exit_idx, body_idx, update_idx; |
| 4311 | |
| 4312 | s = bc_lex_next(&p->l); |
| 4313 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4314 | 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] | 4315 | s = bc_lex_next(&p->l); |
| 4316 | if (s) return s; |
| 4317 | |
| 4318 | if (p->l.t.t != BC_LEX_SCOLON) |
| 4319 | s = bc_parse_expr(p, 0, bc_parse_next_for); |
| 4320 | else |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 4321 | s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4322 | |
| 4323 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4324 | 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] | 4325 | s = bc_lex_next(&p->l); |
| 4326 | if (s) return s; |
| 4327 | |
| 4328 | cond_idx = p->func->labels.len; |
| 4329 | update_idx = cond_idx + 1; |
| 4330 | body_idx = update_idx + 1; |
| 4331 | exit_idx = body_idx + 1; |
| 4332 | |
| 4333 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4334 | |
| 4335 | if (p->l.t.t != BC_LEX_SCOLON) |
| 4336 | s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_for); |
| 4337 | else |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 4338 | s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4339 | |
| 4340 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4341 | 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] | 4342 | |
| 4343 | s = bc_lex_next(&p->l); |
| 4344 | if (s) return s; |
| 4345 | |
| 4346 | bc_parse_push(p, BC_INST_JUMP_ZERO); |
| 4347 | bc_parse_pushIndex(p, exit_idx); |
| 4348 | bc_parse_push(p, BC_INST_JUMP); |
| 4349 | bc_parse_pushIndex(p, body_idx); |
| 4350 | |
| 4351 | ip.idx = p->func->labels.len; |
| 4352 | |
| 4353 | bc_vec_push(&p->conds, &update_idx); |
| 4354 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4355 | |
| 4356 | if (p->l.t.t != BC_LEX_RPAREN) |
| 4357 | s = bc_parse_expr(p, 0, bc_parse_next_rel); |
| 4358 | else |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 4359 | s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4360 | |
| 4361 | if (s) return s; |
| 4362 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4363 | 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] | 4364 | bc_parse_push(p, BC_INST_JUMP); |
| 4365 | bc_parse_pushIndex(p, cond_idx); |
| 4366 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4367 | |
| 4368 | ip.idx = exit_idx; |
| 4369 | ip.func = 1; |
| 4370 | ip.len = 0; |
| 4371 | |
| 4372 | bc_vec_push(&p->exits, &ip); |
| 4373 | bc_vec_push(&p->func->labels, &ip.idx); |
| 4374 | bc_lex_next(&p->l); |
| 4375 | bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER); |
| 4376 | |
| 4377 | return BC_STATUS_SUCCESS; |
| 4378 | } |
| 4379 | |
| 4380 | static BcStatus bc_parse_loopExit(BcParse *p, BcLexType type) |
| 4381 | { |
| 4382 | BcStatus s; |
| 4383 | size_t i; |
| 4384 | BcInstPtr *ip; |
| 4385 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4386 | if (!BC_PARSE_LOOP(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4387 | |
| 4388 | if (type == BC_LEX_KEY_BREAK) { |
| 4389 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4390 | if (p->exits.len == 0) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4391 | |
| 4392 | i = p->exits.len - 1; |
| 4393 | ip = bc_vec_item(&p->exits, i); |
| 4394 | |
| 4395 | 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] | 4396 | if (i >= p->exits.len && !ip->func) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4397 | |
| 4398 | i = ip->idx; |
| 4399 | } |
| 4400 | else |
| 4401 | i = *((size_t *) bc_vec_top(&p->conds)); |
| 4402 | |
| 4403 | bc_parse_push(p, BC_INST_JUMP); |
| 4404 | bc_parse_pushIndex(p, i); |
| 4405 | |
| 4406 | s = bc_lex_next(&p->l); |
| 4407 | if (s) return s; |
| 4408 | |
| 4409 | 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] | 4410 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4411 | |
| 4412 | return bc_lex_next(&p->l); |
| 4413 | } |
| 4414 | |
| 4415 | static BcStatus bc_parse_func(BcParse *p) |
| 4416 | { |
| 4417 | BcStatus s; |
| 4418 | bool var, comma = false; |
| 4419 | uint8_t flags; |
| 4420 | char *name; |
| 4421 | |
| 4422 | s = bc_lex_next(&p->l); |
| 4423 | if (s) return s; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4424 | if (p->l.t.t != BC_LEX_NAME) |
| 4425 | return bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4426 | |
| 4427 | name = xstrdup(p->l.t.v.v); |
| 4428 | bc_parse_addFunc(p, name, &p->fidx); |
| 4429 | |
| 4430 | s = bc_lex_next(&p->l); |
| 4431 | if (s) return s; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4432 | if (p->l.t.t != BC_LEX_LPAREN) |
| 4433 | return bc_error("bad function definition"); |
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 | while (p->l.t.t != BC_LEX_RPAREN) { |
| 4438 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4439 | if (p->l.t.t != BC_LEX_NAME) |
| 4440 | return bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4441 | |
| 4442 | ++p->func->nparams; |
| 4443 | |
| 4444 | name = xstrdup(p->l.t.v.v); |
| 4445 | s = bc_lex_next(&p->l); |
| 4446 | if (s) goto err; |
| 4447 | |
| 4448 | var = p->l.t.t != BC_LEX_LBRACKET; |
| 4449 | |
| 4450 | if (!var) { |
| 4451 | |
| 4452 | s = bc_lex_next(&p->l); |
| 4453 | if (s) goto err; |
| 4454 | |
| 4455 | if (p->l.t.t != BC_LEX_RBRACKET) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4456 | s = bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4457 | goto err; |
| 4458 | } |
| 4459 | |
| 4460 | s = bc_lex_next(&p->l); |
| 4461 | if (s) goto err; |
| 4462 | } |
| 4463 | |
| 4464 | comma = p->l.t.t == BC_LEX_COMMA; |
| 4465 | if (comma) { |
| 4466 | s = bc_lex_next(&p->l); |
| 4467 | if (s) goto err; |
| 4468 | } |
| 4469 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 4470 | s = zbc_func_insert(p->func, name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4471 | if (s) goto err; |
| 4472 | } |
| 4473 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4474 | if (comma) return bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4475 | |
| 4476 | flags = BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_BODY; |
| 4477 | bc_parse_startBody(p, flags); |
| 4478 | |
| 4479 | s = bc_lex_next(&p->l); |
| 4480 | if (s) return s; |
| 4481 | |
| 4482 | if (p->l.t.t != BC_LEX_LBRACE) |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 4483 | 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] | 4484 | |
| 4485 | return s; |
| 4486 | |
| 4487 | err: |
| 4488 | free(name); |
| 4489 | return s; |
| 4490 | } |
| 4491 | |
| 4492 | static BcStatus bc_parse_auto(BcParse *p) |
| 4493 | { |
| 4494 | BcStatus s; |
| 4495 | bool comma, var, one; |
| 4496 | char *name; |
| 4497 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4498 | if (!p->auto_part) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4499 | s = bc_lex_next(&p->l); |
| 4500 | if (s) return s; |
| 4501 | |
| 4502 | p->auto_part = comma = false; |
| 4503 | one = p->l.t.t == BC_LEX_NAME; |
| 4504 | |
| 4505 | while (p->l.t.t == BC_LEX_NAME) { |
| 4506 | |
| 4507 | name = xstrdup(p->l.t.v.v); |
| 4508 | s = bc_lex_next(&p->l); |
| 4509 | if (s) goto err; |
| 4510 | |
| 4511 | var = p->l.t.t != BC_LEX_LBRACKET; |
| 4512 | if (!var) { |
| 4513 | |
| 4514 | s = bc_lex_next(&p->l); |
| 4515 | if (s) goto err; |
| 4516 | |
| 4517 | if (p->l.t.t != BC_LEX_RBRACKET) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4518 | s = bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4519 | goto err; |
| 4520 | } |
| 4521 | |
| 4522 | s = bc_lex_next(&p->l); |
| 4523 | if (s) goto err; |
| 4524 | } |
| 4525 | |
| 4526 | comma = p->l.t.t == BC_LEX_COMMA; |
| 4527 | if (comma) { |
| 4528 | s = bc_lex_next(&p->l); |
| 4529 | if (s) goto err; |
| 4530 | } |
| 4531 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 4532 | s = zbc_func_insert(p->func, name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4533 | if (s) goto err; |
| 4534 | } |
| 4535 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4536 | if (comma) return bc_error("bad function definition"); |
Denys Vlasenko | abbc433 | 2018-12-03 21:46:41 +0100 | [diff] [blame] | 4537 | if (!one) return bc_error("no auto variable found"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4538 | |
| 4539 | 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] | 4540 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4541 | |
| 4542 | return bc_lex_next(&p->l); |
| 4543 | |
| 4544 | err: |
| 4545 | free(name); |
| 4546 | return s; |
| 4547 | } |
| 4548 | |
| 4549 | static BcStatus bc_parse_body(BcParse *p, bool brace) |
| 4550 | { |
| 4551 | BcStatus s = BC_STATUS_SUCCESS; |
| 4552 | uint8_t *flag_ptr = bc_vec_top(&p->flags); |
| 4553 | |
| 4554 | *flag_ptr &= ~(BC_PARSE_FLAG_BODY); |
| 4555 | |
| 4556 | if (*flag_ptr & BC_PARSE_FLAG_FUNC_INNER) { |
| 4557 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4558 | if (!brace) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4559 | p->auto_part = p->l.t.t != BC_LEX_KEY_AUTO; |
| 4560 | |
| 4561 | if (!p->auto_part) { |
| 4562 | s = bc_parse_auto(p); |
| 4563 | if (s) return s; |
| 4564 | } |
| 4565 | |
| 4566 | if (p->l.t.t == BC_LEX_NLINE) s = bc_lex_next(&p->l); |
| 4567 | } |
| 4568 | else { |
| 4569 | s = bc_parse_stmt(p); |
| 4570 | if (!s && !brace) s = bc_parse_endBody(p, false); |
| 4571 | } |
| 4572 | |
| 4573 | return s; |
| 4574 | } |
| 4575 | |
| 4576 | static BcStatus bc_parse_stmt(BcParse *p) |
| 4577 | { |
| 4578 | BcStatus s = BC_STATUS_SUCCESS; |
| 4579 | |
| 4580 | switch (p->l.t.t) { |
| 4581 | |
| 4582 | case BC_LEX_NLINE: |
| 4583 | { |
| 4584 | return bc_lex_next(&p->l); |
| 4585 | } |
| 4586 | |
| 4587 | case BC_LEX_KEY_ELSE: |
| 4588 | { |
| 4589 | p->auto_part = false; |
| 4590 | break; |
| 4591 | } |
| 4592 | |
| 4593 | case BC_LEX_LBRACE: |
| 4594 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4595 | if (!BC_PARSE_BODY(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4596 | |
| 4597 | ++p->nbraces; |
| 4598 | s = bc_lex_next(&p->l); |
| 4599 | if (s) return s; |
| 4600 | |
| 4601 | return bc_parse_body(p, true); |
| 4602 | } |
| 4603 | |
| 4604 | case BC_LEX_KEY_AUTO: |
| 4605 | { |
| 4606 | return bc_parse_auto(p); |
| 4607 | } |
| 4608 | |
| 4609 | default: |
| 4610 | { |
| 4611 | p->auto_part = false; |
| 4612 | |
| 4613 | if (BC_PARSE_IF_END(p)) { |
| 4614 | bc_parse_noElse(p); |
| 4615 | return BC_STATUS_SUCCESS; |
| 4616 | } |
| 4617 | else if (BC_PARSE_BODY(p)) |
| 4618 | return bc_parse_body(p, false); |
| 4619 | |
| 4620 | break; |
| 4621 | } |
| 4622 | } |
| 4623 | |
| 4624 | switch (p->l.t.t) { |
| 4625 | |
| 4626 | case BC_LEX_OP_INC: |
| 4627 | case BC_LEX_OP_DEC: |
| 4628 | case BC_LEX_OP_MINUS: |
| 4629 | case BC_LEX_OP_BOOL_NOT: |
| 4630 | case BC_LEX_LPAREN: |
| 4631 | case BC_LEX_NAME: |
| 4632 | case BC_LEX_NUMBER: |
| 4633 | case BC_LEX_KEY_IBASE: |
| 4634 | case BC_LEX_KEY_LAST: |
| 4635 | case BC_LEX_KEY_LENGTH: |
| 4636 | case BC_LEX_KEY_OBASE: |
| 4637 | case BC_LEX_KEY_READ: |
| 4638 | case BC_LEX_KEY_SCALE: |
| 4639 | case BC_LEX_KEY_SQRT: |
| 4640 | { |
| 4641 | s = bc_parse_expr(p, BC_PARSE_PRINT, bc_parse_next_expr); |
| 4642 | break; |
| 4643 | } |
| 4644 | |
| 4645 | case BC_LEX_KEY_ELSE: |
| 4646 | { |
| 4647 | s = bc_parse_else(p); |
| 4648 | break; |
| 4649 | } |
| 4650 | |
| 4651 | case BC_LEX_SCOLON: |
| 4652 | { |
| 4653 | while (!s && p->l.t.t == BC_LEX_SCOLON) s = bc_lex_next(&p->l); |
| 4654 | break; |
| 4655 | } |
| 4656 | |
| 4657 | case BC_LEX_RBRACE: |
| 4658 | { |
| 4659 | s = bc_parse_endBody(p, true); |
| 4660 | break; |
| 4661 | } |
| 4662 | |
| 4663 | case BC_LEX_STR: |
| 4664 | { |
| 4665 | s = bc_parse_string(p, BC_INST_PRINT_STR); |
| 4666 | break; |
| 4667 | } |
| 4668 | |
| 4669 | case BC_LEX_KEY_BREAK: |
| 4670 | case BC_LEX_KEY_CONTINUE: |
| 4671 | { |
| 4672 | s = bc_parse_loopExit(p, p->l.t.t); |
| 4673 | break; |
| 4674 | } |
| 4675 | |
| 4676 | case BC_LEX_KEY_FOR: |
| 4677 | { |
| 4678 | s = bc_parse_for(p); |
| 4679 | break; |
| 4680 | } |
| 4681 | |
| 4682 | case BC_LEX_KEY_HALT: |
| 4683 | { |
| 4684 | bc_parse_push(p, BC_INST_HALT); |
| 4685 | s = bc_lex_next(&p->l); |
| 4686 | break; |
| 4687 | } |
| 4688 | |
| 4689 | case BC_LEX_KEY_IF: |
| 4690 | { |
| 4691 | s = bc_parse_if(p); |
| 4692 | break; |
| 4693 | } |
| 4694 | |
| 4695 | case BC_LEX_KEY_LIMITS: |
| 4696 | { |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 4697 | // "limits" is a compile-time command, |
| 4698 | // the output is produced at _parse time_. |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4699 | s = bc_lex_next(&p->l); |
| 4700 | if (s) return s; |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 4701 | printf( |
| 4702 | "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n" |
| 4703 | "BC_DIM_MAX = "BC_MAX_DIM_STR "\n" |
| 4704 | "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n" |
| 4705 | "BC_STRING_MAX = "BC_MAX_STRING_STR"\n" |
| 4706 | "BC_NAME_MAX = "BC_MAX_NAME_STR "\n" |
| 4707 | "BC_NUM_MAX = "BC_MAX_NUM_STR "\n" |
| 4708 | "MAX Exponent = "BC_MAX_EXP_STR "\n" |
| 4709 | "Number of vars = "BC_MAX_VARS_STR "\n" |
| 4710 | ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4711 | break; |
| 4712 | } |
| 4713 | |
| 4714 | case BC_LEX_KEY_PRINT: |
| 4715 | { |
| 4716 | s = bc_parse_print(p); |
| 4717 | break; |
| 4718 | } |
| 4719 | |
| 4720 | case BC_LEX_KEY_QUIT: |
| 4721 | { |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 4722 | // "quit" is a compile-time command. For example, |
| 4723 | // "if (0 == 1) quit" terminates when parsing the statement, |
| 4724 | // not when it is executed |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 4725 | QUIT_OR_RETURN_TO_MAIN; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4726 | } |
| 4727 | |
| 4728 | case BC_LEX_KEY_RETURN: |
| 4729 | { |
| 4730 | s = bc_parse_return(p); |
| 4731 | break; |
| 4732 | } |
| 4733 | |
| 4734 | case BC_LEX_KEY_WHILE: |
| 4735 | { |
| 4736 | s = bc_parse_while(p); |
| 4737 | break; |
| 4738 | } |
| 4739 | |
| 4740 | default: |
| 4741 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4742 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4743 | break; |
| 4744 | } |
| 4745 | } |
| 4746 | |
| 4747 | return s; |
| 4748 | } |
| 4749 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 4750 | static FAST_FUNC BcStatus bc_parse_parse(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4751 | { |
| 4752 | BcStatus s; |
| 4753 | |
| 4754 | if (p->l.t.t == BC_LEX_EOF) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4755 | 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] | 4756 | else if (p->l.t.t == BC_LEX_KEY_DEFINE) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4757 | if (!BC_PARSE_CAN_EXEC(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4758 | s = bc_parse_func(p); |
| 4759 | } |
| 4760 | else |
| 4761 | s = bc_parse_stmt(p); |
| 4762 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 4763 | if (s || G_interrupt) { |
| 4764 | bc_parse_reset(p); |
| 4765 | s = BC_STATUS_FAILURE; |
| 4766 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4767 | |
| 4768 | return s; |
| 4769 | } |
| 4770 | |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 4771 | // This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP |
Denys Vlasenko | 050b0fe | 2018-12-05 22:40:44 +0100 | [diff] [blame] | 4772 | 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] | 4773 | { |
| 4774 | BcStatus s = BC_STATUS_SUCCESS; |
| 4775 | BcInst prev = BC_INST_PRINT; |
| 4776 | BcLexType top, t = p->l.t.t; |
| 4777 | size_t nexprs = 0, ops_bgn = p->ops.len; |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 4778 | unsigned nparens, nrelops; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4779 | bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last; |
| 4780 | |
| 4781 | paren_first = p->l.t.t == BC_LEX_LPAREN; |
| 4782 | nparens = nrelops = 0; |
| 4783 | paren_expr = rprn = done = get_token = assign = false; |
| 4784 | bin_last = true; |
| 4785 | |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 4786 | 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] | 4787 | switch (t) { |
| 4788 | |
| 4789 | case BC_LEX_OP_INC: |
| 4790 | case BC_LEX_OP_DEC: |
| 4791 | { |
| 4792 | s = bc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags); |
| 4793 | rprn = get_token = bin_last = false; |
| 4794 | break; |
| 4795 | } |
| 4796 | |
| 4797 | case BC_LEX_OP_MINUS: |
| 4798 | { |
| 4799 | s = bc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs); |
| 4800 | rprn = get_token = false; |
| 4801 | bin_last = prev == BC_INST_MINUS; |
| 4802 | break; |
| 4803 | } |
| 4804 | |
| 4805 | case BC_LEX_OP_ASSIGN_POWER: |
| 4806 | case BC_LEX_OP_ASSIGN_MULTIPLY: |
| 4807 | case BC_LEX_OP_ASSIGN_DIVIDE: |
| 4808 | case BC_LEX_OP_ASSIGN_MODULUS: |
| 4809 | case BC_LEX_OP_ASSIGN_PLUS: |
| 4810 | case BC_LEX_OP_ASSIGN_MINUS: |
| 4811 | case BC_LEX_OP_ASSIGN: |
| 4812 | { |
| 4813 | if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM && |
| 4814 | prev != BC_INST_SCALE && prev != BC_INST_IBASE && |
| 4815 | prev != BC_INST_OBASE && prev != BC_INST_LAST) |
| 4816 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4817 | s = bc_error("bad assignment:" |
| 4818 | " left side must be scale," |
| 4819 | " ibase, obase, last, var," |
| 4820 | " or array element" |
| 4821 | ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4822 | break; |
| 4823 | } |
| 4824 | } |
| 4825 | // Fallthrough. |
| 4826 | case BC_LEX_OP_POWER: |
| 4827 | case BC_LEX_OP_MULTIPLY: |
| 4828 | case BC_LEX_OP_DIVIDE: |
| 4829 | case BC_LEX_OP_MODULUS: |
| 4830 | case BC_LEX_OP_PLUS: |
| 4831 | case BC_LEX_OP_REL_EQ: |
| 4832 | case BC_LEX_OP_REL_LE: |
| 4833 | case BC_LEX_OP_REL_GE: |
| 4834 | case BC_LEX_OP_REL_NE: |
| 4835 | case BC_LEX_OP_REL_LT: |
| 4836 | case BC_LEX_OP_REL_GT: |
| 4837 | case BC_LEX_OP_BOOL_NOT: |
| 4838 | case BC_LEX_OP_BOOL_OR: |
| 4839 | case BC_LEX_OP_BOOL_AND: |
| 4840 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4841 | if (((t == BC_LEX_OP_BOOL_NOT) != bin_last) |
| 4842 | || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT) |
| 4843 | ) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4844 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4845 | } |
| 4846 | |
| 4847 | nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT; |
| 4848 | prev = BC_PARSE_TOKEN_INST(t); |
| 4849 | s = bc_parse_operator(p, t, ops_bgn, &nexprs, true); |
| 4850 | rprn = get_token = false; |
| 4851 | bin_last = t != BC_LEX_OP_BOOL_NOT; |
| 4852 | |
| 4853 | break; |
| 4854 | } |
| 4855 | |
| 4856 | case BC_LEX_LPAREN: |
| 4857 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4858 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4859 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4860 | ++nparens; |
| 4861 | paren_expr = rprn = bin_last = false; |
| 4862 | get_token = true; |
| 4863 | bc_vec_push(&p->ops, &t); |
| 4864 | |
| 4865 | break; |
| 4866 | } |
| 4867 | |
| 4868 | case BC_LEX_RPAREN: |
| 4869 | { |
| 4870 | if (bin_last || prev == BC_INST_BOOL_NOT) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4871 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4872 | |
| 4873 | if (nparens == 0) { |
| 4874 | s = BC_STATUS_SUCCESS; |
| 4875 | done = true; |
| 4876 | get_token = false; |
| 4877 | break; |
| 4878 | } |
| 4879 | else if (!paren_expr) |
| 4880 | return BC_STATUS_PARSE_EMPTY_EXP; |
| 4881 | |
| 4882 | --nparens; |
| 4883 | paren_expr = rprn = true; |
| 4884 | get_token = bin_last = false; |
| 4885 | |
| 4886 | s = bc_parse_rightParen(p, ops_bgn, &nexprs); |
| 4887 | |
| 4888 | break; |
| 4889 | } |
| 4890 | |
| 4891 | case BC_LEX_NAME: |
| 4892 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4893 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4894 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4895 | paren_expr = true; |
| 4896 | rprn = get_token = bin_last = false; |
| 4897 | s = bc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL); |
| 4898 | ++nexprs; |
| 4899 | |
| 4900 | break; |
| 4901 | } |
| 4902 | |
| 4903 | case BC_LEX_NUMBER: |
| 4904 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4905 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4906 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4907 | bc_parse_number(p, &prev, &nexprs); |
| 4908 | paren_expr = get_token = true; |
| 4909 | rprn = bin_last = false; |
| 4910 | |
| 4911 | break; |
| 4912 | } |
| 4913 | |
| 4914 | case BC_LEX_KEY_IBASE: |
| 4915 | case BC_LEX_KEY_LAST: |
| 4916 | case BC_LEX_KEY_OBASE: |
| 4917 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4918 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4919 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4920 | prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE); |
| 4921 | bc_parse_push(p, (char) prev); |
| 4922 | |
| 4923 | paren_expr = get_token = true; |
| 4924 | rprn = bin_last = false; |
| 4925 | ++nexprs; |
| 4926 | |
| 4927 | break; |
| 4928 | } |
| 4929 | |
| 4930 | case BC_LEX_KEY_LENGTH: |
| 4931 | case BC_LEX_KEY_SQRT: |
| 4932 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4933 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4934 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4935 | s = bc_parse_builtin(p, t, flags, &prev); |
| 4936 | paren_expr = true; |
| 4937 | rprn = get_token = bin_last = false; |
| 4938 | ++nexprs; |
| 4939 | |
| 4940 | break; |
| 4941 | } |
| 4942 | |
| 4943 | case BC_LEX_KEY_READ: |
| 4944 | { |
| 4945 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4946 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4947 | else if (flags & BC_PARSE_NOREAD) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4948 | s = bc_error_nested_read_call(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4949 | else |
| 4950 | s = bc_parse_read(p); |
| 4951 | |
| 4952 | paren_expr = true; |
| 4953 | rprn = get_token = bin_last = false; |
| 4954 | ++nexprs; |
| 4955 | prev = BC_INST_READ; |
| 4956 | |
| 4957 | break; |
| 4958 | } |
| 4959 | |
| 4960 | case BC_LEX_KEY_SCALE: |
| 4961 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4962 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4963 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4964 | s = bc_parse_scale(p, &prev, flags); |
| 4965 | paren_expr = true; |
| 4966 | rprn = get_token = bin_last = false; |
| 4967 | ++nexprs; |
| 4968 | prev = BC_INST_SCALE; |
| 4969 | |
| 4970 | break; |
| 4971 | } |
| 4972 | |
| 4973 | default: |
| 4974 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4975 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4976 | break; |
| 4977 | } |
| 4978 | } |
| 4979 | |
| 4980 | if (!s && get_token) s = bc_lex_next(&p->l); |
| 4981 | } |
| 4982 | |
| 4983 | if (s) return s; |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 4984 | if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4985 | |
| 4986 | while (p->ops.len > ops_bgn) { |
| 4987 | |
| 4988 | top = BC_PARSE_TOP_OP(p); |
| 4989 | assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN; |
| 4990 | |
| 4991 | if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4992 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4993 | |
| 4994 | bc_parse_push(p, BC_PARSE_TOKEN_INST(top)); |
| 4995 | |
| 4996 | nexprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG; |
| 4997 | bc_vec_pop(&p->ops); |
| 4998 | } |
| 4999 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5000 | if (prev == BC_INST_BOOL_NOT || nexprs != 1) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5001 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5002 | |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 5003 | // next is BcParseNext, byte array of up to 4 BC_LEX's, packed into 32-bit word |
| 5004 | for (;;) { |
| 5005 | if (t == (next & 0x7f)) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5006 | goto ok; |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 5007 | if (next & 0x80) // last element? |
| 5008 | break; |
| 5009 | next >>= 8; |
| 5010 | } |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5011 | return bc_error_bad_expression(); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5012 | ok: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5013 | |
| 5014 | if (!(flags & BC_PARSE_REL) && nrelops) { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 5015 | s = bc_POSIX_does_not_allow("comparison operators outside if or loops"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5016 | if (s) return s; |
| 5017 | } |
| 5018 | else if ((flags & BC_PARSE_REL) && nrelops > 1) { |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 5019 | s = bc_POSIX_requires("exactly one comparison operator per condition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5020 | if (s) return s; |
| 5021 | } |
| 5022 | |
| 5023 | if (flags & BC_PARSE_PRINT) { |
| 5024 | if (paren_first || !assign) bc_parse_push(p, BC_INST_PRINT); |
| 5025 | bc_parse_push(p, BC_INST_POP); |
| 5026 | } |
| 5027 | |
| 5028 | return s; |
| 5029 | } |
| 5030 | |
Denys Vlasenko | 050b0fe | 2018-12-05 22:40:44 +0100 | [diff] [blame] | 5031 | static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next) |
| 5032 | { |
| 5033 | BcStatus s; |
| 5034 | |
| 5035 | s = bc_parse_expr_empty_ok(p, flags, next); |
| 5036 | if (s == BC_STATUS_PARSE_EMPTY_EXP) |
| 5037 | return bc_error("empty expression"); |
| 5038 | return s; |
| 5039 | } |
| 5040 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5041 | static void bc_parse_init(BcParse *p, size_t func) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5042 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5043 | bc_parse_create(p, func, bc_parse_parse, bc_lex_token); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5044 | } |
| 5045 | |
| 5046 | static BcStatus bc_parse_expression(BcParse *p, uint8_t flags) |
| 5047 | { |
| 5048 | return bc_parse_expr(p, flags, bc_parse_next_read); |
| 5049 | } |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 5050 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5051 | #endif // ENABLE_BC |
| 5052 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5053 | #if ENABLE_DC |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 5054 | |
| 5055 | #define DC_PARSE_BUF_LEN ((int) (sizeof(uint32_t) * CHAR_BIT)) |
| 5056 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5057 | static BcStatus dc_parse_register(BcParse *p) |
| 5058 | { |
| 5059 | BcStatus s; |
| 5060 | char *name; |
| 5061 | |
| 5062 | s = bc_lex_next(&p->l); |
| 5063 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5064 | 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] | 5065 | |
| 5066 | name = xstrdup(p->l.t.v.v); |
| 5067 | bc_parse_pushName(p, name); |
| 5068 | |
| 5069 | return s; |
| 5070 | } |
| 5071 | |
| 5072 | static BcStatus dc_parse_string(BcParse *p) |
| 5073 | { |
| 5074 | char *str, *name, b[DC_PARSE_BUF_LEN + 1]; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5075 | size_t idx, len = G.prog.strs.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5076 | |
| 5077 | sprintf(b, "%0*zu", DC_PARSE_BUF_LEN, len); |
| 5078 | name = xstrdup(b); |
| 5079 | |
| 5080 | str = xstrdup(p->l.t.v.v); |
| 5081 | bc_parse_push(p, BC_INST_STR); |
| 5082 | bc_parse_pushIndex(p, len); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5083 | bc_vec_push(&G.prog.strs, &str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5084 | bc_parse_addFunc(p, name, &idx); |
| 5085 | |
| 5086 | return bc_lex_next(&p->l); |
| 5087 | } |
| 5088 | |
| 5089 | static BcStatus dc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store) |
| 5090 | { |
| 5091 | BcStatus s; |
| 5092 | |
| 5093 | bc_parse_push(p, inst); |
| 5094 | if (name) { |
| 5095 | s = dc_parse_register(p); |
| 5096 | if (s) return s; |
| 5097 | } |
| 5098 | |
| 5099 | if (store) { |
| 5100 | bc_parse_push(p, BC_INST_SWAP); |
| 5101 | bc_parse_push(p, BC_INST_ASSIGN); |
| 5102 | bc_parse_push(p, BC_INST_POP); |
| 5103 | } |
| 5104 | |
| 5105 | return bc_lex_next(&p->l); |
| 5106 | } |
| 5107 | |
| 5108 | static BcStatus dc_parse_cond(BcParse *p, uint8_t inst) |
| 5109 | { |
| 5110 | BcStatus s; |
| 5111 | |
| 5112 | bc_parse_push(p, inst); |
| 5113 | bc_parse_push(p, BC_INST_EXEC_COND); |
| 5114 | |
| 5115 | s = dc_parse_register(p); |
| 5116 | if (s) return s; |
| 5117 | |
| 5118 | s = bc_lex_next(&p->l); |
| 5119 | if (s) return s; |
| 5120 | |
| 5121 | if (p->l.t.t == BC_LEX_ELSE) { |
| 5122 | s = dc_parse_register(p); |
| 5123 | if (s) return s; |
| 5124 | s = bc_lex_next(&p->l); |
| 5125 | } |
| 5126 | else |
| 5127 | bc_parse_push(p, BC_PARSE_STREND); |
| 5128 | |
| 5129 | return s; |
| 5130 | } |
| 5131 | |
| 5132 | static BcStatus dc_parse_token(BcParse *p, BcLexType t, uint8_t flags) |
| 5133 | { |
| 5134 | BcStatus s = BC_STATUS_SUCCESS; |
| 5135 | BcInst prev; |
| 5136 | uint8_t inst; |
| 5137 | bool assign, get_token = false; |
| 5138 | |
| 5139 | switch (t) { |
| 5140 | |
| 5141 | case BC_LEX_OP_REL_EQ: |
| 5142 | case BC_LEX_OP_REL_LE: |
| 5143 | case BC_LEX_OP_REL_GE: |
| 5144 | case BC_LEX_OP_REL_NE: |
| 5145 | case BC_LEX_OP_REL_LT: |
| 5146 | case BC_LEX_OP_REL_GT: |
| 5147 | { |
| 5148 | s = dc_parse_cond(p, t - BC_LEX_OP_REL_EQ + BC_INST_REL_EQ); |
| 5149 | break; |
| 5150 | } |
| 5151 | |
| 5152 | case BC_LEX_SCOLON: |
| 5153 | case BC_LEX_COLON: |
| 5154 | { |
| 5155 | s = dc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON); |
| 5156 | break; |
| 5157 | } |
| 5158 | |
| 5159 | case BC_LEX_STR: |
| 5160 | { |
| 5161 | s = dc_parse_string(p); |
| 5162 | break; |
| 5163 | } |
| 5164 | |
| 5165 | case BC_LEX_NEG: |
| 5166 | case BC_LEX_NUMBER: |
| 5167 | { |
| 5168 | if (t == BC_LEX_NEG) { |
| 5169 | s = bc_lex_next(&p->l); |
| 5170 | if (s) return s; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5171 | if (p->l.t.t != BC_LEX_NUMBER) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5172 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5173 | } |
| 5174 | |
| 5175 | bc_parse_number(p, &prev, &p->nbraces); |
| 5176 | |
| 5177 | if (t == BC_LEX_NEG) bc_parse_push(p, BC_INST_NEG); |
| 5178 | get_token = true; |
| 5179 | |
| 5180 | break; |
| 5181 | } |
| 5182 | |
| 5183 | case BC_LEX_KEY_READ: |
| 5184 | { |
| 5185 | if (flags & BC_PARSE_NOREAD) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5186 | s = bc_error_nested_read_call(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5187 | else |
| 5188 | bc_parse_push(p, BC_INST_READ); |
| 5189 | get_token = true; |
| 5190 | break; |
| 5191 | } |
| 5192 | |
| 5193 | case BC_LEX_OP_ASSIGN: |
| 5194 | case BC_LEX_STORE_PUSH: |
| 5195 | { |
| 5196 | assign = t == BC_LEX_OP_ASSIGN; |
| 5197 | inst = assign ? BC_INST_VAR : BC_INST_PUSH_TO_VAR; |
| 5198 | s = dc_parse_mem(p, inst, true, assign); |
| 5199 | break; |
| 5200 | } |
| 5201 | |
| 5202 | case BC_LEX_LOAD: |
| 5203 | case BC_LEX_LOAD_POP: |
| 5204 | { |
| 5205 | inst = t == BC_LEX_LOAD_POP ? BC_INST_PUSH_VAR : BC_INST_LOAD; |
| 5206 | s = dc_parse_mem(p, inst, true, false); |
| 5207 | break; |
| 5208 | } |
| 5209 | |
| 5210 | case BC_LEX_STORE_IBASE: |
| 5211 | case BC_LEX_STORE_SCALE: |
| 5212 | case BC_LEX_STORE_OBASE: |
| 5213 | { |
| 5214 | inst = t - BC_LEX_STORE_IBASE + BC_INST_IBASE; |
| 5215 | s = dc_parse_mem(p, inst, false, true); |
| 5216 | break; |
| 5217 | } |
| 5218 | |
| 5219 | default: |
| 5220 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5221 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5222 | get_token = true; |
| 5223 | break; |
| 5224 | } |
| 5225 | } |
| 5226 | |
| 5227 | if (!s && get_token) s = bc_lex_next(&p->l); |
| 5228 | |
| 5229 | return s; |
| 5230 | } |
| 5231 | |
| 5232 | static BcStatus dc_parse_expr(BcParse *p, uint8_t flags) |
| 5233 | { |
| 5234 | BcStatus s = BC_STATUS_SUCCESS; |
| 5235 | BcInst inst; |
| 5236 | BcLexType t; |
| 5237 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5238 | if (flags & BC_PARSE_NOCALL) p->nbraces = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5239 | |
| 5240 | for (t = p->l.t.t; !s && t != BC_LEX_EOF; t = p->l.t.t) { |
| 5241 | |
| 5242 | inst = dc_parse_insts[t]; |
| 5243 | |
| 5244 | if (inst != BC_INST_INVALID) { |
| 5245 | bc_parse_push(p, inst); |
| 5246 | s = bc_lex_next(&p->l); |
| 5247 | } |
| 5248 | else |
| 5249 | s = dc_parse_token(p, t, flags); |
| 5250 | } |
| 5251 | |
| 5252 | if (!s && p->l.t.t == BC_LEX_EOF && (flags & BC_PARSE_NOCALL)) |
| 5253 | bc_parse_push(p, BC_INST_POP_EXEC); |
| 5254 | |
| 5255 | return s; |
| 5256 | } |
| 5257 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 5258 | static FAST_FUNC BcStatus dc_parse_parse(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5259 | { |
| 5260 | BcStatus s; |
| 5261 | |
| 5262 | if (p->l.t.t == BC_LEX_EOF) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5263 | s = bc_error("end of file"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5264 | else |
| 5265 | s = dc_parse_expr(p, 0); |
| 5266 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 5267 | if (s || G_interrupt) { |
| 5268 | bc_parse_reset(p); |
| 5269 | s = BC_STATUS_FAILURE; |
| 5270 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5271 | |
| 5272 | return s; |
| 5273 | } |
| 5274 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5275 | static void dc_parse_init(BcParse *p, size_t func) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5276 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5277 | bc_parse_create(p, func, dc_parse_parse, dc_lex_token); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5278 | } |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 5279 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5280 | #endif // ENABLE_DC |
| 5281 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5282 | static void common_parse_init(BcParse *p, size_t func) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5283 | { |
| 5284 | if (IS_BC) { |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 5285 | IF_BC(bc_parse_init(p, func);) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5286 | } else { |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 5287 | IF_DC(dc_parse_init(p, func);) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5288 | } |
| 5289 | } |
| 5290 | |
| 5291 | static BcStatus common_parse_expr(BcParse *p, uint8_t flags) |
| 5292 | { |
| 5293 | if (IS_BC) { |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 5294 | IF_BC(return bc_parse_expression(p, flags);) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5295 | } else { |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 5296 | IF_DC(return dc_parse_expr(p, flags);) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5297 | } |
| 5298 | } |
| 5299 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5300 | static BcVec* bc_program_search(char *id, bool var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5301 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5302 | BcId e, *ptr; |
| 5303 | BcVec *v, *map; |
| 5304 | size_t i; |
| 5305 | BcResultData data; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 5306 | int new; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5307 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5308 | v = var ? &G.prog.vars : &G.prog.arrs; |
| 5309 | map = var ? &G.prog.var_map : &G.prog.arr_map; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5310 | |
| 5311 | e.name = id; |
| 5312 | e.idx = v->len; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 5313 | new = bc_map_insert(map, &e, &i); // 1 if insertion was successful |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5314 | |
| 5315 | if (new) { |
| 5316 | bc_array_init(&data.v, var); |
| 5317 | bc_vec_push(v, &data.v); |
| 5318 | } |
| 5319 | |
| 5320 | ptr = bc_vec_item(map, i); |
| 5321 | if (new) ptr->name = xstrdup(e.name); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5322 | return bc_vec_item(v, ptr->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5323 | } |
| 5324 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5325 | static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5326 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5327 | switch (r->t) { |
| 5328 | |
| 5329 | case BC_RESULT_STR: |
| 5330 | case BC_RESULT_TEMP: |
| 5331 | case BC_RESULT_IBASE: |
| 5332 | case BC_RESULT_SCALE: |
| 5333 | case BC_RESULT_OBASE: |
| 5334 | { |
| 5335 | *num = &r->d.n; |
| 5336 | break; |
| 5337 | } |
| 5338 | |
| 5339 | case BC_RESULT_CONSTANT: |
| 5340 | { |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5341 | BcStatus s; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5342 | char **str = bc_vec_item(&G.prog.consts, r->d.id.idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5343 | size_t base_t, len = strlen(*str); |
| 5344 | BcNum *base; |
| 5345 | |
| 5346 | bc_num_init(&r->d.n, len); |
| 5347 | |
| 5348 | hex = hex && len == 1; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5349 | base = hex ? &G.prog.hexb : &G.prog.ib; |
| 5350 | base_t = hex ? BC_NUM_MAX_IBASE : G.prog.ib_t; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5351 | s = zbc_num_parse(&r->d.n, *str, base, base_t); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5352 | |
| 5353 | if (s) { |
| 5354 | bc_num_free(&r->d.n); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5355 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5356 | } |
| 5357 | |
| 5358 | *num = &r->d.n; |
| 5359 | r->t = BC_RESULT_TEMP; |
| 5360 | |
| 5361 | break; |
| 5362 | } |
| 5363 | |
| 5364 | case BC_RESULT_VAR: |
| 5365 | case BC_RESULT_ARRAY: |
| 5366 | case BC_RESULT_ARRAY_ELEM: |
| 5367 | { |
| 5368 | BcVec *v; |
| 5369 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5370 | 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] | 5371 | |
| 5372 | if (r->t == BC_RESULT_ARRAY_ELEM) { |
| 5373 | v = bc_vec_top(v); |
| 5374 | if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1); |
| 5375 | *num = bc_vec_item(v, r->d.id.idx); |
| 5376 | } |
| 5377 | else |
| 5378 | *num = bc_vec_top(v); |
| 5379 | |
| 5380 | break; |
| 5381 | } |
| 5382 | |
| 5383 | case BC_RESULT_LAST: |
| 5384 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5385 | *num = &G.prog.last; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5386 | break; |
| 5387 | } |
| 5388 | |
| 5389 | case BC_RESULT_ONE: |
| 5390 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5391 | *num = &G.prog.one; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5392 | break; |
| 5393 | } |
| 5394 | } |
| 5395 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5396 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5397 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5398 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5399 | # define zbc_program_num(...) (zbc_program_num(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5400 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5401 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5402 | static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5403 | BcResult **r, BcNum **rn, bool assign) |
| 5404 | { |
| 5405 | BcStatus s; |
| 5406 | bool hex; |
| 5407 | BcResultType lt, rt; |
| 5408 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5409 | if (!BC_PROG_STACK(&G.prog.results, 2)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5410 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5411 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5412 | *r = bc_vec_item_rev(&G.prog.results, 0); |
| 5413 | *l = bc_vec_item_rev(&G.prog.results, 1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5414 | |
| 5415 | lt = (*l)->t; |
| 5416 | rt = (*r)->t; |
| 5417 | hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE); |
| 5418 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5419 | s = zbc_program_num(*l, ln, false); |
| 5420 | if (s) RETURN_STATUS(s); |
| 5421 | s = zbc_program_num(*r, rn, hex); |
| 5422 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5423 | |
| 5424 | // We run this again under these conditions in case any vector has been |
| 5425 | // reallocated out from under the BcNums or arrays we had. |
| 5426 | if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5427 | s = zbc_program_num(*l, ln, false); |
| 5428 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5429 | } |
| 5430 | |
| 5431 | if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5432 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5433 | if (!assign && !BC_PROG_NUM((*r), (*ln))) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5434 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5435 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5436 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5437 | } |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 5438 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5439 | # define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 5440 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5441 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5442 | static void bc_program_binOpRetire(BcResult *r) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5443 | { |
| 5444 | r->t = BC_RESULT_TEMP; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5445 | bc_vec_pop(&G.prog.results); |
| 5446 | bc_vec_pop(&G.prog.results); |
| 5447 | bc_vec_push(&G.prog.results, r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5448 | } |
| 5449 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5450 | static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5451 | { |
| 5452 | BcStatus s; |
| 5453 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5454 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5455 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5456 | *r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5457 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5458 | s = zbc_program_num(*r, n, false); |
| 5459 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5460 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5461 | if (!BC_PROG_NUM((*r), (*n))) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5462 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5463 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5464 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5465 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5466 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5467 | # define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5468 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5469 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5470 | static void bc_program_retire(BcResult *r, BcResultType t) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5471 | { |
| 5472 | r->t = t; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5473 | bc_vec_pop(&G.prog.results); |
| 5474 | bc_vec_push(&G.prog.results, r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5475 | } |
| 5476 | |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5477 | static BC_STATUS zbc_program_op(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5478 | { |
| 5479 | BcStatus s; |
| 5480 | BcResult *opd1, *opd2, res; |
| 5481 | BcNum *n1, *n2 = NULL; |
| 5482 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5483 | s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5484 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5485 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5486 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5487 | s = BC_STATUS_SUCCESS; |
| 5488 | #if !ERRORS_ARE_FATAL |
| 5489 | s = |
| 5490 | #endif |
| 5491 | zbc_program_ops[inst - BC_INST_POWER](n1, n2, &res.d.n, G.prog.scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5492 | if (s) goto err; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5493 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5494 | |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5495 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5496 | |
| 5497 | err: |
| 5498 | bc_num_free(&res.d.n); |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5499 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5500 | } |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5501 | #if ERRORS_ARE_FATAL |
| 5502 | # define zbc_program_op(...) (zbc_program_op(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 5503 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5504 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 5505 | static BcStatus bc_program_read(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5506 | { |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5507 | const char *sv_file; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5508 | BcStatus s; |
| 5509 | BcParse parse; |
| 5510 | BcVec buf; |
| 5511 | BcInstPtr ip; |
| 5512 | size_t i; |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 5513 | BcFunc *f = bc_program_func(BC_PROG_READ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5514 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5515 | for (i = 0; i < G.prog.stack.len; ++i) { |
| 5516 | BcInstPtr *ip_ptr = bc_vec_item(&G.prog.stack, i); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5517 | if (ip_ptr->func == BC_PROG_READ) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5518 | return bc_error_nested_read_call(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5519 | } |
| 5520 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 5521 | bc_vec_pop_all(&f->code); |
| 5522 | bc_char_vec_init(&buf); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5523 | |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5524 | sv_file = G.prog.file; |
| 5525 | G.prog.file = NULL; |
| 5526 | |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 5527 | s = bc_read_line(&buf); |
Denys Vlasenko | 4dd3652 | 2018-12-11 22:26:38 +0100 | [diff] [blame] | 5528 | //if (s) goto io_err; - wrong, nonzero return means EOF, not error |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5529 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5530 | common_parse_init(&parse, BC_PROG_READ); |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5531 | bc_lex_file(&parse.l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5532 | |
| 5533 | s = bc_parse_text(&parse, buf.v); |
| 5534 | if (s) goto exec_err; |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5535 | s = common_parse_expr(&parse, BC_PARSE_NOREAD); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5536 | if (s) goto exec_err; |
| 5537 | |
| 5538 | 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] | 5539 | s = bc_error("bad read() expression"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5540 | goto exec_err; |
| 5541 | } |
| 5542 | |
| 5543 | ip.func = BC_PROG_READ; |
| 5544 | ip.idx = 0; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5545 | ip.len = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5546 | |
| 5547 | // Update this pointer, just in case. |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 5548 | f = bc_program_func(BC_PROG_READ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5549 | |
| 5550 | bc_vec_pushByte(&f->code, BC_INST_POP_EXEC); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5551 | bc_vec_push(&G.prog.stack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5552 | |
| 5553 | exec_err: |
| 5554 | bc_parse_free(&parse); |
Denys Vlasenko | 4dd3652 | 2018-12-11 22:26:38 +0100 | [diff] [blame] | 5555 | //io_err: |
| 5556 | G.prog.file = sv_file; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5557 | bc_vec_free(&buf); |
| 5558 | return s; |
| 5559 | } |
| 5560 | |
| 5561 | static size_t bc_program_index(char *code, size_t *bgn) |
| 5562 | { |
| 5563 | char amt = code[(*bgn)++], i = 0; |
| 5564 | size_t res = 0; |
| 5565 | |
| 5566 | for (; i < amt; ++i, ++(*bgn)) |
| 5567 | res |= (((size_t)((int) code[*bgn]) & UCHAR_MAX) << (i * CHAR_BIT)); |
| 5568 | |
| 5569 | return res; |
| 5570 | } |
| 5571 | |
| 5572 | static char *bc_program_name(char *code, size_t *bgn) |
| 5573 | { |
| 5574 | size_t i; |
| 5575 | char c, *s, *str = code + *bgn, *ptr = strchr(str, BC_PARSE_STREND); |
| 5576 | |
| 5577 | s = xmalloc(ptr - str + 1); |
| 5578 | c = code[(*bgn)++]; |
| 5579 | |
| 5580 | for (i = 0; c != 0 && c != BC_PARSE_STREND; c = code[(*bgn)++], ++i) |
| 5581 | s[i] = c; |
| 5582 | |
| 5583 | s[i] = '\0'; |
| 5584 | |
| 5585 | return s; |
| 5586 | } |
| 5587 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 5588 | static void bc_program_printString(const char *str) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5589 | { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5590 | #if ENABLE_DC |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5591 | if (!str[0]) { |
Denys Vlasenko | 2c6f563 | 2018-12-11 21:21:14 +0100 | [diff] [blame] | 5592 | // Example: echo '[]ap' | dc |
| 5593 | // should print two bytes: 0x00, 0x0A |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5594 | bb_putchar('\0'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5595 | return; |
| 5596 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5597 | #endif |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5598 | while (*str) { |
| 5599 | int c = *str++; |
| 5600 | if (c != '\\' || !*str) |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5601 | bb_putchar(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5602 | else { |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5603 | c = *str++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5604 | switch (c) { |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5605 | case 'a': |
| 5606 | bb_putchar('\a'); |
| 5607 | break; |
| 5608 | case 'b': |
| 5609 | bb_putchar('\b'); |
| 5610 | break; |
| 5611 | case '\\': |
| 5612 | case 'e': |
| 5613 | bb_putchar('\\'); |
| 5614 | break; |
| 5615 | case 'f': |
| 5616 | bb_putchar('\f'); |
| 5617 | break; |
| 5618 | case 'n': |
| 5619 | bb_putchar('\n'); |
| 5620 | G.prog.nchars = SIZE_MAX; |
| 5621 | break; |
| 5622 | case 'r': |
| 5623 | bb_putchar('\r'); |
| 5624 | break; |
| 5625 | case 'q': |
| 5626 | bb_putchar('"'); |
| 5627 | break; |
| 5628 | case 't': |
| 5629 | bb_putchar('\t'); |
| 5630 | break; |
| 5631 | default: |
| 5632 | // Just print the backslash and following character. |
| 5633 | bb_putchar('\\'); |
| 5634 | ++G.prog.nchars; |
| 5635 | bb_putchar(c); |
| 5636 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5637 | } |
| 5638 | } |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5639 | ++G.prog.nchars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5640 | } |
| 5641 | } |
| 5642 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5643 | static void bc_num_printNewline(void) |
| 5644 | { |
| 5645 | if (G.prog.nchars == G.prog.len - 1) { |
| 5646 | bb_putchar('\\'); |
| 5647 | bb_putchar('\n'); |
| 5648 | G.prog.nchars = 0; |
| 5649 | } |
| 5650 | } |
| 5651 | |
| 5652 | #if ENABLE_DC |
| 5653 | static FAST_FUNC void bc_num_printChar(size_t num, size_t width, bool radix) |
| 5654 | { |
| 5655 | (void) radix; |
| 5656 | bb_putchar((char) num); |
| 5657 | G.prog.nchars += width; |
| 5658 | } |
| 5659 | #endif |
| 5660 | |
| 5661 | static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix) |
| 5662 | { |
| 5663 | size_t exp, pow; |
| 5664 | |
| 5665 | bc_num_printNewline(); |
| 5666 | bb_putchar(radix ? '.' : ' '); |
| 5667 | ++G.prog.nchars; |
| 5668 | |
| 5669 | bc_num_printNewline(); |
| 5670 | for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10) |
| 5671 | continue; |
| 5672 | |
| 5673 | for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) { |
| 5674 | size_t dig; |
| 5675 | bc_num_printNewline(); |
| 5676 | dig = num / pow; |
| 5677 | num -= dig * pow; |
| 5678 | bb_putchar(((char) dig) + '0'); |
| 5679 | } |
| 5680 | } |
| 5681 | |
| 5682 | static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix) |
| 5683 | { |
| 5684 | if (radix) { |
| 5685 | bc_num_printNewline(); |
| 5686 | bb_putchar('.'); |
| 5687 | G.prog.nchars += 1; |
| 5688 | } |
| 5689 | |
| 5690 | bc_num_printNewline(); |
| 5691 | bb_putchar(bb_hexdigits_upcase[num]); |
| 5692 | G.prog.nchars += width; |
| 5693 | } |
| 5694 | |
| 5695 | static void bc_num_printDecimal(BcNum *n) |
| 5696 | { |
| 5697 | size_t i, rdx = n->rdx - 1; |
| 5698 | |
| 5699 | if (n->neg) bb_putchar('-'); |
| 5700 | G.prog.nchars += n->neg; |
| 5701 | |
| 5702 | for (i = n->len - 1; i < n->len; --i) |
| 5703 | bc_num_printHex((size_t) n->num[i], 1, i == rdx); |
| 5704 | } |
| 5705 | |
| 5706 | static BC_STATUS zbc_num_printNum(BcNum *n, BcNum *base, size_t width, BcNumDigitOp print) |
| 5707 | { |
| 5708 | BcStatus s; |
| 5709 | BcVec stack; |
| 5710 | BcNum intp, fracp, digit, frac_len; |
| 5711 | unsigned long dig, *ptr; |
| 5712 | size_t i; |
| 5713 | bool radix; |
| 5714 | |
| 5715 | if (n->len == 0) { |
| 5716 | print(0, width, false); |
| 5717 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 5718 | } |
| 5719 | |
| 5720 | bc_vec_init(&stack, sizeof(long), NULL); |
| 5721 | bc_num_init(&intp, n->len); |
| 5722 | bc_num_init(&fracp, n->rdx); |
| 5723 | bc_num_init(&digit, width); |
| 5724 | bc_num_init(&frac_len, BC_NUM_INT(n)); |
| 5725 | bc_num_copy(&intp, n); |
| 5726 | bc_num_one(&frac_len); |
| 5727 | |
| 5728 | bc_num_truncate(&intp, intp.rdx); |
| 5729 | s = zbc_num_sub(n, &intp, &fracp, 0); |
| 5730 | if (s) goto err; |
| 5731 | |
| 5732 | while (intp.len != 0) { |
| 5733 | s = zbc_num_divmod(&intp, base, &intp, &digit, 0); |
| 5734 | if (s) goto err; |
| 5735 | s = zbc_num_ulong(&digit, &dig); |
| 5736 | if (s) goto err; |
| 5737 | bc_vec_push(&stack, &dig); |
| 5738 | } |
| 5739 | |
| 5740 | for (i = 0; i < stack.len; ++i) { |
| 5741 | ptr = bc_vec_item_rev(&stack, i); |
| 5742 | print(*ptr, width, false); |
| 5743 | } |
| 5744 | |
| 5745 | if (!n->rdx) goto err; |
| 5746 | |
| 5747 | for (radix = true; frac_len.len <= n->rdx; radix = false) { |
| 5748 | s = zbc_num_mul(&fracp, base, &fracp, n->rdx); |
| 5749 | if (s) goto err; |
| 5750 | s = zbc_num_ulong(&fracp, &dig); |
| 5751 | if (s) goto err; |
| 5752 | bc_num_ulong2num(&intp, dig); |
| 5753 | s = zbc_num_sub(&fracp, &intp, &fracp, 0); |
| 5754 | if (s) goto err; |
| 5755 | print(dig, width, radix); |
| 5756 | s = zbc_num_mul(&frac_len, base, &frac_len, 0); |
| 5757 | if (s) goto err; |
| 5758 | } |
| 5759 | |
| 5760 | err: |
| 5761 | bc_num_free(&frac_len); |
| 5762 | bc_num_free(&digit); |
| 5763 | bc_num_free(&fracp); |
| 5764 | bc_num_free(&intp); |
| 5765 | bc_vec_free(&stack); |
| 5766 | RETURN_STATUS(s); |
| 5767 | } |
| 5768 | #if ERRORS_ARE_FATAL |
| 5769 | # define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 5770 | #endif |
| 5771 | |
| 5772 | static BC_STATUS zbc_num_printBase(BcNum *n) |
| 5773 | { |
| 5774 | BcStatus s; |
| 5775 | size_t width, i; |
| 5776 | BcNumDigitOp print; |
| 5777 | bool neg = n->neg; |
| 5778 | |
| 5779 | if (neg) { |
| 5780 | bb_putchar('-'); |
| 5781 | G.prog.nchars++; |
| 5782 | } |
| 5783 | |
| 5784 | n->neg = false; |
| 5785 | |
| 5786 | if (G.prog.ob_t <= BC_NUM_MAX_IBASE) { |
| 5787 | width = 1; |
| 5788 | print = bc_num_printHex; |
| 5789 | } |
| 5790 | else { |
| 5791 | for (i = G.prog.ob_t - 1, width = 0; i != 0; i /= 10, ++width) |
| 5792 | continue; |
| 5793 | print = bc_num_printDigits; |
| 5794 | } |
| 5795 | |
| 5796 | s = zbc_num_printNum(n, &G.prog.ob, width, print); |
| 5797 | n->neg = neg; |
| 5798 | |
| 5799 | RETURN_STATUS(s); |
| 5800 | } |
| 5801 | #if ERRORS_ARE_FATAL |
| 5802 | # define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 5803 | #endif |
| 5804 | |
| 5805 | #if ENABLE_DC |
| 5806 | static BC_STATUS zbc_num_stream(BcNum *n, BcNum *base) |
| 5807 | { |
| 5808 | RETURN_STATUS(zbc_num_printNum(n, base, 1, bc_num_printChar)); |
| 5809 | } |
| 5810 | #if ERRORS_ARE_FATAL |
| 5811 | # define zbc_num_stream(...) (zbc_num_stream(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 5812 | #endif |
| 5813 | #endif |
| 5814 | |
| 5815 | static BC_STATUS zbc_num_print(BcNum *n, bool newline) |
| 5816 | { |
| 5817 | BcStatus s = BC_STATUS_SUCCESS; |
| 5818 | |
| 5819 | bc_num_printNewline(); |
| 5820 | |
| 5821 | if (n->len == 0) { |
| 5822 | bb_putchar('0'); |
| 5823 | ++G.prog.nchars; |
| 5824 | } |
| 5825 | else if (G.prog.ob_t == 10) |
| 5826 | bc_num_printDecimal(n); |
| 5827 | else |
| 5828 | s = zbc_num_printBase(n); |
| 5829 | |
| 5830 | if (newline) { |
| 5831 | bb_putchar('\n'); |
| 5832 | G.prog.nchars = 0; |
| 5833 | } |
| 5834 | |
| 5835 | RETURN_STATUS(s); |
| 5836 | } |
| 5837 | #if ERRORS_ARE_FATAL |
| 5838 | # define zbc_num_print(...) (zbc_num_print(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 5839 | #endif |
| 5840 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5841 | static BC_STATUS zbc_program_print(char inst, size_t idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5842 | { |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5843 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5844 | BcResult *r; |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5845 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5846 | bool pop = inst != BC_INST_PRINT; |
| 5847 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5848 | if (!BC_PROG_STACK(&G.prog.results, idx + 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5849 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5850 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5851 | r = bc_vec_item_rev(&G.prog.results, idx); |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5852 | num = NULL; // is this NULL necessary? |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5853 | s = zbc_program_num(r, &num, false); |
| 5854 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5855 | |
| 5856 | if (BC_PROG_NUM(r, num)) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5857 | s = zbc_num_print(num, !pop); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5858 | if (!s) bc_num_copy(&G.prog.last, num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5859 | } |
| 5860 | else { |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5861 | char *str; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5862 | |
| 5863 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx; |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 5864 | str = *bc_program_str(idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5865 | |
| 5866 | if (inst == BC_INST_PRINT_STR) { |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5867 | for (;;) { |
| 5868 | char c = *str++; |
| 5869 | if (c == '\0') break; |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5870 | bb_putchar(c); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5871 | ++G.prog.nchars; |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5872 | if (c == '\n') G.prog.nchars = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5873 | } |
| 5874 | } |
| 5875 | else { |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 5876 | bc_program_printString(str); |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5877 | if (inst == BC_INST_PRINT) bb_putchar('\n'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5878 | } |
| 5879 | } |
| 5880 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5881 | if (!s && pop) bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5882 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5883 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5884 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5885 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5886 | # define zbc_program_print(...) (zbc_program_print(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5887 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5888 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5889 | static BC_STATUS zbc_program_negate(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5890 | { |
| 5891 | BcStatus s; |
| 5892 | BcResult res, *ptr; |
| 5893 | BcNum *num = NULL; |
| 5894 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5895 | s = zbc_program_prep(&ptr, &num); |
| 5896 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5897 | |
| 5898 | bc_num_init(&res.d.n, num->len); |
| 5899 | bc_num_copy(&res.d.n, num); |
| 5900 | if (res.d.n.len) res.d.n.neg = !res.d.n.neg; |
| 5901 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5902 | bc_program_retire(&res, BC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5903 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5904 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5905 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5906 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5907 | # define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5908 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5909 | |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 5910 | static BC_STATUS zbc_program_logical(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5911 | { |
| 5912 | BcStatus s; |
| 5913 | BcResult *opd1, *opd2, res; |
| 5914 | BcNum *n1, *n2; |
| 5915 | bool cond = 0; |
| 5916 | ssize_t cmp; |
| 5917 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5918 | s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 5919 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5920 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5921 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5922 | |
| 5923 | if (inst == BC_INST_BOOL_AND) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5924 | 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] | 5925 | else if (inst == BC_INST_BOOL_OR) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5926 | 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] | 5927 | else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5928 | cmp = bc_num_cmp(n1, n2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5929 | switch (inst) { |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5930 | case BC_INST_REL_EQ: |
| 5931 | cond = cmp == 0; |
| 5932 | break; |
| 5933 | case BC_INST_REL_LE: |
| 5934 | cond = cmp <= 0; |
| 5935 | break; |
| 5936 | case BC_INST_REL_GE: |
| 5937 | cond = cmp >= 0; |
| 5938 | break; |
| 5939 | case BC_INST_REL_NE: |
| 5940 | cond = cmp != 0; |
| 5941 | break; |
| 5942 | case BC_INST_REL_LT: |
| 5943 | cond = cmp < 0; |
| 5944 | break; |
| 5945 | case BC_INST_REL_GT: |
| 5946 | cond = cmp > 0; |
| 5947 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5948 | } |
| 5949 | } |
| 5950 | |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5951 | if (cond) bc_num_one(&res.d.n); |
| 5952 | //else bc_num_zero(&res.d.n); - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5953 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5954 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5955 | |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 5956 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5957 | } |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 5958 | #if ERRORS_ARE_FATAL |
| 5959 | # define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 5960 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5961 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5962 | #if ENABLE_DC |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5963 | static BC_STATUS zbc_program_assignStr(BcResult *r, BcVec *v, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5964 | bool push) |
| 5965 | { |
| 5966 | BcNum n2; |
| 5967 | BcResult res; |
| 5968 | |
| 5969 | memset(&n2, 0, sizeof(BcNum)); |
| 5970 | n2.rdx = res.d.id.idx = r->d.id.idx; |
| 5971 | res.t = BC_RESULT_STR; |
| 5972 | |
| 5973 | if (!push) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5974 | if (!BC_PROG_STACK(&G.prog.results, 2)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5975 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5976 | bc_vec_pop(v); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5977 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5978 | } |
| 5979 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5980 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5981 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5982 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5983 | bc_vec_push(v, &n2); |
| 5984 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5985 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5986 | } |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 5987 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5988 | # define zbc_program_assignStr(...) (zbc_program_assignStr(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 5989 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5990 | #endif // ENABLE_DC |
| 5991 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5992 | static BC_STATUS zbc_program_copyToVar(char *name, bool var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5993 | { |
| 5994 | BcStatus s; |
| 5995 | BcResult *ptr, r; |
| 5996 | BcVec *v; |
| 5997 | BcNum *n; |
| 5998 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5999 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6000 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6001 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6002 | ptr = bc_vec_top(&G.prog.results); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6003 | if ((ptr->t == BC_RESULT_ARRAY) != !var) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6004 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6005 | v = bc_program_search(name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6006 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6007 | #if ENABLE_DC |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6008 | if (ptr->t == BC_RESULT_STR && !var) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6009 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
| 6010 | if (ptr->t == BC_RESULT_STR) |
| 6011 | RETURN_STATUS(zbc_program_assignStr(ptr, v, true)); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6012 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6013 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6014 | s = zbc_program_num(ptr, &n, false); |
| 6015 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6016 | |
| 6017 | // Do this once more to make sure that pointers were not invalidated. |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6018 | v = bc_program_search(name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6019 | |
| 6020 | if (var) { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6021 | bc_num_init_DEF_SIZE(&r.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6022 | bc_num_copy(&r.d.n, n); |
| 6023 | } |
| 6024 | else { |
| 6025 | bc_array_init(&r.d.v, true); |
| 6026 | bc_array_copy(&r.d.v, (BcVec *) n); |
| 6027 | } |
| 6028 | |
| 6029 | bc_vec_push(v, &r.d); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6030 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6031 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6032 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6033 | } |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6034 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6035 | # define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6036 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6037 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6038 | static BC_STATUS zbc_program_assign(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6039 | { |
| 6040 | BcStatus s; |
| 6041 | BcResult *left, *right, res; |
| 6042 | BcNum *l = NULL, *r = NULL; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6043 | bool assign = inst == BC_INST_ASSIGN, ib, sc; |
| 6044 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6045 | s = zbc_program_binOpPrep(&left, &l, &right, &r, assign); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6046 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6047 | |
| 6048 | ib = left->t == BC_RESULT_IBASE; |
| 6049 | sc = left->t == BC_RESULT_SCALE; |
| 6050 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6051 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6052 | |
| 6053 | if (right->t == BC_RESULT_STR) { |
| 6054 | |
| 6055 | BcVec *v; |
| 6056 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6057 | if (left->t != BC_RESULT_VAR) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6058 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6059 | v = bc_program_search(left->d.id.name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6060 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6061 | RETURN_STATUS(zbc_program_assignStr(right, v, false)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6062 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6063 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6064 | |
| 6065 | if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP) |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 6066 | RETURN_STATUS(bc_error("bad assignment:" |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6067 | " left side must be scale," |
| 6068 | " ibase, obase, last, var," |
| 6069 | " or array element" |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6070 | )); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6071 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6072 | #if ENABLE_BC |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6073 | if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6074 | RETURN_STATUS(bc_error("divide by zero")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6075 | |
| 6076 | if (assign) |
| 6077 | bc_num_copy(l, r); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6078 | else { |
| 6079 | s = BC_STATUS_SUCCESS; |
| 6080 | #if !ERRORS_ARE_FATAL |
| 6081 | s = |
| 6082 | #endif |
| 6083 | zbc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale); |
| 6084 | } |
| 6085 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6086 | #else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6087 | bc_num_copy(l, r); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6088 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6089 | |
| 6090 | if (ib || sc || left->t == BC_RESULT_OBASE) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6091 | static const char *const msg[] = { |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 6092 | "bad ibase; must be [2,16]", //BC_RESULT_IBASE |
| 6093 | "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE |
| 6094 | NULL, //can't happen //BC_RESULT_LAST |
| 6095 | NULL, //can't happen //BC_RESULT_CONSTANT |
| 6096 | NULL, //can't happen //BC_RESULT_ONE |
| 6097 | "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6098 | }; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6099 | size_t *ptr; |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 6100 | unsigned long val, max; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6101 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6102 | s = zbc_num_ulong(l, &val); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6103 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6104 | s = left->t - BC_RESULT_IBASE; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6105 | if (sc) { |
| 6106 | max = BC_MAX_SCALE; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6107 | ptr = &G.prog.scale; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6108 | } |
| 6109 | else { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6110 | if (val < BC_NUM_MIN_BASE) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6111 | RETURN_STATUS(bc_error(msg[s])); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6112 | max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6113 | ptr = ib ? &G.prog.ib_t : &G.prog.ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6114 | } |
| 6115 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6116 | if (val > max) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6117 | RETURN_STATUS(bc_error(msg[s])); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6118 | if (!sc) |
| 6119 | bc_num_copy(ib ? &G.prog.ib : &G.prog.ob, l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6120 | |
| 6121 | *ptr = (size_t) val; |
| 6122 | s = BC_STATUS_SUCCESS; |
| 6123 | } |
| 6124 | |
| 6125 | bc_num_init(&res.d.n, l->len); |
| 6126 | bc_num_copy(&res.d.n, l); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6127 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6128 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6129 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6130 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6131 | #if ERRORS_ARE_FATAL |
| 6132 | # define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 6133 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6134 | |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6135 | #if !ENABLE_DC |
| 6136 | #define bc_program_pushVar(code, bgn, pop, copy) \ |
| 6137 | bc_program_pushVar(code, bgn) |
| 6138 | // for bc, 'pop' and 'copy' are always false |
| 6139 | #endif |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 6140 | static BC_STATUS bc_program_pushVar(char *code, size_t *bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6141 | bool pop, bool copy) |
| 6142 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6143 | BcResult r; |
| 6144 | char *name = bc_program_name(code, bgn); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6145 | |
| 6146 | r.t = BC_RESULT_VAR; |
| 6147 | r.d.id.name = name; |
| 6148 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6149 | #if ENABLE_DC |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6150 | { |
| 6151 | BcVec *v = bc_program_search(name, true); |
| 6152 | BcNum *num = bc_vec_top(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6153 | |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6154 | if (pop || copy) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6155 | |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6156 | if (!BC_PROG_STACK(v, 2 - copy)) { |
| 6157 | free(name); |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 6158 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6159 | } |
| 6160 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6161 | free(name); |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6162 | name = NULL; |
| 6163 | |
| 6164 | if (!BC_PROG_STR(num)) { |
| 6165 | |
| 6166 | r.t = BC_RESULT_TEMP; |
| 6167 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6168 | bc_num_init_DEF_SIZE(&r.d.n); |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6169 | bc_num_copy(&r.d.n, num); |
| 6170 | } |
| 6171 | else { |
| 6172 | r.t = BC_RESULT_STR; |
| 6173 | r.d.id.idx = num->rdx; |
| 6174 | } |
| 6175 | |
| 6176 | if (!copy) bc_vec_pop(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6177 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6178 | } |
| 6179 | #endif // ENABLE_DC |
| 6180 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6181 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6182 | |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 6183 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6184 | } |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 6185 | #if ERRORS_ARE_FATAL |
| 6186 | # define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 6187 | #else |
| 6188 | # define zbc_program_pushVar(...) bc_program_pushVar(__VA_ARGS__) |
| 6189 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6190 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6191 | static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6192 | char inst) |
| 6193 | { |
| 6194 | BcStatus s = BC_STATUS_SUCCESS; |
| 6195 | BcResult r; |
| 6196 | BcNum *num; |
| 6197 | |
| 6198 | r.d.id.name = bc_program_name(code, bgn); |
| 6199 | |
| 6200 | if (inst == BC_INST_ARRAY) { |
| 6201 | r.t = BC_RESULT_ARRAY; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6202 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6203 | } |
| 6204 | else { |
| 6205 | |
| 6206 | BcResult *operand; |
| 6207 | unsigned long temp; |
| 6208 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6209 | s = zbc_program_prep(&operand, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6210 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6211 | s = zbc_num_ulong(num, &temp); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6212 | if (s) goto err; |
| 6213 | |
| 6214 | if (temp > BC_MAX_DIM) { |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 6215 | 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] | 6216 | goto err; |
| 6217 | } |
| 6218 | |
| 6219 | r.d.id.idx = (size_t) temp; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6220 | bc_program_retire(&r, BC_RESULT_ARRAY_ELEM); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6221 | } |
| 6222 | |
| 6223 | err: |
| 6224 | if (s) free(r.d.id.name); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6225 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6226 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6227 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6228 | # define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6229 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6230 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6231 | #if ENABLE_BC |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6232 | static BC_STATUS zbc_program_incdec(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6233 | { |
| 6234 | BcStatus s; |
| 6235 | BcResult *ptr, res, copy; |
| 6236 | BcNum *num = NULL; |
| 6237 | char inst2 = inst; |
| 6238 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6239 | s = zbc_program_prep(&ptr, &num); |
| 6240 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6241 | |
| 6242 | if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) { |
| 6243 | copy.t = BC_RESULT_TEMP; |
| 6244 | bc_num_init(©.d.n, num->len); |
| 6245 | bc_num_copy(©.d.n, num); |
| 6246 | } |
| 6247 | |
| 6248 | res.t = BC_RESULT_ONE; |
| 6249 | inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ? |
| 6250 | BC_INST_ASSIGN_PLUS : |
| 6251 | BC_INST_ASSIGN_MINUS; |
| 6252 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6253 | bc_vec_push(&G.prog.results, &res); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6254 | s = zbc_program_assign(inst); |
| 6255 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6256 | |
| 6257 | if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6258 | bc_vec_pop(&G.prog.results); |
| 6259 | bc_vec_push(&G.prog.results, ©); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6260 | } |
| 6261 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6262 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6263 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6264 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6265 | # define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6266 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6267 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6268 | static BC_STATUS zbc_program_call(char *code, size_t *idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6269 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6270 | BcInstPtr ip; |
| 6271 | size_t i, nparams = bc_program_index(code, idx); |
| 6272 | BcFunc *func; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6273 | BcId *a; |
| 6274 | BcResultData param; |
| 6275 | BcResult *arg; |
| 6276 | |
| 6277 | ip.idx = 0; |
| 6278 | ip.func = bc_program_index(code, idx); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6279 | func = bc_program_func(ip.func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6280 | |
Denys Vlasenko | 04a1c76 | 2018-12-03 21:10:57 +0100 | [diff] [blame] | 6281 | if (func->code.len == 0) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6282 | RETURN_STATUS(bc_error("undefined function")); |
Denys Vlasenko | 04a1c76 | 2018-12-03 21:10:57 +0100 | [diff] [blame] | 6283 | } |
| 6284 | if (nparams != func->nparams) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6285 | 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] | 6286 | } |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6287 | ip.len = G.prog.results.len - nparams; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6288 | |
| 6289 | for (i = 0; i < nparams; ++i) { |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6290 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6291 | |
| 6292 | a = bc_vec_item(&func->autos, nparams - 1 - i); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6293 | arg = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6294 | |
| 6295 | 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] | 6296 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6297 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6298 | s = zbc_program_copyToVar(a->name, a->idx); |
| 6299 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6300 | } |
| 6301 | |
| 6302 | for (; i < func->autos.len; ++i) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6303 | BcVec *v; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6304 | |
| 6305 | a = bc_vec_item(&func->autos, i); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6306 | v = bc_program_search(a->name, a->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6307 | |
| 6308 | if (a->idx) { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6309 | bc_num_init_DEF_SIZE(¶m.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6310 | bc_vec_push(v, ¶m.n); |
| 6311 | } |
| 6312 | else { |
| 6313 | bc_array_init(¶m.v, true); |
| 6314 | bc_vec_push(v, ¶m.v); |
| 6315 | } |
| 6316 | } |
| 6317 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6318 | bc_vec_push(&G.prog.stack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6319 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6320 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6321 | } |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6322 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6323 | # define zbc_program_call(...) (zbc_program_call(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6324 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6325 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6326 | static BC_STATUS zbc_program_return(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6327 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6328 | BcResult res; |
| 6329 | BcFunc *f; |
| 6330 | size_t i; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6331 | BcInstPtr *ip = bc_vec_top(&G.prog.stack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6332 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6333 | 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] | 6334 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6335 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6336 | f = bc_program_func(ip->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6337 | res.t = BC_RESULT_TEMP; |
| 6338 | |
| 6339 | if (inst == BC_INST_RET) { |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6340 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6341 | BcNum *num; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6342 | BcResult *operand = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6343 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6344 | s = zbc_program_num(operand, &num, false); |
| 6345 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6346 | bc_num_init(&res.d.n, num->len); |
| 6347 | bc_num_copy(&res.d.n, num); |
| 6348 | } |
| 6349 | else { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6350 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 6351 | //bc_num_zero(&res.d.n); - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6352 | } |
| 6353 | |
| 6354 | // We need to pop arguments as well, so this takes that into account. |
| 6355 | for (i = 0; i < f->autos.len; ++i) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6356 | BcVec *v; |
| 6357 | BcId *a = bc_vec_item(&f->autos, i); |
| 6358 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6359 | v = bc_program_search(a->name, a->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6360 | bc_vec_pop(v); |
| 6361 | } |
| 6362 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6363 | bc_vec_npop(&G.prog.results, G.prog.results.len - ip->len); |
| 6364 | bc_vec_push(&G.prog.results, &res); |
| 6365 | bc_vec_pop(&G.prog.stack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6366 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6367 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6368 | } |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6369 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6370 | # define zbc_program_return(...) (zbc_program_return(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6371 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6372 | #endif // ENABLE_BC |
| 6373 | |
| 6374 | static unsigned long bc_program_scale(BcNum *n) |
| 6375 | { |
| 6376 | return (unsigned long) n->rdx; |
| 6377 | } |
| 6378 | |
| 6379 | static unsigned long bc_program_len(BcNum *n) |
| 6380 | { |
Denys Vlasenko | a7f1a36 | 2018-12-10 12:57:01 +0100 | [diff] [blame] | 6381 | size_t len = n->len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6382 | |
Denys Vlasenko | a7f1a36 | 2018-12-10 12:57:01 +0100 | [diff] [blame] | 6383 | if (n->rdx != len) return len; |
| 6384 | for (;;) { |
| 6385 | if (len == 0) break; |
| 6386 | len--; |
| 6387 | if (n->num[len] != 0) break; |
| 6388 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6389 | return len; |
| 6390 | } |
| 6391 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6392 | static BC_STATUS zbc_program_builtin(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6393 | { |
| 6394 | BcStatus s; |
| 6395 | BcResult *opnd; |
| 6396 | BcNum *num = NULL; |
| 6397 | BcResult res; |
| 6398 | bool len = inst == BC_INST_LENGTH; |
| 6399 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6400 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6401 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6402 | opnd = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6403 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6404 | s = zbc_program_num(opnd, &num, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6405 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6406 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6407 | #if ENABLE_DC |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6408 | if (!BC_PROG_NUM(opnd, num) && !len) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6409 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6410 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6411 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6412 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6413 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 6414 | if (inst == BC_INST_SQRT) s = zbc_num_sqrt(num, &res.d.n, G.prog.scale); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6415 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6416 | else if (len != 0 && opnd->t == BC_RESULT_ARRAY) { |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6417 | bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6418 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6419 | #endif |
| 6420 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6421 | else if (len != 0 && !BC_PROG_NUM(opnd, num)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6422 | char **str; |
| 6423 | size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx; |
| 6424 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6425 | str = bc_program_str(idx); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6426 | bc_num_ulong2num(&res.d.n, strlen(*str)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6427 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6428 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6429 | else { |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 6430 | 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] | 6431 | } |
| 6432 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6433 | bc_program_retire(&res, BC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6434 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6435 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6436 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6437 | #if ERRORS_ARE_FATAL |
| 6438 | # define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 6439 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6440 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6441 | #if ENABLE_DC |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6442 | static BC_STATUS zbc_program_divmod(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6443 | { |
| 6444 | BcStatus s; |
| 6445 | BcResult *opd1, *opd2, res, res2; |
| 6446 | BcNum *n1, *n2 = NULL; |
| 6447 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6448 | s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6449 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6450 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6451 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6452 | bc_num_init(&res2.d.n, n2->len); |
| 6453 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 6454 | s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6455 | if (s) goto err; |
| 6456 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6457 | bc_program_binOpRetire(&res2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6458 | res.t = BC_RESULT_TEMP; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6459 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6460 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6461 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6462 | |
| 6463 | err: |
| 6464 | bc_num_free(&res2.d.n); |
| 6465 | bc_num_free(&res.d.n); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6466 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6467 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6468 | #if ERRORS_ARE_FATAL |
| 6469 | # define zbc_program_divmod(...) (zbc_program_divmod(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 6470 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6471 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6472 | static BC_STATUS zbc_program_modexp(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6473 | { |
| 6474 | BcStatus s; |
| 6475 | BcResult *r1, *r2, *r3, res; |
| 6476 | BcNum *n1, *n2, *n3; |
| 6477 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6478 | if (!BC_PROG_STACK(&G.prog.results, 3)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6479 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6480 | s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6481 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6482 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6483 | r1 = bc_vec_item_rev(&G.prog.results, 2); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6484 | s = zbc_program_num(r1, &n1, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6485 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6486 | if (!BC_PROG_NUM(r1, n1)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6487 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6488 | |
| 6489 | // Make sure that the values have their pointers updated, if necessary. |
| 6490 | if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) { |
| 6491 | |
| 6492 | if (r1->t == r2->t) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6493 | s = zbc_program_num(r2, &n2, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6494 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6495 | } |
| 6496 | |
| 6497 | if (r1->t == r3->t) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6498 | s = zbc_program_num(r3, &n3, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6499 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6500 | } |
| 6501 | } |
| 6502 | |
| 6503 | bc_num_init(&res.d.n, n3->len); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 6504 | s = zbc_num_modexp(n1, n2, n3, &res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6505 | if (s) goto err; |
| 6506 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6507 | bc_vec_pop(&G.prog.results); |
| 6508 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6509 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6510 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6511 | |
| 6512 | err: |
| 6513 | bc_num_free(&res.d.n); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6514 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6515 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6516 | #if ERRORS_ARE_FATAL |
| 6517 | # define zbc_program_modexp(...) (zbc_program_modexp(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 6518 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6519 | |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6520 | static void bc_program_stackLen(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6521 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6522 | BcResult res; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6523 | size_t len = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6524 | |
| 6525 | res.t = BC_RESULT_TEMP; |
| 6526 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6527 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6528 | bc_num_ulong2num(&res.d.n, len); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6529 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6530 | } |
| 6531 | |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6532 | static BC_STATUS zbc_program_asciify(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6533 | { |
| 6534 | BcStatus s; |
| 6535 | BcResult *r, res; |
Denys Vlasenko | 4a024c7 | 2018-12-09 13:21:54 +0100 | [diff] [blame] | 6536 | BcNum *num, n; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6537 | char *str, *str2, c; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6538 | size_t len = G.prog.strs.len, idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6539 | unsigned long val; |
| 6540 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6541 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6542 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6543 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6544 | |
Denys Vlasenko | 4a024c7 | 2018-12-09 13:21:54 +0100 | [diff] [blame] | 6545 | num = NULL; // TODO: is this NULL needed? |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6546 | s = zbc_program_num(r, &num, false); |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6547 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6548 | |
| 6549 | if (BC_PROG_NUM(r, num)) { |
| 6550 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6551 | bc_num_init_DEF_SIZE(&n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6552 | bc_num_copy(&n, num); |
| 6553 | bc_num_truncate(&n, n.rdx); |
| 6554 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 6555 | s = zbc_num_mod(&n, &G.prog.strmb, &n, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6556 | if (s) goto num_err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6557 | s = zbc_num_ulong(&n, &val); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6558 | if (s) goto num_err; |
| 6559 | |
| 6560 | c = (char) val; |
| 6561 | |
| 6562 | bc_num_free(&n); |
| 6563 | } |
| 6564 | else { |
| 6565 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx; |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6566 | str2 = *bc_program_str(idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6567 | c = str2[0]; |
| 6568 | } |
| 6569 | |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6570 | str = xzalloc(2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6571 | str[0] = c; |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6572 | //str[1] = '\0'; - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6573 | |
| 6574 | str2 = xstrdup(str); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6575 | bc_program_addFunc(str2, &idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6576 | |
| 6577 | if (idx != len + BC_PROG_REQ_FUNCS) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6578 | for (idx = 0; idx < G.prog.strs.len; ++idx) { |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6579 | if (strcmp(*bc_program_str(idx), str) == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6580 | len = idx; |
| 6581 | break; |
| 6582 | } |
| 6583 | } |
| 6584 | |
| 6585 | free(str); |
| 6586 | } |
| 6587 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6588 | bc_vec_push(&G.prog.strs, &str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6589 | |
| 6590 | res.t = BC_RESULT_STR; |
| 6591 | res.d.id.idx = len; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6592 | bc_vec_pop(&G.prog.results); |
| 6593 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6594 | |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6595 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6596 | |
| 6597 | num_err: |
| 6598 | bc_num_free(&n); |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6599 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6600 | } |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6601 | #if ERRORS_ARE_FATAL |
| 6602 | # define zbc_program_asciify(...) (zbc_program_asciify(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 6603 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6604 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6605 | static BC_STATUS zbc_program_printStream(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6606 | { |
| 6607 | BcStatus s; |
| 6608 | BcResult *r; |
| 6609 | BcNum *n = NULL; |
| 6610 | size_t idx; |
| 6611 | char *str; |
| 6612 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6613 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6614 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6615 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6616 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6617 | s = zbc_program_num(r, &n, false); |
| 6618 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6619 | |
| 6620 | if (BC_PROG_NUM(r, n)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6621 | s = zbc_num_stream(n, &G.prog.strmb); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6622 | else { |
| 6623 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx; |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6624 | str = *bc_program_str(idx); |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 6625 | printf("%s", str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6626 | } |
| 6627 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6628 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6629 | } |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6630 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6631 | # define zbc_program_printStream(...) (zbc_program_printStream(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6632 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6633 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6634 | static BC_STATUS zbc_program_nquit(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6635 | { |
| 6636 | BcStatus s; |
| 6637 | BcResult *opnd; |
| 6638 | BcNum *num = NULL; |
| 6639 | unsigned long val; |
| 6640 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6641 | s = zbc_program_prep(&opnd, &num); |
| 6642 | if (s) RETURN_STATUS(s); |
| 6643 | s = zbc_num_ulong(num, &val); |
| 6644 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6645 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6646 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6647 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6648 | if (G.prog.stack.len < val) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6649 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 6650 | if (G.prog.stack.len == val) { |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 6651 | QUIT_OR_RETURN_TO_MAIN; |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 6652 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6653 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6654 | bc_vec_npop(&G.prog.stack, val); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6655 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6656 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6657 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6658 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6659 | # define zbc_program_nquit(...) (zbc_program_nquit(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6660 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6661 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6662 | static BcStatus bc_program_execStr(char *code, size_t *bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6663 | bool cond) |
| 6664 | { |
| 6665 | BcStatus s = BC_STATUS_SUCCESS; |
| 6666 | BcResult *r; |
| 6667 | char **str; |
| 6668 | BcFunc *f; |
| 6669 | BcParse prs; |
| 6670 | BcInstPtr ip; |
| 6671 | size_t fidx, sidx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6672 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6673 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6674 | return bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6675 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6676 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6677 | |
| 6678 | if (cond) { |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6679 | BcNum *n = n; // for compiler |
| 6680 | bool exec; |
| 6681 | char *name; |
| 6682 | char *then_name = bc_program_name(code, bgn); |
| 6683 | char *else_name = NULL; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6684 | |
| 6685 | if (code[*bgn] == BC_PARSE_STREND) |
| 6686 | (*bgn) += 1; |
| 6687 | else |
| 6688 | else_name = bc_program_name(code, bgn); |
| 6689 | |
| 6690 | exec = r->d.n.len != 0; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6691 | name = then_name; |
| 6692 | if (!exec && else_name != NULL) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6693 | exec = true; |
| 6694 | name = else_name; |
| 6695 | } |
| 6696 | |
| 6697 | if (exec) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6698 | BcVec *v; |
| 6699 | v = bc_program_search(name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6700 | n = bc_vec_top(v); |
| 6701 | } |
| 6702 | |
| 6703 | free(then_name); |
| 6704 | free(else_name); |
| 6705 | |
| 6706 | if (!exec) goto exit; |
| 6707 | if (!BC_PROG_STR(n)) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6708 | s = bc_error_variable_is_wrong_type(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6709 | goto exit; |
| 6710 | } |
| 6711 | |
| 6712 | sidx = n->rdx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6713 | } else { |
| 6714 | if (r->t == BC_RESULT_STR) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6715 | sidx = r->d.id.idx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6716 | } else if (r->t == BC_RESULT_VAR) { |
| 6717 | BcNum *n; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6718 | s = zbc_program_num(r, &n, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6719 | if (s || !BC_PROG_STR(n)) goto exit; |
| 6720 | sidx = n->rdx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6721 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6722 | goto exit; |
| 6723 | } |
| 6724 | |
| 6725 | fidx = sidx + BC_PROG_REQ_FUNCS; |
| 6726 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6727 | str = bc_program_str(sidx); |
| 6728 | f = bc_program_func(fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6729 | |
| 6730 | if (f->code.len == 0) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6731 | common_parse_init(&prs, fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6732 | s = bc_parse_text(&prs, *str); |
| 6733 | if (s) goto err; |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 6734 | s = common_parse_expr(&prs, BC_PARSE_NOCALL); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6735 | if (s) goto err; |
| 6736 | |
| 6737 | if (prs.l.t.t != BC_LEX_EOF) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6738 | s = bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6739 | goto err; |
| 6740 | } |
| 6741 | |
| 6742 | bc_parse_free(&prs); |
| 6743 | } |
| 6744 | |
| 6745 | ip.idx = 0; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6746 | ip.len = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6747 | ip.func = fidx; |
| 6748 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6749 | bc_vec_pop(&G.prog.results); |
| 6750 | bc_vec_push(&G.prog.stack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6751 | |
| 6752 | return BC_STATUS_SUCCESS; |
| 6753 | |
| 6754 | err: |
| 6755 | bc_parse_free(&prs); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6756 | f = bc_program_func(fidx); |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 6757 | bc_vec_pop_all(&f->code); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6758 | exit: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6759 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6760 | return s; |
| 6761 | } |
| 6762 | #endif // ENABLE_DC |
| 6763 | |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6764 | static void bc_program_pushGlobal(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6765 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6766 | BcResult res; |
| 6767 | unsigned long val; |
| 6768 | |
| 6769 | res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE; |
| 6770 | if (inst == BC_INST_IBASE) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6771 | val = (unsigned long) G.prog.ib_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6772 | else if (inst == BC_INST_SCALE) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6773 | val = (unsigned long) G.prog.scale; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6774 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6775 | val = (unsigned long) G.prog.ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6776 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6777 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6778 | bc_num_ulong2num(&res.d.n, val); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6779 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6780 | } |
| 6781 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6782 | static void bc_program_addFunc(char *name, size_t *idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6783 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6784 | BcId entry, *entry_ptr; |
| 6785 | BcFunc f; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 6786 | int inserted; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6787 | |
| 6788 | entry.name = name; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6789 | entry.idx = G.prog.fns.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6790 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 6791 | inserted = bc_map_insert(&G.prog.fn_map, &entry, idx); |
| 6792 | if (!inserted) free(name); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6793 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6794 | entry_ptr = bc_vec_item(&G.prog.fn_map, *idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6795 | *idx = entry_ptr->idx; |
| 6796 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 6797 | if (!inserted) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6798 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6799 | BcFunc *func = bc_program_func(entry_ptr->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6800 | |
| 6801 | // We need to reset these, so the function can be repopulated. |
| 6802 | func->nparams = 0; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 6803 | bc_vec_pop_all(&func->autos); |
| 6804 | bc_vec_pop_all(&func->code); |
| 6805 | bc_vec_pop_all(&func->labels); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6806 | } |
| 6807 | else { |
| 6808 | bc_func_init(&f); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6809 | bc_vec_push(&G.prog.fns, &f); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6810 | } |
| 6811 | } |
| 6812 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6813 | static BcStatus bc_program_exec(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6814 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6815 | BcResult r, *ptr; |
| 6816 | BcNum *num; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6817 | BcInstPtr *ip = bc_vec_top(&G.prog.stack); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6818 | BcFunc *func = bc_program_func(ip->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6819 | char *code = func->code.v; |
| 6820 | bool cond = false; |
| 6821 | |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6822 | while (ip->idx < func->code.len) { |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6823 | BcStatus s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6824 | char inst = code[(ip->idx)++]; |
| 6825 | |
| 6826 | switch (inst) { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6827 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6828 | case BC_INST_JUMP_ZERO: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6829 | s = zbc_program_prep(&ptr, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6830 | if (s) return s; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6831 | cond = !bc_num_cmp(num, &G.prog.zero); |
| 6832 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6833 | // Fallthrough. |
| 6834 | case BC_INST_JUMP: { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6835 | size_t *addr; |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6836 | size_t idx = bc_program_index(code, &ip->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6837 | addr = bc_vec_item(&func->labels, idx); |
| 6838 | if (inst == BC_INST_JUMP || cond) ip->idx = *addr; |
| 6839 | break; |
| 6840 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6841 | case BC_INST_CALL: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6842 | s = zbc_program_call(code, &ip->idx); |
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_INC_PRE: |
| 6845 | case BC_INST_DEC_PRE: |
| 6846 | case BC_INST_INC_POST: |
| 6847 | case BC_INST_DEC_POST: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6848 | s = zbc_program_incdec(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6849 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6850 | case BC_INST_HALT: |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 6851 | QUIT_OR_RETURN_TO_MAIN; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6852 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6853 | case BC_INST_RET: |
| 6854 | case BC_INST_RET0: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6855 | s = zbc_program_return(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6856 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6857 | case BC_INST_BOOL_OR: |
| 6858 | case BC_INST_BOOL_AND: |
| 6859 | #endif // ENABLE_BC |
| 6860 | case BC_INST_REL_EQ: |
| 6861 | case BC_INST_REL_LE: |
| 6862 | case BC_INST_REL_GE: |
| 6863 | case BC_INST_REL_NE: |
| 6864 | case BC_INST_REL_LT: |
| 6865 | case BC_INST_REL_GT: |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 6866 | s = zbc_program_logical(inst); |
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_READ: |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6869 | s = bc_program_read(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6870 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6871 | case BC_INST_VAR: |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 6872 | s = zbc_program_pushVar(code, &ip->idx, false, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6873 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6874 | case BC_INST_ARRAY_ELEM: |
| 6875 | case BC_INST_ARRAY: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6876 | s = zbc_program_pushArray(code, &ip->idx, inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6877 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6878 | case BC_INST_LAST: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6879 | r.t = BC_RESULT_LAST; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6880 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6881 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6882 | case BC_INST_IBASE: |
| 6883 | case BC_INST_SCALE: |
| 6884 | case BC_INST_OBASE: |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6885 | bc_program_pushGlobal(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6886 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6887 | case BC_INST_SCALE_FUNC: |
| 6888 | case BC_INST_LENGTH: |
| 6889 | case BC_INST_SQRT: |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6890 | s = zbc_program_builtin(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6891 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6892 | case BC_INST_NUM: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6893 | r.t = BC_RESULT_CONSTANT; |
| 6894 | r.d.id.idx = bc_program_index(code, &ip->idx); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6895 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6896 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6897 | case BC_INST_POP: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6898 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6899 | s = bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6900 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6901 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6902 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6903 | case BC_INST_POP_EXEC: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6904 | bc_vec_pop(&G.prog.stack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6905 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6906 | case BC_INST_PRINT: |
| 6907 | case BC_INST_PRINT_POP: |
| 6908 | case BC_INST_PRINT_STR: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6909 | s = zbc_program_print(inst, 0); |
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_STR: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6912 | r.t = BC_RESULT_STR; |
| 6913 | r.d.id.idx = bc_program_index(code, &ip->idx); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6914 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6915 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6916 | case BC_INST_POWER: |
| 6917 | case BC_INST_MULTIPLY: |
| 6918 | case BC_INST_DIVIDE: |
| 6919 | case BC_INST_MODULUS: |
| 6920 | case BC_INST_PLUS: |
| 6921 | case BC_INST_MINUS: |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 6922 | s = zbc_program_op(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6923 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6924 | case BC_INST_BOOL_NOT: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6925 | s = zbc_program_prep(&ptr, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6926 | if (s) return s; |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6927 | bc_num_init_DEF_SIZE(&r.d.n); |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6928 | if (!bc_num_cmp(num, &G.prog.zero)) |
| 6929 | bc_num_one(&r.d.n); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 6930 | //else bc_num_zero(&r.d.n); - already is |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6931 | bc_program_retire(&r, BC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6932 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6933 | case BC_INST_NEG: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6934 | s = zbc_program_negate(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6935 | break; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6936 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6937 | case BC_INST_ASSIGN_POWER: |
| 6938 | case BC_INST_ASSIGN_MULTIPLY: |
| 6939 | case BC_INST_ASSIGN_DIVIDE: |
| 6940 | case BC_INST_ASSIGN_MODULUS: |
| 6941 | case BC_INST_ASSIGN_PLUS: |
| 6942 | case BC_INST_ASSIGN_MINUS: |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6943 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6944 | case BC_INST_ASSIGN: |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6945 | s = zbc_program_assign(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6946 | break; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6947 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6948 | case BC_INST_MODEXP: |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6949 | s = zbc_program_modexp(); |
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_DIVMOD: |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6952 | s = zbc_program_divmod(); |
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_EXECUTE: |
| 6955 | case BC_INST_EXEC_COND: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6956 | cond = inst == BC_INST_EXEC_COND; |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6957 | s = bc_program_execStr(code, &ip->idx, cond); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6958 | break; |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6959 | case BC_INST_PRINT_STACK: { |
| 6960 | size_t idx; |
| 6961 | for (idx = 0; idx < G.prog.results.len; ++idx) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6962 | s = zbc_program_print(BC_INST_PRINT, idx); |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6963 | if (s) break; |
| 6964 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6965 | break; |
| 6966 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6967 | case BC_INST_CLEAR_STACK: |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 6968 | bc_vec_pop_all(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6969 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6970 | case BC_INST_STACK_LEN: |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6971 | bc_program_stackLen(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6972 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6973 | case BC_INST_DUPLICATE: |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6974 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6975 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6976 | ptr = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6977 | bc_result_copy(&r, ptr); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6978 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6979 | break; |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6980 | case BC_INST_SWAP: { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6981 | BcResult *ptr2; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6982 | if (!BC_PROG_STACK(&G.prog.results, 2)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6983 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6984 | ptr = bc_vec_item_rev(&G.prog.results, 0); |
| 6985 | ptr2 = bc_vec_item_rev(&G.prog.results, 1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6986 | memcpy(&r, ptr, sizeof(BcResult)); |
| 6987 | memcpy(ptr, ptr2, sizeof(BcResult)); |
| 6988 | memcpy(ptr2, &r, sizeof(BcResult)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6989 | break; |
| 6990 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6991 | case BC_INST_ASCIIFY: |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6992 | s = zbc_program_asciify(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6993 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6994 | case BC_INST_PRINT_STREAM: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6995 | s = zbc_program_printStream(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6996 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6997 | case BC_INST_LOAD: |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6998 | case BC_INST_PUSH_VAR: { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6999 | bool copy = inst == BC_INST_LOAD; |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 7000 | s = zbc_program_pushVar(code, &ip->idx, true, copy); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7001 | break; |
| 7002 | } |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 7003 | case BC_INST_PUSH_TO_VAR: { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7004 | char *name = bc_program_name(code, &ip->idx); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 7005 | s = zbc_program_copyToVar(name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7006 | free(name); |
| 7007 | break; |
| 7008 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7009 | case BC_INST_QUIT: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7010 | if (G.prog.stack.len <= 2) |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 7011 | QUIT_OR_RETURN_TO_MAIN; |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 7012 | bc_vec_npop(&G.prog.stack, 2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7013 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7014 | case BC_INST_NQUIT: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 7015 | s = zbc_program_nquit(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7016 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7017 | #endif // ENABLE_DC |
| 7018 | } |
| 7019 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7020 | if (s || G_interrupt) { |
| 7021 | bc_program_reset(); |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 7022 | return s; |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7023 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7024 | |
| 7025 | // If the stack has changed, pointers may be invalid. |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7026 | ip = bc_vec_top(&G.prog.stack); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 7027 | func = bc_program_func(ip->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7028 | code = func->code.v; |
| 7029 | } |
| 7030 | |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 7031 | return BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7032 | } |
| 7033 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7034 | #if ENABLE_BC |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 7035 | static void bc_vm_info(void) |
| 7036 | { |
| 7037 | printf("%s "BB_VER"\n" |
| 7038 | "Copyright (c) 2018 Gavin D. Howard and contributors\n" |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 7039 | , applet_name); |
| 7040 | } |
| 7041 | |
| 7042 | static void bc_args(char **argv) |
| 7043 | { |
| 7044 | unsigned opts; |
| 7045 | int i; |
| 7046 | |
| 7047 | GETOPT_RESET(); |
| 7048 | #if ENABLE_FEATURE_BC_LONG_OPTIONS |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7049 | opts = option_mask32 |= getopt32long(argv, "wvsqli", |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 7050 | "warn\0" No_argument "w" |
| 7051 | "version\0" No_argument "v" |
| 7052 | "standard\0" No_argument "s" |
| 7053 | "quiet\0" No_argument "q" |
| 7054 | "mathlib\0" No_argument "l" |
| 7055 | "interactive\0" No_argument "i" |
| 7056 | ); |
| 7057 | #else |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7058 | opts = option_mask32 |= getopt32(argv, "wvsqli"); |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 7059 | #endif |
| 7060 | if (getenv("POSIXLY_CORRECT")) |
| 7061 | option_mask32 |= BC_FLAG_S; |
| 7062 | |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 7063 | if (opts & BC_FLAG_V) { |
| 7064 | bc_vm_info(); |
| 7065 | exit(0); |
| 7066 | } |
| 7067 | |
| 7068 | for (i = optind; argv[i]; ++i) |
| 7069 | bc_vec_push(&G.files, argv + i); |
| 7070 | } |
| 7071 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7072 | static void bc_vm_envArgs(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7073 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7074 | BcVec v; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7075 | char *buf; |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 7076 | char *env_args = getenv("BC_ENV_ARGS"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7077 | |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7078 | if (!env_args) return; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7079 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7080 | G.env_args = xstrdup(env_args); |
| 7081 | buf = G.env_args; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7082 | |
| 7083 | bc_vec_init(&v, sizeof(char *), NULL); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7084 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7085 | while (*(buf = skip_whitespace(buf)) != '\0') { |
| 7086 | bc_vec_push(&v, &buf); |
| 7087 | buf = skip_non_whitespace(buf); |
| 7088 | if (!*buf) |
| 7089 | break; |
| 7090 | *buf++ = '\0'; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7091 | } |
| 7092 | |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 7093 | // NULL terminate, and pass argv[] so that first arg is argv[1] |
| 7094 | if (sizeof(int) == sizeof(char*)) { |
| 7095 | bc_vec_push(&v, &const_int_0); |
| 7096 | } else { |
| 7097 | static char *const nullptr = NULL; |
| 7098 | bc_vec_push(&v, &nullptr); |
| 7099 | } |
| 7100 | bc_args(((char **)v.v) - 1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7101 | |
| 7102 | bc_vec_free(&v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7103 | } |
| 7104 | #endif // ENABLE_BC |
| 7105 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7106 | static unsigned bc_vm_envLen(const char *var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7107 | { |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7108 | char *lenv; |
| 7109 | unsigned len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7110 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7111 | lenv = getenv(var); |
| 7112 | len = BC_NUM_PRINT_WIDTH; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7113 | if (!lenv) return len; |
| 7114 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7115 | len = bb_strtou(lenv, NULL, 10) - 1; |
| 7116 | if (errno || len < 2 || len >= INT_MAX) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7117 | len = BC_NUM_PRINT_WIDTH; |
| 7118 | |
| 7119 | return len; |
| 7120 | } |
| 7121 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7122 | static BcStatus bc_vm_process(const char *text) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7123 | { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7124 | BcStatus s = bc_parse_text(&G.prs, text); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7125 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7126 | if (s) return s; |
| 7127 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7128 | while (G.prs.l.t.t != BC_LEX_EOF) { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7129 | s = G.prs.parse(&G.prs); |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 7130 | if (s) return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7131 | } |
| 7132 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7133 | if (BC_PARSE_CAN_EXEC(&G.prs)) { |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7134 | s = bc_program_exec(); |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 7135 | fflush_and_check(); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7136 | if (s) |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7137 | bc_program_reset(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7138 | } |
| 7139 | |
| 7140 | return s; |
| 7141 | } |
| 7142 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7143 | static BcStatus bc_vm_file(const char *file) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7144 | { |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7145 | const char *sv_file; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7146 | char *data; |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7147 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7148 | BcFunc *main_func; |
| 7149 | BcInstPtr *ip; |
| 7150 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 7151 | data = bc_read_file(file); |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 7152 | if (!data) return bc_error_fmt("file '%s' is not text", file); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7153 | |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7154 | sv_file = G.prog.file; |
| 7155 | G.prog.file = file; |
| 7156 | bc_lex_file(&G.prs.l); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7157 | s = bc_vm_process(data); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7158 | if (s) goto err; |
| 7159 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 7160 | main_func = bc_program_func(BC_PROG_MAIN); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7161 | ip = bc_vec_item(&G.prog.stack, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7162 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 7163 | if (main_func->code.len < ip->idx) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 7164 | s = bc_error_fmt("file '%s' is not executable", file); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7165 | |
| 7166 | err: |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7167 | G.prog.file = sv_file; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7168 | free(data); |
| 7169 | return s; |
| 7170 | } |
| 7171 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7172 | static BcStatus bc_vm_stdin(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7173 | { |
Denys Vlasenko | a0c421c | 2018-12-02 20:16:52 +0100 | [diff] [blame] | 7174 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7175 | BcVec buf, buffer; |
Denys Vlasenko | 4dd3652 | 2018-12-11 22:26:38 +0100 | [diff] [blame] | 7176 | size_t str; |
| 7177 | bool comment; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7178 | |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7179 | G.prog.file = NULL; |
| 7180 | bc_lex_file(&G.prs.l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7181 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 7182 | bc_char_vec_init(&buffer); |
| 7183 | bc_char_vec_init(&buf); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 7184 | bc_vec_pushZeroByte(&buffer); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7185 | |
| 7186 | // This loop is complex because the vm tries not to send any lines that end |
| 7187 | // with a backslash to the parser. The reason for that is because the parser |
| 7188 | // treats a backslash+newline combo as whitespace, per the bc spec. In that |
| 7189 | // case, and for strings and comments, the parser will expect more stuff. |
Denys Vlasenko | 4dd3652 | 2018-12-11 22:26:38 +0100 | [diff] [blame] | 7190 | comment = false; |
| 7191 | str = 0; |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 7192 | while ((s = bc_read_line(&buf)) == BC_STATUS_SUCCESS) { |
Denys Vlasenko | 4dd3652 | 2018-12-11 22:26:38 +0100 | [diff] [blame] | 7193 | size_t len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7194 | char *string = buf.v; |
| 7195 | |
| 7196 | len = buf.len - 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7197 | if (len == 1) { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7198 | if (str && buf.v[0] == G.send) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7199 | str -= 1; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7200 | else if (buf.v[0] == G.sbgn) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7201 | str += 1; |
| 7202 | } |
| 7203 | else if (len > 1 || comment) { |
Denys Vlasenko | 4dd3652 | 2018-12-11 22:26:38 +0100 | [diff] [blame] | 7204 | size_t i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7205 | for (i = 0; i < len; ++i) { |
Denys Vlasenko | a0c421c | 2018-12-02 20:16:52 +0100 | [diff] [blame] | 7206 | bool notend = len > i + 1; |
| 7207 | char c = string[i]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7208 | |
| 7209 | if (i - 1 > len || string[i - 1] != '\\') { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7210 | if (G.sbgn == G.send) |
| 7211 | str ^= c == G.sbgn; |
| 7212 | else if (c == G.send) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7213 | str -= 1; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7214 | else if (c == G.sbgn) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7215 | str += 1; |
| 7216 | } |
| 7217 | |
| 7218 | if (c == '/' && notend && !comment && string[i + 1] == '*') { |
| 7219 | comment = true; |
| 7220 | break; |
| 7221 | } |
| 7222 | else if (c == '*' && notend && comment && string[i + 1] == '/') |
| 7223 | comment = false; |
| 7224 | } |
| 7225 | |
| 7226 | if (str || comment || string[len - 2] == '\\') { |
| 7227 | bc_vec_concat(&buffer, buf.v); |
| 7228 | continue; |
| 7229 | } |
| 7230 | } |
| 7231 | |
| 7232 | bc_vec_concat(&buffer, buf.v); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7233 | s = bc_vm_process(buffer.v); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7234 | if (s) { |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7235 | if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin) { |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 7236 | // Debug config, non-interactive mode: |
| 7237 | // return all the way back to main. |
| 7238 | // Non-debug builds do not come here, they exit. |
| 7239 | break; |
| 7240 | } |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7241 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7242 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 7243 | bc_vec_pop_all(&buffer); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7244 | } |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 7245 | if (s == BC_STATUS_EOF) // input EOF (^D) is not an error |
| 7246 | s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7247 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 7248 | if (str) { |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7249 | s = bc_error("string end could not be found"); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 7250 | } |
| 7251 | else if (comment) { |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7252 | s = bc_error("comment end could not be found"); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 7253 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7254 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7255 | bc_vec_free(&buf); |
| 7256 | bc_vec_free(&buffer); |
| 7257 | return s; |
| 7258 | } |
| 7259 | |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7260 | #if ENABLE_BC |
| 7261 | static const char bc_lib[] = { |
| 7262 | "scale=20" |
| 7263 | "\n" "define e(x){" |
| 7264 | "\n" "auto b,s,n,r,d,i,p,f,v" |
| 7265 | "\n" "b=ibase" |
| 7266 | "\n" "ibase=A" |
| 7267 | "\n" "if(x<0){" |
| 7268 | "\n" "n=1" |
| 7269 | "\n" "x=-x" |
| 7270 | "\n" "}" |
| 7271 | "\n" "s=scale" |
| 7272 | "\n" "r=6+s+0.44*x" |
| 7273 | "\n" "scale=scale(x)+1" |
| 7274 | "\n" "while(x>1){" |
| 7275 | "\n" "d+=1" |
| 7276 | "\n" "x/=2" |
| 7277 | "\n" "scale+=1" |
| 7278 | "\n" "}" |
| 7279 | "\n" "scale=r" |
| 7280 | "\n" "r=x+1" |
| 7281 | "\n" "p=x" |
| 7282 | "\n" "f=v=1" |
| 7283 | "\n" "for(i=2;v!=0;++i){" |
| 7284 | "\n" "p*=x" |
| 7285 | "\n" "f*=i" |
| 7286 | "\n" "v=p/f" |
| 7287 | "\n" "r+=v" |
| 7288 | "\n" "}" |
| 7289 | "\n" "while((d--)!=0)r*=r" |
| 7290 | "\n" "scale=s" |
| 7291 | "\n" "ibase=b" |
| 7292 | "\n" "if(n!=0)return(1/r)" |
| 7293 | "\n" "return(r/1)" |
| 7294 | "\n" "}" |
| 7295 | "\n" "define l(x){" |
| 7296 | "\n" "auto b,s,r,p,a,q,i,v" |
| 7297 | "\n" "b=ibase" |
| 7298 | "\n" "ibase=A" |
| 7299 | "\n" "if(x<=0){" |
| 7300 | "\n" "r=(1-10^scale)/1" |
| 7301 | "\n" "ibase=b" |
| 7302 | "\n" "return(r)" |
| 7303 | "\n" "}" |
| 7304 | "\n" "s=scale" |
| 7305 | "\n" "scale+=6" |
| 7306 | "\n" "p=2" |
| 7307 | "\n" "while(x>=2){" |
| 7308 | "\n" "p*=2" |
| 7309 | "\n" "x=sqrt(x)" |
| 7310 | "\n" "}" |
| 7311 | "\n" "while(x<=0.5){" |
| 7312 | "\n" "p*=2" |
| 7313 | "\n" "x=sqrt(x)" |
| 7314 | "\n" "}" |
| 7315 | "\n" "r=a=(x-1)/(x+1)" |
| 7316 | "\n" "q=a*a" |
| 7317 | "\n" "v=1" |
| 7318 | "\n" "for(i=3;v!=0;i+=2){" |
| 7319 | "\n" "a*=q" |
| 7320 | "\n" "v=a/i" |
| 7321 | "\n" "r+=v" |
| 7322 | "\n" "}" |
| 7323 | "\n" "r*=p" |
| 7324 | "\n" "scale=s" |
| 7325 | "\n" "ibase=b" |
| 7326 | "\n" "return(r/1)" |
| 7327 | "\n" "}" |
| 7328 | "\n" "define s(x){" |
| 7329 | "\n" "auto b,s,r,n,a,q,i" |
| 7330 | "\n" "b=ibase" |
| 7331 | "\n" "ibase=A" |
| 7332 | "\n" "s=scale" |
| 7333 | "\n" "scale=1.1*s+2" |
| 7334 | "\n" "a=a(1)" |
| 7335 | "\n" "if(x<0){" |
| 7336 | "\n" "n=1" |
| 7337 | "\n" "x=-x" |
| 7338 | "\n" "}" |
| 7339 | "\n" "scale=0" |
| 7340 | "\n" "q=(x/a+2)/4" |
| 7341 | "\n" "x=x-4*q*a" |
| 7342 | "\n" "if(q%2!=0)x=-x" |
| 7343 | "\n" "scale=s+2" |
| 7344 | "\n" "r=a=x" |
| 7345 | "\n" "q=-x*x" |
| 7346 | "\n" "for(i=3;a!=0;i+=2){" |
| 7347 | "\n" "a*=q/(i*(i-1))" |
| 7348 | "\n" "r+=a" |
| 7349 | "\n" "}" |
| 7350 | "\n" "scale=s" |
| 7351 | "\n" "ibase=b" |
| 7352 | "\n" "if(n!=0)return(-r/1)" |
| 7353 | "\n" "return(r/1)" |
| 7354 | "\n" "}" |
| 7355 | "\n" "define c(x){" |
| 7356 | "\n" "auto b,s" |
| 7357 | "\n" "b=ibase" |
| 7358 | "\n" "ibase=A" |
| 7359 | "\n" "s=scale" |
| 7360 | "\n" "scale*=1.2" |
| 7361 | "\n" "x=s(2*a(1)+x)" |
| 7362 | "\n" "scale=s" |
| 7363 | "\n" "ibase=b" |
| 7364 | "\n" "return(x/1)" |
| 7365 | "\n" "}" |
| 7366 | "\n" "define a(x){" |
| 7367 | "\n" "auto b,s,r,n,a,m,t,f,i,u" |
| 7368 | "\n" "b=ibase" |
| 7369 | "\n" "ibase=A" |
| 7370 | "\n" "n=1" |
| 7371 | "\n" "if(x<0){" |
| 7372 | "\n" "n=-1" |
| 7373 | "\n" "x=-x" |
| 7374 | "\n" "}" |
| 7375 | "\n" "if(x==1){" |
| 7376 | "\n" "if(scale<65){" |
| 7377 | "\n" "return(.7853981633974483096156608458198757210492923498437764552437361480/n)" |
| 7378 | "\n" "}" |
| 7379 | "\n" "}" |
| 7380 | "\n" "if(x==.2){" |
| 7381 | "\n" "if(scale<65){" |
| 7382 | "\n" "return(.1973955598498807583700497651947902934475851037878521015176889402/n)" |
| 7383 | "\n" "}" |
| 7384 | "\n" "}" |
| 7385 | "\n" "s=scale" |
| 7386 | "\n" "if(x>.2){" |
| 7387 | "\n" "scale+=5" |
| 7388 | "\n" "a=a(.2)" |
| 7389 | "\n" "}" |
| 7390 | "\n" "scale=s+3" |
| 7391 | "\n" "while(x>.2){" |
| 7392 | "\n" "m+=1" |
| 7393 | "\n" "x=(x-.2)/(1+.2*x)" |
| 7394 | "\n" "}" |
| 7395 | "\n" "r=u=x" |
| 7396 | "\n" "f=-x*x" |
| 7397 | "\n" "t=1" |
| 7398 | "\n" "for(i=3;t!=0;i+=2){" |
| 7399 | "\n" "u*=f" |
| 7400 | "\n" "t=u/i" |
| 7401 | "\n" "r+=t" |
| 7402 | "\n" "}" |
| 7403 | "\n" "scale=s" |
| 7404 | "\n" "ibase=b" |
| 7405 | "\n" "return((m*a+r)/n)" |
| 7406 | "\n" "}" |
| 7407 | "\n" "define j(n,x){" |
| 7408 | "\n" "auto b,s,o,a,i,v,f" |
| 7409 | "\n" "b=ibase" |
| 7410 | "\n" "ibase=A" |
| 7411 | "\n" "s=scale" |
| 7412 | "\n" "scale=0" |
| 7413 | "\n" "n/=1" |
| 7414 | "\n" "if(n<0){" |
| 7415 | "\n" "n=-n" |
| 7416 | "\n" "if(n%2==1)o=1" |
| 7417 | "\n" "}" |
| 7418 | "\n" "a=1" |
| 7419 | "\n" "for(i=2;i<=n;++i)a*=i" |
| 7420 | "\n" "scale=1.5*s" |
| 7421 | "\n" "a=(x^n)/2^n/a" |
| 7422 | "\n" "r=v=1" |
| 7423 | "\n" "f=-x*x/4" |
| 7424 | "\n" "scale=scale+length(a)-scale(a)" |
| 7425 | "\n" "for(i=1;v!=0;++i){" |
| 7426 | "\n" "v=v*f/i/(n+i)" |
| 7427 | "\n" "r+=v" |
| 7428 | "\n" "}" |
| 7429 | "\n" "scale=s" |
| 7430 | "\n" "ibase=b" |
| 7431 | "\n" "if(o!=0)a=-a" |
| 7432 | "\n" "return(a*r/1)" |
| 7433 | "\n" "}" |
| 7434 | }; |
| 7435 | #endif // ENABLE_BC |
| 7436 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7437 | static BcStatus bc_vm_exec(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7438 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7439 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7440 | size_t i; |
| 7441 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7442 | #if ENABLE_BC |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 7443 | if (option_mask32 & BC_FLAG_L) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7444 | |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7445 | // We know that internal library is not buggy, |
| 7446 | // thus error checking is normally disabled. |
| 7447 | # define DEBUG_LIB 0 |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7448 | bc_lex_file(&G.prs.l); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7449 | s = bc_parse_text(&G.prs, bc_lib); |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7450 | if (DEBUG_LIB && s) return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7451 | |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7452 | while (G.prs.l.t.t != BC_LEX_EOF) { |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7453 | s = G.prs.parse(&G.prs); |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7454 | if (DEBUG_LIB && s) return s; |
| 7455 | } |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7456 | s = bc_program_exec(); |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7457 | if (DEBUG_LIB && s) return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7458 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7459 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7460 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7461 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7462 | for (i = 0; !s && i < G.files.len; ++i) |
| 7463 | s = bc_vm_file(*((char **) bc_vec_item(&G.files, i))); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7464 | if (ENABLE_FEATURE_CLEAN_UP && s && !G_ttyin) { |
| 7465 | // Debug config, non-interactive mode: |
| 7466 | // return all the way back to main. |
| 7467 | // Non-debug builds do not come here, they exit. |
| 7468 | return s; |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7469 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7470 | |
Denys Vlasenko | 91cde95 | 2018-12-10 20:56:08 +0100 | [diff] [blame] | 7471 | if (IS_BC || (option_mask32 & BC_FLAG_I)) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7472 | s = bc_vm_stdin(); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7473 | |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7474 | if (!s && !BC_PARSE_CAN_EXEC(&G.prs)) |
| 7475 | s = bc_vm_process(""); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7476 | |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 7477 | return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7478 | } |
| 7479 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7480 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 7481 | static void bc_program_free(void) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7482 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7483 | bc_num_free(&G.prog.ib); |
| 7484 | bc_num_free(&G.prog.ob); |
| 7485 | bc_num_free(&G.prog.hexb); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7486 | # if ENABLE_DC |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7487 | bc_num_free(&G.prog.strmb); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7488 | # endif |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7489 | bc_vec_free(&G.prog.fns); |
| 7490 | bc_vec_free(&G.prog.fn_map); |
| 7491 | bc_vec_free(&G.prog.vars); |
| 7492 | bc_vec_free(&G.prog.var_map); |
| 7493 | bc_vec_free(&G.prog.arrs); |
| 7494 | bc_vec_free(&G.prog.arr_map); |
| 7495 | bc_vec_free(&G.prog.strs); |
| 7496 | bc_vec_free(&G.prog.consts); |
| 7497 | bc_vec_free(&G.prog.results); |
| 7498 | bc_vec_free(&G.prog.stack); |
| 7499 | bc_num_free(&G.prog.last); |
| 7500 | bc_num_free(&G.prog.zero); |
| 7501 | bc_num_free(&G.prog.one); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7502 | } |
| 7503 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7504 | static void bc_vm_free(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7505 | { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7506 | bc_vec_free(&G.files); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7507 | bc_program_free(); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7508 | bc_parse_free(&G.prs); |
| 7509 | free(G.env_args); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7510 | } |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7511 | #endif |
| 7512 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7513 | static void bc_program_init(void) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7514 | { |
| 7515 | size_t idx; |
| 7516 | BcInstPtr ip; |
| 7517 | |
| 7518 | /* memset(&G.prog, 0, sizeof(G.prog)); - already is */ |
| 7519 | memset(&ip, 0, sizeof(BcInstPtr)); |
| 7520 | |
| 7521 | /* G.prog.nchars = G.prog.scale = 0; - already is */ |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7522 | bc_num_init_DEF_SIZE(&G.prog.ib); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7523 | bc_num_ten(&G.prog.ib); |
| 7524 | G.prog.ib_t = 10; |
| 7525 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7526 | bc_num_init_DEF_SIZE(&G.prog.ob); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7527 | bc_num_ten(&G.prog.ob); |
| 7528 | G.prog.ob_t = 10; |
| 7529 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7530 | bc_num_init_DEF_SIZE(&G.prog.hexb); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7531 | bc_num_ten(&G.prog.hexb); |
| 7532 | G.prog.hexb.num[0] = 6; |
| 7533 | |
| 7534 | #if ENABLE_DC |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7535 | bc_num_init_DEF_SIZE(&G.prog.strmb); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7536 | bc_num_ulong2num(&G.prog.strmb, UCHAR_MAX + 1); |
| 7537 | #endif |
| 7538 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7539 | bc_num_init_DEF_SIZE(&G.prog.last); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 7540 | //bc_num_zero(&G.prog.last); - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7541 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7542 | bc_num_init_DEF_SIZE(&G.prog.zero); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 7543 | //bc_num_zero(&G.prog.zero); - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7544 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7545 | bc_num_init_DEF_SIZE(&G.prog.one); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7546 | bc_num_one(&G.prog.one); |
| 7547 | |
| 7548 | bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7549 | bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7550 | |
Denys Vlasenko | 1f67e93 | 2018-12-03 00:08:59 +0100 | [diff] [blame] | 7551 | bc_program_addFunc(xstrdup("(main)"), &idx); |
| 7552 | bc_program_addFunc(xstrdup("(read)"), &idx); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7553 | |
| 7554 | bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7555 | bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7556 | |
| 7557 | bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7558 | bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7559 | |
| 7560 | bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free); |
| 7561 | bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free); |
| 7562 | bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free); |
| 7563 | bc_vec_init(&G.prog.stack, sizeof(BcInstPtr), NULL); |
| 7564 | bc_vec_push(&G.prog.stack, &ip); |
| 7565 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7566 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7567 | static int bc_vm_init(const char *env_len) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7568 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7569 | #if ENABLE_FEATURE_EDITING |
| 7570 | G.line_input_state = new_line_input_t(DO_HISTORY); |
| 7571 | #endif |
| 7572 | G.prog.len = bc_vm_envLen(env_len); |
| 7573 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7574 | bc_vec_init(&G.files, sizeof(char *), NULL); |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7575 | if (IS_BC) |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 7576 | IF_BC(bc_vm_envArgs();) |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7577 | bc_program_init(); |
Denys Vlasenko | f0f069b | 2018-12-11 23:22:52 +0100 | [diff] [blame^] | 7578 | common_parse_init(&G.prs, BC_PROG_MAIN); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7579 | |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7580 | if (isatty(0)) { |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7581 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7582 | G_ttyin = 1; |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7583 | // With SA_RESTART, most system calls will restart |
| 7584 | // (IOW: they won't fail with EINTR). |
| 7585 | // In particular, this means ^C won't cause |
| 7586 | // stdout to get into "error state" if SIGINT hits |
| 7587 | // within write() syscall. |
| 7588 | // The downside is that ^C while line input is taken |
| 7589 | // will only be handled after [Enter] since read() |
| 7590 | // from stdin is not interrupted by ^C either, |
| 7591 | // it restarts, thus fgetc() does not return on ^C. |
| 7592 | signal_SA_RESTART_empty_mask(SIGINT, record_signo); |
| 7593 | |
| 7594 | // Without SA_RESTART, this exhibits a bug: |
| 7595 | // "while (1) print 1" and try ^C-ing it. |
| 7596 | // Intermittently, instead of returning to input line, |
| 7597 | // you'll get "output error: Interrupted system call" |
| 7598 | // and exit. |
| 7599 | //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo); |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7600 | #endif |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7601 | return 1; // "tty" |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7602 | } |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7603 | return 0; // "not a tty" |
| 7604 | } |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7605 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7606 | static BcStatus bc_vm_run(void) |
| 7607 | { |
| 7608 | BcStatus st = bc_vm_exec(); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7609 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 7610 | if (G_exiting) // it was actually "halt" or "quit" |
| 7611 | st = EXIT_SUCCESS; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7612 | bc_vm_free(); |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 7613 | # if ENABLE_FEATURE_EDITING |
| 7614 | free_line_input_t(G.line_input_state); |
| 7615 | # endif |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 7616 | FREE_G(); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7617 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7618 | return st; |
| 7619 | } |
| 7620 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7621 | #if ENABLE_BC |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7622 | int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7623 | int bc_main(int argc UNUSED_PARAM, char **argv) |
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 | int is_tty; |
| 7626 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7627 | INIT_G(); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7628 | G.sbgn = G.send = '"'; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7629 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7630 | is_tty = bc_vm_init("BC_LINE_LENGTH"); |
| 7631 | |
| 7632 | bc_args(argv); |
| 7633 | |
| 7634 | if (is_tty && !(option_mask32 & BC_FLAG_Q)) |
| 7635 | bc_vm_info(); |
| 7636 | |
| 7637 | return bc_vm_run(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7638 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7639 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7640 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7641 | #if ENABLE_DC |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7642 | int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7643 | int dc_main(int argc UNUSED_PARAM, char **argv) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7644 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7645 | int noscript; |
| 7646 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7647 | INIT_G(); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7648 | G.sbgn = '['; |
| 7649 | G.send = ']'; |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 7650 | /* |
| 7651 | * TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width |
| 7652 | * 1 char wider than bc from the same package. |
| 7653 | * Both default width, and xC_LINE_LENGTH=N are wider: |
| 7654 | * "DC_LINE_LENGTH=5 dc -e'123456 p'" prints: |
| 7655 | * 1234\ |
| 7656 | * 56 |
| 7657 | * "echo '123456' | BC_LINE_LENGTH=5 bc" prints: |
| 7658 | * 123\ |
| 7659 | * 456 |
| 7660 | * Do the same, or it's a bug? |
| 7661 | */ |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7662 | bc_vm_init("DC_LINE_LENGTH"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7663 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7664 | // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs |
| 7665 | noscript = BC_FLAG_I; |
| 7666 | for (;;) { |
| 7667 | int n = getopt(argc, argv, "e:f:x"); |
| 7668 | if (n <= 0) |
| 7669 | break; |
| 7670 | switch (n) { |
| 7671 | case 'e': |
| 7672 | noscript = 0; |
| 7673 | n = bc_vm_process(optarg); |
| 7674 | if (n) return n; |
| 7675 | break; |
| 7676 | case 'f': |
| 7677 | noscript = 0; |
| 7678 | bc_vm_file(optarg); |
| 7679 | break; |
| 7680 | case 'x': |
| 7681 | option_mask32 |= DC_FLAG_X; |
| 7682 | break; |
| 7683 | default: |
| 7684 | bb_show_usage(); |
| 7685 | } |
| 7686 | } |
| 7687 | argv += optind; |
| 7688 | |
| 7689 | while (*argv) { |
| 7690 | noscript = 0; |
| 7691 | bc_vec_push(&G.files, argv++); |
| 7692 | } |
| 7693 | |
| 7694 | option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin |
| 7695 | |
| 7696 | return bc_vm_run(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7697 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7698 | #endif |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 7699 | |
| 7700 | #endif // not DC_SMALL |