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 | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 1361 | static BcStatus bc_read_line(BcVec *vec) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1362 | { |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 1363 | BcStatus s; |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1364 | bool bad_chars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1365 | |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 1366 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 1367 | do { |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1368 | int c; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1369 | |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1370 | bad_chars = 0; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1371 | bc_vec_pop_all(vec); |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1372 | |
| 1373 | fflush_and_check(); |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1374 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1375 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | b9c321d | 2018-12-07 12:41:42 +0100 | [diff] [blame] | 1376 | if (G_interrupt) { // ^C was pressed |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1377 | intr: |
Denys Vlasenko | b9c321d | 2018-12-07 12:41:42 +0100 | [diff] [blame] | 1378 | G_interrupt = 0; |
Denys Vlasenko | ac6ed11 | 2018-12-08 21:39:10 +0100 | [diff] [blame] | 1379 | // GNU bc says "interrupted execution." |
| 1380 | // GNU dc says "Interrupt!" |
| 1381 | fputs("\ninterrupted execution\n", stderr); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1382 | } |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1383 | # if ENABLE_FEATURE_EDITING |
| 1384 | if (G_ttyin) { |
| 1385 | int n, i; |
| 1386 | # define line_buf bb_common_bufsiz1 |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 1387 | n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE); |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1388 | if (n <= 0) { // read errors or EOF, or ^D, or ^C |
| 1389 | if (n == 0) // ^C |
| 1390 | goto intr; |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 1391 | s = BC_STATUS_EOF; |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1392 | break; |
| 1393 | } |
| 1394 | i = 0; |
| 1395 | for (;;) { |
| 1396 | c = line_buf[i++]; |
| 1397 | if (!c) break; |
| 1398 | bad_chars |= push_input_byte(vec, c); |
| 1399 | } |
| 1400 | # undef line_buf |
| 1401 | } else |
| 1402 | # endif |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1403 | #endif |
Denys Vlasenko | ed84935 | 2018-12-06 10:26:13 +0100 | [diff] [blame] | 1404 | { |
Denys Vlasenko | ed84935 | 2018-12-06 10:26:13 +0100 | [diff] [blame] | 1405 | IF_FEATURE_BC_SIGNALS(errno = 0;) |
| 1406 | do { |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1407 | c = fgetc(stdin); |
| 1408 | #if ENABLE_FEATURE_BC_SIGNALS && !ENABLE_FEATURE_EDITING |
| 1409 | // Both conditions appear simultaneously, check both just in case |
Denys Vlasenko | b9c321d | 2018-12-07 12:41:42 +0100 | [diff] [blame] | 1410 | if (errno == EINTR || G_interrupt) { |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1411 | // ^C was pressed |
| 1412 | clearerr(stdin); |
| 1413 | goto intr; |
| 1414 | } |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1415 | #endif |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1416 | if (c == EOF) { |
Denys Vlasenko | ed84935 | 2018-12-06 10:26:13 +0100 | [diff] [blame] | 1417 | if (ferror(stdin)) |
| 1418 | quit(); // this emits error message |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 1419 | s = BC_STATUS_EOF; |
Denys Vlasenko | ed84935 | 2018-12-06 10:26:13 +0100 | [diff] [blame] | 1420 | // Note: EOF does not append '\n', therefore: |
| 1421 | // printf 'print 123\n' | bc - works |
| 1422 | // printf 'print 123' | bc - fails (syntax error) |
| 1423 | break; |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1424 | } |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1425 | bad_chars |= push_input_byte(vec, c); |
| 1426 | } while (c != '\n'); |
Denys Vlasenko | ed84935 | 2018-12-06 10:26:13 +0100 | [diff] [blame] | 1427 | } |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1428 | } while (bad_chars); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1429 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1430 | bc_vec_pushZeroByte(vec); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1431 | |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 1432 | return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1433 | } |
| 1434 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 1435 | static char* bc_read_file(const char *path) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1436 | { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 1437 | char *buf; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 1438 | size_t size = ((size_t) -1); |
| 1439 | size_t i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1440 | |
Denys Vlasenko | 4c9455f | 2018-12-06 15:21:39 +0100 | [diff] [blame] | 1441 | // Never returns NULL (dies on errors) |
| 1442 | buf = xmalloc_xopen_read_close(path, &size); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1443 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 1444 | for (i = 0; i < size; ++i) { |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1445 | char c = buf[i]; |
| 1446 | if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'? |
| 1447 | || c > 0x7e |
| 1448 | ) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 1449 | free(buf); |
| 1450 | buf = NULL; |
| 1451 | break; |
| 1452 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1453 | } |
| 1454 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 1455 | return buf; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1456 | } |
| 1457 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1458 | static void bc_num_setToZero(BcNum *n, size_t scale) |
| 1459 | { |
| 1460 | n->len = 0; |
| 1461 | n->neg = false; |
| 1462 | n->rdx = scale; |
| 1463 | } |
| 1464 | |
| 1465 | static void bc_num_zero(BcNum *n) |
| 1466 | { |
| 1467 | bc_num_setToZero(n, 0); |
| 1468 | } |
| 1469 | |
| 1470 | static void bc_num_one(BcNum *n) |
| 1471 | { |
| 1472 | bc_num_setToZero(n, 0); |
| 1473 | n->len = 1; |
| 1474 | n->num[0] = 1; |
| 1475 | } |
| 1476 | |
| 1477 | static void bc_num_ten(BcNum *n) |
| 1478 | { |
| 1479 | bc_num_setToZero(n, 0); |
| 1480 | n->len = 2; |
| 1481 | n->num[0] = 0; |
| 1482 | n->num[1] = 1; |
| 1483 | } |
| 1484 | |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1485 | // Note: this also sets BcNum to zero |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1486 | static void bc_num_init(BcNum *n, size_t req) |
| 1487 | { |
| 1488 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1489 | //memset(n, 0, sizeof(BcNum)); - cleared by assignments below |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1490 | n->num = xmalloc(req); |
| 1491 | n->cap = req; |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1492 | n->rdx = 0; |
| 1493 | n->len = 0; |
| 1494 | n->neg = false; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1495 | } |
| 1496 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 1497 | static void bc_num_init_DEF_SIZE(BcNum *n) |
| 1498 | { |
| 1499 | bc_num_init(n, BC_NUM_DEF_SIZE); |
| 1500 | } |
| 1501 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1502 | static void bc_num_expand(BcNum *n, size_t req) |
| 1503 | { |
| 1504 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
| 1505 | if (req > n->cap) { |
| 1506 | n->num = xrealloc(n->num, req); |
| 1507 | n->cap = req; |
| 1508 | } |
| 1509 | } |
| 1510 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1511 | static FAST_FUNC void bc_num_free(void *num) |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1512 | { |
| 1513 | free(((BcNum *) num)->num); |
| 1514 | } |
| 1515 | |
| 1516 | static void bc_num_copy(BcNum *d, BcNum *s) |
| 1517 | { |
| 1518 | if (d != s) { |
| 1519 | bc_num_expand(d, s->cap); |
| 1520 | d->len = s->len; |
| 1521 | d->neg = s->neg; |
| 1522 | d->rdx = s->rdx; |
| 1523 | memcpy(d->num, s->num, sizeof(BcDig) * d->len); |
| 1524 | } |
| 1525 | } |
| 1526 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1527 | static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p) |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1528 | { |
| 1529 | size_t i; |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1530 | unsigned long pow, result; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1531 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1532 | if (n->neg) RETURN_STATUS(bc_error("negative number")); |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1533 | |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1534 | for (result = 0, pow = 1, i = n->rdx; i < n->len; ++i) { |
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 | unsigned long prev = result, powprev = pow; |
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 | result += ((unsigned long) n->num[i]) * pow; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1539 | pow *= 10; |
| 1540 | |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1541 | if (result < prev || pow < powprev) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1542 | RETURN_STATUS(bc_error("overflow")); |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1543 | prev = result; |
| 1544 | powprev = pow; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1545 | } |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1546 | *result_p = result; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1547 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1548 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1549 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 1550 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1551 | # define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 1552 | #endif |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1553 | |
| 1554 | static void bc_num_ulong2num(BcNum *n, unsigned long val) |
| 1555 | { |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1556 | BcDig *ptr; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1557 | |
| 1558 | bc_num_zero(n); |
| 1559 | |
| 1560 | if (val == 0) return; |
| 1561 | |
Denys Vlasenko | b696d9e | 2018-12-10 12:22:15 +0100 | [diff] [blame] | 1562 | if (ULONG_MAX == 0xffffffffUL) |
| 1563 | bc_num_expand(n, 10); // 10 digits: 4294967295 |
Denys Vlasenko | c665c18 | 2018-12-10 15:15:42 +0100 | [diff] [blame] | 1564 | if (ULONG_MAX == 0xffffffffffffffffULL) |
Denys Vlasenko | b696d9e | 2018-12-10 12:22:15 +0100 | [diff] [blame] | 1565 | bc_num_expand(n, 20); // 20 digits: 18446744073709551615 |
Denys Vlasenko | c665c18 | 2018-12-10 15:15:42 +0100 | [diff] [blame] | 1566 | BUILD_BUG_ON(ULONG_MAX > 0xffffffffffffffffULL); |
Denys Vlasenko | b696d9e | 2018-12-10 12:22:15 +0100 | [diff] [blame] | 1567 | |
| 1568 | ptr = n->num; |
| 1569 | for (;;) { |
| 1570 | n->len++; |
| 1571 | *ptr++ = val % 10; |
| 1572 | val /= 10; |
| 1573 | if (val == 0) break; |
| 1574 | } |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1575 | } |
| 1576 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1577 | static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1578 | size_t len) |
| 1579 | { |
| 1580 | size_t i, j; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1581 | for (i = 0; i < len; ++i) { |
| 1582 | for (a[i] -= b[i], j = 0; a[i + j] < 0;) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1583 | a[i + j++] += 10; |
| 1584 | a[i + j] -= 1; |
| 1585 | } |
| 1586 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len) |
| 1590 | { |
| 1591 | size_t i; |
| 1592 | int c = 0; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1593 | for (i = len - 1; i < len && !(c = a[i] - b[i]); --i); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1594 | return BC_NUM_NEG(i + 1, c < 0); |
| 1595 | } |
| 1596 | |
| 1597 | static ssize_t bc_num_cmp(BcNum *a, BcNum *b) |
| 1598 | { |
| 1599 | size_t i, min, a_int, b_int, diff; |
| 1600 | BcDig *max_num, *min_num; |
| 1601 | bool a_max, neg = false; |
| 1602 | ssize_t cmp; |
| 1603 | |
| 1604 | if (a == b) return 0; |
| 1605 | if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg); |
| 1606 | if (b->len == 0) return BC_NUM_NEG(1, a->neg); |
| 1607 | if (a->neg) { |
| 1608 | if (b->neg) |
| 1609 | neg = true; |
| 1610 | else |
| 1611 | return -1; |
| 1612 | } |
| 1613 | else if (b->neg) |
| 1614 | return 1; |
| 1615 | |
| 1616 | a_int = BC_NUM_INT(a); |
| 1617 | b_int = BC_NUM_INT(b); |
| 1618 | a_int -= b_int; |
| 1619 | a_max = (a->rdx > b->rdx); |
| 1620 | |
| 1621 | if (a_int != 0) return (ssize_t) a_int; |
| 1622 | |
| 1623 | if (a_max) { |
| 1624 | min = b->rdx; |
| 1625 | diff = a->rdx - b->rdx; |
| 1626 | max_num = a->num + diff; |
| 1627 | min_num = b->num; |
| 1628 | } |
| 1629 | else { |
| 1630 | min = a->rdx; |
| 1631 | diff = b->rdx - a->rdx; |
| 1632 | max_num = b->num + diff; |
| 1633 | min_num = a->num; |
| 1634 | } |
| 1635 | |
| 1636 | cmp = bc_num_compare(max_num, min_num, b_int + min); |
| 1637 | if (cmp != 0) return BC_NUM_NEG(cmp, (!a_max) != neg); |
| 1638 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1639 | for (max_num -= diff, i = diff - 1; i < diff; --i) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1640 | if (max_num[i]) return BC_NUM_NEG(1, (!a_max) != neg); |
| 1641 | } |
| 1642 | |
| 1643 | return 0; |
| 1644 | } |
| 1645 | |
| 1646 | static void bc_num_truncate(BcNum *n, size_t places) |
| 1647 | { |
| 1648 | if (places == 0) return; |
| 1649 | |
| 1650 | n->rdx -= places; |
| 1651 | |
| 1652 | if (n->len != 0) { |
| 1653 | n->len -= places; |
| 1654 | memmove(n->num, n->num + places, n->len * sizeof(BcDig)); |
| 1655 | } |
| 1656 | } |
| 1657 | |
| 1658 | static void bc_num_extend(BcNum *n, size_t places) |
| 1659 | { |
| 1660 | size_t len = n->len + places; |
| 1661 | |
| 1662 | if (places != 0) { |
| 1663 | |
| 1664 | if (n->cap < len) bc_num_expand(n, len); |
| 1665 | |
| 1666 | memmove(n->num + places, n->num, sizeof(BcDig) * n->len); |
| 1667 | memset(n->num, 0, sizeof(BcDig) * places); |
| 1668 | |
| 1669 | n->len += places; |
| 1670 | n->rdx += places; |
| 1671 | } |
| 1672 | } |
| 1673 | |
| 1674 | static void bc_num_clean(BcNum *n) |
| 1675 | { |
| 1676 | while (n->len > 0 && n->num[n->len - 1] == 0) --n->len; |
| 1677 | if (n->len == 0) |
| 1678 | n->neg = false; |
| 1679 | else if (n->len < n->rdx) |
| 1680 | n->len = n->rdx; |
| 1681 | } |
| 1682 | |
| 1683 | static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2) |
| 1684 | { |
| 1685 | if (n->rdx < scale) |
| 1686 | bc_num_extend(n, scale - n->rdx); |
| 1687 | else |
| 1688 | bc_num_truncate(n, n->rdx - scale); |
| 1689 | |
| 1690 | bc_num_clean(n); |
| 1691 | if (n->len != 0) n->neg = !neg1 != !neg2; |
| 1692 | } |
| 1693 | |
| 1694 | static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a, |
| 1695 | BcNum *restrict b) |
| 1696 | { |
| 1697 | if (idx < n->len) { |
| 1698 | |
| 1699 | b->len = n->len - idx; |
| 1700 | a->len = idx; |
| 1701 | a->rdx = b->rdx = 0; |
| 1702 | |
| 1703 | memcpy(b->num, n->num + idx, b->len * sizeof(BcDig)); |
| 1704 | memcpy(a->num, n->num, idx * sizeof(BcDig)); |
| 1705 | } |
| 1706 | else { |
| 1707 | bc_num_zero(b); |
| 1708 | bc_num_copy(a, n); |
| 1709 | } |
| 1710 | |
| 1711 | bc_num_clean(a); |
| 1712 | bc_num_clean(b); |
| 1713 | } |
| 1714 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1715 | static BC_STATUS zbc_num_shift(BcNum *n, size_t places) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1716 | { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1717 | if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 1718 | |
| 1719 | // This check makes sense only if size_t is (much) larger than BC_MAX_NUM. |
| 1720 | if (SIZE_MAX > (BC_MAX_NUM | 0xff)) { |
| 1721 | if (places + n->len > BC_MAX_NUM) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1722 | 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] | 1723 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1724 | |
| 1725 | if (n->rdx >= places) |
| 1726 | n->rdx -= places; |
| 1727 | else { |
| 1728 | bc_num_extend(n, places - n->rdx); |
| 1729 | n->rdx = 0; |
| 1730 | } |
| 1731 | |
| 1732 | bc_num_clean(n); |
| 1733 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1734 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1735 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 1736 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1737 | # define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 1738 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1739 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1740 | 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] | 1741 | { |
| 1742 | BcNum one; |
| 1743 | BcDig num[2]; |
| 1744 | |
| 1745 | one.cap = 2; |
| 1746 | one.num = num; |
| 1747 | bc_num_one(&one); |
| 1748 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1749 | RETURN_STATUS(zbc_num_div(&one, a, b, scale)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1750 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1751 | #if ERRORS_ARE_FATAL |
| 1752 | # define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 1753 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1754 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1755 | 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] | 1756 | { |
| 1757 | BcDig *ptr, *ptr_a, *ptr_b, *ptr_c; |
| 1758 | size_t i, max, min_rdx, min_int, diff, a_int, b_int; |
| 1759 | int carry, in; |
| 1760 | |
| 1761 | // Because this function doesn't need to use scale (per the bc spec), |
| 1762 | // I am hijacking it to say whether it's doing an add or a subtract. |
| 1763 | |
| 1764 | if (a->len == 0) { |
| 1765 | bc_num_copy(c, b); |
| 1766 | if (sub && c->len) c->neg = !c->neg; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1767 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1768 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1769 | if (b->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1770 | bc_num_copy(c, a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1771 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1772 | } |
| 1773 | |
| 1774 | c->neg = a->neg; |
| 1775 | c->rdx = BC_MAX(a->rdx, b->rdx); |
| 1776 | min_rdx = BC_MIN(a->rdx, b->rdx); |
| 1777 | c->len = 0; |
| 1778 | |
| 1779 | if (a->rdx > b->rdx) { |
| 1780 | diff = a->rdx - b->rdx; |
| 1781 | ptr = a->num; |
| 1782 | ptr_a = a->num + diff; |
| 1783 | ptr_b = b->num; |
| 1784 | } |
| 1785 | else { |
| 1786 | diff = b->rdx - a->rdx; |
| 1787 | ptr = b->num; |
| 1788 | ptr_a = a->num; |
| 1789 | ptr_b = b->num + diff; |
| 1790 | } |
| 1791 | |
| 1792 | for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i]; |
| 1793 | |
| 1794 | ptr_c += diff; |
| 1795 | a_int = BC_NUM_INT(a); |
| 1796 | b_int = BC_NUM_INT(b); |
| 1797 | |
| 1798 | if (a_int > b_int) { |
| 1799 | min_int = b_int; |
| 1800 | max = a_int; |
| 1801 | ptr = ptr_a; |
| 1802 | } |
| 1803 | else { |
| 1804 | min_int = a_int; |
| 1805 | max = b_int; |
| 1806 | ptr = ptr_b; |
| 1807 | } |
| 1808 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1809 | 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] | 1810 | in = ((int) ptr_a[i]) + ((int) ptr_b[i]) + carry; |
| 1811 | carry = in / 10; |
| 1812 | ptr_c[i] = (BcDig)(in % 10); |
| 1813 | } |
| 1814 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1815 | for (; i < max + min_rdx; ++i, ++c->len) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1816 | in = ((int) ptr[i]) + carry; |
| 1817 | carry = in / 10; |
| 1818 | ptr_c[i] = (BcDig)(in % 10); |
| 1819 | } |
| 1820 | |
| 1821 | if (carry != 0) c->num[c->len++] = (BcDig) carry; |
| 1822 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1823 | 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] | 1824 | } |
| 1825 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1826 | 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] | 1827 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1828 | ssize_t cmp; |
| 1829 | BcNum *minuend, *subtrahend; |
| 1830 | size_t start; |
| 1831 | bool aneg, bneg, neg; |
| 1832 | |
| 1833 | // Because this function doesn't need to use scale (per the bc spec), |
| 1834 | // I am hijacking it to say whether it's doing an add or a subtract. |
| 1835 | |
| 1836 | if (a->len == 0) { |
| 1837 | bc_num_copy(c, b); |
| 1838 | if (sub && c->len) c->neg = !c->neg; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1839 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1840 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1841 | if (b->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1842 | bc_num_copy(c, a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1843 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1844 | } |
| 1845 | |
| 1846 | aneg = a->neg; |
| 1847 | bneg = b->neg; |
| 1848 | a->neg = b->neg = false; |
| 1849 | |
| 1850 | cmp = bc_num_cmp(a, b); |
| 1851 | |
| 1852 | a->neg = aneg; |
| 1853 | b->neg = bneg; |
| 1854 | |
| 1855 | if (cmp == 0) { |
| 1856 | bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx)); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1857 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1858 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1859 | if (cmp > 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1860 | neg = a->neg; |
| 1861 | minuend = a; |
| 1862 | subtrahend = b; |
| 1863 | } |
| 1864 | else { |
| 1865 | neg = b->neg; |
| 1866 | if (sub) neg = !neg; |
| 1867 | minuend = b; |
| 1868 | subtrahend = a; |
| 1869 | } |
| 1870 | |
| 1871 | bc_num_copy(c, minuend); |
| 1872 | c->neg = neg; |
| 1873 | |
| 1874 | if (c->rdx < subtrahend->rdx) { |
| 1875 | bc_num_extend(c, subtrahend->rdx - c->rdx); |
| 1876 | start = 0; |
| 1877 | } |
| 1878 | else |
| 1879 | start = c->rdx - subtrahend->rdx; |
| 1880 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1881 | bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1882 | |
| 1883 | bc_num_clean(c); |
| 1884 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1885 | 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] | 1886 | } |
| 1887 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1888 | 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] | 1889 | BcNum *restrict c) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1890 | #if ERRORS_ARE_FATAL |
| 1891 | # define zbc_num_k(...) (zbc_num_k(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 1892 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1893 | { |
| 1894 | BcStatus s; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1895 | 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] | 1896 | BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1897 | bool aone; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1898 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1899 | if (a->len == 0 || b->len == 0) { |
| 1900 | bc_num_zero(c); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1901 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1902 | } |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1903 | aone = BC_NUM_ONE(a); |
| 1904 | if (aone || BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1905 | bc_num_copy(c, aone ? b : a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1906 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1907 | } |
| 1908 | |
| 1909 | if (a->len + b->len < BC_NUM_KARATSUBA_LEN || |
| 1910 | a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN) |
| 1911 | { |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1912 | size_t i, j, len; |
Denys Vlasenko | b3cb901 | 2018-12-05 19:05:32 +0100 | [diff] [blame] | 1913 | unsigned carry; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1914 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1915 | bc_num_expand(c, a->len + b->len + 1); |
| 1916 | |
| 1917 | memset(c->num, 0, sizeof(BcDig) * c->cap); |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1918 | c->len = len = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1919 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1920 | for (i = 0; i < b->len; ++i) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1921 | |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1922 | carry = 0; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1923 | for (j = 0; j < a->len; ++j) { |
Denys Vlasenko | b3cb901 | 2018-12-05 19:05:32 +0100 | [diff] [blame] | 1924 | unsigned in = c->num[i + j]; |
| 1925 | in += ((unsigned) a->num[j]) * ((unsigned) b->num[i]) + carry; |
| 1926 | // note: compilers prefer _unsigned_ div/const |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1927 | carry = in / 10; |
| 1928 | c->num[i + j] = (BcDig)(in % 10); |
| 1929 | } |
| 1930 | |
| 1931 | c->num[i + j] += (BcDig) carry; |
| 1932 | len = BC_MAX(len, i + j + !!carry); |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 1933 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1934 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 1935 | // a=2^1000000 |
| 1936 | // a*a <- without check below, this will not be interruptible |
| 1937 | if (G_interrupt) return BC_STATUS_FAILURE; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1938 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1939 | } |
| 1940 | |
| 1941 | c->len = len; |
| 1942 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1943 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1944 | } |
| 1945 | |
| 1946 | bc_num_init(&l1, max); |
| 1947 | bc_num_init(&h1, max); |
| 1948 | bc_num_init(&l2, max); |
| 1949 | bc_num_init(&h2, max); |
| 1950 | bc_num_init(&m1, max); |
| 1951 | bc_num_init(&m2, max); |
| 1952 | bc_num_init(&z0, max); |
| 1953 | bc_num_init(&z1, max); |
| 1954 | bc_num_init(&z2, max); |
| 1955 | bc_num_init(&temp, max + max); |
| 1956 | |
| 1957 | bc_num_split(a, max2, &l1, &h1); |
| 1958 | bc_num_split(b, max2, &l2, &h2); |
| 1959 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1960 | s = zbc_num_add(&h1, &l1, &m1, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1961 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1962 | s = zbc_num_add(&h2, &l2, &m2, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1963 | if (s) goto err; |
| 1964 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1965 | s = zbc_num_k(&h1, &h2, &z0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1966 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1967 | s = zbc_num_k(&m1, &m2, &z1); |
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(&l1, &l2, &z2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1970 | if (s) goto err; |
| 1971 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1972 | s = zbc_num_sub(&z1, &z0, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1973 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1974 | s = zbc_num_sub(&temp, &z2, &z1, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1975 | if (s) goto err; |
| 1976 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1977 | s = zbc_num_shift(&z0, max2 * 2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1978 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1979 | s = zbc_num_shift(&z1, max2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1980 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1981 | s = zbc_num_add(&z0, &z1, &temp, 0); |
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(&temp, &z2, c, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1984 | |
| 1985 | err: |
| 1986 | bc_num_free(&temp); |
| 1987 | bc_num_free(&z2); |
| 1988 | bc_num_free(&z1); |
| 1989 | bc_num_free(&z0); |
| 1990 | bc_num_free(&m2); |
| 1991 | bc_num_free(&m1); |
| 1992 | bc_num_free(&h2); |
| 1993 | bc_num_free(&l2); |
| 1994 | bc_num_free(&h1); |
| 1995 | bc_num_free(&l1); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1996 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1997 | } |
| 1998 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1999 | 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] | 2000 | { |
| 2001 | BcStatus s; |
| 2002 | BcNum cpa, cpb; |
| 2003 | size_t maxrdx = BC_MAX(a->rdx, b->rdx); |
| 2004 | |
| 2005 | scale = BC_MAX(scale, a->rdx); |
| 2006 | scale = BC_MAX(scale, b->rdx); |
| 2007 | scale = BC_MIN(a->rdx + b->rdx, scale); |
| 2008 | maxrdx = BC_MAX(maxrdx, scale); |
| 2009 | |
| 2010 | bc_num_init(&cpa, a->len); |
| 2011 | bc_num_init(&cpb, b->len); |
| 2012 | |
| 2013 | bc_num_copy(&cpa, a); |
| 2014 | bc_num_copy(&cpb, b); |
| 2015 | cpa.neg = cpb.neg = false; |
| 2016 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2017 | s = zbc_num_shift(&cpa, maxrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2018 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2019 | s = zbc_num_shift(&cpb, maxrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2020 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2021 | s = zbc_num_k(&cpa, &cpb, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2022 | if (s) goto err; |
| 2023 | |
| 2024 | maxrdx += scale; |
| 2025 | bc_num_expand(c, c->len + maxrdx); |
| 2026 | |
| 2027 | if (c->len < maxrdx) { |
| 2028 | memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig)); |
| 2029 | c->len += maxrdx; |
| 2030 | } |
| 2031 | |
| 2032 | c->rdx = maxrdx; |
| 2033 | bc_num_retireMul(c, scale, a->neg, b->neg); |
| 2034 | |
| 2035 | err: |
| 2036 | bc_num_free(&cpb); |
| 2037 | bc_num_free(&cpa); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2038 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2039 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2040 | #if ERRORS_ARE_FATAL |
| 2041 | # define zbc_num_m(...) (zbc_num_m(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 2042 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2043 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2044 | 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] | 2045 | { |
| 2046 | BcStatus s = BC_STATUS_SUCCESS; |
| 2047 | BcDig *n, *p, q; |
| 2048 | size_t len, end, i; |
| 2049 | BcNum cp; |
| 2050 | bool zero = true; |
| 2051 | |
| 2052 | if (b->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2053 | RETURN_STATUS(bc_error("divide by zero")); |
| 2054 | if (a->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2055 | bc_num_setToZero(c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2056 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2057 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2058 | if (BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2059 | bc_num_copy(c, a); |
| 2060 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2061 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | bc_num_init(&cp, BC_NUM_MREQ(a, b, scale)); |
| 2065 | bc_num_copy(&cp, a); |
| 2066 | len = b->len; |
| 2067 | |
| 2068 | if (len > cp.len) { |
| 2069 | bc_num_expand(&cp, len + 2); |
| 2070 | bc_num_extend(&cp, len - cp.len); |
| 2071 | } |
| 2072 | |
| 2073 | if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx); |
| 2074 | cp.rdx -= b->rdx; |
| 2075 | if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx); |
| 2076 | |
| 2077 | if (b->rdx == b->len) { |
| 2078 | for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1]; |
| 2079 | len -= i - 1; |
| 2080 | } |
| 2081 | |
| 2082 | if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1); |
| 2083 | |
| 2084 | // We want an extra zero in front to make things simpler. |
| 2085 | cp.num[cp.len++] = 0; |
| 2086 | end = cp.len - len; |
| 2087 | |
| 2088 | bc_num_expand(c, cp.len); |
| 2089 | |
| 2090 | bc_num_zero(c); |
| 2091 | memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig)); |
| 2092 | c->rdx = cp.rdx; |
| 2093 | c->len = cp.len; |
| 2094 | p = b->num; |
| 2095 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2096 | for (i = end - 1; !s && i < end; --i) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2097 | n = cp.num + i; |
| 2098 | 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] | 2099 | bc_num_subArrays(n, p, len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2100 | c->num[i] = q; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2101 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 2102 | // a=2^100000 |
| 2103 | // scale=40000 |
| 2104 | // 1/a <- without check below, this will not be interruptible |
| 2105 | if (G_interrupt) { |
| 2106 | s = BC_STATUS_FAILURE; |
| 2107 | break; |
| 2108 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2109 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2110 | } |
| 2111 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2112 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2113 | bc_num_free(&cp); |
| 2114 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2115 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2116 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2117 | #if ERRORS_ARE_FATAL |
| 2118 | # define zbc_num_d(...) (zbc_num_d(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 2119 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2120 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2121 | 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] | 2122 | BcNum *restrict d, size_t scale, size_t ts) |
| 2123 | { |
| 2124 | BcStatus s; |
| 2125 | BcNum temp; |
| 2126 | bool neg; |
| 2127 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2128 | if (b->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2129 | RETURN_STATUS(bc_error("divide by zero")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2130 | |
| 2131 | if (a->len == 0) { |
| 2132 | bc_num_setToZero(d, ts); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2133 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2134 | } |
| 2135 | |
| 2136 | bc_num_init(&temp, d->cap); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2137 | s = zbc_num_d(a, b, c, scale); |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 2138 | if (s) goto err; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2139 | |
| 2140 | if (scale != 0) scale = ts; |
| 2141 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2142 | s = zbc_num_m(c, b, &temp, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2143 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2144 | s = zbc_num_sub(a, &temp, d, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2145 | if (s) goto err; |
| 2146 | |
| 2147 | if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx); |
| 2148 | |
| 2149 | neg = d->neg; |
| 2150 | bc_num_retireMul(d, ts, a->neg, b->neg); |
| 2151 | d->neg = neg; |
| 2152 | |
| 2153 | err: |
| 2154 | bc_num_free(&temp); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2155 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2156 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2157 | #if ERRORS_ARE_FATAL |
| 2158 | # define zbc_num_r(...) (zbc_num_r(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 2159 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2160 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2161 | 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] | 2162 | { |
| 2163 | BcStatus s; |
| 2164 | BcNum c1; |
| 2165 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 2166 | |
| 2167 | bc_num_init(&c1, len); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2168 | s = zbc_num_r(a, b, &c1, c, scale, ts); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2169 | bc_num_free(&c1); |
| 2170 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2171 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2172 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2173 | #if ERRORS_ARE_FATAL |
| 2174 | # define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 2175 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2176 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2177 | 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] | 2178 | { |
| 2179 | BcStatus s = BC_STATUS_SUCCESS; |
| 2180 | BcNum copy; |
| 2181 | unsigned long pow; |
| 2182 | size_t i, powrdx, resrdx; |
| 2183 | bool neg, zero; |
| 2184 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2185 | if (b->rdx) RETURN_STATUS(bc_error("non integer number")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2186 | |
| 2187 | if (b->len == 0) { |
| 2188 | bc_num_one(c); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2189 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2190 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2191 | if (a->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2192 | bc_num_setToZero(c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2193 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2194 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2195 | if (BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2196 | if (!b->neg) |
| 2197 | bc_num_copy(c, a); |
| 2198 | else |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2199 | s = zbc_num_inv(a, c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2200 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2201 | } |
| 2202 | |
| 2203 | neg = b->neg; |
| 2204 | b->neg = false; |
| 2205 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2206 | s = zbc_num_ulong(b, &pow); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2207 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2208 | |
| 2209 | bc_num_init(©, a->len); |
| 2210 | bc_num_copy(©, a); |
| 2211 | |
Denys Vlasenko | 2d615fe | 2018-12-07 16:22:45 +0100 | [diff] [blame] | 2212 | if (!neg) { |
| 2213 | if (a->rdx > scale) |
| 2214 | scale = a->rdx; |
| 2215 | if (a->rdx * pow < scale) |
| 2216 | scale = a->rdx * pow; |
| 2217 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2218 | |
| 2219 | b->neg = neg; |
| 2220 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2221 | for (powrdx = a->rdx; !(pow & 1); pow >>= 1) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2222 | powrdx <<= 1; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2223 | s = zbc_num_mul(©, ©, ©, powrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2224 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2225 | // Not needed: zbc_num_mul() has a check for ^C: |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 2226 | //if (G_interrupt) { |
| 2227 | // s = BC_STATUS_FAILURE; |
| 2228 | // goto err; |
| 2229 | //} |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2230 | } |
| 2231 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2232 | bc_num_copy(c, ©); |
| 2233 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2234 | for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2235 | |
| 2236 | powrdx <<= 1; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2237 | s = zbc_num_mul(©, ©, ©, powrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2238 | if (s) goto err; |
| 2239 | |
| 2240 | if (pow & 1) { |
| 2241 | resrdx += powrdx; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2242 | s = zbc_num_mul(c, ©, c, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2243 | if (s) goto err; |
| 2244 | } |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2245 | // Not needed: zbc_num_mul() has a check for ^C: |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 2246 | //if (G_interrupt) { |
| 2247 | // s = BC_STATUS_FAILURE; |
| 2248 | // goto err; |
| 2249 | //} |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2250 | } |
| 2251 | |
| 2252 | if (neg) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2253 | s = zbc_num_inv(c, c, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2254 | if (s) goto err; |
| 2255 | } |
| 2256 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2257 | if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale); |
| 2258 | |
| 2259 | // We can't use bc_num_clean() here. |
| 2260 | for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i]; |
| 2261 | if (zero) bc_num_setToZero(c, scale); |
| 2262 | |
| 2263 | err: |
| 2264 | bc_num_free(©); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2265 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2266 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2267 | #if ERRORS_ARE_FATAL |
| 2268 | # define zbc_num_p(...) (zbc_num_p(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 2269 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2270 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2271 | 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] | 2272 | BcNumBinaryOp op, size_t req) |
| 2273 | { |
| 2274 | BcStatus s; |
| 2275 | BcNum num2, *ptr_a, *ptr_b; |
| 2276 | bool init = false; |
| 2277 | |
| 2278 | if (c == a) { |
| 2279 | ptr_a = &num2; |
| 2280 | memcpy(ptr_a, c, sizeof(BcNum)); |
| 2281 | init = true; |
| 2282 | } |
| 2283 | else |
| 2284 | ptr_a = a; |
| 2285 | |
| 2286 | if (c == b) { |
| 2287 | ptr_b = &num2; |
| 2288 | if (c != a) { |
| 2289 | memcpy(ptr_b, c, sizeof(BcNum)); |
| 2290 | init = true; |
| 2291 | } |
| 2292 | } |
| 2293 | else |
| 2294 | ptr_b = b; |
| 2295 | |
| 2296 | if (init) |
| 2297 | bc_num_init(c, req); |
| 2298 | else |
| 2299 | bc_num_expand(c, req); |
| 2300 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2301 | #if !ERRORS_ARE_FATAL |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2302 | s = op(ptr_a, ptr_b, c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2303 | #else |
| 2304 | op(ptr_a, ptr_b, c, scale); |
| 2305 | s = BC_STATUS_SUCCESS; |
| 2306 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2307 | |
| 2308 | if (init) bc_num_free(&num2); |
| 2309 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2310 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2311 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2312 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2313 | # define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2314 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2315 | |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2316 | static bool bc_num_strValid(const char *val, size_t base) |
| 2317 | { |
| 2318 | BcDig b; |
| 2319 | bool radix; |
| 2320 | |
| 2321 | b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A'); |
| 2322 | radix = false; |
| 2323 | for (;;) { |
| 2324 | BcDig c = *val++; |
| 2325 | if (c == '\0') |
| 2326 | break; |
| 2327 | if (c == '.') { |
| 2328 | if (radix) return false; |
| 2329 | radix = true; |
| 2330 | continue; |
| 2331 | } |
| 2332 | if (c < '0' || c >= b || (c > '9' && c < 'A')) |
| 2333 | return false; |
| 2334 | } |
| 2335 | return true; |
| 2336 | } |
| 2337 | |
| 2338 | // Note: n is already "bc_num_zero()"ed, |
| 2339 | // leading zeroes in "val" are removed |
| 2340 | static void bc_num_parseDecimal(BcNum *n, const char *val) |
| 2341 | { |
| 2342 | size_t len, i; |
| 2343 | const char *ptr; |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2344 | |
| 2345 | len = strlen(val); |
| 2346 | if (len == 0) |
| 2347 | return; |
| 2348 | |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2349 | bc_num_expand(n, len); |
| 2350 | |
| 2351 | ptr = strchr(val, '.'); |
| 2352 | |
| 2353 | n->rdx = 0; |
| 2354 | if (ptr != NULL) |
| 2355 | n->rdx = (size_t)((val + len) - (ptr + 1)); |
| 2356 | |
Denys Vlasenko | dafbc2c | 2018-12-10 15:38:52 +0100 | [diff] [blame] | 2357 | for (i = 0; val[i]; ++i) { |
| 2358 | if (val[i] != '0' && val[i] != '.') { |
| 2359 | // Not entirely zero value - convert it, and exit |
| 2360 | i = len - 1; |
| 2361 | for (;;) { |
| 2362 | n->num[n->len] = val[i] - '0'; |
| 2363 | ++n->len; |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2364 | skip_dot: |
Denys Vlasenko | dafbc2c | 2018-12-10 15:38:52 +0100 | [diff] [blame] | 2365 | if ((ssize_t)--i == (ssize_t)-1) break; |
| 2366 | if (val[i] == '.') goto skip_dot; |
| 2367 | } |
| 2368 | break; |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2369 | } |
| 2370 | } |
Denys Vlasenko | dafbc2c | 2018-12-10 15:38:52 +0100 | [diff] [blame] | 2371 | // if this is reached, the value is entirely zero |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | // Note: n is already "bc_num_zero()"ed, |
| 2375 | // leading zeroes in "val" are removed |
| 2376 | static void bc_num_parseBase(BcNum *n, const char *val, BcNum *base) |
| 2377 | { |
| 2378 | BcStatus s; |
| 2379 | BcNum temp, mult, result; |
| 2380 | BcDig c = '\0'; |
| 2381 | unsigned long v; |
| 2382 | size_t i, digits; |
| 2383 | |
| 2384 | for (i = 0; ; ++i) { |
| 2385 | if (val[i] == '\0') |
| 2386 | return; |
| 2387 | if (val[i] != '.' && val[i] != '0') |
| 2388 | break; |
| 2389 | } |
| 2390 | |
| 2391 | bc_num_init_DEF_SIZE(&temp); |
| 2392 | bc_num_init_DEF_SIZE(&mult); |
| 2393 | |
| 2394 | for (;;) { |
| 2395 | c = *val++; |
| 2396 | if (c == '\0') goto int_err; |
| 2397 | if (c == '.') break; |
| 2398 | |
| 2399 | v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10); |
| 2400 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2401 | s = zbc_num_mul(n, base, &mult, 0); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2402 | if (s) goto int_err; |
| 2403 | bc_num_ulong2num(&temp, v); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2404 | s = zbc_num_add(&mult, &temp, n, 0); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2405 | if (s) goto int_err; |
| 2406 | } |
| 2407 | |
| 2408 | bc_num_init(&result, base->len); |
| 2409 | //bc_num_zero(&result); - already is |
| 2410 | bc_num_one(&mult); |
| 2411 | |
| 2412 | digits = 0; |
| 2413 | for (;;) { |
| 2414 | c = *val++; |
| 2415 | if (c == '\0') break; |
| 2416 | digits++; |
| 2417 | |
| 2418 | v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10); |
| 2419 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2420 | s = zbc_num_mul(&result, base, &result, 0); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2421 | if (s) goto err; |
| 2422 | bc_num_ulong2num(&temp, v); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2423 | s = zbc_num_add(&result, &temp, &result, 0); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2424 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2425 | s = zbc_num_mul(&mult, base, &mult, 0); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2426 | if (s) goto err; |
| 2427 | } |
| 2428 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2429 | s = zbc_num_div(&result, &mult, &result, digits); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2430 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2431 | s = zbc_num_add(n, &result, n, digits); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2432 | if (s) goto err; |
| 2433 | |
| 2434 | if (n->len != 0) { |
| 2435 | if (n->rdx < digits) bc_num_extend(n, digits - n->rdx); |
| 2436 | } else |
| 2437 | bc_num_zero(n); |
| 2438 | |
| 2439 | err: |
| 2440 | bc_num_free(&result); |
| 2441 | int_err: |
| 2442 | bc_num_free(&mult); |
| 2443 | bc_num_free(&temp); |
| 2444 | } |
| 2445 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2446 | 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] | 2447 | size_t base_t) |
| 2448 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2449 | if (!bc_num_strValid(val, base_t)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2450 | RETURN_STATUS(bc_error("bad number string")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2451 | |
Denys Vlasenko | 4a024c7 | 2018-12-09 13:21:54 +0100 | [diff] [blame] | 2452 | bc_num_zero(n); |
| 2453 | while (*val == '0') val++; |
| 2454 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2455 | if (base_t == 10) |
| 2456 | bc_num_parseDecimal(n, val); |
| 2457 | else |
| 2458 | bc_num_parseBase(n, val, base); |
| 2459 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2460 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2461 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2462 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2463 | # define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2464 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2465 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2466 | 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] | 2467 | { |
| 2468 | BcStatus s; |
| 2469 | BcNum num1, num2, half, f, fprime, *x0, *x1, *temp; |
| 2470 | size_t pow, len, digs, digs1, resrdx, req, times = 0; |
| 2471 | ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX; |
| 2472 | |
| 2473 | req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1; |
| 2474 | bc_num_expand(b, req); |
| 2475 | |
| 2476 | if (a->len == 0) { |
| 2477 | bc_num_setToZero(b, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2478 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2479 | } |
| 2480 | else if (a->neg) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2481 | RETURN_STATUS(bc_error("negative number")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2482 | else if (BC_NUM_ONE(a)) { |
| 2483 | bc_num_one(b); |
| 2484 | bc_num_extend(b, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2485 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2486 | } |
| 2487 | |
| 2488 | scale = BC_MAX(scale, a->rdx) + 1; |
| 2489 | len = a->len + scale; |
| 2490 | |
| 2491 | bc_num_init(&num1, len); |
| 2492 | bc_num_init(&num2, len); |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 2493 | bc_num_init_DEF_SIZE(&half); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2494 | |
| 2495 | bc_num_one(&half); |
| 2496 | half.num[0] = 5; |
| 2497 | half.rdx = 1; |
| 2498 | |
| 2499 | bc_num_init(&f, len); |
| 2500 | bc_num_init(&fprime, len); |
| 2501 | |
| 2502 | x0 = &num1; |
| 2503 | x1 = &num2; |
| 2504 | |
| 2505 | bc_num_one(x0); |
| 2506 | pow = BC_NUM_INT(a); |
| 2507 | |
| 2508 | if (pow) { |
| 2509 | |
| 2510 | if (pow & 1) |
| 2511 | x0->num[0] = 2; |
| 2512 | else |
| 2513 | x0->num[0] = 6; |
| 2514 | |
| 2515 | pow -= 2 - (pow & 1); |
| 2516 | |
| 2517 | bc_num_extend(x0, pow); |
| 2518 | |
| 2519 | // Make sure to move the radix back. |
| 2520 | x0->rdx -= pow; |
| 2521 | } |
| 2522 | |
| 2523 | x0->rdx = digs = digs1 = 0; |
| 2524 | resrdx = scale + 2; |
| 2525 | len = BC_NUM_INT(x0) + resrdx - 1; |
| 2526 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2527 | while (cmp != 0 || digs < len) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2528 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2529 | s = zbc_num_div(a, x0, &f, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2530 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2531 | s = zbc_num_add(x0, &f, &fprime, 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_mul(&fprime, &half, x1, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2534 | if (s) goto err; |
| 2535 | |
| 2536 | cmp = bc_num_cmp(x1, x0); |
| 2537 | digs = x1->len - (unsigned long long) llabs(cmp); |
| 2538 | |
| 2539 | if (cmp == cmp2 && digs == digs1) |
| 2540 | times += 1; |
| 2541 | else |
| 2542 | times = 0; |
| 2543 | |
| 2544 | resrdx += times > 4; |
| 2545 | |
| 2546 | cmp2 = cmp1; |
| 2547 | cmp1 = cmp; |
| 2548 | digs1 = digs; |
| 2549 | |
| 2550 | temp = x0; |
| 2551 | x0 = x1; |
| 2552 | x1 = temp; |
| 2553 | } |
| 2554 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2555 | bc_num_copy(b, x0); |
| 2556 | scale -= 1; |
| 2557 | if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale); |
| 2558 | |
| 2559 | err: |
| 2560 | bc_num_free(&fprime); |
| 2561 | bc_num_free(&f); |
| 2562 | bc_num_free(&half); |
| 2563 | bc_num_free(&num2); |
| 2564 | bc_num_free(&num1); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2565 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2566 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2567 | #if ERRORS_ARE_FATAL |
| 2568 | # define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 2569 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2570 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2571 | 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] | 2572 | size_t scale) |
| 2573 | { |
| 2574 | BcStatus s; |
| 2575 | BcNum num2, *ptr_a; |
| 2576 | bool init = false; |
| 2577 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 2578 | |
| 2579 | if (c == a) { |
| 2580 | memcpy(&num2, c, sizeof(BcNum)); |
| 2581 | ptr_a = &num2; |
| 2582 | bc_num_init(c, len); |
| 2583 | init = true; |
| 2584 | } |
| 2585 | else { |
| 2586 | ptr_a = a; |
| 2587 | bc_num_expand(c, len); |
| 2588 | } |
| 2589 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2590 | s = zbc_num_r(ptr_a, b, c, d, scale, ts); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2591 | |
| 2592 | if (init) bc_num_free(&num2); |
| 2593 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2594 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2595 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2596 | #if ERRORS_ARE_FATAL |
| 2597 | # define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 2598 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2599 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2600 | #if ENABLE_DC |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2601 | 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] | 2602 | { |
| 2603 | BcStatus s; |
| 2604 | BcNum base, exp, two, temp; |
| 2605 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2606 | if (c->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2607 | RETURN_STATUS(bc_error("divide by zero")); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2608 | if (a->rdx || b->rdx || c->rdx) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2609 | RETURN_STATUS(bc_error("non integer number")); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2610 | if (b->neg) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2611 | RETURN_STATUS(bc_error("negative number")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2612 | |
| 2613 | bc_num_expand(d, c->len); |
| 2614 | bc_num_init(&base, c->len); |
| 2615 | bc_num_init(&exp, b->len); |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 2616 | bc_num_init_DEF_SIZE(&two); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2617 | bc_num_init(&temp, b->len); |
| 2618 | |
| 2619 | bc_num_one(&two); |
| 2620 | two.num[0] = 2; |
| 2621 | bc_num_one(d); |
| 2622 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2623 | s = zbc_num_rem(a, c, &base, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2624 | if (s) goto err; |
| 2625 | bc_num_copy(&exp, b); |
| 2626 | |
| 2627 | while (exp.len != 0) { |
| 2628 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2629 | s = zbc_num_divmod(&exp, &two, &exp, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2630 | if (s) goto err; |
| 2631 | |
| 2632 | if (BC_NUM_ONE(&temp)) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2633 | s = zbc_num_mul(d, &base, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2634 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2635 | s = zbc_num_rem(&temp, c, d, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2636 | if (s) goto err; |
| 2637 | } |
| 2638 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2639 | s = zbc_num_mul(&base, &base, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2640 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2641 | s = zbc_num_rem(&temp, c, &base, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2642 | if (s) goto err; |
| 2643 | } |
| 2644 | |
| 2645 | err: |
| 2646 | bc_num_free(&temp); |
| 2647 | bc_num_free(&two); |
| 2648 | bc_num_free(&exp); |
| 2649 | bc_num_free(&base); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2650 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2651 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2652 | #if ERRORS_ARE_FATAL |
| 2653 | # define zbc_num_modexp(...) (zbc_num_modexp(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 2654 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2655 | #endif // ENABLE_DC |
| 2656 | |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 2657 | #if ENABLE_BC |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2658 | static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2659 | { |
| 2660 | BcId a; |
| 2661 | size_t i; |
| 2662 | |
| 2663 | for (i = 0; i < f->autos.len; ++i) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2664 | if (strcmp(name, ((BcId *) bc_vec_item(&f->autos, i))->name) == 0) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2665 | 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] | 2666 | } |
| 2667 | |
| 2668 | a.idx = var; |
| 2669 | a.name = name; |
| 2670 | |
| 2671 | bc_vec_push(&f->autos, &a); |
| 2672 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2673 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2674 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2675 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2676 | # define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2677 | #endif |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 2678 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2679 | |
| 2680 | static void bc_func_init(BcFunc *f) |
| 2681 | { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 2682 | bc_char_vec_init(&f->code); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2683 | bc_vec_init(&f->autos, sizeof(BcId), bc_id_free); |
| 2684 | bc_vec_init(&f->labels, sizeof(size_t), NULL); |
| 2685 | f->nparams = 0; |
| 2686 | } |
| 2687 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2688 | static FAST_FUNC void bc_func_free(void *func) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2689 | { |
| 2690 | BcFunc *f = (BcFunc *) func; |
| 2691 | bc_vec_free(&f->code); |
| 2692 | bc_vec_free(&f->autos); |
| 2693 | bc_vec_free(&f->labels); |
| 2694 | } |
| 2695 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2696 | static void bc_array_expand(BcVec *a, size_t len); |
| 2697 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2698 | static void bc_array_init(BcVec *a, bool nums) |
| 2699 | { |
| 2700 | if (nums) |
| 2701 | bc_vec_init(a, sizeof(BcNum), bc_num_free); |
| 2702 | else |
| 2703 | bc_vec_init(a, sizeof(BcVec), bc_vec_free); |
| 2704 | bc_array_expand(a, 1); |
| 2705 | } |
| 2706 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2707 | static void bc_array_expand(BcVec *a, size_t len) |
| 2708 | { |
| 2709 | BcResultData data; |
| 2710 | |
| 2711 | if (a->size == sizeof(BcNum) && a->dtor == bc_num_free) { |
| 2712 | while (len > a->len) { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 2713 | bc_num_init_DEF_SIZE(&data.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2714 | bc_vec_push(a, &data.n); |
| 2715 | } |
| 2716 | } |
| 2717 | else { |
| 2718 | while (len > a->len) { |
| 2719 | bc_array_init(&data.v, true); |
| 2720 | bc_vec_push(a, &data.v); |
| 2721 | } |
| 2722 | } |
| 2723 | } |
| 2724 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2725 | static void bc_array_copy(BcVec *d, const BcVec *s) |
| 2726 | { |
| 2727 | size_t i; |
| 2728 | |
| 2729 | bc_vec_pop_all(d); |
| 2730 | bc_vec_expand(d, s->cap); |
| 2731 | d->len = s->len; |
| 2732 | |
| 2733 | for (i = 0; i < s->len; ++i) { |
| 2734 | BcNum *dnum = bc_vec_item(d, i), *snum = bc_vec_item(s, i); |
| 2735 | bc_num_init(dnum, snum->len); |
| 2736 | bc_num_copy(dnum, snum); |
| 2737 | } |
| 2738 | } |
| 2739 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2740 | static FAST_FUNC void bc_string_free(void *string) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2741 | { |
| 2742 | free(*((char **) string)); |
| 2743 | } |
| 2744 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2745 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2746 | static void bc_result_copy(BcResult *d, BcResult *src) |
| 2747 | { |
| 2748 | d->t = src->t; |
| 2749 | |
| 2750 | switch (d->t) { |
| 2751 | |
| 2752 | case BC_RESULT_TEMP: |
| 2753 | case BC_RESULT_IBASE: |
| 2754 | case BC_RESULT_SCALE: |
| 2755 | case BC_RESULT_OBASE: |
| 2756 | { |
| 2757 | bc_num_init(&d->d.n, src->d.n.len); |
| 2758 | bc_num_copy(&d->d.n, &src->d.n); |
| 2759 | break; |
| 2760 | } |
| 2761 | |
| 2762 | case BC_RESULT_VAR: |
| 2763 | case BC_RESULT_ARRAY: |
| 2764 | case BC_RESULT_ARRAY_ELEM: |
| 2765 | { |
| 2766 | d->d.id.name = xstrdup(src->d.id.name); |
| 2767 | break; |
| 2768 | } |
| 2769 | |
| 2770 | case BC_RESULT_CONSTANT: |
| 2771 | case BC_RESULT_LAST: |
| 2772 | case BC_RESULT_ONE: |
| 2773 | case BC_RESULT_STR: |
| 2774 | { |
| 2775 | memcpy(&d->d.n, &src->d.n, sizeof(BcNum)); |
| 2776 | break; |
| 2777 | } |
| 2778 | } |
| 2779 | } |
| 2780 | #endif // ENABLE_DC |
| 2781 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2782 | static FAST_FUNC void bc_result_free(void *result) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2783 | { |
| 2784 | BcResult *r = (BcResult *) result; |
| 2785 | |
| 2786 | switch (r->t) { |
| 2787 | |
| 2788 | case BC_RESULT_TEMP: |
| 2789 | case BC_RESULT_IBASE: |
| 2790 | case BC_RESULT_SCALE: |
| 2791 | case BC_RESULT_OBASE: |
| 2792 | { |
| 2793 | bc_num_free(&r->d.n); |
| 2794 | break; |
| 2795 | } |
| 2796 | |
| 2797 | case BC_RESULT_VAR: |
| 2798 | case BC_RESULT_ARRAY: |
| 2799 | case BC_RESULT_ARRAY_ELEM: |
| 2800 | { |
| 2801 | free(r->d.id.name); |
| 2802 | break; |
| 2803 | } |
| 2804 | |
| 2805 | default: |
| 2806 | { |
| 2807 | // Do nothing. |
| 2808 | break; |
| 2809 | } |
| 2810 | } |
| 2811 | } |
| 2812 | |
| 2813 | static void bc_lex_lineComment(BcLex *l) |
| 2814 | { |
| 2815 | l->t.t = BC_LEX_WHITESPACE; |
| 2816 | while (l->i < l->len && l->buf[l->i++] != '\n'); |
| 2817 | --l->i; |
| 2818 | } |
| 2819 | |
| 2820 | static void bc_lex_whitespace(BcLex *l) |
| 2821 | { |
| 2822 | char c; |
| 2823 | l->t.t = BC_LEX_WHITESPACE; |
| 2824 | for (c = l->buf[l->i]; c != '\n' && isspace(c); c = l->buf[++l->i]); |
| 2825 | } |
| 2826 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2827 | static BC_STATUS zbc_lex_number(BcLex *l, char start) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2828 | { |
| 2829 | const char *buf = l->buf + l->i; |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2830 | size_t len, bslashes, i, ccnt; |
| 2831 | bool pt; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2832 | |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2833 | pt = (start == '.'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2834 | l->t.t = BC_LEX_NUMBER; |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2835 | bslashes = 0; |
| 2836 | ccnt = i = 0; |
| 2837 | for (;;) { |
| 2838 | char c = buf[i]; |
| 2839 | if (c == '\0') |
| 2840 | break; |
| 2841 | if (c == '\\' && buf[i + 1] == '\n') { |
| 2842 | i += 2; |
| 2843 | bslashes++; |
| 2844 | continue; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2845 | } |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2846 | if (!isdigit(c) && (c < 'A' || c > 'F')) { |
| 2847 | if (c != '.') break; |
| 2848 | // if '.' was already seen, stop on second one: |
| 2849 | if (pt) break; |
| 2850 | pt = 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2851 | } |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2852 | // buf[i] is one of "0-9A-F." |
| 2853 | i++; |
| 2854 | if (c != '.') |
| 2855 | ccnt = i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2856 | } |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2857 | //i is buf[i] index of the first not-yet-parsed char |
| 2858 | l->i += i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2859 | |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2860 | //ccnt is the number of chars in the number string, excluding possible |
| 2861 | //trailing "." and possible following trailing "\<newline>"(s). |
| 2862 | len = ccnt - bslashes * 2 + 1; // +1 byte for NUL termination |
| 2863 | |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 2864 | // This check makes sense only if size_t is (much) larger than BC_MAX_NUM. |
| 2865 | if (SIZE_MAX > (BC_MAX_NUM | 0xff)) { |
| 2866 | if (len > BC_MAX_NUM) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2867 | 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] | 2868 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2869 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 2870 | bc_vec_pop_all(&l->t.v); |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2871 | bc_vec_expand(&l->t.v, 1 + len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2872 | bc_vec_push(&l->t.v, &start); |
| 2873 | |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2874 | while (ccnt != 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2875 | // If we have hit a backslash, skip it. We don't have |
| 2876 | // to check for a newline because it's guaranteed. |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2877 | if (*buf == '\\') { |
| 2878 | buf += 2; |
| 2879 | ccnt -= 2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2880 | continue; |
| 2881 | } |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2882 | bc_vec_push(&l->t.v, buf); |
| 2883 | buf++; |
| 2884 | ccnt--; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2885 | } |
| 2886 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 2887 | bc_vec_pushZeroByte(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2888 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2889 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2890 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2891 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2892 | # define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 2893 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2894 | |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 2895 | static void bc_lex_name(BcLex *l) |
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 | size_t i; |
| 2898 | const char *buf; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2899 | |
| 2900 | l->t.t = BC_LEX_NAME; |
| 2901 | |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 2902 | i = 0; |
| 2903 | buf = l->buf + l->i - 1; |
| 2904 | for (;;) { |
| 2905 | char c = buf[i]; |
| 2906 | if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break; |
| 2907 | i++; |
| 2908 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2909 | |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 2910 | #if 0 // We do not protect against people with gigabyte-long names |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 2911 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
| 2912 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
| 2913 | if (i > BC_MAX_STRING) |
| 2914 | return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]"); |
| 2915 | } |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 2916 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2917 | bc_vec_string(&l->t.v, i, buf); |
| 2918 | |
| 2919 | // Increment the index. We minus 1 because it has already been incremented. |
| 2920 | l->i += i - 1; |
| 2921 | |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 2922 | //return BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2923 | } |
| 2924 | |
| 2925 | static void bc_lex_init(BcLex *l, BcLexNext next) |
| 2926 | { |
| 2927 | l->next = next; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 2928 | bc_char_vec_init(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2929 | } |
| 2930 | |
| 2931 | static void bc_lex_free(BcLex *l) |
| 2932 | { |
| 2933 | bc_vec_free(&l->t.v); |
| 2934 | } |
| 2935 | |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 2936 | static void bc_lex_file(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2937 | { |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 2938 | G.err_line = l->line = 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2939 | l->newline = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2940 | } |
| 2941 | |
| 2942 | static BcStatus bc_lex_next(BcLex *l) |
| 2943 | { |
| 2944 | BcStatus s; |
| 2945 | |
| 2946 | l->t.last = l->t.t; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2947 | 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] | 2948 | |
| 2949 | l->line += l->newline; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 2950 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2951 | l->t.t = BC_LEX_EOF; |
| 2952 | |
| 2953 | l->newline = (l->i == l->len); |
| 2954 | if (l->newline) return BC_STATUS_SUCCESS; |
| 2955 | |
| 2956 | // Loop until failure or we don't have whitespace. This |
| 2957 | // is so the parser doesn't get inundated with whitespace. |
| 2958 | do { |
| 2959 | s = l->next(l); |
| 2960 | } while (!s && l->t.t == BC_LEX_WHITESPACE); |
| 2961 | |
| 2962 | return s; |
| 2963 | } |
| 2964 | |
| 2965 | static BcStatus bc_lex_text(BcLex *l, const char *text) |
| 2966 | { |
| 2967 | l->buf = text; |
| 2968 | l->i = 0; |
| 2969 | l->len = strlen(text); |
| 2970 | l->t.t = l->t.last = BC_LEX_INVALID; |
| 2971 | return bc_lex_next(l); |
| 2972 | } |
| 2973 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2974 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2975 | static BcStatus bc_lex_identifier(BcLex *l) |
| 2976 | { |
| 2977 | BcStatus s; |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 2978 | unsigned i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2979 | const char *buf = l->buf + l->i - 1; |
| 2980 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 2981 | for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) { |
| 2982 | const char *keyword8 = bc_lex_kws[i].name8; |
| 2983 | unsigned j = 0; |
| 2984 | while (buf[j] != '\0' && buf[j] == keyword8[j]) { |
| 2985 | j++; |
| 2986 | if (j == 8) goto match; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2987 | } |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 2988 | if (keyword8[j] != '\0') |
| 2989 | continue; |
| 2990 | match: |
| 2991 | // buf starts with keyword bc_lex_kws[i] |
| 2992 | l->t.t = BC_LEX_KEY_1st_keyword + i; |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 2993 | if (!bc_lex_kws_POSIX(i)) { |
Denys Vlasenko | 0d7e46b | 2018-12-05 18:31:19 +0100 | [diff] [blame] | 2994 | 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] | 2995 | if (s) return s; |
| 2996 | } |
| 2997 | |
| 2998 | // We minus 1 because the index has already been incremented. |
| 2999 | l->i += j - 1; |
| 3000 | return BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3001 | } |
| 3002 | |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 3003 | bc_lex_name(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3004 | |
Denys Vlasenko | 0d7e46b | 2018-12-05 18:31:19 +0100 | [diff] [blame] | 3005 | if (l->t.v.len > 2) { |
| 3006 | // Prevent this: |
| 3007 | // >>> qwe=1 |
| 3008 | // bc: POSIX only allows one character names; the following is bad: 'qwe=1 |
| 3009 | // ' |
| 3010 | unsigned len = strchrnul(buf, '\n') - buf; |
| 3011 | s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf); |
| 3012 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3013 | |
| 3014 | return s; |
| 3015 | } |
| 3016 | |
| 3017 | static BcStatus bc_lex_string(BcLex *l) |
| 3018 | { |
| 3019 | size_t len, nls = 0, i = l->i; |
| 3020 | char c; |
| 3021 | |
| 3022 | l->t.t = BC_LEX_STR; |
| 3023 | |
| 3024 | for (c = l->buf[i]; c != 0 && c != '"'; c = l->buf[++i]) nls += (c == '\n'); |
| 3025 | |
| 3026 | if (c == '\0') { |
| 3027 | l->i = i; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3028 | return bc_error("string end could not be found"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3029 | } |
| 3030 | |
| 3031 | len = i - l->i; |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 3032 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
| 3033 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
| 3034 | if (len > BC_MAX_STRING) |
| 3035 | return bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"); |
| 3036 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3037 | bc_vec_string(&l->t.v, len, l->buf + l->i); |
| 3038 | |
| 3039 | l->i = i + 1; |
| 3040 | l->line += nls; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3041 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3042 | |
| 3043 | return BC_STATUS_SUCCESS; |
| 3044 | } |
| 3045 | |
| 3046 | static void bc_lex_assign(BcLex *l, BcLexType with, BcLexType without) |
| 3047 | { |
| 3048 | if (l->buf[l->i] == '=') { |
| 3049 | ++l->i; |
| 3050 | l->t.t = with; |
| 3051 | } |
| 3052 | else |
| 3053 | l->t.t = without; |
| 3054 | } |
| 3055 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3056 | static BC_STATUS zbc_lex_comment(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3057 | { |
| 3058 | size_t i, nls = 0; |
| 3059 | const char *buf = l->buf; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3060 | |
| 3061 | l->t.t = BC_LEX_WHITESPACE; |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3062 | i = ++l->i; |
| 3063 | for (;;) { |
| 3064 | char c = buf[i]; |
| 3065 | check_star: |
| 3066 | if (c == '*') { |
| 3067 | c = buf[++i]; |
| 3068 | if (c == '/') |
| 3069 | break; |
| 3070 | goto check_star; |
| 3071 | } |
| 3072 | if (c == '\0') { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3073 | l->i = i; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3074 | RETURN_STATUS(bc_error("comment end could not be found")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3075 | } |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3076 | nls += (c == '\n'); |
| 3077 | i++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3078 | } |
| 3079 | |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3080 | l->i = i + 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3081 | l->line += nls; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3082 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3083 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3084 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3085 | } |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 3086 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3087 | # define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 3088 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3089 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 3090 | static FAST_FUNC BcStatus bc_lex_token(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3091 | { |
| 3092 | BcStatus s = BC_STATUS_SUCCESS; |
| 3093 | char c = l->buf[l->i++], c2; |
| 3094 | |
| 3095 | // This is the workhorse of the lexer. |
| 3096 | switch (c) { |
| 3097 | |
| 3098 | case '\0': |
| 3099 | case '\n': |
| 3100 | { |
| 3101 | l->newline = true; |
| 3102 | l->t.t = !c ? BC_LEX_EOF : BC_LEX_NLINE; |
| 3103 | break; |
| 3104 | } |
| 3105 | |
| 3106 | case '\t': |
| 3107 | case '\v': |
| 3108 | case '\f': |
| 3109 | case '\r': |
| 3110 | case ' ': |
| 3111 | { |
| 3112 | bc_lex_whitespace(l); |
| 3113 | break; |
| 3114 | } |
| 3115 | |
| 3116 | case '!': |
| 3117 | { |
| 3118 | bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT); |
| 3119 | |
| 3120 | if (l->t.t == BC_LEX_OP_BOOL_NOT) { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3121 | s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3122 | if (s) return s; |
| 3123 | } |
| 3124 | |
| 3125 | break; |
| 3126 | } |
| 3127 | |
| 3128 | case '"': |
| 3129 | { |
| 3130 | s = bc_lex_string(l); |
| 3131 | break; |
| 3132 | } |
| 3133 | |
| 3134 | case '#': |
| 3135 | { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3136 | s = bc_POSIX_does_not_allow("'#' script comments"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3137 | if (s) return s; |
| 3138 | |
| 3139 | bc_lex_lineComment(l); |
| 3140 | |
| 3141 | break; |
| 3142 | } |
| 3143 | |
| 3144 | case '%': |
| 3145 | { |
| 3146 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS); |
| 3147 | break; |
| 3148 | } |
| 3149 | |
| 3150 | case '&': |
| 3151 | { |
| 3152 | c2 = l->buf[l->i]; |
| 3153 | if (c2 == '&') { |
| 3154 | |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3155 | s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3156 | if (s) return s; |
| 3157 | |
| 3158 | ++l->i; |
| 3159 | l->t.t = BC_LEX_OP_BOOL_AND; |
| 3160 | } |
| 3161 | else { |
| 3162 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3163 | s = bc_error_bad_character('&'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3164 | } |
| 3165 | |
| 3166 | break; |
| 3167 | } |
| 3168 | |
| 3169 | case '(': |
| 3170 | case ')': |
| 3171 | { |
| 3172 | l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN); |
| 3173 | break; |
| 3174 | } |
| 3175 | |
| 3176 | case '*': |
| 3177 | { |
| 3178 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY); |
| 3179 | break; |
| 3180 | } |
| 3181 | |
| 3182 | case '+': |
| 3183 | { |
| 3184 | c2 = l->buf[l->i]; |
| 3185 | if (c2 == '+') { |
| 3186 | ++l->i; |
| 3187 | l->t.t = BC_LEX_OP_INC; |
| 3188 | } |
| 3189 | else |
| 3190 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS); |
| 3191 | break; |
| 3192 | } |
| 3193 | |
| 3194 | case ',': |
| 3195 | { |
| 3196 | l->t.t = BC_LEX_COMMA; |
| 3197 | break; |
| 3198 | } |
| 3199 | |
| 3200 | case '-': |
| 3201 | { |
| 3202 | c2 = l->buf[l->i]; |
| 3203 | if (c2 == '-') { |
| 3204 | ++l->i; |
| 3205 | l->t.t = BC_LEX_OP_DEC; |
| 3206 | } |
| 3207 | else |
| 3208 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS); |
| 3209 | break; |
| 3210 | } |
| 3211 | |
| 3212 | case '.': |
| 3213 | { |
| 3214 | if (isdigit(l->buf[l->i])) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3215 | s = zbc_lex_number(l, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3216 | else { |
| 3217 | l->t.t = BC_LEX_KEY_LAST; |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3218 | 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] | 3219 | } |
| 3220 | break; |
| 3221 | } |
| 3222 | |
| 3223 | case '/': |
| 3224 | { |
| 3225 | c2 = l->buf[l->i]; |
| 3226 | if (c2 == '*') |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3227 | s = zbc_lex_comment(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3228 | else |
| 3229 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE); |
| 3230 | break; |
| 3231 | } |
| 3232 | |
| 3233 | case '0': |
| 3234 | case '1': |
| 3235 | case '2': |
| 3236 | case '3': |
| 3237 | case '4': |
| 3238 | case '5': |
| 3239 | case '6': |
| 3240 | case '7': |
| 3241 | case '8': |
| 3242 | case '9': |
| 3243 | case 'A': |
| 3244 | case 'B': |
| 3245 | case 'C': |
| 3246 | case 'D': |
| 3247 | case 'E': |
| 3248 | case 'F': |
| 3249 | { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3250 | s = zbc_lex_number(l, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3251 | break; |
| 3252 | } |
| 3253 | |
| 3254 | case ';': |
| 3255 | { |
| 3256 | l->t.t = BC_LEX_SCOLON; |
| 3257 | break; |
| 3258 | } |
| 3259 | |
| 3260 | case '<': |
| 3261 | { |
| 3262 | bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT); |
| 3263 | break; |
| 3264 | } |
| 3265 | |
| 3266 | case '=': |
| 3267 | { |
| 3268 | bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN); |
| 3269 | break; |
| 3270 | } |
| 3271 | |
| 3272 | case '>': |
| 3273 | { |
| 3274 | bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT); |
| 3275 | break; |
| 3276 | } |
| 3277 | |
| 3278 | case '[': |
| 3279 | case ']': |
| 3280 | { |
| 3281 | l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET); |
| 3282 | break; |
| 3283 | } |
| 3284 | |
| 3285 | case '\\': |
| 3286 | { |
| 3287 | if (l->buf[l->i] == '\n') { |
| 3288 | l->t.t = BC_LEX_WHITESPACE; |
| 3289 | ++l->i; |
| 3290 | } |
| 3291 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3292 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3293 | break; |
| 3294 | } |
| 3295 | |
| 3296 | case '^': |
| 3297 | { |
| 3298 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER); |
| 3299 | break; |
| 3300 | } |
| 3301 | |
| 3302 | case 'a': |
| 3303 | case 'b': |
| 3304 | case 'c': |
| 3305 | case 'd': |
| 3306 | case 'e': |
| 3307 | case 'f': |
| 3308 | case 'g': |
| 3309 | case 'h': |
| 3310 | case 'i': |
| 3311 | case 'j': |
| 3312 | case 'k': |
| 3313 | case 'l': |
| 3314 | case 'm': |
| 3315 | case 'n': |
| 3316 | case 'o': |
| 3317 | case 'p': |
| 3318 | case 'q': |
| 3319 | case 'r': |
| 3320 | case 's': |
| 3321 | case 't': |
| 3322 | case 'u': |
| 3323 | case 'v': |
| 3324 | case 'w': |
| 3325 | case 'x': |
| 3326 | case 'y': |
| 3327 | case 'z': |
| 3328 | { |
| 3329 | s = bc_lex_identifier(l); |
| 3330 | break; |
| 3331 | } |
| 3332 | |
| 3333 | case '{': |
| 3334 | case '}': |
| 3335 | { |
| 3336 | l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE); |
| 3337 | break; |
| 3338 | } |
| 3339 | |
| 3340 | case '|': |
| 3341 | { |
| 3342 | c2 = l->buf[l->i]; |
| 3343 | |
| 3344 | if (c2 == '|') { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3345 | s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3346 | if (s) return s; |
| 3347 | |
| 3348 | ++l->i; |
| 3349 | l->t.t = BC_LEX_OP_BOOL_OR; |
| 3350 | } |
| 3351 | else { |
| 3352 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3353 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3354 | } |
| 3355 | |
| 3356 | break; |
| 3357 | } |
| 3358 | |
| 3359 | default: |
| 3360 | { |
| 3361 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3362 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3363 | break; |
| 3364 | } |
| 3365 | } |
| 3366 | |
| 3367 | return s; |
| 3368 | } |
| 3369 | #endif // ENABLE_BC |
| 3370 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 3371 | #if ENABLE_DC |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3372 | static BC_STATUS zdc_lex_register(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3373 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3374 | if (isspace(l->buf[l->i - 1])) { |
| 3375 | bc_lex_whitespace(l); |
| 3376 | ++l->i; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 3377 | if (!G_exreg) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3378 | RETURN_STATUS(bc_error("extended register")); |
| 3379 | bc_lex_name(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3380 | } |
| 3381 | else { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3382 | bc_vec_pop_all(&l->t.v); |
Denys Vlasenko | e55a572 | 2018-12-06 12:47:17 +0100 | [diff] [blame] | 3383 | bc_vec_push(&l->t.v, &l->buf[l->i - 1]); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 3384 | bc_vec_pushZeroByte(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3385 | l->t.t = BC_LEX_NAME; |
| 3386 | } |
| 3387 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3388 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3389 | } |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 3390 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3391 | # define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 3392 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3393 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3394 | static BC_STATUS zdc_lex_string(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3395 | { |
| 3396 | size_t depth = 1, nls = 0, i = l->i; |
| 3397 | char c; |
| 3398 | |
| 3399 | l->t.t = BC_LEX_STR; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3400 | bc_vec_pop_all(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3401 | |
| 3402 | for (c = l->buf[i]; c != 0 && depth; c = l->buf[++i]) { |
| 3403 | |
| 3404 | depth += (c == '[' && (i == l->i || l->buf[i - 1] != '\\')); |
| 3405 | depth -= (c == ']' && (i == l->i || l->buf[i - 1] != '\\')); |
| 3406 | nls += (c == '\n'); |
| 3407 | |
| 3408 | if (depth) bc_vec_push(&l->t.v, &c); |
| 3409 | } |
| 3410 | |
| 3411 | if (c == '\0') { |
| 3412 | l->i = i; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3413 | RETURN_STATUS(bc_error("string end could not be found")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3414 | } |
| 3415 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 3416 | bc_vec_pushZeroByte(&l->t.v); |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 3417 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
| 3418 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
| 3419 | if (i - l->i > BC_MAX_STRING) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3420 | 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] | 3421 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3422 | |
| 3423 | l->i = i; |
| 3424 | l->line += nls; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3425 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3426 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3427 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3428 | } |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 3429 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3430 | # define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 3431 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3432 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 3433 | static FAST_FUNC BcStatus dc_lex_token(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3434 | { |
| 3435 | BcStatus s = BC_STATUS_SUCCESS; |
| 3436 | char c = l->buf[l->i++], c2; |
| 3437 | size_t i; |
| 3438 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3439 | for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) { |
| 3440 | if (l->t.last == dc_lex_regs[i]) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3441 | return zdc_lex_register(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3442 | } |
| 3443 | |
| 3444 | if (c >= '%' && c <= '~' && |
| 3445 | (l->t.t = dc_lex_tokens[(c - '%')]) != BC_LEX_INVALID) |
| 3446 | { |
| 3447 | return s; |
| 3448 | } |
| 3449 | |
| 3450 | // This is the workhorse of the lexer. |
| 3451 | switch (c) { |
| 3452 | |
| 3453 | case '\0': |
| 3454 | { |
| 3455 | l->t.t = BC_LEX_EOF; |
| 3456 | break; |
| 3457 | } |
| 3458 | |
| 3459 | case '\n': |
| 3460 | case '\t': |
| 3461 | case '\v': |
| 3462 | case '\f': |
| 3463 | case '\r': |
| 3464 | case ' ': |
| 3465 | { |
| 3466 | l->newline = (c == '\n'); |
| 3467 | bc_lex_whitespace(l); |
| 3468 | break; |
| 3469 | } |
| 3470 | |
| 3471 | case '!': |
| 3472 | { |
| 3473 | c2 = l->buf[l->i]; |
| 3474 | |
| 3475 | if (c2 == '=') |
| 3476 | l->t.t = BC_LEX_OP_REL_NE; |
| 3477 | else if (c2 == '<') |
| 3478 | l->t.t = BC_LEX_OP_REL_LE; |
| 3479 | else if (c2 == '>') |
| 3480 | l->t.t = BC_LEX_OP_REL_GE; |
| 3481 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3482 | return bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3483 | |
| 3484 | ++l->i; |
| 3485 | break; |
| 3486 | } |
| 3487 | |
| 3488 | case '#': |
| 3489 | { |
| 3490 | bc_lex_lineComment(l); |
| 3491 | break; |
| 3492 | } |
| 3493 | |
| 3494 | case '.': |
| 3495 | { |
| 3496 | if (isdigit(l->buf[l->i])) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3497 | s = zbc_lex_number(l, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3498 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3499 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3500 | break; |
| 3501 | } |
| 3502 | |
| 3503 | case '0': |
| 3504 | case '1': |
| 3505 | case '2': |
| 3506 | case '3': |
| 3507 | case '4': |
| 3508 | case '5': |
| 3509 | case '6': |
| 3510 | case '7': |
| 3511 | case '8': |
| 3512 | case '9': |
| 3513 | case 'A': |
| 3514 | case 'B': |
| 3515 | case 'C': |
| 3516 | case 'D': |
| 3517 | case 'E': |
| 3518 | case 'F': |
| 3519 | { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3520 | s = zbc_lex_number(l, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3521 | break; |
| 3522 | } |
| 3523 | |
| 3524 | case '[': |
| 3525 | { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3526 | s = zdc_lex_string(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3527 | break; |
| 3528 | } |
| 3529 | |
| 3530 | default: |
| 3531 | { |
| 3532 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3533 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3534 | break; |
| 3535 | } |
| 3536 | } |
| 3537 | |
| 3538 | return s; |
| 3539 | } |
| 3540 | #endif // ENABLE_DC |
| 3541 | |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3542 | static void bc_program_addFunc(char *name, size_t *idx); |
| 3543 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3544 | static void bc_parse_addFunc(BcParse *p, char *name, size_t *idx) |
| 3545 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3546 | bc_program_addFunc(name, idx); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 3547 | p->func = bc_program_func(p->fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3548 | } |
| 3549 | |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 3550 | #define bc_parse_push(p, i) bc_vec_pushByte(&(p)->func->code, (char) (i)) |
| 3551 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3552 | static void bc_parse_pushName(BcParse *p, char *name) |
| 3553 | { |
| 3554 | size_t i = 0, len = strlen(name); |
| 3555 | |
| 3556 | for (; i < len; ++i) bc_parse_push(p, name[i]); |
| 3557 | bc_parse_push(p, BC_PARSE_STREND); |
| 3558 | |
| 3559 | free(name); |
| 3560 | } |
| 3561 | |
| 3562 | static void bc_parse_pushIndex(BcParse *p, size_t idx) |
| 3563 | { |
| 3564 | unsigned char amt, i, nums[sizeof(size_t)]; |
| 3565 | |
| 3566 | for (amt = 0; idx; ++amt) { |
| 3567 | nums[amt] = (char) idx; |
| 3568 | idx = (idx & ((unsigned long) ~(UCHAR_MAX))) >> sizeof(char) * CHAR_BIT; |
| 3569 | } |
| 3570 | |
| 3571 | bc_parse_push(p, amt); |
| 3572 | for (i = 0; i < amt; ++i) bc_parse_push(p, nums[i]); |
| 3573 | } |
| 3574 | |
| 3575 | static void bc_parse_number(BcParse *p, BcInst *prev, size_t *nexs) |
| 3576 | { |
| 3577 | char *num = xstrdup(p->l.t.v.v); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3578 | size_t idx = G.prog.consts.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3579 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3580 | bc_vec_push(&G.prog.consts, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3581 | |
| 3582 | bc_parse_push(p, BC_INST_NUM); |
| 3583 | bc_parse_pushIndex(p, idx); |
| 3584 | |
| 3585 | ++(*nexs); |
| 3586 | (*prev) = BC_INST_NUM; |
| 3587 | } |
| 3588 | |
| 3589 | static BcStatus bc_parse_text(BcParse *p, const char *text) |
| 3590 | { |
| 3591 | BcStatus s; |
| 3592 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 3593 | p->func = bc_program_func(p->fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3594 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3595 | if (!text[0] && !BC_PARSE_CAN_EXEC(p)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3596 | p->l.t.t = BC_LEX_INVALID; |
| 3597 | s = p->parse(p); |
| 3598 | if (s) return s; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3599 | if (!BC_PARSE_CAN_EXEC(p)) |
| 3600 | return bc_error("file is not executable"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3601 | } |
| 3602 | |
| 3603 | return bc_lex_text(&p->l, text); |
| 3604 | } |
| 3605 | |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3606 | // Called when parsing or execution detects a failure, |
| 3607 | // resets execution structures. |
| 3608 | static void bc_program_reset(void) |
| 3609 | { |
| 3610 | BcFunc *f; |
| 3611 | BcInstPtr *ip; |
| 3612 | |
| 3613 | bc_vec_npop(&G.prog.stack, G.prog.stack.len - 1); |
| 3614 | bc_vec_pop_all(&G.prog.results); |
| 3615 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 3616 | f = bc_program_func(0); |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3617 | ip = bc_vec_top(&G.prog.stack); |
| 3618 | ip->idx = f->code.len; |
| 3619 | } |
| 3620 | |
Denys Vlasenko | e55a572 | 2018-12-06 12:47:17 +0100 | [diff] [blame] | 3621 | #define bc_parse_updateFunc(p, f) \ |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 3622 | ((p)->func = bc_program_func((p)->fidx = (f))) |
Denys Vlasenko | e55a572 | 2018-12-06 12:47:17 +0100 | [diff] [blame] | 3623 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 3624 | // Called when bc/dc_parse_parse() detects a failure, |
| 3625 | // resets parsing structures. |
| 3626 | static void bc_parse_reset(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3627 | { |
| 3628 | if (p->fidx != BC_PROG_MAIN) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3629 | p->func->nparams = 0; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3630 | bc_vec_pop_all(&p->func->code); |
| 3631 | bc_vec_pop_all(&p->func->autos); |
| 3632 | bc_vec_pop_all(&p->func->labels); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3633 | |
| 3634 | bc_parse_updateFunc(p, BC_PROG_MAIN); |
| 3635 | } |
| 3636 | |
| 3637 | p->l.i = p->l.len; |
| 3638 | p->l.t.t = BC_LEX_EOF; |
| 3639 | p->auto_part = (p->nbraces = 0); |
| 3640 | |
| 3641 | bc_vec_npop(&p->flags, p->flags.len - 1); |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3642 | bc_vec_pop_all(&p->exits); |
| 3643 | bc_vec_pop_all(&p->conds); |
| 3644 | bc_vec_pop_all(&p->ops); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3645 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 3646 | bc_program_reset(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3647 | } |
| 3648 | |
| 3649 | static void bc_parse_free(BcParse *p) |
| 3650 | { |
| 3651 | bc_vec_free(&p->flags); |
| 3652 | bc_vec_free(&p->exits); |
| 3653 | bc_vec_free(&p->conds); |
| 3654 | bc_vec_free(&p->ops); |
| 3655 | bc_lex_free(&p->l); |
| 3656 | } |
| 3657 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3658 | static void bc_parse_create(BcParse *p, size_t func, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3659 | BcParseParse parse, BcLexNext next) |
| 3660 | { |
| 3661 | memset(p, 0, sizeof(BcParse)); |
| 3662 | |
| 3663 | bc_lex_init(&p->l, next); |
| 3664 | bc_vec_init(&p->flags, sizeof(uint8_t), NULL); |
| 3665 | bc_vec_init(&p->exits, sizeof(BcInstPtr), NULL); |
| 3666 | bc_vec_init(&p->conds, sizeof(size_t), NULL); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 3667 | bc_vec_pushZeroByte(&p->flags); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3668 | bc_vec_init(&p->ops, sizeof(BcLexType), NULL); |
| 3669 | |
| 3670 | p->parse = parse; |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 3671 | // p->auto_part = p->nbraces = 0; - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3672 | bc_parse_updateFunc(p, func); |
| 3673 | } |
| 3674 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 3675 | #if ENABLE_BC |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 3676 | |
| 3677 | #define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops))) |
| 3678 | #define BC_PARSE_LEAF(p, rparen) \ |
| 3679 | (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \ |
| 3680 | (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST) |
| 3681 | |
| 3682 | // We can calculate the conversion between tokens and exprs by subtracting the |
| 3683 | // position of the first operator in the lex enum and adding the position of the |
| 3684 | // first in the expr enum. Note: This only works for binary operators. |
| 3685 | #define BC_PARSE_TOKEN_INST(t) ((char) ((t) -BC_LEX_NEG + BC_INST_NEG)) |
| 3686 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3687 | static BcStatus bc_parse_else(BcParse *p); |
| 3688 | static BcStatus bc_parse_stmt(BcParse *p); |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 3689 | static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next); |
Denys Vlasenko | 050b0fe | 2018-12-05 22:40:44 +0100 | [diff] [blame] | 3690 | 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] | 3691 | |
| 3692 | static BcStatus bc_parse_operator(BcParse *p, BcLexType type, size_t start, |
| 3693 | size_t *nexprs, bool next) |
| 3694 | { |
| 3695 | BcStatus s = BC_STATUS_SUCCESS; |
| 3696 | BcLexType t; |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 3697 | char l, r = bc_parse_op_PREC(type - BC_LEX_OP_INC); |
| 3698 | bool left = bc_parse_op_LEFT(type - BC_LEX_OP_INC); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3699 | |
| 3700 | while (p->ops.len > start) { |
| 3701 | |
| 3702 | t = BC_PARSE_TOP_OP(p); |
| 3703 | if (t == BC_LEX_LPAREN) break; |
| 3704 | |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 3705 | l = bc_parse_op_PREC(t - BC_LEX_OP_INC); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3706 | if (l >= r && (l != r || !left)) break; |
| 3707 | |
| 3708 | bc_parse_push(p, BC_PARSE_TOKEN_INST(t)); |
| 3709 | bc_vec_pop(&p->ops); |
| 3710 | *nexprs -= t != BC_LEX_OP_BOOL_NOT && t != BC_LEX_NEG; |
| 3711 | } |
| 3712 | |
| 3713 | bc_vec_push(&p->ops, &type); |
| 3714 | if (next) s = bc_lex_next(&p->l); |
| 3715 | |
| 3716 | return s; |
| 3717 | } |
| 3718 | |
| 3719 | static BcStatus bc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs) |
| 3720 | { |
| 3721 | BcLexType top; |
| 3722 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3723 | if (p->ops.len <= ops_bgn) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3724 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3725 | top = BC_PARSE_TOP_OP(p); |
| 3726 | |
| 3727 | while (top != BC_LEX_LPAREN) { |
| 3728 | |
| 3729 | bc_parse_push(p, BC_PARSE_TOKEN_INST(top)); |
| 3730 | |
| 3731 | bc_vec_pop(&p->ops); |
| 3732 | *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG; |
| 3733 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3734 | if (p->ops.len <= ops_bgn) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3735 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3736 | top = BC_PARSE_TOP_OP(p); |
| 3737 | } |
| 3738 | |
| 3739 | bc_vec_pop(&p->ops); |
| 3740 | |
| 3741 | return bc_lex_next(&p->l); |
| 3742 | } |
| 3743 | |
| 3744 | static BcStatus bc_parse_params(BcParse *p, uint8_t flags) |
| 3745 | { |
| 3746 | BcStatus s; |
| 3747 | bool comma = false; |
| 3748 | size_t nparams; |
| 3749 | |
| 3750 | s = bc_lex_next(&p->l); |
| 3751 | if (s) return s; |
| 3752 | |
| 3753 | for (nparams = 0; p->l.t.t != BC_LEX_RPAREN; ++nparams) { |
| 3754 | |
| 3755 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; |
| 3756 | s = bc_parse_expr(p, flags, bc_parse_next_param); |
| 3757 | if (s) return s; |
| 3758 | |
| 3759 | comma = p->l.t.t == BC_LEX_COMMA; |
| 3760 | if (comma) { |
| 3761 | s = bc_lex_next(&p->l); |
| 3762 | if (s) return s; |
| 3763 | } |
| 3764 | } |
| 3765 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3766 | if (comma) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3767 | bc_parse_push(p, BC_INST_CALL); |
| 3768 | bc_parse_pushIndex(p, nparams); |
| 3769 | |
| 3770 | return BC_STATUS_SUCCESS; |
| 3771 | } |
| 3772 | |
| 3773 | static BcStatus bc_parse_call(BcParse *p, char *name, uint8_t flags) |
| 3774 | { |
| 3775 | BcStatus s; |
| 3776 | BcId entry, *entry_ptr; |
| 3777 | size_t idx; |
| 3778 | |
| 3779 | entry.name = name; |
| 3780 | |
| 3781 | s = bc_parse_params(p, flags); |
| 3782 | if (s) goto err; |
| 3783 | |
| 3784 | if (p->l.t.t != BC_LEX_RPAREN) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3785 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3786 | goto err; |
| 3787 | } |
| 3788 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3789 | idx = bc_map_index(&G.prog.fn_map, &entry); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3790 | |
| 3791 | if (idx == BC_VEC_INVALID_IDX) { |
| 3792 | name = xstrdup(entry.name); |
| 3793 | bc_parse_addFunc(p, name, &idx); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3794 | idx = bc_map_index(&G.prog.fn_map, &entry); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3795 | free(entry.name); |
| 3796 | } |
| 3797 | else |
| 3798 | free(name); |
| 3799 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3800 | entry_ptr = bc_vec_item(&G.prog.fn_map, idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3801 | bc_parse_pushIndex(p, entry_ptr->idx); |
| 3802 | |
| 3803 | return bc_lex_next(&p->l); |
| 3804 | |
| 3805 | err: |
| 3806 | free(name); |
| 3807 | return s; |
| 3808 | } |
| 3809 | |
| 3810 | static BcStatus bc_parse_name(BcParse *p, BcInst *type, uint8_t flags) |
| 3811 | { |
| 3812 | BcStatus s; |
| 3813 | char *name; |
| 3814 | |
| 3815 | name = xstrdup(p->l.t.v.v); |
| 3816 | s = bc_lex_next(&p->l); |
| 3817 | if (s) goto err; |
| 3818 | |
| 3819 | if (p->l.t.t == BC_LEX_LBRACKET) { |
| 3820 | |
| 3821 | s = bc_lex_next(&p->l); |
| 3822 | if (s) goto err; |
| 3823 | |
| 3824 | if (p->l.t.t == BC_LEX_RBRACKET) { |
| 3825 | |
| 3826 | if (!(flags & BC_PARSE_ARRAY)) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3827 | s = bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3828 | goto err; |
| 3829 | } |
| 3830 | |
| 3831 | *type = BC_INST_ARRAY; |
| 3832 | } |
| 3833 | else { |
| 3834 | |
| 3835 | *type = BC_INST_ARRAY_ELEM; |
| 3836 | |
| 3837 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); |
| 3838 | s = bc_parse_expr(p, flags, bc_parse_next_elem); |
| 3839 | if (s) goto err; |
| 3840 | } |
| 3841 | |
| 3842 | s = bc_lex_next(&p->l); |
| 3843 | if (s) goto err; |
| 3844 | bc_parse_push(p, *type); |
| 3845 | bc_parse_pushName(p, name); |
| 3846 | } |
| 3847 | else if (p->l.t.t == BC_LEX_LPAREN) { |
| 3848 | |
| 3849 | if (flags & BC_PARSE_NOCALL) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3850 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3851 | goto err; |
| 3852 | } |
| 3853 | |
| 3854 | *type = BC_INST_CALL; |
| 3855 | s = bc_parse_call(p, name, flags); |
| 3856 | } |
| 3857 | else { |
| 3858 | *type = BC_INST_VAR; |
| 3859 | bc_parse_push(p, BC_INST_VAR); |
| 3860 | bc_parse_pushName(p, name); |
| 3861 | } |
| 3862 | |
| 3863 | return s; |
| 3864 | |
| 3865 | err: |
| 3866 | free(name); |
| 3867 | return s; |
| 3868 | } |
| 3869 | |
| 3870 | static BcStatus bc_parse_read(BcParse *p) |
| 3871 | { |
| 3872 | BcStatus s; |
| 3873 | |
| 3874 | s = bc_lex_next(&p->l); |
| 3875 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3876 | 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] | 3877 | |
| 3878 | s = bc_lex_next(&p->l); |
| 3879 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3880 | 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] | 3881 | |
| 3882 | bc_parse_push(p, BC_INST_READ); |
| 3883 | |
| 3884 | return bc_lex_next(&p->l); |
| 3885 | } |
| 3886 | |
| 3887 | static BcStatus bc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags, |
| 3888 | BcInst *prev) |
| 3889 | { |
| 3890 | BcStatus s; |
| 3891 | |
| 3892 | s = bc_lex_next(&p->l); |
| 3893 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3894 | 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] | 3895 | |
| 3896 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; |
| 3897 | |
| 3898 | s = bc_lex_next(&p->l); |
| 3899 | if (s) return s; |
| 3900 | |
| 3901 | s = bc_parse_expr(p, flags, bc_parse_next_rel); |
| 3902 | if (s) return s; |
| 3903 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3904 | 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] | 3905 | |
| 3906 | *prev = (type == BC_LEX_KEY_LENGTH) ? BC_INST_LENGTH : BC_INST_SQRT; |
| 3907 | bc_parse_push(p, *prev); |
| 3908 | |
| 3909 | return bc_lex_next(&p->l); |
| 3910 | } |
| 3911 | |
| 3912 | static BcStatus bc_parse_scale(BcParse *p, BcInst *type, uint8_t flags) |
| 3913 | { |
| 3914 | BcStatus s; |
| 3915 | |
| 3916 | s = bc_lex_next(&p->l); |
| 3917 | if (s) return s; |
| 3918 | |
| 3919 | if (p->l.t.t != BC_LEX_LPAREN) { |
| 3920 | *type = BC_INST_SCALE; |
| 3921 | bc_parse_push(p, BC_INST_SCALE); |
| 3922 | return BC_STATUS_SUCCESS; |
| 3923 | } |
| 3924 | |
| 3925 | *type = BC_INST_SCALE_FUNC; |
| 3926 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); |
| 3927 | |
| 3928 | s = bc_lex_next(&p->l); |
| 3929 | if (s) return s; |
| 3930 | |
| 3931 | s = bc_parse_expr(p, flags, bc_parse_next_rel); |
| 3932 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3933 | 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] | 3934 | bc_parse_push(p, BC_INST_SCALE_FUNC); |
| 3935 | |
| 3936 | return bc_lex_next(&p->l); |
| 3937 | } |
| 3938 | |
| 3939 | static BcStatus bc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr, |
| 3940 | size_t *nexprs, uint8_t flags) |
| 3941 | { |
| 3942 | BcStatus s; |
| 3943 | BcLexType type; |
| 3944 | char inst; |
| 3945 | BcInst etype = *prev; |
| 3946 | |
| 3947 | if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM || |
| 3948 | etype == BC_INST_SCALE || etype == BC_INST_LAST || |
| 3949 | etype == BC_INST_IBASE || etype == BC_INST_OBASE) |
| 3950 | { |
| 3951 | *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC); |
| 3952 | bc_parse_push(p, inst); |
| 3953 | s = bc_lex_next(&p->l); |
| 3954 | } |
| 3955 | else { |
| 3956 | |
| 3957 | *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC); |
| 3958 | *paren_expr = true; |
| 3959 | |
| 3960 | s = bc_lex_next(&p->l); |
| 3961 | if (s) return s; |
| 3962 | type = p->l.t.t; |
| 3963 | |
| 3964 | // Because we parse the next part of the expression |
| 3965 | // right here, we need to increment this. |
| 3966 | *nexprs = *nexprs + 1; |
| 3967 | |
| 3968 | switch (type) { |
| 3969 | |
| 3970 | case BC_LEX_NAME: |
| 3971 | { |
| 3972 | s = bc_parse_name(p, prev, flags | BC_PARSE_NOCALL); |
| 3973 | break; |
| 3974 | } |
| 3975 | |
| 3976 | case BC_LEX_KEY_IBASE: |
| 3977 | case BC_LEX_KEY_LAST: |
| 3978 | case BC_LEX_KEY_OBASE: |
| 3979 | { |
| 3980 | bc_parse_push(p, type - BC_LEX_KEY_IBASE + BC_INST_IBASE); |
| 3981 | s = bc_lex_next(&p->l); |
| 3982 | break; |
| 3983 | } |
| 3984 | |
| 3985 | case BC_LEX_KEY_SCALE: |
| 3986 | { |
| 3987 | s = bc_lex_next(&p->l); |
| 3988 | if (s) return s; |
| 3989 | if (p->l.t.t == BC_LEX_LPAREN) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3990 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3991 | else |
| 3992 | bc_parse_push(p, BC_INST_SCALE); |
| 3993 | break; |
| 3994 | } |
| 3995 | |
| 3996 | default: |
| 3997 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3998 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3999 | break; |
| 4000 | } |
| 4001 | } |
| 4002 | |
| 4003 | if (!s) bc_parse_push(p, inst); |
| 4004 | } |
| 4005 | |
| 4006 | return s; |
| 4007 | } |
| 4008 | |
| 4009 | static BcStatus bc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn, |
| 4010 | bool rparen, size_t *nexprs) |
| 4011 | { |
| 4012 | BcStatus s; |
| 4013 | BcLexType type; |
| 4014 | BcInst etype = *prev; |
| 4015 | |
| 4016 | s = bc_lex_next(&p->l); |
| 4017 | if (s) return s; |
| 4018 | |
| 4019 | type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST || |
| 4020 | (etype >= BC_INST_NUM && etype <= BC_INST_SQRT) ? |
| 4021 | BC_LEX_OP_MINUS : |
| 4022 | BC_LEX_NEG; |
| 4023 | *prev = BC_PARSE_TOKEN_INST(type); |
| 4024 | |
| 4025 | // We can just push onto the op stack because this is the largest |
| 4026 | // precedence operator that gets pushed. Inc/dec does not. |
| 4027 | if (type != BC_LEX_OP_MINUS) |
| 4028 | bc_vec_push(&p->ops, &type); |
| 4029 | else |
| 4030 | s = bc_parse_operator(p, type, ops_bgn, nexprs, false); |
| 4031 | |
| 4032 | return s; |
| 4033 | } |
| 4034 | |
| 4035 | static BcStatus bc_parse_string(BcParse *p, char inst) |
| 4036 | { |
| 4037 | char *str = xstrdup(p->l.t.v.v); |
| 4038 | |
| 4039 | bc_parse_push(p, BC_INST_STR); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 4040 | bc_parse_pushIndex(p, G.prog.strs.len); |
| 4041 | bc_vec_push(&G.prog.strs, &str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4042 | bc_parse_push(p, inst); |
| 4043 | |
| 4044 | return bc_lex_next(&p->l); |
| 4045 | } |
| 4046 | |
| 4047 | static BcStatus bc_parse_print(BcParse *p) |
| 4048 | { |
| 4049 | BcStatus s; |
| 4050 | BcLexType type; |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4051 | bool comma; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4052 | |
| 4053 | s = bc_lex_next(&p->l); |
| 4054 | if (s) return s; |
| 4055 | |
| 4056 | type = p->l.t.t; |
| 4057 | |
| 4058 | if (type == BC_LEX_SCOLON || type == BC_LEX_NLINE) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4059 | return bc_error("bad print statement"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4060 | |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4061 | comma = false; |
| 4062 | while (type != BC_LEX_SCOLON && type != BC_LEX_NLINE) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4063 | |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4064 | if (type == BC_LEX_STR) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4065 | s = bc_parse_string(p, BC_INST_PRINT_POP); |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4066 | if (s) return s; |
| 4067 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4068 | s = bc_parse_expr(p, 0, bc_parse_next_print); |
| 4069 | if (s) return s; |
| 4070 | bc_parse_push(p, BC_INST_PRINT_POP); |
| 4071 | } |
| 4072 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4073 | comma = p->l.t.t == BC_LEX_COMMA; |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4074 | if (comma) { |
| 4075 | s = bc_lex_next(&p->l); |
| 4076 | if (s) return s; |
| 4077 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4078 | type = p->l.t.t; |
| 4079 | } |
| 4080 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4081 | if (comma) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4082 | |
| 4083 | return bc_lex_next(&p->l); |
| 4084 | } |
| 4085 | |
| 4086 | static BcStatus bc_parse_return(BcParse *p) |
| 4087 | { |
| 4088 | BcStatus s; |
| 4089 | BcLexType t; |
| 4090 | bool paren; |
| 4091 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4092 | if (!BC_PARSE_FUNC(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4093 | |
| 4094 | s = bc_lex_next(&p->l); |
| 4095 | if (s) return s; |
| 4096 | |
| 4097 | t = p->l.t.t; |
| 4098 | paren = t == BC_LEX_LPAREN; |
| 4099 | |
| 4100 | if (t == BC_LEX_NLINE || t == BC_LEX_SCOLON) |
| 4101 | bc_parse_push(p, BC_INST_RET0); |
| 4102 | else { |
| 4103 | |
Denys Vlasenko | 050b0fe | 2018-12-05 22:40:44 +0100 | [diff] [blame] | 4104 | s = bc_parse_expr_empty_ok(p, 0, bc_parse_next_expr); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4105 | if (s == BC_STATUS_PARSE_EMPTY_EXP) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4106 | bc_parse_push(p, BC_INST_RET0); |
| 4107 | s = bc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4108 | } |
Denys Vlasenko | 452df92 | 2018-12-05 20:28:26 +0100 | [diff] [blame] | 4109 | if (s) return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4110 | |
| 4111 | if (!paren || p->l.t.last != BC_LEX_RPAREN) { |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 4112 | s = bc_POSIX_requires("parentheses around return expressions"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4113 | if (s) return s; |
| 4114 | } |
| 4115 | |
| 4116 | bc_parse_push(p, BC_INST_RET); |
| 4117 | } |
| 4118 | |
| 4119 | return s; |
| 4120 | } |
| 4121 | |
| 4122 | static BcStatus bc_parse_endBody(BcParse *p, bool brace) |
| 4123 | { |
| 4124 | BcStatus s = BC_STATUS_SUCCESS; |
| 4125 | |
| 4126 | if (p->flags.len <= 1 || (brace && p->nbraces == 0)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4127 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4128 | |
| 4129 | if (brace) { |
| 4130 | |
| 4131 | if (p->l.t.t == BC_LEX_RBRACE) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4132 | if (!p->nbraces) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4133 | --p->nbraces; |
| 4134 | s = bc_lex_next(&p->l); |
| 4135 | if (s) return s; |
| 4136 | } |
| 4137 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4138 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4139 | } |
| 4140 | |
| 4141 | if (BC_PARSE_IF(p)) { |
| 4142 | |
| 4143 | uint8_t *flag_ptr; |
| 4144 | |
| 4145 | while (p->l.t.t == BC_LEX_NLINE) { |
| 4146 | s = bc_lex_next(&p->l); |
| 4147 | if (s) return s; |
| 4148 | } |
| 4149 | |
| 4150 | bc_vec_pop(&p->flags); |
| 4151 | |
| 4152 | flag_ptr = BC_PARSE_TOP_FLAG_PTR(p); |
| 4153 | *flag_ptr = (*flag_ptr | BC_PARSE_FLAG_IF_END); |
| 4154 | |
| 4155 | if (p->l.t.t == BC_LEX_KEY_ELSE) s = bc_parse_else(p); |
| 4156 | } |
| 4157 | else if (BC_PARSE_ELSE(p)) { |
| 4158 | |
| 4159 | BcInstPtr *ip; |
| 4160 | size_t *label; |
| 4161 | |
| 4162 | bc_vec_pop(&p->flags); |
| 4163 | |
| 4164 | ip = bc_vec_top(&p->exits); |
| 4165 | label = bc_vec_item(&p->func->labels, ip->idx); |
| 4166 | *label = p->func->code.len; |
| 4167 | |
| 4168 | bc_vec_pop(&p->exits); |
| 4169 | } |
| 4170 | else if (BC_PARSE_FUNC_INNER(p)) { |
| 4171 | bc_parse_push(p, BC_INST_RET0); |
| 4172 | bc_parse_updateFunc(p, BC_PROG_MAIN); |
| 4173 | bc_vec_pop(&p->flags); |
| 4174 | } |
| 4175 | else { |
| 4176 | |
| 4177 | BcInstPtr *ip = bc_vec_top(&p->exits); |
| 4178 | size_t *label = bc_vec_top(&p->conds); |
| 4179 | |
| 4180 | bc_parse_push(p, BC_INST_JUMP); |
| 4181 | bc_parse_pushIndex(p, *label); |
| 4182 | |
| 4183 | label = bc_vec_item(&p->func->labels, ip->idx); |
| 4184 | *label = p->func->code.len; |
| 4185 | |
| 4186 | bc_vec_pop(&p->flags); |
| 4187 | bc_vec_pop(&p->exits); |
| 4188 | bc_vec_pop(&p->conds); |
| 4189 | } |
| 4190 | |
| 4191 | return s; |
| 4192 | } |
| 4193 | |
| 4194 | static void bc_parse_startBody(BcParse *p, uint8_t flags) |
| 4195 | { |
| 4196 | uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p); |
| 4197 | flags |= (*flag_ptr & (BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_LOOP)); |
| 4198 | flags |= BC_PARSE_FLAG_BODY; |
| 4199 | bc_vec_push(&p->flags, &flags); |
| 4200 | } |
| 4201 | |
| 4202 | static void bc_parse_noElse(BcParse *p) |
| 4203 | { |
| 4204 | BcInstPtr *ip; |
| 4205 | size_t *label; |
| 4206 | uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p); |
| 4207 | |
| 4208 | *flag_ptr = (*flag_ptr & ~(BC_PARSE_FLAG_IF_END)); |
| 4209 | |
| 4210 | ip = bc_vec_top(&p->exits); |
| 4211 | label = bc_vec_item(&p->func->labels, ip->idx); |
| 4212 | *label = p->func->code.len; |
| 4213 | |
| 4214 | bc_vec_pop(&p->exits); |
| 4215 | } |
| 4216 | |
| 4217 | static BcStatus bc_parse_if(BcParse *p) |
| 4218 | { |
| 4219 | BcStatus s; |
| 4220 | BcInstPtr ip; |
| 4221 | |
| 4222 | s = bc_lex_next(&p->l); |
| 4223 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4224 | 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] | 4225 | |
| 4226 | s = bc_lex_next(&p->l); |
| 4227 | if (s) return s; |
| 4228 | s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel); |
| 4229 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4230 | 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] | 4231 | |
| 4232 | s = bc_lex_next(&p->l); |
| 4233 | if (s) return s; |
| 4234 | bc_parse_push(p, BC_INST_JUMP_ZERO); |
| 4235 | |
| 4236 | ip.idx = p->func->labels.len; |
| 4237 | ip.func = ip.len = 0; |
| 4238 | |
| 4239 | bc_parse_pushIndex(p, ip.idx); |
| 4240 | bc_vec_push(&p->exits, &ip); |
| 4241 | bc_vec_push(&p->func->labels, &ip.idx); |
| 4242 | bc_parse_startBody(p, BC_PARSE_FLAG_IF); |
| 4243 | |
| 4244 | return BC_STATUS_SUCCESS; |
| 4245 | } |
| 4246 | |
| 4247 | static BcStatus bc_parse_else(BcParse *p) |
| 4248 | { |
| 4249 | BcInstPtr ip; |
| 4250 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4251 | if (!BC_PARSE_IF_END(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4252 | |
| 4253 | ip.idx = p->func->labels.len; |
| 4254 | ip.func = ip.len = 0; |
| 4255 | |
| 4256 | bc_parse_push(p, BC_INST_JUMP); |
| 4257 | bc_parse_pushIndex(p, ip.idx); |
| 4258 | |
| 4259 | bc_parse_noElse(p); |
| 4260 | |
| 4261 | bc_vec_push(&p->exits, &ip); |
| 4262 | bc_vec_push(&p->func->labels, &ip.idx); |
| 4263 | bc_parse_startBody(p, BC_PARSE_FLAG_ELSE); |
| 4264 | |
| 4265 | return bc_lex_next(&p->l); |
| 4266 | } |
| 4267 | |
| 4268 | static BcStatus bc_parse_while(BcParse *p) |
| 4269 | { |
| 4270 | BcStatus s; |
| 4271 | BcInstPtr ip; |
| 4272 | |
| 4273 | s = bc_lex_next(&p->l); |
| 4274 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4275 | 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] | 4276 | s = bc_lex_next(&p->l); |
| 4277 | if (s) return s; |
| 4278 | |
| 4279 | ip.idx = p->func->labels.len; |
| 4280 | |
| 4281 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4282 | bc_vec_push(&p->conds, &ip.idx); |
| 4283 | |
| 4284 | ip.idx = p->func->labels.len; |
| 4285 | ip.func = 1; |
| 4286 | ip.len = 0; |
| 4287 | |
| 4288 | bc_vec_push(&p->exits, &ip); |
| 4289 | bc_vec_push(&p->func->labels, &ip.idx); |
| 4290 | |
| 4291 | s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel); |
| 4292 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4293 | 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] | 4294 | s = bc_lex_next(&p->l); |
| 4295 | if (s) return s; |
| 4296 | |
| 4297 | bc_parse_push(p, BC_INST_JUMP_ZERO); |
| 4298 | bc_parse_pushIndex(p, ip.idx); |
| 4299 | bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER); |
| 4300 | |
| 4301 | return BC_STATUS_SUCCESS; |
| 4302 | } |
| 4303 | |
| 4304 | static BcStatus bc_parse_for(BcParse *p) |
| 4305 | { |
| 4306 | BcStatus s; |
| 4307 | BcInstPtr ip; |
| 4308 | size_t cond_idx, exit_idx, body_idx, update_idx; |
| 4309 | |
| 4310 | s = bc_lex_next(&p->l); |
| 4311 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4312 | 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] | 4313 | s = bc_lex_next(&p->l); |
| 4314 | if (s) return s; |
| 4315 | |
| 4316 | if (p->l.t.t != BC_LEX_SCOLON) |
| 4317 | s = bc_parse_expr(p, 0, bc_parse_next_for); |
| 4318 | else |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 4319 | s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4320 | |
| 4321 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4322 | 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] | 4323 | s = bc_lex_next(&p->l); |
| 4324 | if (s) return s; |
| 4325 | |
| 4326 | cond_idx = p->func->labels.len; |
| 4327 | update_idx = cond_idx + 1; |
| 4328 | body_idx = update_idx + 1; |
| 4329 | exit_idx = body_idx + 1; |
| 4330 | |
| 4331 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4332 | |
| 4333 | if (p->l.t.t != BC_LEX_SCOLON) |
| 4334 | s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_for); |
| 4335 | else |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 4336 | s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4337 | |
| 4338 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4339 | 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] | 4340 | |
| 4341 | s = bc_lex_next(&p->l); |
| 4342 | if (s) return s; |
| 4343 | |
| 4344 | bc_parse_push(p, BC_INST_JUMP_ZERO); |
| 4345 | bc_parse_pushIndex(p, exit_idx); |
| 4346 | bc_parse_push(p, BC_INST_JUMP); |
| 4347 | bc_parse_pushIndex(p, body_idx); |
| 4348 | |
| 4349 | ip.idx = p->func->labels.len; |
| 4350 | |
| 4351 | bc_vec_push(&p->conds, &update_idx); |
| 4352 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4353 | |
| 4354 | if (p->l.t.t != BC_LEX_RPAREN) |
| 4355 | s = bc_parse_expr(p, 0, bc_parse_next_rel); |
| 4356 | else |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 4357 | s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4358 | |
| 4359 | if (s) return s; |
| 4360 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4361 | 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] | 4362 | bc_parse_push(p, BC_INST_JUMP); |
| 4363 | bc_parse_pushIndex(p, cond_idx); |
| 4364 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4365 | |
| 4366 | ip.idx = exit_idx; |
| 4367 | ip.func = 1; |
| 4368 | ip.len = 0; |
| 4369 | |
| 4370 | bc_vec_push(&p->exits, &ip); |
| 4371 | bc_vec_push(&p->func->labels, &ip.idx); |
| 4372 | bc_lex_next(&p->l); |
| 4373 | bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER); |
| 4374 | |
| 4375 | return BC_STATUS_SUCCESS; |
| 4376 | } |
| 4377 | |
| 4378 | static BcStatus bc_parse_loopExit(BcParse *p, BcLexType type) |
| 4379 | { |
| 4380 | BcStatus s; |
| 4381 | size_t i; |
| 4382 | BcInstPtr *ip; |
| 4383 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4384 | if (!BC_PARSE_LOOP(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4385 | |
| 4386 | if (type == BC_LEX_KEY_BREAK) { |
| 4387 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4388 | if (p->exits.len == 0) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4389 | |
| 4390 | i = p->exits.len - 1; |
| 4391 | ip = bc_vec_item(&p->exits, i); |
| 4392 | |
| 4393 | 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] | 4394 | if (i >= p->exits.len && !ip->func) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4395 | |
| 4396 | i = ip->idx; |
| 4397 | } |
| 4398 | else |
| 4399 | i = *((size_t *) bc_vec_top(&p->conds)); |
| 4400 | |
| 4401 | bc_parse_push(p, BC_INST_JUMP); |
| 4402 | bc_parse_pushIndex(p, i); |
| 4403 | |
| 4404 | s = bc_lex_next(&p->l); |
| 4405 | if (s) return s; |
| 4406 | |
| 4407 | 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] | 4408 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4409 | |
| 4410 | return bc_lex_next(&p->l); |
| 4411 | } |
| 4412 | |
| 4413 | static BcStatus bc_parse_func(BcParse *p) |
| 4414 | { |
| 4415 | BcStatus s; |
| 4416 | bool var, comma = false; |
| 4417 | uint8_t flags; |
| 4418 | char *name; |
| 4419 | |
| 4420 | s = bc_lex_next(&p->l); |
| 4421 | if (s) return s; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4422 | if (p->l.t.t != BC_LEX_NAME) |
| 4423 | return bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4424 | |
| 4425 | name = xstrdup(p->l.t.v.v); |
| 4426 | bc_parse_addFunc(p, name, &p->fidx); |
| 4427 | |
| 4428 | s = bc_lex_next(&p->l); |
| 4429 | if (s) return s; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4430 | if (p->l.t.t != BC_LEX_LPAREN) |
| 4431 | return bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4432 | s = bc_lex_next(&p->l); |
| 4433 | if (s) return s; |
| 4434 | |
| 4435 | while (p->l.t.t != BC_LEX_RPAREN) { |
| 4436 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4437 | if (p->l.t.t != BC_LEX_NAME) |
| 4438 | return bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4439 | |
| 4440 | ++p->func->nparams; |
| 4441 | |
| 4442 | name = xstrdup(p->l.t.v.v); |
| 4443 | s = bc_lex_next(&p->l); |
| 4444 | if (s) goto err; |
| 4445 | |
| 4446 | var = p->l.t.t != BC_LEX_LBRACKET; |
| 4447 | |
| 4448 | if (!var) { |
| 4449 | |
| 4450 | s = bc_lex_next(&p->l); |
| 4451 | if (s) goto err; |
| 4452 | |
| 4453 | if (p->l.t.t != BC_LEX_RBRACKET) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4454 | s = bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4455 | goto err; |
| 4456 | } |
| 4457 | |
| 4458 | s = bc_lex_next(&p->l); |
| 4459 | if (s) goto err; |
| 4460 | } |
| 4461 | |
| 4462 | comma = p->l.t.t == BC_LEX_COMMA; |
| 4463 | if (comma) { |
| 4464 | s = bc_lex_next(&p->l); |
| 4465 | if (s) goto err; |
| 4466 | } |
| 4467 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 4468 | s = zbc_func_insert(p->func, name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4469 | if (s) goto err; |
| 4470 | } |
| 4471 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4472 | if (comma) return bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4473 | |
| 4474 | flags = BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_BODY; |
| 4475 | bc_parse_startBody(p, flags); |
| 4476 | |
| 4477 | s = bc_lex_next(&p->l); |
| 4478 | if (s) return s; |
| 4479 | |
| 4480 | if (p->l.t.t != BC_LEX_LBRACE) |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 4481 | 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] | 4482 | |
| 4483 | return s; |
| 4484 | |
| 4485 | err: |
| 4486 | free(name); |
| 4487 | return s; |
| 4488 | } |
| 4489 | |
| 4490 | static BcStatus bc_parse_auto(BcParse *p) |
| 4491 | { |
| 4492 | BcStatus s; |
| 4493 | bool comma, var, one; |
| 4494 | char *name; |
| 4495 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4496 | if (!p->auto_part) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4497 | s = bc_lex_next(&p->l); |
| 4498 | if (s) return s; |
| 4499 | |
| 4500 | p->auto_part = comma = false; |
| 4501 | one = p->l.t.t == BC_LEX_NAME; |
| 4502 | |
| 4503 | while (p->l.t.t == BC_LEX_NAME) { |
| 4504 | |
| 4505 | name = xstrdup(p->l.t.v.v); |
| 4506 | s = bc_lex_next(&p->l); |
| 4507 | if (s) goto err; |
| 4508 | |
| 4509 | var = p->l.t.t != BC_LEX_LBRACKET; |
| 4510 | if (!var) { |
| 4511 | |
| 4512 | s = bc_lex_next(&p->l); |
| 4513 | if (s) goto err; |
| 4514 | |
| 4515 | if (p->l.t.t != BC_LEX_RBRACKET) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4516 | s = bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4517 | goto err; |
| 4518 | } |
| 4519 | |
| 4520 | s = bc_lex_next(&p->l); |
| 4521 | if (s) goto err; |
| 4522 | } |
| 4523 | |
| 4524 | comma = p->l.t.t == BC_LEX_COMMA; |
| 4525 | if (comma) { |
| 4526 | s = bc_lex_next(&p->l); |
| 4527 | if (s) goto err; |
| 4528 | } |
| 4529 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 4530 | s = zbc_func_insert(p->func, name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4531 | if (s) goto err; |
| 4532 | } |
| 4533 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4534 | if (comma) return bc_error("bad function definition"); |
Denys Vlasenko | abbc433 | 2018-12-03 21:46:41 +0100 | [diff] [blame] | 4535 | if (!one) return bc_error("no auto variable found"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4536 | |
| 4537 | 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] | 4538 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4539 | |
| 4540 | return bc_lex_next(&p->l); |
| 4541 | |
| 4542 | err: |
| 4543 | free(name); |
| 4544 | return s; |
| 4545 | } |
| 4546 | |
| 4547 | static BcStatus bc_parse_body(BcParse *p, bool brace) |
| 4548 | { |
| 4549 | BcStatus s = BC_STATUS_SUCCESS; |
| 4550 | uint8_t *flag_ptr = bc_vec_top(&p->flags); |
| 4551 | |
| 4552 | *flag_ptr &= ~(BC_PARSE_FLAG_BODY); |
| 4553 | |
| 4554 | if (*flag_ptr & BC_PARSE_FLAG_FUNC_INNER) { |
| 4555 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4556 | if (!brace) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4557 | p->auto_part = p->l.t.t != BC_LEX_KEY_AUTO; |
| 4558 | |
| 4559 | if (!p->auto_part) { |
| 4560 | s = bc_parse_auto(p); |
| 4561 | if (s) return s; |
| 4562 | } |
| 4563 | |
| 4564 | if (p->l.t.t == BC_LEX_NLINE) s = bc_lex_next(&p->l); |
| 4565 | } |
| 4566 | else { |
| 4567 | s = bc_parse_stmt(p); |
| 4568 | if (!s && !brace) s = bc_parse_endBody(p, false); |
| 4569 | } |
| 4570 | |
| 4571 | return s; |
| 4572 | } |
| 4573 | |
| 4574 | static BcStatus bc_parse_stmt(BcParse *p) |
| 4575 | { |
| 4576 | BcStatus s = BC_STATUS_SUCCESS; |
| 4577 | |
| 4578 | switch (p->l.t.t) { |
| 4579 | |
| 4580 | case BC_LEX_NLINE: |
| 4581 | { |
| 4582 | return bc_lex_next(&p->l); |
| 4583 | } |
| 4584 | |
| 4585 | case BC_LEX_KEY_ELSE: |
| 4586 | { |
| 4587 | p->auto_part = false; |
| 4588 | break; |
| 4589 | } |
| 4590 | |
| 4591 | case BC_LEX_LBRACE: |
| 4592 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4593 | if (!BC_PARSE_BODY(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4594 | |
| 4595 | ++p->nbraces; |
| 4596 | s = bc_lex_next(&p->l); |
| 4597 | if (s) return s; |
| 4598 | |
| 4599 | return bc_parse_body(p, true); |
| 4600 | } |
| 4601 | |
| 4602 | case BC_LEX_KEY_AUTO: |
| 4603 | { |
| 4604 | return bc_parse_auto(p); |
| 4605 | } |
| 4606 | |
| 4607 | default: |
| 4608 | { |
| 4609 | p->auto_part = false; |
| 4610 | |
| 4611 | if (BC_PARSE_IF_END(p)) { |
| 4612 | bc_parse_noElse(p); |
| 4613 | return BC_STATUS_SUCCESS; |
| 4614 | } |
| 4615 | else if (BC_PARSE_BODY(p)) |
| 4616 | return bc_parse_body(p, false); |
| 4617 | |
| 4618 | break; |
| 4619 | } |
| 4620 | } |
| 4621 | |
| 4622 | switch (p->l.t.t) { |
| 4623 | |
| 4624 | case BC_LEX_OP_INC: |
| 4625 | case BC_LEX_OP_DEC: |
| 4626 | case BC_LEX_OP_MINUS: |
| 4627 | case BC_LEX_OP_BOOL_NOT: |
| 4628 | case BC_LEX_LPAREN: |
| 4629 | case BC_LEX_NAME: |
| 4630 | case BC_LEX_NUMBER: |
| 4631 | case BC_LEX_KEY_IBASE: |
| 4632 | case BC_LEX_KEY_LAST: |
| 4633 | case BC_LEX_KEY_LENGTH: |
| 4634 | case BC_LEX_KEY_OBASE: |
| 4635 | case BC_LEX_KEY_READ: |
| 4636 | case BC_LEX_KEY_SCALE: |
| 4637 | case BC_LEX_KEY_SQRT: |
| 4638 | { |
| 4639 | s = bc_parse_expr(p, BC_PARSE_PRINT, bc_parse_next_expr); |
| 4640 | break; |
| 4641 | } |
| 4642 | |
| 4643 | case BC_LEX_KEY_ELSE: |
| 4644 | { |
| 4645 | s = bc_parse_else(p); |
| 4646 | break; |
| 4647 | } |
| 4648 | |
| 4649 | case BC_LEX_SCOLON: |
| 4650 | { |
| 4651 | while (!s && p->l.t.t == BC_LEX_SCOLON) s = bc_lex_next(&p->l); |
| 4652 | break; |
| 4653 | } |
| 4654 | |
| 4655 | case BC_LEX_RBRACE: |
| 4656 | { |
| 4657 | s = bc_parse_endBody(p, true); |
| 4658 | break; |
| 4659 | } |
| 4660 | |
| 4661 | case BC_LEX_STR: |
| 4662 | { |
| 4663 | s = bc_parse_string(p, BC_INST_PRINT_STR); |
| 4664 | break; |
| 4665 | } |
| 4666 | |
| 4667 | case BC_LEX_KEY_BREAK: |
| 4668 | case BC_LEX_KEY_CONTINUE: |
| 4669 | { |
| 4670 | s = bc_parse_loopExit(p, p->l.t.t); |
| 4671 | break; |
| 4672 | } |
| 4673 | |
| 4674 | case BC_LEX_KEY_FOR: |
| 4675 | { |
| 4676 | s = bc_parse_for(p); |
| 4677 | break; |
| 4678 | } |
| 4679 | |
| 4680 | case BC_LEX_KEY_HALT: |
| 4681 | { |
| 4682 | bc_parse_push(p, BC_INST_HALT); |
| 4683 | s = bc_lex_next(&p->l); |
| 4684 | break; |
| 4685 | } |
| 4686 | |
| 4687 | case BC_LEX_KEY_IF: |
| 4688 | { |
| 4689 | s = bc_parse_if(p); |
| 4690 | break; |
| 4691 | } |
| 4692 | |
| 4693 | case BC_LEX_KEY_LIMITS: |
| 4694 | { |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 4695 | // "limits" is a compile-time command, |
| 4696 | // the output is produced at _parse time_. |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4697 | s = bc_lex_next(&p->l); |
| 4698 | if (s) return s; |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 4699 | printf( |
| 4700 | "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n" |
| 4701 | "BC_DIM_MAX = "BC_MAX_DIM_STR "\n" |
| 4702 | "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n" |
| 4703 | "BC_STRING_MAX = "BC_MAX_STRING_STR"\n" |
| 4704 | "BC_NAME_MAX = "BC_MAX_NAME_STR "\n" |
| 4705 | "BC_NUM_MAX = "BC_MAX_NUM_STR "\n" |
| 4706 | "MAX Exponent = "BC_MAX_EXP_STR "\n" |
| 4707 | "Number of vars = "BC_MAX_VARS_STR "\n" |
| 4708 | ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4709 | break; |
| 4710 | } |
| 4711 | |
| 4712 | case BC_LEX_KEY_PRINT: |
| 4713 | { |
| 4714 | s = bc_parse_print(p); |
| 4715 | break; |
| 4716 | } |
| 4717 | |
| 4718 | case BC_LEX_KEY_QUIT: |
| 4719 | { |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 4720 | // "quit" is a compile-time command. For example, |
| 4721 | // "if (0 == 1) quit" terminates when parsing the statement, |
| 4722 | // not when it is executed |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 4723 | QUIT_OR_RETURN_TO_MAIN; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4724 | } |
| 4725 | |
| 4726 | case BC_LEX_KEY_RETURN: |
| 4727 | { |
| 4728 | s = bc_parse_return(p); |
| 4729 | break; |
| 4730 | } |
| 4731 | |
| 4732 | case BC_LEX_KEY_WHILE: |
| 4733 | { |
| 4734 | s = bc_parse_while(p); |
| 4735 | break; |
| 4736 | } |
| 4737 | |
| 4738 | default: |
| 4739 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4740 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4741 | break; |
| 4742 | } |
| 4743 | } |
| 4744 | |
| 4745 | return s; |
| 4746 | } |
| 4747 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 4748 | static FAST_FUNC BcStatus bc_parse_parse(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4749 | { |
| 4750 | BcStatus s; |
| 4751 | |
| 4752 | if (p->l.t.t == BC_LEX_EOF) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4753 | 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] | 4754 | else if (p->l.t.t == BC_LEX_KEY_DEFINE) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4755 | if (!BC_PARSE_CAN_EXEC(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4756 | s = bc_parse_func(p); |
| 4757 | } |
| 4758 | else |
| 4759 | s = bc_parse_stmt(p); |
| 4760 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 4761 | if (s || G_interrupt) { |
| 4762 | bc_parse_reset(p); |
| 4763 | s = BC_STATUS_FAILURE; |
| 4764 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4765 | |
| 4766 | return s; |
| 4767 | } |
| 4768 | |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 4769 | // 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] | 4770 | 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] | 4771 | { |
| 4772 | BcStatus s = BC_STATUS_SUCCESS; |
| 4773 | BcInst prev = BC_INST_PRINT; |
| 4774 | BcLexType top, t = p->l.t.t; |
| 4775 | size_t nexprs = 0, ops_bgn = p->ops.len; |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 4776 | unsigned nparens, nrelops; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4777 | bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last; |
| 4778 | |
| 4779 | paren_first = p->l.t.t == BC_LEX_LPAREN; |
| 4780 | nparens = nrelops = 0; |
| 4781 | paren_expr = rprn = done = get_token = assign = false; |
| 4782 | bin_last = true; |
| 4783 | |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 4784 | 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] | 4785 | switch (t) { |
| 4786 | |
| 4787 | case BC_LEX_OP_INC: |
| 4788 | case BC_LEX_OP_DEC: |
| 4789 | { |
| 4790 | s = bc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags); |
| 4791 | rprn = get_token = bin_last = false; |
| 4792 | break; |
| 4793 | } |
| 4794 | |
| 4795 | case BC_LEX_OP_MINUS: |
| 4796 | { |
| 4797 | s = bc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs); |
| 4798 | rprn = get_token = false; |
| 4799 | bin_last = prev == BC_INST_MINUS; |
| 4800 | break; |
| 4801 | } |
| 4802 | |
| 4803 | case BC_LEX_OP_ASSIGN_POWER: |
| 4804 | case BC_LEX_OP_ASSIGN_MULTIPLY: |
| 4805 | case BC_LEX_OP_ASSIGN_DIVIDE: |
| 4806 | case BC_LEX_OP_ASSIGN_MODULUS: |
| 4807 | case BC_LEX_OP_ASSIGN_PLUS: |
| 4808 | case BC_LEX_OP_ASSIGN_MINUS: |
| 4809 | case BC_LEX_OP_ASSIGN: |
| 4810 | { |
| 4811 | if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM && |
| 4812 | prev != BC_INST_SCALE && prev != BC_INST_IBASE && |
| 4813 | prev != BC_INST_OBASE && prev != BC_INST_LAST) |
| 4814 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4815 | s = bc_error("bad assignment:" |
| 4816 | " left side must be scale," |
| 4817 | " ibase, obase, last, var," |
| 4818 | " or array element" |
| 4819 | ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4820 | break; |
| 4821 | } |
| 4822 | } |
| 4823 | // Fallthrough. |
| 4824 | case BC_LEX_OP_POWER: |
| 4825 | case BC_LEX_OP_MULTIPLY: |
| 4826 | case BC_LEX_OP_DIVIDE: |
| 4827 | case BC_LEX_OP_MODULUS: |
| 4828 | case BC_LEX_OP_PLUS: |
| 4829 | case BC_LEX_OP_REL_EQ: |
| 4830 | case BC_LEX_OP_REL_LE: |
| 4831 | case BC_LEX_OP_REL_GE: |
| 4832 | case BC_LEX_OP_REL_NE: |
| 4833 | case BC_LEX_OP_REL_LT: |
| 4834 | case BC_LEX_OP_REL_GT: |
| 4835 | case BC_LEX_OP_BOOL_NOT: |
| 4836 | case BC_LEX_OP_BOOL_OR: |
| 4837 | case BC_LEX_OP_BOOL_AND: |
| 4838 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4839 | if (((t == BC_LEX_OP_BOOL_NOT) != bin_last) |
| 4840 | || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT) |
| 4841 | ) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4842 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4843 | } |
| 4844 | |
| 4845 | nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT; |
| 4846 | prev = BC_PARSE_TOKEN_INST(t); |
| 4847 | s = bc_parse_operator(p, t, ops_bgn, &nexprs, true); |
| 4848 | rprn = get_token = false; |
| 4849 | bin_last = t != BC_LEX_OP_BOOL_NOT; |
| 4850 | |
| 4851 | break; |
| 4852 | } |
| 4853 | |
| 4854 | case BC_LEX_LPAREN: |
| 4855 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4856 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4857 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4858 | ++nparens; |
| 4859 | paren_expr = rprn = bin_last = false; |
| 4860 | get_token = true; |
| 4861 | bc_vec_push(&p->ops, &t); |
| 4862 | |
| 4863 | break; |
| 4864 | } |
| 4865 | |
| 4866 | case BC_LEX_RPAREN: |
| 4867 | { |
| 4868 | if (bin_last || prev == BC_INST_BOOL_NOT) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4869 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4870 | |
| 4871 | if (nparens == 0) { |
| 4872 | s = BC_STATUS_SUCCESS; |
| 4873 | done = true; |
| 4874 | get_token = false; |
| 4875 | break; |
| 4876 | } |
| 4877 | else if (!paren_expr) |
| 4878 | return BC_STATUS_PARSE_EMPTY_EXP; |
| 4879 | |
| 4880 | --nparens; |
| 4881 | paren_expr = rprn = true; |
| 4882 | get_token = bin_last = false; |
| 4883 | |
| 4884 | s = bc_parse_rightParen(p, ops_bgn, &nexprs); |
| 4885 | |
| 4886 | break; |
| 4887 | } |
| 4888 | |
| 4889 | case BC_LEX_NAME: |
| 4890 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4891 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4892 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4893 | paren_expr = true; |
| 4894 | rprn = get_token = bin_last = false; |
| 4895 | s = bc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL); |
| 4896 | ++nexprs; |
| 4897 | |
| 4898 | break; |
| 4899 | } |
| 4900 | |
| 4901 | case BC_LEX_NUMBER: |
| 4902 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4903 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4904 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4905 | bc_parse_number(p, &prev, &nexprs); |
| 4906 | paren_expr = get_token = true; |
| 4907 | rprn = bin_last = false; |
| 4908 | |
| 4909 | break; |
| 4910 | } |
| 4911 | |
| 4912 | case BC_LEX_KEY_IBASE: |
| 4913 | case BC_LEX_KEY_LAST: |
| 4914 | case BC_LEX_KEY_OBASE: |
| 4915 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4916 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4917 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4918 | prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE); |
| 4919 | bc_parse_push(p, (char) prev); |
| 4920 | |
| 4921 | paren_expr = get_token = true; |
| 4922 | rprn = bin_last = false; |
| 4923 | ++nexprs; |
| 4924 | |
| 4925 | break; |
| 4926 | } |
| 4927 | |
| 4928 | case BC_LEX_KEY_LENGTH: |
| 4929 | case BC_LEX_KEY_SQRT: |
| 4930 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4931 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4932 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4933 | s = bc_parse_builtin(p, t, flags, &prev); |
| 4934 | paren_expr = true; |
| 4935 | rprn = get_token = bin_last = false; |
| 4936 | ++nexprs; |
| 4937 | |
| 4938 | break; |
| 4939 | } |
| 4940 | |
| 4941 | case BC_LEX_KEY_READ: |
| 4942 | { |
| 4943 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4944 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4945 | else if (flags & BC_PARSE_NOREAD) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4946 | s = bc_error_nested_read_call(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4947 | else |
| 4948 | s = bc_parse_read(p); |
| 4949 | |
| 4950 | paren_expr = true; |
| 4951 | rprn = get_token = bin_last = false; |
| 4952 | ++nexprs; |
| 4953 | prev = BC_INST_READ; |
| 4954 | |
| 4955 | break; |
| 4956 | } |
| 4957 | |
| 4958 | case BC_LEX_KEY_SCALE: |
| 4959 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4960 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4961 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4962 | s = bc_parse_scale(p, &prev, flags); |
| 4963 | paren_expr = true; |
| 4964 | rprn = get_token = bin_last = false; |
| 4965 | ++nexprs; |
| 4966 | prev = BC_INST_SCALE; |
| 4967 | |
| 4968 | break; |
| 4969 | } |
| 4970 | |
| 4971 | default: |
| 4972 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4973 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4974 | break; |
| 4975 | } |
| 4976 | } |
| 4977 | |
| 4978 | if (!s && get_token) s = bc_lex_next(&p->l); |
| 4979 | } |
| 4980 | |
| 4981 | if (s) return s; |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 4982 | if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4983 | |
| 4984 | while (p->ops.len > ops_bgn) { |
| 4985 | |
| 4986 | top = BC_PARSE_TOP_OP(p); |
| 4987 | assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN; |
| 4988 | |
| 4989 | if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4990 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4991 | |
| 4992 | bc_parse_push(p, BC_PARSE_TOKEN_INST(top)); |
| 4993 | |
| 4994 | nexprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG; |
| 4995 | bc_vec_pop(&p->ops); |
| 4996 | } |
| 4997 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4998 | if (prev == BC_INST_BOOL_NOT || nexprs != 1) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4999 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5000 | |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 5001 | // next is BcParseNext, byte array of up to 4 BC_LEX's, packed into 32-bit word |
| 5002 | for (;;) { |
| 5003 | if (t == (next & 0x7f)) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5004 | goto ok; |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 5005 | if (next & 0x80) // last element? |
| 5006 | break; |
| 5007 | next >>= 8; |
| 5008 | } |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5009 | return bc_error_bad_expression(); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5010 | ok: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5011 | |
| 5012 | if (!(flags & BC_PARSE_REL) && nrelops) { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 5013 | s = bc_POSIX_does_not_allow("comparison operators outside if or loops"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5014 | if (s) return s; |
| 5015 | } |
| 5016 | else if ((flags & BC_PARSE_REL) && nrelops > 1) { |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 5017 | s = bc_POSIX_requires("exactly one comparison operator per condition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5018 | if (s) return s; |
| 5019 | } |
| 5020 | |
| 5021 | if (flags & BC_PARSE_PRINT) { |
| 5022 | if (paren_first || !assign) bc_parse_push(p, BC_INST_PRINT); |
| 5023 | bc_parse_push(p, BC_INST_POP); |
| 5024 | } |
| 5025 | |
| 5026 | return s; |
| 5027 | } |
| 5028 | |
Denys Vlasenko | 050b0fe | 2018-12-05 22:40:44 +0100 | [diff] [blame] | 5029 | static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next) |
| 5030 | { |
| 5031 | BcStatus s; |
| 5032 | |
| 5033 | s = bc_parse_expr_empty_ok(p, flags, next); |
| 5034 | if (s == BC_STATUS_PARSE_EMPTY_EXP) |
| 5035 | return bc_error("empty expression"); |
| 5036 | return s; |
| 5037 | } |
| 5038 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5039 | static void bc_parse_init(BcParse *p, size_t func) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5040 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5041 | bc_parse_create(p, func, bc_parse_parse, bc_lex_token); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5042 | } |
| 5043 | |
| 5044 | static BcStatus bc_parse_expression(BcParse *p, uint8_t flags) |
| 5045 | { |
| 5046 | return bc_parse_expr(p, flags, bc_parse_next_read); |
| 5047 | } |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 5048 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5049 | #endif // ENABLE_BC |
| 5050 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5051 | #if ENABLE_DC |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 5052 | |
| 5053 | #define DC_PARSE_BUF_LEN ((int) (sizeof(uint32_t) * CHAR_BIT)) |
| 5054 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5055 | static BcStatus dc_parse_register(BcParse *p) |
| 5056 | { |
| 5057 | BcStatus s; |
| 5058 | char *name; |
| 5059 | |
| 5060 | s = bc_lex_next(&p->l); |
| 5061 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5062 | 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] | 5063 | |
| 5064 | name = xstrdup(p->l.t.v.v); |
| 5065 | bc_parse_pushName(p, name); |
| 5066 | |
| 5067 | return s; |
| 5068 | } |
| 5069 | |
| 5070 | static BcStatus dc_parse_string(BcParse *p) |
| 5071 | { |
| 5072 | char *str, *name, b[DC_PARSE_BUF_LEN + 1]; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5073 | size_t idx, len = G.prog.strs.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5074 | |
| 5075 | sprintf(b, "%0*zu", DC_PARSE_BUF_LEN, len); |
| 5076 | name = xstrdup(b); |
| 5077 | |
| 5078 | str = xstrdup(p->l.t.v.v); |
| 5079 | bc_parse_push(p, BC_INST_STR); |
| 5080 | bc_parse_pushIndex(p, len); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5081 | bc_vec_push(&G.prog.strs, &str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5082 | bc_parse_addFunc(p, name, &idx); |
| 5083 | |
| 5084 | return bc_lex_next(&p->l); |
| 5085 | } |
| 5086 | |
| 5087 | static BcStatus dc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store) |
| 5088 | { |
| 5089 | BcStatus s; |
| 5090 | |
| 5091 | bc_parse_push(p, inst); |
| 5092 | if (name) { |
| 5093 | s = dc_parse_register(p); |
| 5094 | if (s) return s; |
| 5095 | } |
| 5096 | |
| 5097 | if (store) { |
| 5098 | bc_parse_push(p, BC_INST_SWAP); |
| 5099 | bc_parse_push(p, BC_INST_ASSIGN); |
| 5100 | bc_parse_push(p, BC_INST_POP); |
| 5101 | } |
| 5102 | |
| 5103 | return bc_lex_next(&p->l); |
| 5104 | } |
| 5105 | |
| 5106 | static BcStatus dc_parse_cond(BcParse *p, uint8_t inst) |
| 5107 | { |
| 5108 | BcStatus s; |
| 5109 | |
| 5110 | bc_parse_push(p, inst); |
| 5111 | bc_parse_push(p, BC_INST_EXEC_COND); |
| 5112 | |
| 5113 | s = dc_parse_register(p); |
| 5114 | if (s) return s; |
| 5115 | |
| 5116 | s = bc_lex_next(&p->l); |
| 5117 | if (s) return s; |
| 5118 | |
| 5119 | if (p->l.t.t == BC_LEX_ELSE) { |
| 5120 | s = dc_parse_register(p); |
| 5121 | if (s) return s; |
| 5122 | s = bc_lex_next(&p->l); |
| 5123 | } |
| 5124 | else |
| 5125 | bc_parse_push(p, BC_PARSE_STREND); |
| 5126 | |
| 5127 | return s; |
| 5128 | } |
| 5129 | |
| 5130 | static BcStatus dc_parse_token(BcParse *p, BcLexType t, uint8_t flags) |
| 5131 | { |
| 5132 | BcStatus s = BC_STATUS_SUCCESS; |
| 5133 | BcInst prev; |
| 5134 | uint8_t inst; |
| 5135 | bool assign, get_token = false; |
| 5136 | |
| 5137 | switch (t) { |
| 5138 | |
| 5139 | case BC_LEX_OP_REL_EQ: |
| 5140 | case BC_LEX_OP_REL_LE: |
| 5141 | case BC_LEX_OP_REL_GE: |
| 5142 | case BC_LEX_OP_REL_NE: |
| 5143 | case BC_LEX_OP_REL_LT: |
| 5144 | case BC_LEX_OP_REL_GT: |
| 5145 | { |
| 5146 | s = dc_parse_cond(p, t - BC_LEX_OP_REL_EQ + BC_INST_REL_EQ); |
| 5147 | break; |
| 5148 | } |
| 5149 | |
| 5150 | case BC_LEX_SCOLON: |
| 5151 | case BC_LEX_COLON: |
| 5152 | { |
| 5153 | s = dc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON); |
| 5154 | break; |
| 5155 | } |
| 5156 | |
| 5157 | case BC_LEX_STR: |
| 5158 | { |
| 5159 | s = dc_parse_string(p); |
| 5160 | break; |
| 5161 | } |
| 5162 | |
| 5163 | case BC_LEX_NEG: |
| 5164 | case BC_LEX_NUMBER: |
| 5165 | { |
| 5166 | if (t == BC_LEX_NEG) { |
| 5167 | s = bc_lex_next(&p->l); |
| 5168 | if (s) return s; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5169 | if (p->l.t.t != BC_LEX_NUMBER) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5170 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5171 | } |
| 5172 | |
| 5173 | bc_parse_number(p, &prev, &p->nbraces); |
| 5174 | |
| 5175 | if (t == BC_LEX_NEG) bc_parse_push(p, BC_INST_NEG); |
| 5176 | get_token = true; |
| 5177 | |
| 5178 | break; |
| 5179 | } |
| 5180 | |
| 5181 | case BC_LEX_KEY_READ: |
| 5182 | { |
| 5183 | if (flags & BC_PARSE_NOREAD) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5184 | s = bc_error_nested_read_call(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5185 | else |
| 5186 | bc_parse_push(p, BC_INST_READ); |
| 5187 | get_token = true; |
| 5188 | break; |
| 5189 | } |
| 5190 | |
| 5191 | case BC_LEX_OP_ASSIGN: |
| 5192 | case BC_LEX_STORE_PUSH: |
| 5193 | { |
| 5194 | assign = t == BC_LEX_OP_ASSIGN; |
| 5195 | inst = assign ? BC_INST_VAR : BC_INST_PUSH_TO_VAR; |
| 5196 | s = dc_parse_mem(p, inst, true, assign); |
| 5197 | break; |
| 5198 | } |
| 5199 | |
| 5200 | case BC_LEX_LOAD: |
| 5201 | case BC_LEX_LOAD_POP: |
| 5202 | { |
| 5203 | inst = t == BC_LEX_LOAD_POP ? BC_INST_PUSH_VAR : BC_INST_LOAD; |
| 5204 | s = dc_parse_mem(p, inst, true, false); |
| 5205 | break; |
| 5206 | } |
| 5207 | |
| 5208 | case BC_LEX_STORE_IBASE: |
| 5209 | case BC_LEX_STORE_SCALE: |
| 5210 | case BC_LEX_STORE_OBASE: |
| 5211 | { |
| 5212 | inst = t - BC_LEX_STORE_IBASE + BC_INST_IBASE; |
| 5213 | s = dc_parse_mem(p, inst, false, true); |
| 5214 | break; |
| 5215 | } |
| 5216 | |
| 5217 | default: |
| 5218 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5219 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5220 | get_token = true; |
| 5221 | break; |
| 5222 | } |
| 5223 | } |
| 5224 | |
| 5225 | if (!s && get_token) s = bc_lex_next(&p->l); |
| 5226 | |
| 5227 | return s; |
| 5228 | } |
| 5229 | |
| 5230 | static BcStatus dc_parse_expr(BcParse *p, uint8_t flags) |
| 5231 | { |
| 5232 | BcStatus s = BC_STATUS_SUCCESS; |
| 5233 | BcInst inst; |
| 5234 | BcLexType t; |
| 5235 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5236 | if (flags & BC_PARSE_NOCALL) p->nbraces = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5237 | |
| 5238 | for (t = p->l.t.t; !s && t != BC_LEX_EOF; t = p->l.t.t) { |
| 5239 | |
| 5240 | inst = dc_parse_insts[t]; |
| 5241 | |
| 5242 | if (inst != BC_INST_INVALID) { |
| 5243 | bc_parse_push(p, inst); |
| 5244 | s = bc_lex_next(&p->l); |
| 5245 | } |
| 5246 | else |
| 5247 | s = dc_parse_token(p, t, flags); |
| 5248 | } |
| 5249 | |
| 5250 | if (!s && p->l.t.t == BC_LEX_EOF && (flags & BC_PARSE_NOCALL)) |
| 5251 | bc_parse_push(p, BC_INST_POP_EXEC); |
| 5252 | |
| 5253 | return s; |
| 5254 | } |
| 5255 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 5256 | static FAST_FUNC BcStatus dc_parse_parse(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5257 | { |
| 5258 | BcStatus s; |
| 5259 | |
| 5260 | if (p->l.t.t == BC_LEX_EOF) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5261 | s = bc_error("end of file"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5262 | else |
| 5263 | s = dc_parse_expr(p, 0); |
| 5264 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 5265 | if (s || G_interrupt) { |
| 5266 | bc_parse_reset(p); |
| 5267 | s = BC_STATUS_FAILURE; |
| 5268 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5269 | |
| 5270 | return s; |
| 5271 | } |
| 5272 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5273 | static void dc_parse_init(BcParse *p, size_t func) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5274 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5275 | bc_parse_create(p, func, dc_parse_parse, dc_lex_token); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5276 | } |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 5277 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5278 | #endif // ENABLE_DC |
| 5279 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5280 | static void common_parse_init(BcParse *p, size_t func) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5281 | { |
| 5282 | if (IS_BC) { |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 5283 | IF_BC(bc_parse_init(p, func);) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5284 | } else { |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 5285 | IF_DC(dc_parse_init(p, func);) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5286 | } |
| 5287 | } |
| 5288 | |
| 5289 | static BcStatus common_parse_expr(BcParse *p, uint8_t flags) |
| 5290 | { |
| 5291 | if (IS_BC) { |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 5292 | IF_BC(return bc_parse_expression(p, flags);) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5293 | } else { |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 5294 | IF_DC(return dc_parse_expr(p, flags);) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5295 | } |
| 5296 | } |
| 5297 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5298 | static BcVec* bc_program_search(char *id, bool var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5299 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5300 | BcId e, *ptr; |
| 5301 | BcVec *v, *map; |
| 5302 | size_t i; |
| 5303 | BcResultData data; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 5304 | int new; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5305 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5306 | v = var ? &G.prog.vars : &G.prog.arrs; |
| 5307 | map = var ? &G.prog.var_map : &G.prog.arr_map; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5308 | |
| 5309 | e.name = id; |
| 5310 | e.idx = v->len; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 5311 | new = bc_map_insert(map, &e, &i); // 1 if insertion was successful |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5312 | |
| 5313 | if (new) { |
| 5314 | bc_array_init(&data.v, var); |
| 5315 | bc_vec_push(v, &data.v); |
| 5316 | } |
| 5317 | |
| 5318 | ptr = bc_vec_item(map, i); |
| 5319 | if (new) ptr->name = xstrdup(e.name); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5320 | return bc_vec_item(v, ptr->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5321 | } |
| 5322 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5323 | static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5324 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5325 | switch (r->t) { |
| 5326 | |
| 5327 | case BC_RESULT_STR: |
| 5328 | case BC_RESULT_TEMP: |
| 5329 | case BC_RESULT_IBASE: |
| 5330 | case BC_RESULT_SCALE: |
| 5331 | case BC_RESULT_OBASE: |
| 5332 | { |
| 5333 | *num = &r->d.n; |
| 5334 | break; |
| 5335 | } |
| 5336 | |
| 5337 | case BC_RESULT_CONSTANT: |
| 5338 | { |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5339 | BcStatus s; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5340 | char **str = bc_vec_item(&G.prog.consts, r->d.id.idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5341 | size_t base_t, len = strlen(*str); |
| 5342 | BcNum *base; |
| 5343 | |
| 5344 | bc_num_init(&r->d.n, len); |
| 5345 | |
| 5346 | hex = hex && len == 1; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5347 | base = hex ? &G.prog.hexb : &G.prog.ib; |
| 5348 | base_t = hex ? BC_NUM_MAX_IBASE : G.prog.ib_t; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5349 | s = zbc_num_parse(&r->d.n, *str, base, base_t); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5350 | |
| 5351 | if (s) { |
| 5352 | bc_num_free(&r->d.n); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5353 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5354 | } |
| 5355 | |
| 5356 | *num = &r->d.n; |
| 5357 | r->t = BC_RESULT_TEMP; |
| 5358 | |
| 5359 | break; |
| 5360 | } |
| 5361 | |
| 5362 | case BC_RESULT_VAR: |
| 5363 | case BC_RESULT_ARRAY: |
| 5364 | case BC_RESULT_ARRAY_ELEM: |
| 5365 | { |
| 5366 | BcVec *v; |
| 5367 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5368 | 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] | 5369 | |
| 5370 | if (r->t == BC_RESULT_ARRAY_ELEM) { |
| 5371 | v = bc_vec_top(v); |
| 5372 | if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1); |
| 5373 | *num = bc_vec_item(v, r->d.id.idx); |
| 5374 | } |
| 5375 | else |
| 5376 | *num = bc_vec_top(v); |
| 5377 | |
| 5378 | break; |
| 5379 | } |
| 5380 | |
| 5381 | case BC_RESULT_LAST: |
| 5382 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5383 | *num = &G.prog.last; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5384 | break; |
| 5385 | } |
| 5386 | |
| 5387 | case BC_RESULT_ONE: |
| 5388 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5389 | *num = &G.prog.one; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5390 | break; |
| 5391 | } |
| 5392 | } |
| 5393 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5394 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5395 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5396 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5397 | # define zbc_program_num(...) (zbc_program_num(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5398 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5399 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5400 | static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5401 | BcResult **r, BcNum **rn, bool assign) |
| 5402 | { |
| 5403 | BcStatus s; |
| 5404 | bool hex; |
| 5405 | BcResultType lt, rt; |
| 5406 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5407 | if (!BC_PROG_STACK(&G.prog.results, 2)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5408 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5409 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5410 | *r = bc_vec_item_rev(&G.prog.results, 0); |
| 5411 | *l = bc_vec_item_rev(&G.prog.results, 1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5412 | |
| 5413 | lt = (*l)->t; |
| 5414 | rt = (*r)->t; |
| 5415 | hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE); |
| 5416 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5417 | s = zbc_program_num(*l, ln, false); |
| 5418 | if (s) RETURN_STATUS(s); |
| 5419 | s = zbc_program_num(*r, rn, hex); |
| 5420 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5421 | |
| 5422 | // We run this again under these conditions in case any vector has been |
| 5423 | // reallocated out from under the BcNums or arrays we had. |
| 5424 | if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5425 | s = zbc_program_num(*l, ln, false); |
| 5426 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5427 | } |
| 5428 | |
| 5429 | if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5430 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5431 | if (!assign && !BC_PROG_NUM((*r), (*ln))) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5432 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5433 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5434 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5435 | } |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 5436 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5437 | # define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 5438 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5439 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5440 | static void bc_program_binOpRetire(BcResult *r) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5441 | { |
| 5442 | r->t = BC_RESULT_TEMP; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5443 | bc_vec_pop(&G.prog.results); |
| 5444 | bc_vec_pop(&G.prog.results); |
| 5445 | bc_vec_push(&G.prog.results, r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5446 | } |
| 5447 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5448 | static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5449 | { |
| 5450 | BcStatus s; |
| 5451 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5452 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5453 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5454 | *r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5455 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5456 | s = zbc_program_num(*r, n, false); |
| 5457 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5458 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5459 | if (!BC_PROG_NUM((*r), (*n))) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5460 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5461 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5462 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5463 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5464 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5465 | # define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5466 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5467 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5468 | static void bc_program_retire(BcResult *r, BcResultType t) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5469 | { |
| 5470 | r->t = t; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5471 | bc_vec_pop(&G.prog.results); |
| 5472 | bc_vec_push(&G.prog.results, r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5473 | } |
| 5474 | |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5475 | static BC_STATUS zbc_program_op(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5476 | { |
| 5477 | BcStatus s; |
| 5478 | BcResult *opd1, *opd2, res; |
| 5479 | BcNum *n1, *n2 = NULL; |
| 5480 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5481 | s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5482 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5483 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5484 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5485 | s = BC_STATUS_SUCCESS; |
| 5486 | #if !ERRORS_ARE_FATAL |
| 5487 | s = |
| 5488 | #endif |
| 5489 | 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] | 5490 | if (s) goto err; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5491 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5492 | |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5493 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5494 | |
| 5495 | err: |
| 5496 | bc_num_free(&res.d.n); |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5497 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5498 | } |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5499 | #if ERRORS_ARE_FATAL |
| 5500 | # define zbc_program_op(...) (zbc_program_op(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 5501 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5502 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 5503 | static BcStatus bc_program_read(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5504 | { |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5505 | const char *sv_file; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5506 | BcStatus s; |
| 5507 | BcParse parse; |
| 5508 | BcVec buf; |
| 5509 | BcInstPtr ip; |
| 5510 | size_t i; |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 5511 | BcFunc *f = bc_program_func(BC_PROG_READ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5512 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5513 | for (i = 0; i < G.prog.stack.len; ++i) { |
| 5514 | BcInstPtr *ip_ptr = bc_vec_item(&G.prog.stack, i); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5515 | if (ip_ptr->func == BC_PROG_READ) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5516 | return bc_error_nested_read_call(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5517 | } |
| 5518 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 5519 | bc_vec_pop_all(&f->code); |
| 5520 | bc_char_vec_init(&buf); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5521 | |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5522 | sv_file = G.prog.file; |
| 5523 | G.prog.file = NULL; |
| 5524 | |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 5525 | s = bc_read_line(&buf); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5526 | if (s) goto io_err; |
| 5527 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5528 | common_parse_init(&parse, BC_PROG_READ); |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5529 | bc_lex_file(&parse.l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5530 | |
| 5531 | s = bc_parse_text(&parse, buf.v); |
| 5532 | if (s) goto exec_err; |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5533 | s = common_parse_expr(&parse, BC_PARSE_NOREAD); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5534 | if (s) goto exec_err; |
| 5535 | |
| 5536 | 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] | 5537 | s = bc_error("bad read() expression"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5538 | goto exec_err; |
| 5539 | } |
| 5540 | |
| 5541 | ip.func = BC_PROG_READ; |
| 5542 | ip.idx = 0; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5543 | ip.len = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5544 | |
| 5545 | // Update this pointer, just in case. |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 5546 | f = bc_program_func(BC_PROG_READ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5547 | |
| 5548 | bc_vec_pushByte(&f->code, BC_INST_POP_EXEC); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5549 | bc_vec_push(&G.prog.stack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5550 | |
| 5551 | exec_err: |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5552 | G.prog.file = sv_file; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5553 | bc_parse_free(&parse); |
| 5554 | io_err: |
| 5555 | bc_vec_free(&buf); |
| 5556 | return s; |
| 5557 | } |
| 5558 | |
| 5559 | static size_t bc_program_index(char *code, size_t *bgn) |
| 5560 | { |
| 5561 | char amt = code[(*bgn)++], i = 0; |
| 5562 | size_t res = 0; |
| 5563 | |
| 5564 | for (; i < amt; ++i, ++(*bgn)) |
| 5565 | res |= (((size_t)((int) code[*bgn]) & UCHAR_MAX) << (i * CHAR_BIT)); |
| 5566 | |
| 5567 | return res; |
| 5568 | } |
| 5569 | |
| 5570 | static char *bc_program_name(char *code, size_t *bgn) |
| 5571 | { |
| 5572 | size_t i; |
| 5573 | char c, *s, *str = code + *bgn, *ptr = strchr(str, BC_PARSE_STREND); |
| 5574 | |
| 5575 | s = xmalloc(ptr - str + 1); |
| 5576 | c = code[(*bgn)++]; |
| 5577 | |
| 5578 | for (i = 0; c != 0 && c != BC_PARSE_STREND; c = code[(*bgn)++], ++i) |
| 5579 | s[i] = c; |
| 5580 | |
| 5581 | s[i] = '\0'; |
| 5582 | |
| 5583 | return s; |
| 5584 | } |
| 5585 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 5586 | static void bc_program_printString(const char *str) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5587 | { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5588 | #if ENABLE_DC |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame^] | 5589 | // Huh? Example when this happens? |
| 5590 | if (!str[0]) { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5591 | bb_putchar('\0'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5592 | return; |
| 5593 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5594 | #endif |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame^] | 5595 | while (*str) { |
| 5596 | int c = *str++; |
| 5597 | if (c != '\\' || !*str) |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5598 | bb_putchar(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5599 | else { |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame^] | 5600 | c = *str++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5601 | switch (c) { |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame^] | 5602 | case 'a': |
| 5603 | bb_putchar('\a'); |
| 5604 | break; |
| 5605 | case 'b': |
| 5606 | bb_putchar('\b'); |
| 5607 | break; |
| 5608 | case '\\': |
| 5609 | case 'e': |
| 5610 | bb_putchar('\\'); |
| 5611 | break; |
| 5612 | case 'f': |
| 5613 | bb_putchar('\f'); |
| 5614 | break; |
| 5615 | case 'n': |
| 5616 | bb_putchar('\n'); |
| 5617 | G.prog.nchars = SIZE_MAX; |
| 5618 | break; |
| 5619 | case 'r': |
| 5620 | bb_putchar('\r'); |
| 5621 | break; |
| 5622 | case 'q': |
| 5623 | bb_putchar('"'); |
| 5624 | break; |
| 5625 | case 't': |
| 5626 | bb_putchar('\t'); |
| 5627 | break; |
| 5628 | default: |
| 5629 | // Just print the backslash and following character. |
| 5630 | bb_putchar('\\'); |
| 5631 | ++G.prog.nchars; |
| 5632 | bb_putchar(c); |
| 5633 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5634 | } |
| 5635 | } |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame^] | 5636 | ++G.prog.nchars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5637 | } |
| 5638 | } |
| 5639 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5640 | static void bc_num_printNewline(void) |
| 5641 | { |
| 5642 | if (G.prog.nchars == G.prog.len - 1) { |
| 5643 | bb_putchar('\\'); |
| 5644 | bb_putchar('\n'); |
| 5645 | G.prog.nchars = 0; |
| 5646 | } |
| 5647 | } |
| 5648 | |
| 5649 | #if ENABLE_DC |
| 5650 | static FAST_FUNC void bc_num_printChar(size_t num, size_t width, bool radix) |
| 5651 | { |
| 5652 | (void) radix; |
| 5653 | bb_putchar((char) num); |
| 5654 | G.prog.nchars += width; |
| 5655 | } |
| 5656 | #endif |
| 5657 | |
| 5658 | static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix) |
| 5659 | { |
| 5660 | size_t exp, pow; |
| 5661 | |
| 5662 | bc_num_printNewline(); |
| 5663 | bb_putchar(radix ? '.' : ' '); |
| 5664 | ++G.prog.nchars; |
| 5665 | |
| 5666 | bc_num_printNewline(); |
| 5667 | for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10) |
| 5668 | continue; |
| 5669 | |
| 5670 | for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) { |
| 5671 | size_t dig; |
| 5672 | bc_num_printNewline(); |
| 5673 | dig = num / pow; |
| 5674 | num -= dig * pow; |
| 5675 | bb_putchar(((char) dig) + '0'); |
| 5676 | } |
| 5677 | } |
| 5678 | |
| 5679 | static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix) |
| 5680 | { |
| 5681 | if (radix) { |
| 5682 | bc_num_printNewline(); |
| 5683 | bb_putchar('.'); |
| 5684 | G.prog.nchars += 1; |
| 5685 | } |
| 5686 | |
| 5687 | bc_num_printNewline(); |
| 5688 | bb_putchar(bb_hexdigits_upcase[num]); |
| 5689 | G.prog.nchars += width; |
| 5690 | } |
| 5691 | |
| 5692 | static void bc_num_printDecimal(BcNum *n) |
| 5693 | { |
| 5694 | size_t i, rdx = n->rdx - 1; |
| 5695 | |
| 5696 | if (n->neg) bb_putchar('-'); |
| 5697 | G.prog.nchars += n->neg; |
| 5698 | |
| 5699 | for (i = n->len - 1; i < n->len; --i) |
| 5700 | bc_num_printHex((size_t) n->num[i], 1, i == rdx); |
| 5701 | } |
| 5702 | |
| 5703 | static BC_STATUS zbc_num_printNum(BcNum *n, BcNum *base, size_t width, BcNumDigitOp print) |
| 5704 | { |
| 5705 | BcStatus s; |
| 5706 | BcVec stack; |
| 5707 | BcNum intp, fracp, digit, frac_len; |
| 5708 | unsigned long dig, *ptr; |
| 5709 | size_t i; |
| 5710 | bool radix; |
| 5711 | |
| 5712 | if (n->len == 0) { |
| 5713 | print(0, width, false); |
| 5714 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 5715 | } |
| 5716 | |
| 5717 | bc_vec_init(&stack, sizeof(long), NULL); |
| 5718 | bc_num_init(&intp, n->len); |
| 5719 | bc_num_init(&fracp, n->rdx); |
| 5720 | bc_num_init(&digit, width); |
| 5721 | bc_num_init(&frac_len, BC_NUM_INT(n)); |
| 5722 | bc_num_copy(&intp, n); |
| 5723 | bc_num_one(&frac_len); |
| 5724 | |
| 5725 | bc_num_truncate(&intp, intp.rdx); |
| 5726 | s = zbc_num_sub(n, &intp, &fracp, 0); |
| 5727 | if (s) goto err; |
| 5728 | |
| 5729 | while (intp.len != 0) { |
| 5730 | s = zbc_num_divmod(&intp, base, &intp, &digit, 0); |
| 5731 | if (s) goto err; |
| 5732 | s = zbc_num_ulong(&digit, &dig); |
| 5733 | if (s) goto err; |
| 5734 | bc_vec_push(&stack, &dig); |
| 5735 | } |
| 5736 | |
| 5737 | for (i = 0; i < stack.len; ++i) { |
| 5738 | ptr = bc_vec_item_rev(&stack, i); |
| 5739 | print(*ptr, width, false); |
| 5740 | } |
| 5741 | |
| 5742 | if (!n->rdx) goto err; |
| 5743 | |
| 5744 | for (radix = true; frac_len.len <= n->rdx; radix = false) { |
| 5745 | s = zbc_num_mul(&fracp, base, &fracp, n->rdx); |
| 5746 | if (s) goto err; |
| 5747 | s = zbc_num_ulong(&fracp, &dig); |
| 5748 | if (s) goto err; |
| 5749 | bc_num_ulong2num(&intp, dig); |
| 5750 | s = zbc_num_sub(&fracp, &intp, &fracp, 0); |
| 5751 | if (s) goto err; |
| 5752 | print(dig, width, radix); |
| 5753 | s = zbc_num_mul(&frac_len, base, &frac_len, 0); |
| 5754 | if (s) goto err; |
| 5755 | } |
| 5756 | |
| 5757 | err: |
| 5758 | bc_num_free(&frac_len); |
| 5759 | bc_num_free(&digit); |
| 5760 | bc_num_free(&fracp); |
| 5761 | bc_num_free(&intp); |
| 5762 | bc_vec_free(&stack); |
| 5763 | RETURN_STATUS(s); |
| 5764 | } |
| 5765 | #if ERRORS_ARE_FATAL |
| 5766 | # define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 5767 | #endif |
| 5768 | |
| 5769 | static BC_STATUS zbc_num_printBase(BcNum *n) |
| 5770 | { |
| 5771 | BcStatus s; |
| 5772 | size_t width, i; |
| 5773 | BcNumDigitOp print; |
| 5774 | bool neg = n->neg; |
| 5775 | |
| 5776 | if (neg) { |
| 5777 | bb_putchar('-'); |
| 5778 | G.prog.nchars++; |
| 5779 | } |
| 5780 | |
| 5781 | n->neg = false; |
| 5782 | |
| 5783 | if (G.prog.ob_t <= BC_NUM_MAX_IBASE) { |
| 5784 | width = 1; |
| 5785 | print = bc_num_printHex; |
| 5786 | } |
| 5787 | else { |
| 5788 | for (i = G.prog.ob_t - 1, width = 0; i != 0; i /= 10, ++width) |
| 5789 | continue; |
| 5790 | print = bc_num_printDigits; |
| 5791 | } |
| 5792 | |
| 5793 | s = zbc_num_printNum(n, &G.prog.ob, width, print); |
| 5794 | n->neg = neg; |
| 5795 | |
| 5796 | RETURN_STATUS(s); |
| 5797 | } |
| 5798 | #if ERRORS_ARE_FATAL |
| 5799 | # define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 5800 | #endif |
| 5801 | |
| 5802 | #if ENABLE_DC |
| 5803 | static BC_STATUS zbc_num_stream(BcNum *n, BcNum *base) |
| 5804 | { |
| 5805 | RETURN_STATUS(zbc_num_printNum(n, base, 1, bc_num_printChar)); |
| 5806 | } |
| 5807 | #if ERRORS_ARE_FATAL |
| 5808 | # define zbc_num_stream(...) (zbc_num_stream(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 5809 | #endif |
| 5810 | #endif |
| 5811 | |
| 5812 | static BC_STATUS zbc_num_print(BcNum *n, bool newline) |
| 5813 | { |
| 5814 | BcStatus s = BC_STATUS_SUCCESS; |
| 5815 | |
| 5816 | bc_num_printNewline(); |
| 5817 | |
| 5818 | if (n->len == 0) { |
| 5819 | bb_putchar('0'); |
| 5820 | ++G.prog.nchars; |
| 5821 | } |
| 5822 | else if (G.prog.ob_t == 10) |
| 5823 | bc_num_printDecimal(n); |
| 5824 | else |
| 5825 | s = zbc_num_printBase(n); |
| 5826 | |
| 5827 | if (newline) { |
| 5828 | bb_putchar('\n'); |
| 5829 | G.prog.nchars = 0; |
| 5830 | } |
| 5831 | |
| 5832 | RETURN_STATUS(s); |
| 5833 | } |
| 5834 | #if ERRORS_ARE_FATAL |
| 5835 | # define zbc_num_print(...) (zbc_num_print(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 5836 | #endif |
| 5837 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5838 | static BC_STATUS zbc_program_print(char inst, size_t idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5839 | { |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5840 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5841 | BcResult *r; |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5842 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5843 | bool pop = inst != BC_INST_PRINT; |
| 5844 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5845 | if (!BC_PROG_STACK(&G.prog.results, idx + 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5846 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5847 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5848 | r = bc_vec_item_rev(&G.prog.results, idx); |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5849 | num = NULL; // is this NULL necessary? |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5850 | s = zbc_program_num(r, &num, false); |
| 5851 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5852 | |
| 5853 | if (BC_PROG_NUM(r, num)) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5854 | s = zbc_num_print(num, !pop); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5855 | if (!s) bc_num_copy(&G.prog.last, num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5856 | } |
| 5857 | else { |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5858 | char *str; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5859 | |
| 5860 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx; |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 5861 | str = *bc_program_str(idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5862 | |
| 5863 | if (inst == BC_INST_PRINT_STR) { |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5864 | for (;;) { |
| 5865 | char c = *str++; |
| 5866 | if (c == '\0') break; |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5867 | bb_putchar(c); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5868 | ++G.prog.nchars; |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5869 | if (c == '\n') G.prog.nchars = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5870 | } |
| 5871 | } |
| 5872 | else { |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 5873 | bc_program_printString(str); |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5874 | if (inst == BC_INST_PRINT) bb_putchar('\n'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5875 | } |
| 5876 | } |
| 5877 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5878 | if (!s && pop) bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5879 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5880 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5881 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5882 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5883 | # define zbc_program_print(...) (zbc_program_print(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5884 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5885 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5886 | static BC_STATUS zbc_program_negate(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5887 | { |
| 5888 | BcStatus s; |
| 5889 | BcResult res, *ptr; |
| 5890 | BcNum *num = NULL; |
| 5891 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5892 | s = zbc_program_prep(&ptr, &num); |
| 5893 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5894 | |
| 5895 | bc_num_init(&res.d.n, num->len); |
| 5896 | bc_num_copy(&res.d.n, num); |
| 5897 | if (res.d.n.len) res.d.n.neg = !res.d.n.neg; |
| 5898 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5899 | bc_program_retire(&res, BC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5900 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5901 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5902 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5903 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5904 | # define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5905 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5906 | |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 5907 | static BC_STATUS zbc_program_logical(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5908 | { |
| 5909 | BcStatus s; |
| 5910 | BcResult *opd1, *opd2, res; |
| 5911 | BcNum *n1, *n2; |
| 5912 | bool cond = 0; |
| 5913 | ssize_t cmp; |
| 5914 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5915 | s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 5916 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5917 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5918 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5919 | |
| 5920 | if (inst == BC_INST_BOOL_AND) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5921 | 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] | 5922 | else if (inst == BC_INST_BOOL_OR) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5923 | 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] | 5924 | else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5925 | cmp = bc_num_cmp(n1, n2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5926 | switch (inst) { |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5927 | case BC_INST_REL_EQ: |
| 5928 | cond = cmp == 0; |
| 5929 | break; |
| 5930 | case BC_INST_REL_LE: |
| 5931 | cond = cmp <= 0; |
| 5932 | break; |
| 5933 | case BC_INST_REL_GE: |
| 5934 | cond = cmp >= 0; |
| 5935 | break; |
| 5936 | case BC_INST_REL_NE: |
| 5937 | cond = cmp != 0; |
| 5938 | break; |
| 5939 | case BC_INST_REL_LT: |
| 5940 | cond = cmp < 0; |
| 5941 | break; |
| 5942 | case BC_INST_REL_GT: |
| 5943 | cond = cmp > 0; |
| 5944 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5945 | } |
| 5946 | } |
| 5947 | |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5948 | if (cond) bc_num_one(&res.d.n); |
| 5949 | //else bc_num_zero(&res.d.n); - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5950 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5951 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5952 | |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 5953 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5954 | } |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 5955 | #if ERRORS_ARE_FATAL |
| 5956 | # define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 5957 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5958 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5959 | #if ENABLE_DC |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5960 | static BC_STATUS zbc_program_assignStr(BcResult *r, BcVec *v, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5961 | bool push) |
| 5962 | { |
| 5963 | BcNum n2; |
| 5964 | BcResult res; |
| 5965 | |
| 5966 | memset(&n2, 0, sizeof(BcNum)); |
| 5967 | n2.rdx = res.d.id.idx = r->d.id.idx; |
| 5968 | res.t = BC_RESULT_STR; |
| 5969 | |
| 5970 | if (!push) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5971 | if (!BC_PROG_STACK(&G.prog.results, 2)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5972 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5973 | bc_vec_pop(v); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5974 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5975 | } |
| 5976 | |
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 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5979 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5980 | bc_vec_push(v, &n2); |
| 5981 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5982 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5983 | } |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 5984 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5985 | # define zbc_program_assignStr(...) (zbc_program_assignStr(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 5986 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5987 | #endif // ENABLE_DC |
| 5988 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5989 | static BC_STATUS zbc_program_copyToVar(char *name, bool var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5990 | { |
| 5991 | BcStatus s; |
| 5992 | BcResult *ptr, r; |
| 5993 | BcVec *v; |
| 5994 | BcNum *n; |
| 5995 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5996 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5997 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5998 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5999 | ptr = bc_vec_top(&G.prog.results); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6000 | if ((ptr->t == BC_RESULT_ARRAY) != !var) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6001 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6002 | v = bc_program_search(name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6003 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6004 | #if ENABLE_DC |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6005 | if (ptr->t == BC_RESULT_STR && !var) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6006 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
| 6007 | if (ptr->t == BC_RESULT_STR) |
| 6008 | RETURN_STATUS(zbc_program_assignStr(ptr, v, true)); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6009 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6010 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6011 | s = zbc_program_num(ptr, &n, false); |
| 6012 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6013 | |
| 6014 | // Do this once more to make sure that pointers were not invalidated. |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6015 | v = bc_program_search(name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6016 | |
| 6017 | if (var) { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6018 | bc_num_init_DEF_SIZE(&r.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6019 | bc_num_copy(&r.d.n, n); |
| 6020 | } |
| 6021 | else { |
| 6022 | bc_array_init(&r.d.v, true); |
| 6023 | bc_array_copy(&r.d.v, (BcVec *) n); |
| 6024 | } |
| 6025 | |
| 6026 | bc_vec_push(v, &r.d); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6027 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6028 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6029 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6030 | } |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6031 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6032 | # define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6033 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6034 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6035 | static BC_STATUS zbc_program_assign(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6036 | { |
| 6037 | BcStatus s; |
| 6038 | BcResult *left, *right, res; |
| 6039 | BcNum *l = NULL, *r = NULL; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6040 | bool assign = inst == BC_INST_ASSIGN, ib, sc; |
| 6041 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6042 | s = zbc_program_binOpPrep(&left, &l, &right, &r, assign); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6043 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6044 | |
| 6045 | ib = left->t == BC_RESULT_IBASE; |
| 6046 | sc = left->t == BC_RESULT_SCALE; |
| 6047 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6048 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6049 | |
| 6050 | if (right->t == BC_RESULT_STR) { |
| 6051 | |
| 6052 | BcVec *v; |
| 6053 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6054 | if (left->t != BC_RESULT_VAR) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6055 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6056 | v = bc_program_search(left->d.id.name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6057 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6058 | RETURN_STATUS(zbc_program_assignStr(right, v, false)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6059 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6060 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6061 | |
| 6062 | if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP) |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 6063 | RETURN_STATUS(bc_error("bad assignment:" |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6064 | " left side must be scale," |
| 6065 | " ibase, obase, last, var," |
| 6066 | " or array element" |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6067 | )); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6068 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6069 | #if ENABLE_BC |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6070 | 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] | 6071 | RETURN_STATUS(bc_error("divide by zero")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6072 | |
| 6073 | if (assign) |
| 6074 | bc_num_copy(l, r); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6075 | else { |
| 6076 | s = BC_STATUS_SUCCESS; |
| 6077 | #if !ERRORS_ARE_FATAL |
| 6078 | s = |
| 6079 | #endif |
| 6080 | zbc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale); |
| 6081 | } |
| 6082 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6083 | #else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6084 | bc_num_copy(l, r); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6085 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6086 | |
| 6087 | if (ib || sc || left->t == BC_RESULT_OBASE) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6088 | static const char *const msg[] = { |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 6089 | "bad ibase; must be [2,16]", //BC_RESULT_IBASE |
| 6090 | "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE |
| 6091 | NULL, //can't happen //BC_RESULT_LAST |
| 6092 | NULL, //can't happen //BC_RESULT_CONSTANT |
| 6093 | NULL, //can't happen //BC_RESULT_ONE |
| 6094 | "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6095 | }; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6096 | size_t *ptr; |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 6097 | unsigned long val, max; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6098 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6099 | s = zbc_num_ulong(l, &val); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6100 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6101 | s = left->t - BC_RESULT_IBASE; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6102 | if (sc) { |
| 6103 | max = BC_MAX_SCALE; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6104 | ptr = &G.prog.scale; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6105 | } |
| 6106 | else { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6107 | if (val < BC_NUM_MIN_BASE) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6108 | RETURN_STATUS(bc_error(msg[s])); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6109 | max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6110 | ptr = ib ? &G.prog.ib_t : &G.prog.ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6111 | } |
| 6112 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6113 | if (val > max) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6114 | RETURN_STATUS(bc_error(msg[s])); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6115 | if (!sc) |
| 6116 | bc_num_copy(ib ? &G.prog.ib : &G.prog.ob, l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6117 | |
| 6118 | *ptr = (size_t) val; |
| 6119 | s = BC_STATUS_SUCCESS; |
| 6120 | } |
| 6121 | |
| 6122 | bc_num_init(&res.d.n, l->len); |
| 6123 | bc_num_copy(&res.d.n, l); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6124 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6125 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6126 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6127 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6128 | #if ERRORS_ARE_FATAL |
| 6129 | # define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 6130 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6131 | |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6132 | #if !ENABLE_DC |
| 6133 | #define bc_program_pushVar(code, bgn, pop, copy) \ |
| 6134 | bc_program_pushVar(code, bgn) |
| 6135 | // for bc, 'pop' and 'copy' are always false |
| 6136 | #endif |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 6137 | static BC_STATUS bc_program_pushVar(char *code, size_t *bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6138 | bool pop, bool copy) |
| 6139 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6140 | BcResult r; |
| 6141 | char *name = bc_program_name(code, bgn); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6142 | |
| 6143 | r.t = BC_RESULT_VAR; |
| 6144 | r.d.id.name = name; |
| 6145 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6146 | #if ENABLE_DC |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6147 | { |
| 6148 | BcVec *v = bc_program_search(name, true); |
| 6149 | BcNum *num = bc_vec_top(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6150 | |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6151 | if (pop || copy) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6152 | |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6153 | if (!BC_PROG_STACK(v, 2 - copy)) { |
| 6154 | free(name); |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 6155 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6156 | } |
| 6157 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6158 | free(name); |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6159 | name = NULL; |
| 6160 | |
| 6161 | if (!BC_PROG_STR(num)) { |
| 6162 | |
| 6163 | r.t = BC_RESULT_TEMP; |
| 6164 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6165 | bc_num_init_DEF_SIZE(&r.d.n); |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6166 | bc_num_copy(&r.d.n, num); |
| 6167 | } |
| 6168 | else { |
| 6169 | r.t = BC_RESULT_STR; |
| 6170 | r.d.id.idx = num->rdx; |
| 6171 | } |
| 6172 | |
| 6173 | if (!copy) bc_vec_pop(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6174 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6175 | } |
| 6176 | #endif // ENABLE_DC |
| 6177 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6178 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6179 | |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 6180 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6181 | } |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 6182 | #if ERRORS_ARE_FATAL |
| 6183 | # define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 6184 | #else |
| 6185 | # define zbc_program_pushVar(...) bc_program_pushVar(__VA_ARGS__) |
| 6186 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6187 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6188 | static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6189 | char inst) |
| 6190 | { |
| 6191 | BcStatus s = BC_STATUS_SUCCESS; |
| 6192 | BcResult r; |
| 6193 | BcNum *num; |
| 6194 | |
| 6195 | r.d.id.name = bc_program_name(code, bgn); |
| 6196 | |
| 6197 | if (inst == BC_INST_ARRAY) { |
| 6198 | r.t = BC_RESULT_ARRAY; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6199 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6200 | } |
| 6201 | else { |
| 6202 | |
| 6203 | BcResult *operand; |
| 6204 | unsigned long temp; |
| 6205 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6206 | s = zbc_program_prep(&operand, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6207 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6208 | s = zbc_num_ulong(num, &temp); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6209 | if (s) goto err; |
| 6210 | |
| 6211 | if (temp > BC_MAX_DIM) { |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 6212 | 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] | 6213 | goto err; |
| 6214 | } |
| 6215 | |
| 6216 | r.d.id.idx = (size_t) temp; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6217 | bc_program_retire(&r, BC_RESULT_ARRAY_ELEM); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6218 | } |
| 6219 | |
| 6220 | err: |
| 6221 | if (s) free(r.d.id.name); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6222 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6223 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6224 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6225 | # define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6226 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6227 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6228 | #if ENABLE_BC |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6229 | static BC_STATUS zbc_program_incdec(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6230 | { |
| 6231 | BcStatus s; |
| 6232 | BcResult *ptr, res, copy; |
| 6233 | BcNum *num = NULL; |
| 6234 | char inst2 = inst; |
| 6235 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6236 | s = zbc_program_prep(&ptr, &num); |
| 6237 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6238 | |
| 6239 | if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) { |
| 6240 | copy.t = BC_RESULT_TEMP; |
| 6241 | bc_num_init(©.d.n, num->len); |
| 6242 | bc_num_copy(©.d.n, num); |
| 6243 | } |
| 6244 | |
| 6245 | res.t = BC_RESULT_ONE; |
| 6246 | inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ? |
| 6247 | BC_INST_ASSIGN_PLUS : |
| 6248 | BC_INST_ASSIGN_MINUS; |
| 6249 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6250 | bc_vec_push(&G.prog.results, &res); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6251 | s = zbc_program_assign(inst); |
| 6252 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6253 | |
| 6254 | if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6255 | bc_vec_pop(&G.prog.results); |
| 6256 | bc_vec_push(&G.prog.results, ©); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6257 | } |
| 6258 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6259 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6260 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6261 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6262 | # define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6263 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6264 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6265 | static BC_STATUS zbc_program_call(char *code, size_t *idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6266 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6267 | BcInstPtr ip; |
| 6268 | size_t i, nparams = bc_program_index(code, idx); |
| 6269 | BcFunc *func; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6270 | BcId *a; |
| 6271 | BcResultData param; |
| 6272 | BcResult *arg; |
| 6273 | |
| 6274 | ip.idx = 0; |
| 6275 | ip.func = bc_program_index(code, idx); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6276 | func = bc_program_func(ip.func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6277 | |
Denys Vlasenko | 04a1c76 | 2018-12-03 21:10:57 +0100 | [diff] [blame] | 6278 | if (func->code.len == 0) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6279 | RETURN_STATUS(bc_error("undefined function")); |
Denys Vlasenko | 04a1c76 | 2018-12-03 21:10:57 +0100 | [diff] [blame] | 6280 | } |
| 6281 | if (nparams != func->nparams) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6282 | 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] | 6283 | } |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6284 | ip.len = G.prog.results.len - nparams; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6285 | |
| 6286 | for (i = 0; i < nparams; ++i) { |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6287 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6288 | |
| 6289 | a = bc_vec_item(&func->autos, nparams - 1 - i); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6290 | arg = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6291 | |
| 6292 | 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] | 6293 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6294 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6295 | s = zbc_program_copyToVar(a->name, a->idx); |
| 6296 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6297 | } |
| 6298 | |
| 6299 | for (; i < func->autos.len; ++i) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6300 | BcVec *v; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6301 | |
| 6302 | a = bc_vec_item(&func->autos, i); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6303 | v = bc_program_search(a->name, a->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6304 | |
| 6305 | if (a->idx) { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6306 | bc_num_init_DEF_SIZE(¶m.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6307 | bc_vec_push(v, ¶m.n); |
| 6308 | } |
| 6309 | else { |
| 6310 | bc_array_init(¶m.v, true); |
| 6311 | bc_vec_push(v, ¶m.v); |
| 6312 | } |
| 6313 | } |
| 6314 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6315 | bc_vec_push(&G.prog.stack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6316 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6317 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6318 | } |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6319 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6320 | # define zbc_program_call(...) (zbc_program_call(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6321 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6322 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6323 | static BC_STATUS zbc_program_return(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6324 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6325 | BcResult res; |
| 6326 | BcFunc *f; |
| 6327 | size_t i; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6328 | BcInstPtr *ip = bc_vec_top(&G.prog.stack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6329 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6330 | 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] | 6331 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6332 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6333 | f = bc_program_func(ip->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6334 | res.t = BC_RESULT_TEMP; |
| 6335 | |
| 6336 | if (inst == BC_INST_RET) { |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6337 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6338 | BcNum *num; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6339 | BcResult *operand = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6340 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6341 | s = zbc_program_num(operand, &num, false); |
| 6342 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6343 | bc_num_init(&res.d.n, num->len); |
| 6344 | bc_num_copy(&res.d.n, num); |
| 6345 | } |
| 6346 | else { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6347 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 6348 | //bc_num_zero(&res.d.n); - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6349 | } |
| 6350 | |
| 6351 | // We need to pop arguments as well, so this takes that into account. |
| 6352 | for (i = 0; i < f->autos.len; ++i) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6353 | BcVec *v; |
| 6354 | BcId *a = bc_vec_item(&f->autos, i); |
| 6355 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6356 | v = bc_program_search(a->name, a->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6357 | bc_vec_pop(v); |
| 6358 | } |
| 6359 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6360 | bc_vec_npop(&G.prog.results, G.prog.results.len - ip->len); |
| 6361 | bc_vec_push(&G.prog.results, &res); |
| 6362 | bc_vec_pop(&G.prog.stack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6363 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6364 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6365 | } |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6366 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6367 | # define zbc_program_return(...) (zbc_program_return(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6368 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6369 | #endif // ENABLE_BC |
| 6370 | |
| 6371 | static unsigned long bc_program_scale(BcNum *n) |
| 6372 | { |
| 6373 | return (unsigned long) n->rdx; |
| 6374 | } |
| 6375 | |
| 6376 | static unsigned long bc_program_len(BcNum *n) |
| 6377 | { |
Denys Vlasenko | a7f1a36 | 2018-12-10 12:57:01 +0100 | [diff] [blame] | 6378 | size_t len = n->len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6379 | |
Denys Vlasenko | a7f1a36 | 2018-12-10 12:57:01 +0100 | [diff] [blame] | 6380 | if (n->rdx != len) return len; |
| 6381 | for (;;) { |
| 6382 | if (len == 0) break; |
| 6383 | len--; |
| 6384 | if (n->num[len] != 0) break; |
| 6385 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6386 | return len; |
| 6387 | } |
| 6388 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6389 | static BC_STATUS zbc_program_builtin(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6390 | { |
| 6391 | BcStatus s; |
| 6392 | BcResult *opnd; |
| 6393 | BcNum *num = NULL; |
| 6394 | BcResult res; |
| 6395 | bool len = inst == BC_INST_LENGTH; |
| 6396 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6397 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6398 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6399 | opnd = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6400 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6401 | s = zbc_program_num(opnd, &num, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6402 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6403 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6404 | #if ENABLE_DC |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6405 | if (!BC_PROG_NUM(opnd, num) && !len) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6406 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6407 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6408 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6409 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6410 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 6411 | 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] | 6412 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6413 | else if (len != 0 && opnd->t == BC_RESULT_ARRAY) { |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6414 | bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6415 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6416 | #endif |
| 6417 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6418 | else if (len != 0 && !BC_PROG_NUM(opnd, num)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6419 | char **str; |
| 6420 | size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx; |
| 6421 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6422 | str = bc_program_str(idx); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6423 | bc_num_ulong2num(&res.d.n, strlen(*str)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6424 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6425 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6426 | else { |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 6427 | 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] | 6428 | } |
| 6429 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6430 | bc_program_retire(&res, BC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6431 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6432 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6433 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6434 | #if ERRORS_ARE_FATAL |
| 6435 | # define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 6436 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6437 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6438 | #if ENABLE_DC |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6439 | static BC_STATUS zbc_program_divmod(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6440 | { |
| 6441 | BcStatus s; |
| 6442 | BcResult *opd1, *opd2, res, res2; |
| 6443 | BcNum *n1, *n2 = NULL; |
| 6444 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6445 | s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6446 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6447 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6448 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6449 | bc_num_init(&res2.d.n, n2->len); |
| 6450 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 6451 | 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] | 6452 | if (s) goto err; |
| 6453 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6454 | bc_program_binOpRetire(&res2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6455 | res.t = BC_RESULT_TEMP; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6456 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6457 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6458 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6459 | |
| 6460 | err: |
| 6461 | bc_num_free(&res2.d.n); |
| 6462 | bc_num_free(&res.d.n); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6463 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6464 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6465 | #if ERRORS_ARE_FATAL |
| 6466 | # define zbc_program_divmod(...) (zbc_program_divmod(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 6467 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6468 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6469 | static BC_STATUS zbc_program_modexp(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6470 | { |
| 6471 | BcStatus s; |
| 6472 | BcResult *r1, *r2, *r3, res; |
| 6473 | BcNum *n1, *n2, *n3; |
| 6474 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6475 | if (!BC_PROG_STACK(&G.prog.results, 3)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6476 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6477 | s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6478 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6479 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6480 | r1 = bc_vec_item_rev(&G.prog.results, 2); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6481 | s = zbc_program_num(r1, &n1, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6482 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6483 | if (!BC_PROG_NUM(r1, n1)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6484 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6485 | |
| 6486 | // Make sure that the values have their pointers updated, if necessary. |
| 6487 | if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) { |
| 6488 | |
| 6489 | if (r1->t == r2->t) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6490 | s = zbc_program_num(r2, &n2, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6491 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6492 | } |
| 6493 | |
| 6494 | if (r1->t == r3->t) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6495 | s = zbc_program_num(r3, &n3, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6496 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6497 | } |
| 6498 | } |
| 6499 | |
| 6500 | bc_num_init(&res.d.n, n3->len); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 6501 | s = zbc_num_modexp(n1, n2, n3, &res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6502 | if (s) goto err; |
| 6503 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6504 | bc_vec_pop(&G.prog.results); |
| 6505 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6506 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6507 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6508 | |
| 6509 | err: |
| 6510 | bc_num_free(&res.d.n); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6511 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6512 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6513 | #if ERRORS_ARE_FATAL |
| 6514 | # define zbc_program_modexp(...) (zbc_program_modexp(__VA_ARGS__), BC_STATUS_SUCCESS) |
| 6515 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6516 | |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6517 | static void bc_program_stackLen(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6518 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6519 | BcResult res; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6520 | size_t len = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6521 | |
| 6522 | res.t = BC_RESULT_TEMP; |
| 6523 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6524 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6525 | bc_num_ulong2num(&res.d.n, len); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6526 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6527 | } |
| 6528 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6529 | static BcStatus bc_program_asciify(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6530 | { |
| 6531 | BcStatus s; |
| 6532 | BcResult *r, res; |
Denys Vlasenko | 4a024c7 | 2018-12-09 13:21:54 +0100 | [diff] [blame] | 6533 | BcNum *num, n; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6534 | char *str, *str2, c; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6535 | size_t len = G.prog.strs.len, idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6536 | unsigned long val; |
| 6537 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6538 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6539 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6540 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6541 | |
Denys Vlasenko | 4a024c7 | 2018-12-09 13:21:54 +0100 | [diff] [blame] | 6542 | num = NULL; // TODO: is this NULL needed? |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6543 | s = zbc_program_num(r, &num, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6544 | if (s) return s; |
| 6545 | |
| 6546 | if (BC_PROG_NUM(r, num)) { |
| 6547 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6548 | bc_num_init_DEF_SIZE(&n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6549 | bc_num_copy(&n, num); |
| 6550 | bc_num_truncate(&n, n.rdx); |
| 6551 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 6552 | s = zbc_num_mod(&n, &G.prog.strmb, &n, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6553 | if (s) goto num_err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6554 | s = zbc_num_ulong(&n, &val); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6555 | if (s) goto num_err; |
| 6556 | |
| 6557 | c = (char) val; |
| 6558 | |
| 6559 | bc_num_free(&n); |
| 6560 | } |
| 6561 | else { |
| 6562 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx; |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6563 | str2 = *bc_program_str(idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6564 | c = str2[0]; |
| 6565 | } |
| 6566 | |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6567 | str = xzalloc(2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6568 | str[0] = c; |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6569 | //str[1] = '\0'; - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6570 | |
| 6571 | str2 = xstrdup(str); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6572 | bc_program_addFunc(str2, &idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6573 | |
| 6574 | if (idx != len + BC_PROG_REQ_FUNCS) { |
| 6575 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6576 | for (idx = 0; idx < G.prog.strs.len; ++idx) { |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6577 | if (strcmp(*bc_program_str(idx), str) == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6578 | len = idx; |
| 6579 | break; |
| 6580 | } |
| 6581 | } |
| 6582 | |
| 6583 | free(str); |
| 6584 | } |
| 6585 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6586 | bc_vec_push(&G.prog.strs, &str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6587 | |
| 6588 | res.t = BC_RESULT_STR; |
| 6589 | res.d.id.idx = len; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6590 | bc_vec_pop(&G.prog.results); |
| 6591 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6592 | |
| 6593 | return BC_STATUS_SUCCESS; |
| 6594 | |
| 6595 | num_err: |
| 6596 | bc_num_free(&n); |
| 6597 | return s; |
| 6598 | } |
| 6599 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6600 | static BC_STATUS zbc_program_printStream(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6601 | { |
| 6602 | BcStatus s; |
| 6603 | BcResult *r; |
| 6604 | BcNum *n = NULL; |
| 6605 | size_t idx; |
| 6606 | char *str; |
| 6607 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6608 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6609 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6610 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6611 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6612 | s = zbc_program_num(r, &n, false); |
| 6613 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6614 | |
| 6615 | if (BC_PROG_NUM(r, n)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6616 | s = zbc_num_stream(n, &G.prog.strmb); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6617 | else { |
| 6618 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx; |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6619 | str = *bc_program_str(idx); |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 6620 | printf("%s", str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6621 | } |
| 6622 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6623 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6624 | } |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6625 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6626 | # define zbc_program_printStream(...) (zbc_program_printStream(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 6627 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6628 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6629 | static BC_STATUS zbc_program_nquit(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6630 | { |
| 6631 | BcStatus s; |
| 6632 | BcResult *opnd; |
| 6633 | BcNum *num = NULL; |
| 6634 | unsigned long val; |
| 6635 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6636 | s = zbc_program_prep(&opnd, &num); |
| 6637 | if (s) RETURN_STATUS(s); |
| 6638 | s = zbc_num_ulong(num, &val); |
| 6639 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6640 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6641 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6642 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6643 | if (G.prog.stack.len < val) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6644 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 6645 | if (G.prog.stack.len == val) { |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 6646 | QUIT_OR_RETURN_TO_MAIN; |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 6647 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6648 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6649 | bc_vec_npop(&G.prog.stack, val); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6650 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6651 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6652 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6653 | #if ERRORS_ARE_FATAL |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6654 | # define zbc_program_nquit(...) (zbc_program_nquit(__VA_ARGS__), BC_STATUS_SUCCESS) |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6655 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6656 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6657 | static BcStatus bc_program_execStr(char *code, size_t *bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6658 | bool cond) |
| 6659 | { |
| 6660 | BcStatus s = BC_STATUS_SUCCESS; |
| 6661 | BcResult *r; |
| 6662 | char **str; |
| 6663 | BcFunc *f; |
| 6664 | BcParse prs; |
| 6665 | BcInstPtr ip; |
| 6666 | size_t fidx, sidx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6667 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6668 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6669 | return bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6670 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6671 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6672 | |
| 6673 | if (cond) { |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6674 | BcNum *n = n; // for compiler |
| 6675 | bool exec; |
| 6676 | char *name; |
| 6677 | char *then_name = bc_program_name(code, bgn); |
| 6678 | char *else_name = NULL; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6679 | |
| 6680 | if (code[*bgn] == BC_PARSE_STREND) |
| 6681 | (*bgn) += 1; |
| 6682 | else |
| 6683 | else_name = bc_program_name(code, bgn); |
| 6684 | |
| 6685 | exec = r->d.n.len != 0; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6686 | name = then_name; |
| 6687 | if (!exec && else_name != NULL) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6688 | exec = true; |
| 6689 | name = else_name; |
| 6690 | } |
| 6691 | |
| 6692 | if (exec) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6693 | BcVec *v; |
| 6694 | v = bc_program_search(name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6695 | n = bc_vec_top(v); |
| 6696 | } |
| 6697 | |
| 6698 | free(then_name); |
| 6699 | free(else_name); |
| 6700 | |
| 6701 | if (!exec) goto exit; |
| 6702 | if (!BC_PROG_STR(n)) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6703 | s = bc_error_variable_is_wrong_type(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6704 | goto exit; |
| 6705 | } |
| 6706 | |
| 6707 | sidx = n->rdx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6708 | } else { |
| 6709 | if (r->t == BC_RESULT_STR) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6710 | sidx = r->d.id.idx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6711 | } else if (r->t == BC_RESULT_VAR) { |
| 6712 | BcNum *n; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6713 | s = zbc_program_num(r, &n, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6714 | if (s || !BC_PROG_STR(n)) goto exit; |
| 6715 | sidx = n->rdx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6716 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6717 | goto exit; |
| 6718 | } |
| 6719 | |
| 6720 | fidx = sidx + BC_PROG_REQ_FUNCS; |
| 6721 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6722 | str = bc_program_str(sidx); |
| 6723 | f = bc_program_func(fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6724 | |
| 6725 | if (f->code.len == 0) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6726 | common_parse_init(&prs, fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6727 | s = bc_parse_text(&prs, *str); |
| 6728 | if (s) goto err; |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 6729 | s = common_parse_expr(&prs, BC_PARSE_NOCALL); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6730 | if (s) goto err; |
| 6731 | |
| 6732 | if (prs.l.t.t != BC_LEX_EOF) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6733 | s = bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6734 | goto err; |
| 6735 | } |
| 6736 | |
| 6737 | bc_parse_free(&prs); |
| 6738 | } |
| 6739 | |
| 6740 | ip.idx = 0; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6741 | ip.len = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6742 | ip.func = fidx; |
| 6743 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6744 | bc_vec_pop(&G.prog.results); |
| 6745 | bc_vec_push(&G.prog.stack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6746 | |
| 6747 | return BC_STATUS_SUCCESS; |
| 6748 | |
| 6749 | err: |
| 6750 | bc_parse_free(&prs); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6751 | f = bc_program_func(fidx); |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 6752 | bc_vec_pop_all(&f->code); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6753 | exit: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6754 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6755 | return s; |
| 6756 | } |
| 6757 | #endif // ENABLE_DC |
| 6758 | |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6759 | static void bc_program_pushGlobal(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6760 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6761 | BcResult res; |
| 6762 | unsigned long val; |
| 6763 | |
| 6764 | res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE; |
| 6765 | if (inst == BC_INST_IBASE) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6766 | val = (unsigned long) G.prog.ib_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6767 | else if (inst == BC_INST_SCALE) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6768 | val = (unsigned long) G.prog.scale; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6769 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6770 | val = (unsigned long) G.prog.ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6771 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6772 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6773 | bc_num_ulong2num(&res.d.n, val); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6774 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6775 | } |
| 6776 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6777 | static void bc_program_addFunc(char *name, size_t *idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6778 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6779 | BcId entry, *entry_ptr; |
| 6780 | BcFunc f; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 6781 | int inserted; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6782 | |
| 6783 | entry.name = name; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6784 | entry.idx = G.prog.fns.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6785 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 6786 | inserted = bc_map_insert(&G.prog.fn_map, &entry, idx); |
| 6787 | if (!inserted) free(name); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6788 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6789 | entry_ptr = bc_vec_item(&G.prog.fn_map, *idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6790 | *idx = entry_ptr->idx; |
| 6791 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 6792 | if (!inserted) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6793 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6794 | BcFunc *func = bc_program_func(entry_ptr->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6795 | |
| 6796 | // We need to reset these, so the function can be repopulated. |
| 6797 | func->nparams = 0; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 6798 | bc_vec_pop_all(&func->autos); |
| 6799 | bc_vec_pop_all(&func->code); |
| 6800 | bc_vec_pop_all(&func->labels); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6801 | } |
| 6802 | else { |
| 6803 | bc_func_init(&f); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6804 | bc_vec_push(&G.prog.fns, &f); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6805 | } |
| 6806 | } |
| 6807 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6808 | static BcStatus bc_program_exec(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6809 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6810 | BcResult r, *ptr; |
| 6811 | BcNum *num; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6812 | BcInstPtr *ip = bc_vec_top(&G.prog.stack); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6813 | BcFunc *func = bc_program_func(ip->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6814 | char *code = func->code.v; |
| 6815 | bool cond = false; |
| 6816 | |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6817 | while (ip->idx < func->code.len) { |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6818 | BcStatus s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6819 | char inst = code[(ip->idx)++]; |
| 6820 | |
| 6821 | switch (inst) { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6822 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6823 | case BC_INST_JUMP_ZERO: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6824 | s = zbc_program_prep(&ptr, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6825 | if (s) return s; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6826 | cond = !bc_num_cmp(num, &G.prog.zero); |
| 6827 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6828 | // Fallthrough. |
| 6829 | case BC_INST_JUMP: { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6830 | size_t *addr; |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6831 | size_t idx = bc_program_index(code, &ip->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6832 | addr = bc_vec_item(&func->labels, idx); |
| 6833 | if (inst == BC_INST_JUMP || cond) ip->idx = *addr; |
| 6834 | break; |
| 6835 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6836 | case BC_INST_CALL: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6837 | s = zbc_program_call(code, &ip->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6838 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6839 | case BC_INST_INC_PRE: |
| 6840 | case BC_INST_DEC_PRE: |
| 6841 | case BC_INST_INC_POST: |
| 6842 | case BC_INST_DEC_POST: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6843 | s = zbc_program_incdec(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6844 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6845 | case BC_INST_HALT: |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 6846 | QUIT_OR_RETURN_TO_MAIN; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6847 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6848 | case BC_INST_RET: |
| 6849 | case BC_INST_RET0: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6850 | s = zbc_program_return(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6851 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6852 | case BC_INST_BOOL_OR: |
| 6853 | case BC_INST_BOOL_AND: |
| 6854 | #endif // ENABLE_BC |
| 6855 | case BC_INST_REL_EQ: |
| 6856 | case BC_INST_REL_LE: |
| 6857 | case BC_INST_REL_GE: |
| 6858 | case BC_INST_REL_NE: |
| 6859 | case BC_INST_REL_LT: |
| 6860 | case BC_INST_REL_GT: |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 6861 | s = zbc_program_logical(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6862 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6863 | case BC_INST_READ: |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6864 | s = bc_program_read(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6865 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6866 | case BC_INST_VAR: |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 6867 | s = zbc_program_pushVar(code, &ip->idx, false, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6868 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6869 | case BC_INST_ARRAY_ELEM: |
| 6870 | case BC_INST_ARRAY: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6871 | s = zbc_program_pushArray(code, &ip->idx, inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6872 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6873 | case BC_INST_LAST: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6874 | r.t = BC_RESULT_LAST; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6875 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6876 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6877 | case BC_INST_IBASE: |
| 6878 | case BC_INST_SCALE: |
| 6879 | case BC_INST_OBASE: |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6880 | bc_program_pushGlobal(inst); |
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_SCALE_FUNC: |
| 6883 | case BC_INST_LENGTH: |
| 6884 | case BC_INST_SQRT: |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6885 | s = zbc_program_builtin(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_NUM: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6888 | r.t = BC_RESULT_CONSTANT; |
| 6889 | r.d.id.idx = bc_program_index(code, &ip->idx); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6890 | bc_vec_push(&G.prog.results, &r); |
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_POP: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6893 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6894 | s = bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6895 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6896 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6897 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6898 | case BC_INST_POP_EXEC: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6899 | bc_vec_pop(&G.prog.stack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6900 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6901 | case BC_INST_PRINT: |
| 6902 | case BC_INST_PRINT_POP: |
| 6903 | case BC_INST_PRINT_STR: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6904 | s = zbc_program_print(inst, 0); |
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_STR: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6907 | r.t = BC_RESULT_STR; |
| 6908 | r.d.id.idx = bc_program_index(code, &ip->idx); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6909 | bc_vec_push(&G.prog.results, &r); |
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_POWER: |
| 6912 | case BC_INST_MULTIPLY: |
| 6913 | case BC_INST_DIVIDE: |
| 6914 | case BC_INST_MODULUS: |
| 6915 | case BC_INST_PLUS: |
| 6916 | case BC_INST_MINUS: |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 6917 | s = zbc_program_op(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6918 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6919 | case BC_INST_BOOL_NOT: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6920 | s = zbc_program_prep(&ptr, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6921 | if (s) return s; |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6922 | bc_num_init_DEF_SIZE(&r.d.n); |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6923 | if (!bc_num_cmp(num, &G.prog.zero)) |
| 6924 | bc_num_one(&r.d.n); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 6925 | //else bc_num_zero(&r.d.n); - already is |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6926 | bc_program_retire(&r, BC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6927 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6928 | case BC_INST_NEG: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6929 | s = zbc_program_negate(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6930 | break; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6931 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6932 | case BC_INST_ASSIGN_POWER: |
| 6933 | case BC_INST_ASSIGN_MULTIPLY: |
| 6934 | case BC_INST_ASSIGN_DIVIDE: |
| 6935 | case BC_INST_ASSIGN_MODULUS: |
| 6936 | case BC_INST_ASSIGN_PLUS: |
| 6937 | case BC_INST_ASSIGN_MINUS: |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6938 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6939 | case BC_INST_ASSIGN: |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6940 | s = zbc_program_assign(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6941 | break; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6942 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6943 | case BC_INST_MODEXP: |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6944 | s = zbc_program_modexp(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6945 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6946 | case BC_INST_DIVMOD: |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6947 | s = zbc_program_divmod(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6948 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6949 | case BC_INST_EXECUTE: |
| 6950 | case BC_INST_EXEC_COND: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6951 | cond = inst == BC_INST_EXEC_COND; |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6952 | s = bc_program_execStr(code, &ip->idx, cond); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6953 | break; |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6954 | case BC_INST_PRINT_STACK: { |
| 6955 | size_t idx; |
| 6956 | for (idx = 0; idx < G.prog.results.len; ++idx) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6957 | s = zbc_program_print(BC_INST_PRINT, idx); |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6958 | if (s) break; |
| 6959 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6960 | break; |
| 6961 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6962 | case BC_INST_CLEAR_STACK: |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 6963 | bc_vec_pop_all(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6964 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6965 | case BC_INST_STACK_LEN: |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6966 | bc_program_stackLen(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6967 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6968 | case BC_INST_DUPLICATE: |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6969 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6970 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6971 | ptr = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6972 | bc_result_copy(&r, ptr); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6973 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6974 | break; |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6975 | case BC_INST_SWAP: { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6976 | BcResult *ptr2; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6977 | if (!BC_PROG_STACK(&G.prog.results, 2)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6978 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6979 | ptr = bc_vec_item_rev(&G.prog.results, 0); |
| 6980 | ptr2 = bc_vec_item_rev(&G.prog.results, 1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6981 | memcpy(&r, ptr, sizeof(BcResult)); |
| 6982 | memcpy(ptr, ptr2, sizeof(BcResult)); |
| 6983 | memcpy(ptr2, &r, sizeof(BcResult)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6984 | break; |
| 6985 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6986 | case BC_INST_ASCIIFY: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6987 | s = bc_program_asciify(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6988 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6989 | case BC_INST_PRINT_STREAM: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6990 | s = zbc_program_printStream(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6991 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6992 | case BC_INST_LOAD: |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6993 | case BC_INST_PUSH_VAR: { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6994 | bool copy = inst == BC_INST_LOAD; |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 6995 | s = zbc_program_pushVar(code, &ip->idx, true, copy); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6996 | break; |
| 6997 | } |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6998 | case BC_INST_PUSH_TO_VAR: { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6999 | char *name = bc_program_name(code, &ip->idx); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 7000 | s = zbc_program_copyToVar(name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7001 | free(name); |
| 7002 | break; |
| 7003 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7004 | case BC_INST_QUIT: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7005 | if (G.prog.stack.len <= 2) |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 7006 | QUIT_OR_RETURN_TO_MAIN; |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 7007 | bc_vec_npop(&G.prog.stack, 2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7008 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7009 | case BC_INST_NQUIT: |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 7010 | s = zbc_program_nquit(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7011 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7012 | #endif // ENABLE_DC |
| 7013 | } |
| 7014 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7015 | if (s || G_interrupt) { |
| 7016 | bc_program_reset(); |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 7017 | return s; |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7018 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7019 | |
| 7020 | // If the stack has changed, pointers may be invalid. |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7021 | ip = bc_vec_top(&G.prog.stack); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 7022 | func = bc_program_func(ip->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7023 | code = func->code.v; |
| 7024 | } |
| 7025 | |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 7026 | return BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7027 | } |
| 7028 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7029 | #if ENABLE_BC |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 7030 | static void bc_vm_info(void) |
| 7031 | { |
| 7032 | printf("%s "BB_VER"\n" |
| 7033 | "Copyright (c) 2018 Gavin D. Howard and contributors\n" |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 7034 | , applet_name); |
| 7035 | } |
| 7036 | |
| 7037 | static void bc_args(char **argv) |
| 7038 | { |
| 7039 | unsigned opts; |
| 7040 | int i; |
| 7041 | |
| 7042 | GETOPT_RESET(); |
| 7043 | #if ENABLE_FEATURE_BC_LONG_OPTIONS |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7044 | opts = option_mask32 |= getopt32long(argv, "wvsqli", |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 7045 | "warn\0" No_argument "w" |
| 7046 | "version\0" No_argument "v" |
| 7047 | "standard\0" No_argument "s" |
| 7048 | "quiet\0" No_argument "q" |
| 7049 | "mathlib\0" No_argument "l" |
| 7050 | "interactive\0" No_argument "i" |
| 7051 | ); |
| 7052 | #else |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7053 | opts = option_mask32 |= getopt32(argv, "wvsqli"); |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 7054 | #endif |
| 7055 | if (getenv("POSIXLY_CORRECT")) |
| 7056 | option_mask32 |= BC_FLAG_S; |
| 7057 | |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 7058 | if (opts & BC_FLAG_V) { |
| 7059 | bc_vm_info(); |
| 7060 | exit(0); |
| 7061 | } |
| 7062 | |
| 7063 | for (i = optind; argv[i]; ++i) |
| 7064 | bc_vec_push(&G.files, argv + i); |
| 7065 | } |
| 7066 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7067 | static void bc_vm_envArgs(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7068 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7069 | BcVec v; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7070 | char *buf; |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 7071 | char *env_args = getenv("BC_ENV_ARGS"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7072 | |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7073 | if (!env_args) return; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7074 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7075 | G.env_args = xstrdup(env_args); |
| 7076 | buf = G.env_args; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7077 | |
| 7078 | bc_vec_init(&v, sizeof(char *), NULL); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7079 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7080 | while (*(buf = skip_whitespace(buf)) != '\0') { |
| 7081 | bc_vec_push(&v, &buf); |
| 7082 | buf = skip_non_whitespace(buf); |
| 7083 | if (!*buf) |
| 7084 | break; |
| 7085 | *buf++ = '\0'; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7086 | } |
| 7087 | |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 7088 | // NULL terminate, and pass argv[] so that first arg is argv[1] |
| 7089 | if (sizeof(int) == sizeof(char*)) { |
| 7090 | bc_vec_push(&v, &const_int_0); |
| 7091 | } else { |
| 7092 | static char *const nullptr = NULL; |
| 7093 | bc_vec_push(&v, &nullptr); |
| 7094 | } |
| 7095 | bc_args(((char **)v.v) - 1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7096 | |
| 7097 | bc_vec_free(&v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7098 | } |
| 7099 | #endif // ENABLE_BC |
| 7100 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7101 | static unsigned bc_vm_envLen(const char *var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7102 | { |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7103 | char *lenv; |
| 7104 | unsigned len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7105 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7106 | lenv = getenv(var); |
| 7107 | len = BC_NUM_PRINT_WIDTH; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7108 | if (!lenv) return len; |
| 7109 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7110 | len = bb_strtou(lenv, NULL, 10) - 1; |
| 7111 | if (errno || len < 2 || len >= INT_MAX) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7112 | len = BC_NUM_PRINT_WIDTH; |
| 7113 | |
| 7114 | return len; |
| 7115 | } |
| 7116 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7117 | static BcStatus bc_vm_process(const char *text) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7118 | { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7119 | BcStatus s = bc_parse_text(&G.prs, text); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7120 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7121 | if (s) return s; |
| 7122 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7123 | while (G.prs.l.t.t != BC_LEX_EOF) { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7124 | s = G.prs.parse(&G.prs); |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 7125 | if (s) return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7126 | } |
| 7127 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7128 | if (BC_PARSE_CAN_EXEC(&G.prs)) { |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7129 | s = bc_program_exec(); |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 7130 | fflush_and_check(); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7131 | if (s) |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7132 | bc_program_reset(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7133 | } |
| 7134 | |
| 7135 | return s; |
| 7136 | } |
| 7137 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7138 | static BcStatus bc_vm_file(const char *file) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7139 | { |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7140 | const char *sv_file; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7141 | char *data; |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7142 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7143 | BcFunc *main_func; |
| 7144 | BcInstPtr *ip; |
| 7145 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 7146 | data = bc_read_file(file); |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 7147 | if (!data) return bc_error_fmt("file '%s' is not text", file); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7148 | |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7149 | sv_file = G.prog.file; |
| 7150 | G.prog.file = file; |
| 7151 | bc_lex_file(&G.prs.l); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7152 | s = bc_vm_process(data); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7153 | if (s) goto err; |
| 7154 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 7155 | main_func = bc_program_func(BC_PROG_MAIN); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7156 | ip = bc_vec_item(&G.prog.stack, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7157 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 7158 | if (main_func->code.len < ip->idx) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 7159 | s = bc_error_fmt("file '%s' is not executable", file); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7160 | |
| 7161 | err: |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7162 | G.prog.file = sv_file; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7163 | free(data); |
| 7164 | return s; |
| 7165 | } |
| 7166 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7167 | static BcStatus bc_vm_stdin(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7168 | { |
Denys Vlasenko | a0c421c | 2018-12-02 20:16:52 +0100 | [diff] [blame] | 7169 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7170 | BcVec buf, buffer; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7171 | size_t len, i, str = 0; |
Denys Vlasenko | a0c421c | 2018-12-02 20:16:52 +0100 | [diff] [blame] | 7172 | bool comment = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7173 | |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7174 | G.prog.file = NULL; |
| 7175 | bc_lex_file(&G.prs.l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7176 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 7177 | bc_char_vec_init(&buffer); |
| 7178 | bc_char_vec_init(&buf); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 7179 | bc_vec_pushZeroByte(&buffer); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7180 | |
| 7181 | // This loop is complex because the vm tries not to send any lines that end |
| 7182 | // with a backslash to the parser. The reason for that is because the parser |
| 7183 | // treats a backslash+newline combo as whitespace, per the bc spec. In that |
| 7184 | // case, and for strings and comments, the parser will expect more stuff. |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 7185 | while ((s = bc_read_line(&buf)) == BC_STATUS_SUCCESS) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7186 | |
| 7187 | char *string = buf.v; |
| 7188 | |
| 7189 | len = buf.len - 1; |
| 7190 | |
| 7191 | if (len == 1) { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7192 | if (str && buf.v[0] == G.send) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7193 | str -= 1; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7194 | else if (buf.v[0] == G.sbgn) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7195 | str += 1; |
| 7196 | } |
| 7197 | else if (len > 1 || comment) { |
| 7198 | |
| 7199 | for (i = 0; i < len; ++i) { |
| 7200 | |
Denys Vlasenko | a0c421c | 2018-12-02 20:16:52 +0100 | [diff] [blame] | 7201 | bool notend = len > i + 1; |
| 7202 | char c = string[i]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7203 | |
| 7204 | if (i - 1 > len || string[i - 1] != '\\') { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7205 | if (G.sbgn == G.send) |
| 7206 | str ^= c == G.sbgn; |
| 7207 | else if (c == G.send) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7208 | str -= 1; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7209 | else if (c == G.sbgn) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7210 | str += 1; |
| 7211 | } |
| 7212 | |
| 7213 | if (c == '/' && notend && !comment && string[i + 1] == '*') { |
| 7214 | comment = true; |
| 7215 | break; |
| 7216 | } |
| 7217 | else if (c == '*' && notend && comment && string[i + 1] == '/') |
| 7218 | comment = false; |
| 7219 | } |
| 7220 | |
| 7221 | if (str || comment || string[len - 2] == '\\') { |
| 7222 | bc_vec_concat(&buffer, buf.v); |
| 7223 | continue; |
| 7224 | } |
| 7225 | } |
| 7226 | |
| 7227 | bc_vec_concat(&buffer, buf.v); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7228 | s = bc_vm_process(buffer.v); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7229 | if (s) { |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7230 | if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin) { |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 7231 | // Debug config, non-interactive mode: |
| 7232 | // return all the way back to main. |
| 7233 | // Non-debug builds do not come here, they exit. |
| 7234 | break; |
| 7235 | } |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7236 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7237 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 7238 | bc_vec_pop_all(&buffer); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7239 | } |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 7240 | if (s == BC_STATUS_EOF) // input EOF (^D) is not an error |
| 7241 | s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7242 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 7243 | if (str) { |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7244 | s = bc_error("string end could not be found"); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 7245 | } |
| 7246 | else if (comment) { |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7247 | s = bc_error("comment end could not be found"); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 7248 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7249 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7250 | bc_vec_free(&buf); |
| 7251 | bc_vec_free(&buffer); |
| 7252 | return s; |
| 7253 | } |
| 7254 | |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7255 | #if ENABLE_BC |
| 7256 | static const char bc_lib[] = { |
| 7257 | "scale=20" |
| 7258 | "\n" "define e(x){" |
| 7259 | "\n" "auto b,s,n,r,d,i,p,f,v" |
| 7260 | "\n" "b=ibase" |
| 7261 | "\n" "ibase=A" |
| 7262 | "\n" "if(x<0){" |
| 7263 | "\n" "n=1" |
| 7264 | "\n" "x=-x" |
| 7265 | "\n" "}" |
| 7266 | "\n" "s=scale" |
| 7267 | "\n" "r=6+s+0.44*x" |
| 7268 | "\n" "scale=scale(x)+1" |
| 7269 | "\n" "while(x>1){" |
| 7270 | "\n" "d+=1" |
| 7271 | "\n" "x/=2" |
| 7272 | "\n" "scale+=1" |
| 7273 | "\n" "}" |
| 7274 | "\n" "scale=r" |
| 7275 | "\n" "r=x+1" |
| 7276 | "\n" "p=x" |
| 7277 | "\n" "f=v=1" |
| 7278 | "\n" "for(i=2;v!=0;++i){" |
| 7279 | "\n" "p*=x" |
| 7280 | "\n" "f*=i" |
| 7281 | "\n" "v=p/f" |
| 7282 | "\n" "r+=v" |
| 7283 | "\n" "}" |
| 7284 | "\n" "while((d--)!=0)r*=r" |
| 7285 | "\n" "scale=s" |
| 7286 | "\n" "ibase=b" |
| 7287 | "\n" "if(n!=0)return(1/r)" |
| 7288 | "\n" "return(r/1)" |
| 7289 | "\n" "}" |
| 7290 | "\n" "define l(x){" |
| 7291 | "\n" "auto b,s,r,p,a,q,i,v" |
| 7292 | "\n" "b=ibase" |
| 7293 | "\n" "ibase=A" |
| 7294 | "\n" "if(x<=0){" |
| 7295 | "\n" "r=(1-10^scale)/1" |
| 7296 | "\n" "ibase=b" |
| 7297 | "\n" "return(r)" |
| 7298 | "\n" "}" |
| 7299 | "\n" "s=scale" |
| 7300 | "\n" "scale+=6" |
| 7301 | "\n" "p=2" |
| 7302 | "\n" "while(x>=2){" |
| 7303 | "\n" "p*=2" |
| 7304 | "\n" "x=sqrt(x)" |
| 7305 | "\n" "}" |
| 7306 | "\n" "while(x<=0.5){" |
| 7307 | "\n" "p*=2" |
| 7308 | "\n" "x=sqrt(x)" |
| 7309 | "\n" "}" |
| 7310 | "\n" "r=a=(x-1)/(x+1)" |
| 7311 | "\n" "q=a*a" |
| 7312 | "\n" "v=1" |
| 7313 | "\n" "for(i=3;v!=0;i+=2){" |
| 7314 | "\n" "a*=q" |
| 7315 | "\n" "v=a/i" |
| 7316 | "\n" "r+=v" |
| 7317 | "\n" "}" |
| 7318 | "\n" "r*=p" |
| 7319 | "\n" "scale=s" |
| 7320 | "\n" "ibase=b" |
| 7321 | "\n" "return(r/1)" |
| 7322 | "\n" "}" |
| 7323 | "\n" "define s(x){" |
| 7324 | "\n" "auto b,s,r,n,a,q,i" |
| 7325 | "\n" "b=ibase" |
| 7326 | "\n" "ibase=A" |
| 7327 | "\n" "s=scale" |
| 7328 | "\n" "scale=1.1*s+2" |
| 7329 | "\n" "a=a(1)" |
| 7330 | "\n" "if(x<0){" |
| 7331 | "\n" "n=1" |
| 7332 | "\n" "x=-x" |
| 7333 | "\n" "}" |
| 7334 | "\n" "scale=0" |
| 7335 | "\n" "q=(x/a+2)/4" |
| 7336 | "\n" "x=x-4*q*a" |
| 7337 | "\n" "if(q%2!=0)x=-x" |
| 7338 | "\n" "scale=s+2" |
| 7339 | "\n" "r=a=x" |
| 7340 | "\n" "q=-x*x" |
| 7341 | "\n" "for(i=3;a!=0;i+=2){" |
| 7342 | "\n" "a*=q/(i*(i-1))" |
| 7343 | "\n" "r+=a" |
| 7344 | "\n" "}" |
| 7345 | "\n" "scale=s" |
| 7346 | "\n" "ibase=b" |
| 7347 | "\n" "if(n!=0)return(-r/1)" |
| 7348 | "\n" "return(r/1)" |
| 7349 | "\n" "}" |
| 7350 | "\n" "define c(x){" |
| 7351 | "\n" "auto b,s" |
| 7352 | "\n" "b=ibase" |
| 7353 | "\n" "ibase=A" |
| 7354 | "\n" "s=scale" |
| 7355 | "\n" "scale*=1.2" |
| 7356 | "\n" "x=s(2*a(1)+x)" |
| 7357 | "\n" "scale=s" |
| 7358 | "\n" "ibase=b" |
| 7359 | "\n" "return(x/1)" |
| 7360 | "\n" "}" |
| 7361 | "\n" "define a(x){" |
| 7362 | "\n" "auto b,s,r,n,a,m,t,f,i,u" |
| 7363 | "\n" "b=ibase" |
| 7364 | "\n" "ibase=A" |
| 7365 | "\n" "n=1" |
| 7366 | "\n" "if(x<0){" |
| 7367 | "\n" "n=-1" |
| 7368 | "\n" "x=-x" |
| 7369 | "\n" "}" |
| 7370 | "\n" "if(x==1){" |
| 7371 | "\n" "if(scale<65){" |
| 7372 | "\n" "return(.7853981633974483096156608458198757210492923498437764552437361480/n)" |
| 7373 | "\n" "}" |
| 7374 | "\n" "}" |
| 7375 | "\n" "if(x==.2){" |
| 7376 | "\n" "if(scale<65){" |
| 7377 | "\n" "return(.1973955598498807583700497651947902934475851037878521015176889402/n)" |
| 7378 | "\n" "}" |
| 7379 | "\n" "}" |
| 7380 | "\n" "s=scale" |
| 7381 | "\n" "if(x>.2){" |
| 7382 | "\n" "scale+=5" |
| 7383 | "\n" "a=a(.2)" |
| 7384 | "\n" "}" |
| 7385 | "\n" "scale=s+3" |
| 7386 | "\n" "while(x>.2){" |
| 7387 | "\n" "m+=1" |
| 7388 | "\n" "x=(x-.2)/(1+.2*x)" |
| 7389 | "\n" "}" |
| 7390 | "\n" "r=u=x" |
| 7391 | "\n" "f=-x*x" |
| 7392 | "\n" "t=1" |
| 7393 | "\n" "for(i=3;t!=0;i+=2){" |
| 7394 | "\n" "u*=f" |
| 7395 | "\n" "t=u/i" |
| 7396 | "\n" "r+=t" |
| 7397 | "\n" "}" |
| 7398 | "\n" "scale=s" |
| 7399 | "\n" "ibase=b" |
| 7400 | "\n" "return((m*a+r)/n)" |
| 7401 | "\n" "}" |
| 7402 | "\n" "define j(n,x){" |
| 7403 | "\n" "auto b,s,o,a,i,v,f" |
| 7404 | "\n" "b=ibase" |
| 7405 | "\n" "ibase=A" |
| 7406 | "\n" "s=scale" |
| 7407 | "\n" "scale=0" |
| 7408 | "\n" "n/=1" |
| 7409 | "\n" "if(n<0){" |
| 7410 | "\n" "n=-n" |
| 7411 | "\n" "if(n%2==1)o=1" |
| 7412 | "\n" "}" |
| 7413 | "\n" "a=1" |
| 7414 | "\n" "for(i=2;i<=n;++i)a*=i" |
| 7415 | "\n" "scale=1.5*s" |
| 7416 | "\n" "a=(x^n)/2^n/a" |
| 7417 | "\n" "r=v=1" |
| 7418 | "\n" "f=-x*x/4" |
| 7419 | "\n" "scale=scale+length(a)-scale(a)" |
| 7420 | "\n" "for(i=1;v!=0;++i){" |
| 7421 | "\n" "v=v*f/i/(n+i)" |
| 7422 | "\n" "r+=v" |
| 7423 | "\n" "}" |
| 7424 | "\n" "scale=s" |
| 7425 | "\n" "ibase=b" |
| 7426 | "\n" "if(o!=0)a=-a" |
| 7427 | "\n" "return(a*r/1)" |
| 7428 | "\n" "}" |
| 7429 | }; |
| 7430 | #endif // ENABLE_BC |
| 7431 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7432 | static BcStatus bc_vm_exec(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7433 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7434 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7435 | size_t i; |
| 7436 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7437 | #if ENABLE_BC |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 7438 | if (option_mask32 & BC_FLAG_L) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7439 | |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7440 | // We know that internal library is not buggy, |
| 7441 | // thus error checking is normally disabled. |
| 7442 | # define DEBUG_LIB 0 |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7443 | bc_lex_file(&G.prs.l); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7444 | s = bc_parse_text(&G.prs, bc_lib); |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7445 | if (DEBUG_LIB && s) return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7446 | |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7447 | while (G.prs.l.t.t != BC_LEX_EOF) { |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7448 | s = G.prs.parse(&G.prs); |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7449 | if (DEBUG_LIB && s) return s; |
| 7450 | } |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7451 | s = bc_program_exec(); |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7452 | if (DEBUG_LIB && s) return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7453 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7454 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7455 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7456 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7457 | for (i = 0; !s && i < G.files.len; ++i) |
| 7458 | s = bc_vm_file(*((char **) bc_vec_item(&G.files, i))); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7459 | if (ENABLE_FEATURE_CLEAN_UP && s && !G_ttyin) { |
| 7460 | // Debug config, non-interactive mode: |
| 7461 | // return all the way back to main. |
| 7462 | // Non-debug builds do not come here, they exit. |
| 7463 | return s; |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7464 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7465 | |
Denys Vlasenko | 91cde95 | 2018-12-10 20:56:08 +0100 | [diff] [blame] | 7466 | if (IS_BC || (option_mask32 & BC_FLAG_I)) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7467 | s = bc_vm_stdin(); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7468 | |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7469 | if (!s && !BC_PARSE_CAN_EXEC(&G.prs)) |
| 7470 | s = bc_vm_process(""); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7471 | |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 7472 | return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7473 | } |
| 7474 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7475 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 7476 | static void bc_program_free(void) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7477 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7478 | bc_num_free(&G.prog.ib); |
| 7479 | bc_num_free(&G.prog.ob); |
| 7480 | bc_num_free(&G.prog.hexb); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7481 | # if ENABLE_DC |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7482 | bc_num_free(&G.prog.strmb); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7483 | # endif |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7484 | bc_vec_free(&G.prog.fns); |
| 7485 | bc_vec_free(&G.prog.fn_map); |
| 7486 | bc_vec_free(&G.prog.vars); |
| 7487 | bc_vec_free(&G.prog.var_map); |
| 7488 | bc_vec_free(&G.prog.arrs); |
| 7489 | bc_vec_free(&G.prog.arr_map); |
| 7490 | bc_vec_free(&G.prog.strs); |
| 7491 | bc_vec_free(&G.prog.consts); |
| 7492 | bc_vec_free(&G.prog.results); |
| 7493 | bc_vec_free(&G.prog.stack); |
| 7494 | bc_num_free(&G.prog.last); |
| 7495 | bc_num_free(&G.prog.zero); |
| 7496 | bc_num_free(&G.prog.one); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7497 | } |
| 7498 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7499 | static void bc_vm_free(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7500 | { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7501 | bc_vec_free(&G.files); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7502 | bc_program_free(); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7503 | bc_parse_free(&G.prs); |
| 7504 | free(G.env_args); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7505 | } |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7506 | #endif |
| 7507 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7508 | static void bc_program_init(void) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7509 | { |
| 7510 | size_t idx; |
| 7511 | BcInstPtr ip; |
| 7512 | |
| 7513 | /* memset(&G.prog, 0, sizeof(G.prog)); - already is */ |
| 7514 | memset(&ip, 0, sizeof(BcInstPtr)); |
| 7515 | |
| 7516 | /* G.prog.nchars = G.prog.scale = 0; - already is */ |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7517 | bc_num_init_DEF_SIZE(&G.prog.ib); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7518 | bc_num_ten(&G.prog.ib); |
| 7519 | G.prog.ib_t = 10; |
| 7520 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7521 | bc_num_init_DEF_SIZE(&G.prog.ob); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7522 | bc_num_ten(&G.prog.ob); |
| 7523 | G.prog.ob_t = 10; |
| 7524 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7525 | bc_num_init_DEF_SIZE(&G.prog.hexb); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7526 | bc_num_ten(&G.prog.hexb); |
| 7527 | G.prog.hexb.num[0] = 6; |
| 7528 | |
| 7529 | #if ENABLE_DC |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7530 | bc_num_init_DEF_SIZE(&G.prog.strmb); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7531 | bc_num_ulong2num(&G.prog.strmb, UCHAR_MAX + 1); |
| 7532 | #endif |
| 7533 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7534 | bc_num_init_DEF_SIZE(&G.prog.last); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 7535 | //bc_num_zero(&G.prog.last); - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7536 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7537 | bc_num_init_DEF_SIZE(&G.prog.zero); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 7538 | //bc_num_zero(&G.prog.zero); - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7539 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7540 | bc_num_init_DEF_SIZE(&G.prog.one); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7541 | bc_num_one(&G.prog.one); |
| 7542 | |
| 7543 | bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7544 | bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7545 | |
Denys Vlasenko | 1f67e93 | 2018-12-03 00:08:59 +0100 | [diff] [blame] | 7546 | bc_program_addFunc(xstrdup("(main)"), &idx); |
| 7547 | bc_program_addFunc(xstrdup("(read)"), &idx); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7548 | |
| 7549 | bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7550 | bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7551 | |
| 7552 | bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7553 | bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7554 | |
| 7555 | bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free); |
| 7556 | bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free); |
| 7557 | bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free); |
| 7558 | bc_vec_init(&G.prog.stack, sizeof(BcInstPtr), NULL); |
| 7559 | bc_vec_push(&G.prog.stack, &ip); |
| 7560 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7561 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7562 | static int bc_vm_init(const char *env_len) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7563 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7564 | #if ENABLE_FEATURE_EDITING |
| 7565 | G.line_input_state = new_line_input_t(DO_HISTORY); |
| 7566 | #endif |
| 7567 | G.prog.len = bc_vm_envLen(env_len); |
| 7568 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7569 | bc_vec_init(&G.files, sizeof(char *), NULL); |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7570 | if (IS_BC) |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 7571 | IF_BC(bc_vm_envArgs();) |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7572 | bc_program_init(); |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 7573 | if (IS_BC) { |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 7574 | IF_BC(bc_parse_init(&G.prs, BC_PROG_MAIN);) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 7575 | } else { |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 7576 | IF_DC(dc_parse_init(&G.prs, BC_PROG_MAIN);) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 7577 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7578 | |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7579 | if (isatty(0)) { |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7580 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7581 | G_ttyin = 1; |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7582 | // With SA_RESTART, most system calls will restart |
| 7583 | // (IOW: they won't fail with EINTR). |
| 7584 | // In particular, this means ^C won't cause |
| 7585 | // stdout to get into "error state" if SIGINT hits |
| 7586 | // within write() syscall. |
| 7587 | // The downside is that ^C while line input is taken |
| 7588 | // will only be handled after [Enter] since read() |
| 7589 | // from stdin is not interrupted by ^C either, |
| 7590 | // it restarts, thus fgetc() does not return on ^C. |
| 7591 | signal_SA_RESTART_empty_mask(SIGINT, record_signo); |
| 7592 | |
| 7593 | // Without SA_RESTART, this exhibits a bug: |
| 7594 | // "while (1) print 1" and try ^C-ing it. |
| 7595 | // Intermittently, instead of returning to input line, |
| 7596 | // you'll get "output error: Interrupted system call" |
| 7597 | // and exit. |
| 7598 | //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo); |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7599 | #endif |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7600 | return 1; // "tty" |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7601 | } |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7602 | return 0; // "not a tty" |
| 7603 | } |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7604 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7605 | static BcStatus bc_vm_run(void) |
| 7606 | { |
| 7607 | BcStatus st = bc_vm_exec(); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7608 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 7609 | if (G_exiting) // it was actually "halt" or "quit" |
| 7610 | st = EXIT_SUCCESS; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7611 | bc_vm_free(); |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 7612 | # if ENABLE_FEATURE_EDITING |
| 7613 | free_line_input_t(G.line_input_state); |
| 7614 | # endif |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 7615 | FREE_G(); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7616 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7617 | return st; |
| 7618 | } |
| 7619 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7620 | #if ENABLE_BC |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7621 | int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7622 | int bc_main(int argc UNUSED_PARAM, char **argv) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7623 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7624 | int is_tty; |
| 7625 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7626 | INIT_G(); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7627 | G.sbgn = G.send = '"'; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7628 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7629 | is_tty = bc_vm_init("BC_LINE_LENGTH"); |
| 7630 | |
| 7631 | bc_args(argv); |
| 7632 | |
| 7633 | if (is_tty && !(option_mask32 & BC_FLAG_Q)) |
| 7634 | bc_vm_info(); |
| 7635 | |
| 7636 | return bc_vm_run(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7637 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7638 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7639 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7640 | #if ENABLE_DC |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7641 | int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7642 | int dc_main(int argc UNUSED_PARAM, char **argv) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7643 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7644 | int noscript; |
| 7645 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7646 | INIT_G(); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7647 | G.sbgn = '['; |
| 7648 | G.send = ']'; |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 7649 | /* |
| 7650 | * TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width |
| 7651 | * 1 char wider than bc from the same package. |
| 7652 | * Both default width, and xC_LINE_LENGTH=N are wider: |
| 7653 | * "DC_LINE_LENGTH=5 dc -e'123456 p'" prints: |
| 7654 | * 1234\ |
| 7655 | * 56 |
| 7656 | * "echo '123456' | BC_LINE_LENGTH=5 bc" prints: |
| 7657 | * 123\ |
| 7658 | * 456 |
| 7659 | * Do the same, or it's a bug? |
| 7660 | */ |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7661 | bc_vm_init("DC_LINE_LENGTH"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7662 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7663 | // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs |
| 7664 | noscript = BC_FLAG_I; |
| 7665 | for (;;) { |
| 7666 | int n = getopt(argc, argv, "e:f:x"); |
| 7667 | if (n <= 0) |
| 7668 | break; |
| 7669 | switch (n) { |
| 7670 | case 'e': |
| 7671 | noscript = 0; |
| 7672 | n = bc_vm_process(optarg); |
| 7673 | if (n) return n; |
| 7674 | break; |
| 7675 | case 'f': |
| 7676 | noscript = 0; |
| 7677 | bc_vm_file(optarg); |
| 7678 | break; |
| 7679 | case 'x': |
| 7680 | option_mask32 |= DC_FLAG_X; |
| 7681 | break; |
| 7682 | default: |
| 7683 | bb_show_usage(); |
| 7684 | } |
| 7685 | } |
| 7686 | argv += optind; |
| 7687 | |
| 7688 | while (*argv) { |
| 7689 | noscript = 0; |
| 7690 | bc_vec_push(&G.files, argv++); |
| 7691 | } |
| 7692 | |
| 7693 | option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin |
| 7694 | |
| 7695 | return bc_vm_run(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7696 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7697 | #endif |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 7698 | |
| 7699 | #endif // not DC_SMALL |