Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
| 4 | * Copyright (c) 2018 Gavin D. Howard and contributors. |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5 | */ |
| 6 | //config:config BC |
| 7 | //config: bool "bc (45 kb; 49 kb when combined with dc)" |
| 8 | //config: default y |
| 9 | //config: help |
| 10 | //config: bc is a command-line, arbitrary-precision calculator with a |
| 11 | //config: Turing-complete language. See the GNU bc manual |
| 12 | //config: (https://www.gnu.org/software/bc/manual/bc.html) and bc spec |
| 13 | //config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html) |
| 14 | //config: for details. |
| 15 | //config: |
| 16 | //config: This bc has four differences to the GNU bc: |
| 17 | //config: |
| 18 | //config: 1) The period (.) can also be used as a shortcut for "last", as in |
| 19 | //config: the BSD bc. |
| 20 | //config: 2) Arrays are copied before being passed as arguments to |
| 21 | //config: functions. This behavior is required by the bc spec. |
| 22 | //config: 3) Arrays can be passed to the builtin "length" function to get |
| 23 | //config: the number of elements currently in the array. The following |
| 24 | //config: example prints "1": |
| 25 | //config: |
| 26 | //config: a[0] = 0 |
| 27 | //config: length(a[]) |
| 28 | //config: |
| 29 | //config: 4) The precedence of the boolean "not" operator (!) is equal to |
| 30 | //config: that of the unary minus (-), or negation, operator. This still |
| 31 | //config: allows POSIX-compliant scripts to work while somewhat |
| 32 | //config: preserving expected behavior (versus C) and making parsing |
| 33 | //config: easier. |
| 34 | //config: |
| 35 | //config: Options: |
| 36 | //config: |
| 37 | //config: -i --interactive force interactive mode |
| 38 | //config: -l --mathlib use predefined math routines: |
| 39 | //config: |
| 40 | //config: s(expr) = sine of expr in radians |
| 41 | //config: c(expr) = cosine of expr in radians |
| 42 | //config: a(expr) = arctangent of expr, returning |
| 43 | //config: radians |
| 44 | //config: l(expr) = natural log of expr |
| 45 | //config: e(expr) = raises e to the power of expr |
| 46 | //config: j(n, x) = Bessel function of integer order |
| 47 | //config: n of x |
| 48 | //config: |
| 49 | //config: -q --quiet don't print version and copyright. |
| 50 | //config: -s --standard error if any non-POSIX extensions are used. |
| 51 | //config: -w --warn warn if any non-POSIX extensions are used. |
| 52 | //config: -v --version print version and copyright and exit. |
| 53 | //config: |
| 54 | //config: Long options are only available if FEATURE_BC_LONG_OPTIONS is |
| 55 | //config: enabled. |
| 56 | //config: |
| 57 | //config:config DC |
| 58 | //config: bool "dc (38 kb; 49 kb when combined with bc)" |
| 59 | //config: default y |
| 60 | //config: help |
| 61 | //config: dc is a reverse-polish notation command-line calculator which |
| 62 | //config: supports unlimited precision arithmetic. See the FreeBSD man page |
| 63 | //config: (https://www.unix.com/man-page/FreeBSD/1/dc/) and GNU dc manual |
| 64 | //config: (https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html) |
| 65 | //config: for details. |
| 66 | //config: |
| 67 | //config: This dc has a few differences from the two above: |
| 68 | //config: |
| 69 | //config: 1) When printing a byte stream (command "P"), this bc follows what |
| 70 | //config: the FreeBSD dc does. |
| 71 | //config: 2) This dc implements the GNU extensions for divmod ("~") and |
| 72 | //config: modular exponentiation ("|"). |
| 73 | //config: 3) This dc implements all FreeBSD extensions, except for "J" and |
| 74 | //config: "M". |
| 75 | //config: 4) Like the FreeBSD dc, this dc supports extended registers. |
| 76 | //config: However, they are implemented differently. When it encounters |
| 77 | //config: whitespace where a register should be, it skips the whitespace. |
| 78 | //config: If the character following is not a lowercase letter, an error |
| 79 | //config: is issued. Otherwise, the register name is parsed by the |
| 80 | //config: following regex: |
| 81 | //config: |
| 82 | //config: [a-z][a-z0-9_]* |
| 83 | //config: |
| 84 | //config: This generally means that register names will be surrounded by |
| 85 | //config: whitespace. |
| 86 | //config: |
| 87 | //config: Examples: |
| 88 | //config: |
| 89 | //config: l idx s temp L index S temp2 < do_thing |
| 90 | //config: |
| 91 | //config: Also note that, like the FreeBSD dc, extended registers are not |
| 92 | //config: allowed unless the "-x" option is given. |
| 93 | //config: |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 94 | //config:config FEATURE_DC_SMALL |
| 95 | //config: bool "Minimal dc implementation (4.2 kb), not using bc code base" |
| 96 | //config: depends on DC && !BC |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 97 | //config: default n |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 98 | //config: |
| 99 | //config:config FEATURE_DC_LIBM |
| 100 | //config: bool "Enable power and exp functions (requires libm)" |
| 101 | //config: default y |
| 102 | //config: depends on FEATURE_DC_SMALL |
| 103 | //config: help |
| 104 | //config: Enable power and exp functions. |
| 105 | //config: NOTE: This will require libm to be present for linking. |
| 106 | //config: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 107 | //config:config FEATURE_BC_SIGNALS |
| 108 | //config: bool "Enable bc/dc signal handling" |
| 109 | //config: default y |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 110 | //config: depends on (BC || DC) && !FEATURE_DC_SMALL |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 111 | //config: help |
| 112 | //config: Enable signal handling for bc and dc. |
| 113 | //config: |
| 114 | //config:config FEATURE_BC_LONG_OPTIONS |
| 115 | //config: bool "Enable bc/dc long options" |
| 116 | //config: default y |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 117 | //config: depends on (BC || DC) && !FEATURE_DC_SMALL |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 118 | //config: help |
| 119 | //config: Enable long options for bc and dc. |
| 120 | |
| 121 | //applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 122 | //applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 123 | |
| 124 | //kbuild:lib-$(CONFIG_BC) += bc.o |
| 125 | //kbuild:lib-$(CONFIG_DC) += bc.o |
| 126 | |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 127 | //See www.gnu.org/software/bc/manual/bc.html |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 128 | //usage:#define bc_trivial_usage |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 129 | //usage: "[-sqliw] FILE..." |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 130 | //usage: |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 131 | //usage:#define bc_full_usage "\n" |
| 132 | //usage: "\nArbitrary precision calculator" |
| 133 | //usage: "\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 134 | ///////: "\n -i Interactive" - has no effect for now |
| 135 | //usage: "\n -q Quiet" |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 136 | //usage: "\n -l Load standard math library" |
| 137 | //usage: "\n -s Be POSIX compatible" |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 138 | //usage: "\n -w Warn if extensions are used" |
| 139 | ///////: "\n -v Version" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 140 | //usage: "\n" |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 141 | //usage: "\n$BC_LINE_LENGTH changes output width" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 142 | //usage: |
| 143 | //usage:#define bc_example_usage |
| 144 | //usage: "3 + 4.129\n" |
| 145 | //usage: "1903 - 2893\n" |
| 146 | //usage: "-129 * 213.28935\n" |
| 147 | //usage: "12 / -1932\n" |
| 148 | //usage: "12 % 12\n" |
| 149 | //usage: "34 ^ 189\n" |
| 150 | //usage: "scale = 13\n" |
| 151 | //usage: "ibase = 2\n" |
| 152 | //usage: "obase = A\n" |
| 153 | //usage: |
| 154 | //usage:#define dc_trivial_usage |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 155 | //usage: IF_NOT_FEATURE_DC_SMALL("[-x] ")"[-eSCRIPT]... [-fFILE]... [FILE]..." |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 156 | //usage: |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 157 | //usage:#define dc_full_usage "\n" |
| 158 | //usage: "\nTiny RPN calculator. Operations:" |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 159 | //usage: "\n+, -, *, /, %, ~, ^," IF_NOT_FEATURE_DC_SMALL(" |,") |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 160 | //usage: "\np - print top of the stack (without popping)" |
| 161 | //usage: "\nf - print entire stack" |
| 162 | //usage: "\nk - pop the value and set the precision" |
| 163 | //usage: "\ni - pop the value and set input radix" |
| 164 | //usage: "\no - pop the value and set output radix" |
| 165 | //usage: "\nExamples: dc -e'2 2 + p' -> 4, dc -e'8 8 * 2 2 + / p' -> 16" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 166 | //usage: |
| 167 | //usage:#define dc_example_usage |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 168 | //usage: "$ dc -e'2 2 + p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 169 | //usage: "4\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 170 | //usage: "$ dc -e'8 8 \\* 2 2 + / p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 171 | //usage: "16\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 172 | //usage: "$ dc -e'0 1 & p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 173 | //usage: "0\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 174 | //usage: "$ dc -e'0 1 | p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 175 | //usage: "1\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 176 | //usage: "$ echo '72 9 / 8 * p' | dc\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 177 | //usage: "64\n" |
| 178 | |
| 179 | #include "libbb.h" |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 180 | #include "common_bufsiz.h" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 181 | |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 182 | #if ENABLE_FEATURE_DC_SMALL |
| 183 | # include "dc.c" |
| 184 | #else |
| 185 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 186 | typedef enum BcStatus { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 187 | BC_STATUS_SUCCESS = 0, |
| 188 | BC_STATUS_FAILURE = 1, |
| 189 | BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr() uses this |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 190 | BC_STATUS_EOF = 3, // bc_vm_stdin() uses this |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 191 | } BcStatus; |
| 192 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 193 | #define BC_VEC_INVALID_IDX ((size_t) -1) |
| 194 | #define BC_VEC_START_CAP (1 << 5) |
| 195 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 196 | typedef void (*BcVecFree)(void *) FAST_FUNC; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 197 | |
| 198 | typedef struct BcVec { |
| 199 | char *v; |
| 200 | size_t len; |
| 201 | size_t cap; |
| 202 | size_t size; |
| 203 | BcVecFree dtor; |
| 204 | } BcVec; |
| 205 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 206 | typedef signed char BcDig; |
| 207 | |
| 208 | typedef struct BcNum { |
| 209 | BcDig *restrict num; |
| 210 | size_t rdx; |
| 211 | size_t len; |
| 212 | size_t cap; |
| 213 | bool neg; |
| 214 | } BcNum; |
| 215 | |
Denys Vlasenko | 2fa11b6 | 2018-12-06 12:34:39 +0100 | [diff] [blame] | 216 | #define BC_NUM_MIN_BASE ((unsigned long) 2) |
| 217 | #define BC_NUM_MAX_IBASE ((unsigned long) 16) |
| 218 | // larger value might speed up BIGNUM calculations a bit: |
| 219 | #define BC_NUM_DEF_SIZE (16) |
| 220 | #define BC_NUM_PRINT_WIDTH (69) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 221 | |
Denys Vlasenko | 2fa11b6 | 2018-12-06 12:34:39 +0100 | [diff] [blame] | 222 | #define BC_NUM_KARATSUBA_LEN (32) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 223 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 224 | typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC; |
Denys Vlasenko | 2a8ad48 | 2018-12-08 21:56:37 +0100 | [diff] [blame] | 225 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 226 | typedef BcStatus (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 227 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 228 | static BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) FAST_FUNC; |
| 229 | static BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) FAST_FUNC; |
| 230 | static BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) FAST_FUNC; |
| 231 | static BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) FAST_FUNC; |
| 232 | static BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) FAST_FUNC; |
| 233 | static BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) FAST_FUNC; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 234 | static BcStatus bc_num_sqrt(BcNum *a, BcNum *b, size_t scale); |
| 235 | static BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, |
| 236 | size_t scale); |
| 237 | |
| 238 | typedef enum BcInst { |
| 239 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 240 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 241 | BC_INST_INC_PRE, |
| 242 | BC_INST_DEC_PRE, |
| 243 | BC_INST_INC_POST, |
| 244 | BC_INST_DEC_POST, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 245 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 246 | |
| 247 | BC_INST_NEG, |
| 248 | |
| 249 | BC_INST_POWER, |
| 250 | BC_INST_MULTIPLY, |
| 251 | BC_INST_DIVIDE, |
| 252 | BC_INST_MODULUS, |
| 253 | BC_INST_PLUS, |
| 254 | BC_INST_MINUS, |
| 255 | |
| 256 | BC_INST_REL_EQ, |
| 257 | BC_INST_REL_LE, |
| 258 | BC_INST_REL_GE, |
| 259 | BC_INST_REL_NE, |
| 260 | BC_INST_REL_LT, |
| 261 | BC_INST_REL_GT, |
| 262 | |
| 263 | BC_INST_BOOL_NOT, |
| 264 | BC_INST_BOOL_OR, |
| 265 | BC_INST_BOOL_AND, |
| 266 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 267 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 268 | BC_INST_ASSIGN_POWER, |
| 269 | BC_INST_ASSIGN_MULTIPLY, |
| 270 | BC_INST_ASSIGN_DIVIDE, |
| 271 | BC_INST_ASSIGN_MODULUS, |
| 272 | BC_INST_ASSIGN_PLUS, |
| 273 | BC_INST_ASSIGN_MINUS, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 274 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 275 | BC_INST_ASSIGN, |
| 276 | |
| 277 | BC_INST_NUM, |
| 278 | BC_INST_VAR, |
| 279 | BC_INST_ARRAY_ELEM, |
| 280 | BC_INST_ARRAY, |
| 281 | |
| 282 | BC_INST_SCALE_FUNC, |
| 283 | BC_INST_IBASE, |
| 284 | BC_INST_SCALE, |
| 285 | BC_INST_LAST, |
| 286 | BC_INST_LENGTH, |
| 287 | BC_INST_READ, |
| 288 | BC_INST_OBASE, |
| 289 | BC_INST_SQRT, |
| 290 | |
| 291 | BC_INST_PRINT, |
| 292 | BC_INST_PRINT_POP, |
| 293 | BC_INST_STR, |
| 294 | BC_INST_PRINT_STR, |
| 295 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 296 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 297 | BC_INST_JUMP, |
| 298 | BC_INST_JUMP_ZERO, |
| 299 | |
| 300 | BC_INST_CALL, |
| 301 | |
| 302 | BC_INST_RET, |
| 303 | BC_INST_RET0, |
| 304 | |
| 305 | BC_INST_HALT, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 306 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 307 | |
| 308 | BC_INST_POP, |
| 309 | BC_INST_POP_EXEC, |
| 310 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 311 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 312 | BC_INST_MODEXP, |
| 313 | BC_INST_DIVMOD, |
| 314 | |
| 315 | BC_INST_EXECUTE, |
| 316 | BC_INST_EXEC_COND, |
| 317 | |
| 318 | BC_INST_ASCIIFY, |
| 319 | BC_INST_PRINT_STREAM, |
| 320 | |
| 321 | BC_INST_PRINT_STACK, |
| 322 | BC_INST_CLEAR_STACK, |
| 323 | BC_INST_STACK_LEN, |
| 324 | BC_INST_DUPLICATE, |
| 325 | BC_INST_SWAP, |
| 326 | |
| 327 | BC_INST_LOAD, |
| 328 | BC_INST_PUSH_VAR, |
| 329 | BC_INST_PUSH_TO_VAR, |
| 330 | |
| 331 | BC_INST_QUIT, |
| 332 | BC_INST_NQUIT, |
| 333 | |
| 334 | BC_INST_INVALID = -1, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 335 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 336 | |
| 337 | } BcInst; |
| 338 | |
| 339 | typedef struct BcId { |
| 340 | char *name; |
| 341 | size_t idx; |
| 342 | } BcId; |
| 343 | |
| 344 | typedef struct BcFunc { |
| 345 | BcVec code; |
| 346 | BcVec labels; |
| 347 | size_t nparams; |
| 348 | BcVec autos; |
| 349 | } BcFunc; |
| 350 | |
| 351 | typedef enum BcResultType { |
| 352 | |
| 353 | BC_RESULT_TEMP, |
| 354 | |
| 355 | BC_RESULT_VAR, |
| 356 | BC_RESULT_ARRAY_ELEM, |
| 357 | BC_RESULT_ARRAY, |
| 358 | |
| 359 | BC_RESULT_STR, |
| 360 | |
| 361 | BC_RESULT_IBASE, |
| 362 | BC_RESULT_SCALE, |
| 363 | BC_RESULT_LAST, |
| 364 | |
| 365 | // These are between to calculate ibase, obase, and last from instructions. |
| 366 | BC_RESULT_CONSTANT, |
| 367 | BC_RESULT_ONE, |
| 368 | |
| 369 | BC_RESULT_OBASE, |
| 370 | |
| 371 | } BcResultType; |
| 372 | |
| 373 | typedef union BcResultData { |
| 374 | BcNum n; |
| 375 | BcVec v; |
| 376 | BcId id; |
| 377 | } BcResultData; |
| 378 | |
| 379 | typedef struct BcResult { |
| 380 | BcResultType t; |
| 381 | BcResultData d; |
| 382 | } BcResult; |
| 383 | |
| 384 | typedef struct BcInstPtr { |
| 385 | size_t func; |
| 386 | size_t idx; |
| 387 | size_t len; |
| 388 | } BcInstPtr; |
| 389 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 390 | // BC_LEX_NEG is not used in lexing; it is only for parsing. |
| 391 | typedef enum BcLexType { |
| 392 | |
| 393 | BC_LEX_EOF, |
| 394 | BC_LEX_INVALID, |
| 395 | |
| 396 | BC_LEX_OP_INC, |
| 397 | BC_LEX_OP_DEC, |
| 398 | |
| 399 | BC_LEX_NEG, |
| 400 | |
| 401 | BC_LEX_OP_POWER, |
| 402 | BC_LEX_OP_MULTIPLY, |
| 403 | BC_LEX_OP_DIVIDE, |
| 404 | BC_LEX_OP_MODULUS, |
| 405 | BC_LEX_OP_PLUS, |
| 406 | BC_LEX_OP_MINUS, |
| 407 | |
| 408 | BC_LEX_OP_REL_EQ, |
| 409 | BC_LEX_OP_REL_LE, |
| 410 | BC_LEX_OP_REL_GE, |
| 411 | BC_LEX_OP_REL_NE, |
| 412 | BC_LEX_OP_REL_LT, |
| 413 | BC_LEX_OP_REL_GT, |
| 414 | |
| 415 | BC_LEX_OP_BOOL_NOT, |
| 416 | BC_LEX_OP_BOOL_OR, |
| 417 | BC_LEX_OP_BOOL_AND, |
| 418 | |
| 419 | BC_LEX_OP_ASSIGN_POWER, |
| 420 | BC_LEX_OP_ASSIGN_MULTIPLY, |
| 421 | BC_LEX_OP_ASSIGN_DIVIDE, |
| 422 | BC_LEX_OP_ASSIGN_MODULUS, |
| 423 | BC_LEX_OP_ASSIGN_PLUS, |
| 424 | BC_LEX_OP_ASSIGN_MINUS, |
| 425 | BC_LEX_OP_ASSIGN, |
| 426 | |
| 427 | BC_LEX_NLINE, |
| 428 | BC_LEX_WHITESPACE, |
| 429 | |
| 430 | BC_LEX_LPAREN, |
| 431 | BC_LEX_RPAREN, |
| 432 | |
| 433 | BC_LEX_LBRACKET, |
| 434 | BC_LEX_COMMA, |
| 435 | BC_LEX_RBRACKET, |
| 436 | |
| 437 | BC_LEX_LBRACE, |
| 438 | BC_LEX_SCOLON, |
| 439 | BC_LEX_RBRACE, |
| 440 | |
| 441 | BC_LEX_STR, |
| 442 | BC_LEX_NAME, |
| 443 | BC_LEX_NUMBER, |
| 444 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 445 | BC_LEX_KEY_1st_keyword, |
| 446 | BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 447 | BC_LEX_KEY_BREAK, |
| 448 | BC_LEX_KEY_CONTINUE, |
| 449 | BC_LEX_KEY_DEFINE, |
| 450 | BC_LEX_KEY_ELSE, |
| 451 | BC_LEX_KEY_FOR, |
| 452 | BC_LEX_KEY_HALT, |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 453 | // code uses "type - BC_LEX_KEY_IBASE + BC_INST_IBASE" construct, |
| 454 | BC_LEX_KEY_IBASE, // relative order should match for: BC_INST_IBASE |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 455 | BC_LEX_KEY_IF, |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 456 | BC_LEX_KEY_LAST, // relative order should match for: BC_INST_LAST |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 457 | BC_LEX_KEY_LENGTH, |
| 458 | BC_LEX_KEY_LIMITS, |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 459 | BC_LEX_KEY_OBASE, // relative order should match for: BC_INST_OBASE |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 460 | BC_LEX_KEY_PRINT, |
| 461 | BC_LEX_KEY_QUIT, |
| 462 | BC_LEX_KEY_READ, |
| 463 | BC_LEX_KEY_RETURN, |
| 464 | BC_LEX_KEY_SCALE, |
| 465 | BC_LEX_KEY_SQRT, |
| 466 | BC_LEX_KEY_WHILE, |
| 467 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 468 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 469 | BC_LEX_EQ_NO_REG, |
| 470 | BC_LEX_OP_MODEXP, |
| 471 | BC_LEX_OP_DIVMOD, |
| 472 | |
| 473 | BC_LEX_COLON, |
| 474 | BC_LEX_ELSE, |
| 475 | BC_LEX_EXECUTE, |
| 476 | BC_LEX_PRINT_STACK, |
| 477 | BC_LEX_CLEAR_STACK, |
| 478 | BC_LEX_STACK_LEVEL, |
| 479 | BC_LEX_DUPLICATE, |
| 480 | BC_LEX_SWAP, |
| 481 | BC_LEX_POP, |
| 482 | |
| 483 | BC_LEX_ASCIIFY, |
| 484 | BC_LEX_PRINT_STREAM, |
| 485 | |
| 486 | BC_LEX_STORE_IBASE, |
| 487 | BC_LEX_STORE_SCALE, |
| 488 | BC_LEX_LOAD, |
| 489 | BC_LEX_LOAD_POP, |
| 490 | BC_LEX_STORE_PUSH, |
| 491 | BC_LEX_STORE_OBASE, |
| 492 | BC_LEX_PRINT_POP, |
| 493 | BC_LEX_NQUIT, |
| 494 | BC_LEX_SCALE_FACTOR, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 495 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 496 | } BcLexType; |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 497 | // must match order of BC_LEX_KEY_foo etc above |
| 498 | #if ENABLE_BC |
| 499 | struct BcLexKeyword { |
| 500 | char name8[8]; |
| 501 | }; |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 502 | #define BC_LEX_KW_ENTRY(a, b) \ |
| 503 | { .name8 = a /*, .posix = b */ } |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 504 | static const struct BcLexKeyword bc_lex_kws[20] = { |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 505 | BC_LEX_KW_ENTRY("auto" , 1), // 0 |
| 506 | BC_LEX_KW_ENTRY("break" , 1), // 1 |
| 507 | BC_LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL |
| 508 | BC_LEX_KW_ENTRY("define" , 1), // 3 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 509 | |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 510 | BC_LEX_KW_ENTRY("else" , 0), // 4 |
| 511 | BC_LEX_KW_ENTRY("for" , 1), // 5 |
| 512 | BC_LEX_KW_ENTRY("halt" , 0), // 6 |
| 513 | BC_LEX_KW_ENTRY("ibase" , 1), // 7 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 514 | |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 515 | BC_LEX_KW_ENTRY("if" , 1), // 8 |
| 516 | BC_LEX_KW_ENTRY("last" , 0), // 9 |
| 517 | BC_LEX_KW_ENTRY("length" , 1), // 10 |
| 518 | BC_LEX_KW_ENTRY("limits" , 0), // 11 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 519 | |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 520 | BC_LEX_KW_ENTRY("obase" , 1), // 12 |
| 521 | BC_LEX_KW_ENTRY("print" , 0), // 13 |
| 522 | BC_LEX_KW_ENTRY("quit" , 1), // 14 |
| 523 | BC_LEX_KW_ENTRY("read" , 0), // 15 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 524 | |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 525 | BC_LEX_KW_ENTRY("return" , 1), // 16 |
| 526 | BC_LEX_KW_ENTRY("scale" , 1), // 17 |
| 527 | BC_LEX_KW_ENTRY("sqrt" , 1), // 18 |
| 528 | BC_LEX_KW_ENTRY("while" , 1), // 19 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 529 | }; |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 530 | #undef BC_LEX_KW_ENTRY |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 531 | enum { |
| 532 | POSIX_KWORD_MASK = 0 |
| 533 | | (1 << 0) |
| 534 | | (1 << 1) |
| 535 | | (0 << 2) |
| 536 | | (1 << 3) |
| 537 | \ |
| 538 | | (0 << 4) |
| 539 | | (1 << 5) |
| 540 | | (0 << 6) |
| 541 | | (1 << 7) |
| 542 | \ |
| 543 | | (1 << 8) |
| 544 | | (0 << 9) |
| 545 | | (1 << 10) |
| 546 | | (0 << 11) |
| 547 | \ |
| 548 | | (1 << 12) |
| 549 | | (0 << 13) |
| 550 | | (1 << 14) |
| 551 | | (0 << 15) |
| 552 | \ |
| 553 | | (1 << 16) |
| 554 | | (1 << 17) |
| 555 | | (1 << 18) |
| 556 | | (1 << 19) |
| 557 | }; |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 558 | #define bc_lex_kws_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK) |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 559 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 560 | |
| 561 | struct BcLex; |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 562 | typedef BcStatus (*BcLexNext)(struct BcLex *) FAST_FUNC; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 563 | |
| 564 | typedef struct BcLex { |
| 565 | |
| 566 | const char *buf; |
| 567 | size_t i; |
| 568 | size_t line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 569 | size_t len; |
| 570 | bool newline; |
| 571 | |
| 572 | struct { |
| 573 | BcLexType t; |
| 574 | BcLexType last; |
| 575 | BcVec v; |
| 576 | } t; |
| 577 | |
| 578 | BcLexNext next; |
| 579 | |
| 580 | } BcLex; |
| 581 | |
| 582 | #define BC_PARSE_STREND ((char) UCHAR_MAX) |
| 583 | |
Denys Vlasenko | e55a572 | 2018-12-06 12:47:17 +0100 | [diff] [blame] | 584 | #define BC_PARSE_REL (1 << 0) |
| 585 | #define BC_PARSE_PRINT (1 << 1) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 586 | #define BC_PARSE_NOCALL (1 << 2) |
| 587 | #define BC_PARSE_NOREAD (1 << 3) |
Denys Vlasenko | e55a572 | 2018-12-06 12:47:17 +0100 | [diff] [blame] | 588 | #define BC_PARSE_ARRAY (1 << 4) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 589 | |
| 590 | #define BC_PARSE_TOP_FLAG_PTR(parse) ((uint8_t *) bc_vec_top(&(parse)->flags)) |
| 591 | #define BC_PARSE_TOP_FLAG(parse) (*(BC_PARSE_TOP_FLAG_PTR(parse))) |
| 592 | |
| 593 | #define BC_PARSE_FLAG_FUNC_INNER (1 << 0) |
| 594 | #define BC_PARSE_FUNC_INNER(parse) \ |
| 595 | (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC_INNER) |
| 596 | |
| 597 | #define BC_PARSE_FLAG_FUNC (1 << 1) |
| 598 | #define BC_PARSE_FUNC(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC) |
| 599 | |
| 600 | #define BC_PARSE_FLAG_BODY (1 << 2) |
| 601 | #define BC_PARSE_BODY(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_BODY) |
| 602 | |
| 603 | #define BC_PARSE_FLAG_LOOP (1 << 3) |
| 604 | #define BC_PARSE_LOOP(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP) |
| 605 | |
| 606 | #define BC_PARSE_FLAG_LOOP_INNER (1 << 4) |
| 607 | #define BC_PARSE_LOOP_INNER(parse) \ |
| 608 | (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP_INNER) |
| 609 | |
| 610 | #define BC_PARSE_FLAG_IF (1 << 5) |
| 611 | #define BC_PARSE_IF(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF) |
| 612 | |
| 613 | #define BC_PARSE_FLAG_ELSE (1 << 6) |
| 614 | #define BC_PARSE_ELSE(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_ELSE) |
| 615 | |
| 616 | #define BC_PARSE_FLAG_IF_END (1 << 7) |
| 617 | #define BC_PARSE_IF_END(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF_END) |
| 618 | |
| 619 | #define BC_PARSE_CAN_EXEC(parse) \ |
| 620 | (!(BC_PARSE_TOP_FLAG(parse) & \ |
| 621 | (BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_BODY | \ |
| 622 | BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER | BC_PARSE_FLAG_IF | \ |
| 623 | BC_PARSE_FLAG_ELSE | BC_PARSE_FLAG_IF_END))) |
| 624 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 625 | struct BcParse; |
| 626 | |
| 627 | struct BcProgram; |
| 628 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 629 | typedef BcStatus (*BcParseParse)(struct BcParse *) FAST_FUNC; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 630 | |
| 631 | typedef struct BcParse { |
| 632 | |
| 633 | BcParseParse parse; |
| 634 | |
| 635 | BcLex l; |
| 636 | |
| 637 | BcVec flags; |
| 638 | |
| 639 | BcVec exits; |
| 640 | BcVec conds; |
| 641 | |
| 642 | BcVec ops; |
| 643 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 644 | BcFunc *func; |
| 645 | size_t fidx; |
| 646 | |
| 647 | size_t nbraces; |
| 648 | bool auto_part; |
| 649 | |
| 650 | } BcParse; |
| 651 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 652 | typedef struct BcProgram { |
| 653 | |
| 654 | size_t len; |
| 655 | size_t scale; |
| 656 | |
| 657 | BcNum ib; |
| 658 | size_t ib_t; |
| 659 | BcNum ob; |
| 660 | size_t ob_t; |
| 661 | |
| 662 | BcNum hexb; |
| 663 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 664 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 665 | BcNum strmb; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 666 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 667 | |
| 668 | BcVec results; |
| 669 | BcVec stack; |
| 670 | |
| 671 | BcVec fns; |
| 672 | BcVec fn_map; |
| 673 | |
| 674 | BcVec vars; |
| 675 | BcVec var_map; |
| 676 | |
| 677 | BcVec arrs; |
| 678 | BcVec arr_map; |
| 679 | |
| 680 | BcVec strs; |
| 681 | BcVec consts; |
| 682 | |
| 683 | const char *file; |
| 684 | |
| 685 | BcNum last; |
| 686 | BcNum zero; |
| 687 | BcNum one; |
| 688 | |
| 689 | size_t nchars; |
| 690 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 691 | } BcProgram; |
| 692 | |
| 693 | #define BC_PROG_STACK(s, n) ((s)->len >= ((size_t) n)) |
| 694 | |
| 695 | #define BC_PROG_MAIN (0) |
| 696 | #define BC_PROG_READ (1) |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 697 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 698 | #define BC_PROG_REQ_FUNCS (2) |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 699 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 700 | |
| 701 | #define BC_PROG_STR(n) (!(n)->num && !(n)->cap) |
| 702 | #define BC_PROG_NUM(r, n) \ |
| 703 | ((r)->t != BC_RESULT_ARRAY && (r)->t != BC_RESULT_STR && !BC_PROG_STR(n)) |
| 704 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 705 | #define BC_FLAG_W (1 << 0) |
| 706 | #define BC_FLAG_V (1 << 1) |
| 707 | #define BC_FLAG_S (1 << 2) |
| 708 | #define BC_FLAG_Q (1 << 3) |
| 709 | #define BC_FLAG_L (1 << 4) |
| 710 | #define BC_FLAG_I (1 << 5) |
| 711 | #define DC_FLAG_X (1 << 6) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 712 | |
| 713 | #define BC_MAX(a, b) ((a) > (b) ? (a) : (b)) |
| 714 | #define BC_MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 715 | |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 716 | #define BC_MAX_OBASE ((unsigned) 999) |
| 717 | #define BC_MAX_DIM ((unsigned) INT_MAX) |
| 718 | #define BC_MAX_SCALE ((unsigned) UINT_MAX) |
| 719 | #define BC_MAX_STRING ((unsigned) UINT_MAX - 1) |
| 720 | #define BC_MAX_NUM BC_MAX_STRING |
| 721 | // Unused apart from "limits" message. Just show a "biggish number" there. |
| 722 | //#define BC_MAX_NAME BC_MAX_STRING |
| 723 | //#define BC_MAX_EXP ((unsigned long) LONG_MAX) |
| 724 | //#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1) |
| 725 | #define BC_MAX_NAME_STR "999999999" |
| 726 | #define BC_MAX_EXP_STR "999999999" |
| 727 | #define BC_MAX_VARS_STR "999999999" |
| 728 | |
| 729 | #define BC_MAX_OBASE_STR "999" |
| 730 | |
| 731 | #if INT_MAX == 2147483647 |
| 732 | # define BC_MAX_DIM_STR "2147483647" |
| 733 | #elif INT_MAX == 9223372036854775807 |
| 734 | # define BC_MAX_DIM_STR "9223372036854775807" |
| 735 | #else |
| 736 | # error Strange INT_MAX |
| 737 | #endif |
| 738 | |
| 739 | #if UINT_MAX == 4294967295 |
| 740 | # define BC_MAX_SCALE_STR "4294967295" |
| 741 | # define BC_MAX_STRING_STR "4294967294" |
| 742 | #elif UINT_MAX == 18446744073709551615 |
| 743 | # define BC_MAX_SCALE_STR "18446744073709551615" |
| 744 | # define BC_MAX_STRING_STR "18446744073709551614" |
| 745 | #else |
| 746 | # error Strange UINT_MAX |
| 747 | #endif |
| 748 | #define BC_MAX_NUM_STR BC_MAX_STRING_STR |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 749 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 750 | struct globals { |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 751 | IF_FEATURE_BC_SIGNALS(smallint ttyin;) |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 752 | IF_FEATURE_CLEAN_UP(smallint exiting;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 753 | char sbgn; |
| 754 | char send; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 755 | |
| 756 | BcParse prs; |
| 757 | BcProgram prog; |
| 758 | |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 759 | // For error messages. Can be set to current parsed line, |
| 760 | // or [TODO] to current executing line (can be before last parsed one) |
| 761 | unsigned err_line; |
| 762 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 763 | BcVec files; |
| 764 | |
| 765 | char *env_args; |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 766 | |
| 767 | #if ENABLE_FEATURE_EDITING |
| 768 | line_input_t *line_input_state; |
| 769 | #endif |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 770 | } FIX_ALIASING; |
| 771 | #define G (*ptr_to_globals) |
| 772 | #define INIT_G() do { \ |
| 773 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 774 | } while (0) |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 775 | #define FREE_G() do { \ |
| 776 | FREE_PTR_TO_GLOBALS(); \ |
| 777 | } while (0) |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 778 | #define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S)) |
| 779 | #define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W)) |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 780 | #define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X)) |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 781 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | b9c321d | 2018-12-07 12:41:42 +0100 | [diff] [blame] | 782 | # define G_interrupt bb_got_signal |
| 783 | # define G_ttyin G.ttyin |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 784 | #else |
Denys Vlasenko | b9c321d | 2018-12-07 12:41:42 +0100 | [diff] [blame] | 785 | # define G_interrupt 0 |
| 786 | # define G_ttyin 0 |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 787 | #endif |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 788 | #if ENABLE_FEATURE_CLEAN_UP |
| 789 | # define G_exiting G.exiting |
| 790 | #else |
| 791 | # define G_exiting 0 |
| 792 | #endif |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 793 | #define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b')) |
| 794 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 795 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 796 | |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 797 | // This is a bit array that corresponds to token types. An entry is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 798 | // true if the token is valid in an expression, false otherwise. |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 799 | enum { |
| 800 | BC_PARSE_EXPRS_BITS = 0 |
| 801 | + ((uint64_t)((0 << 0)+(0 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (0*8)) |
| 802 | + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (1*8)) |
| 803 | + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (2*8)) |
| 804 | + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(0 << 3)+(0 << 4)+(1 << 5)+(1 << 6)+(0 << 7)) << (3*8)) |
| 805 | + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(1 << 6)+(1 << 7)) << (4*8)) |
| 806 | + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (5*8)) |
| 807 | + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (6*8)) |
| 808 | + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(0 << 3) ) << (7*8)) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 809 | }; |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 810 | static ALWAYS_INLINE long bc_parse_exprs(unsigned i) |
| 811 | { |
| 812 | #if ULONG_MAX > 0xffffffff |
| 813 | // 64-bit version (will not work correctly for 32-bit longs!) |
| 814 | return BC_PARSE_EXPRS_BITS & (1UL << i); |
| 815 | #else |
| 816 | // 32-bit version |
| 817 | unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS; |
| 818 | if (i >= 32) { |
| 819 | m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32); |
| 820 | i &= 31; |
| 821 | } |
| 822 | return m & (1UL << i); |
| 823 | #endif |
| 824 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 825 | |
| 826 | // This is an array of data for operators that correspond to token types. |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 827 | static const uint8_t bc_parse_ops[] = { |
| 828 | #define OP(p,l) ((int)(l) * 0x10 + (p)) |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 829 | OP(0, false), OP( 0, false ), // inc dec |
| 830 | OP(1, false), // neg |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 831 | OP(2, false), |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 832 | OP(3, true ), OP( 3, true ), OP( 3, true ), // pow mul div |
| 833 | OP(4, true ), OP( 4, true ), // mod + - |
| 834 | OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < > |
| 835 | OP(1, false), // not |
| 836 | OP(7, true ), OP( 7, true ), // or and |
| 837 | OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= += |
| 838 | OP(5, false), OP( 5, false ), // -= = |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 839 | #undef OP |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 840 | }; |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 841 | #define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f) |
| 842 | #define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 843 | |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 844 | // Byte array of up to 4 BC_LEX's, packed into 32-bit word |
| 845 | typedef uint32_t BcParseNext; |
| 846 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 847 | // These identify what tokens can come after expressions in certain cases. |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 848 | enum { |
| 849 | #define BC_PARSE_NEXT4(a,b,c,d) ( (a) | ((b)<<8) | ((c)<<16) | ((((d)|0x80)<<24)) ) |
| 850 | #define BC_PARSE_NEXT2(a,b) BC_PARSE_NEXT4(a,b,0xff,0xff) |
| 851 | #define BC_PARSE_NEXT1(a) BC_PARSE_NEXT4(a,0xff,0xff,0xff) |
| 852 | bc_parse_next_expr = BC_PARSE_NEXT4(BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_RBRACE, BC_LEX_EOF), |
| 853 | bc_parse_next_param = BC_PARSE_NEXT2(BC_LEX_RPAREN, BC_LEX_COMMA), |
| 854 | bc_parse_next_print = BC_PARSE_NEXT4(BC_LEX_COMMA, BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_EOF), |
| 855 | bc_parse_next_rel = BC_PARSE_NEXT1(BC_LEX_RPAREN), |
| 856 | bc_parse_next_elem = BC_PARSE_NEXT1(BC_LEX_RBRACKET), |
| 857 | bc_parse_next_for = BC_PARSE_NEXT1(BC_LEX_SCOLON), |
| 858 | bc_parse_next_read = BC_PARSE_NEXT2(BC_LEX_NLINE, BC_LEX_EOF), |
| 859 | #undef BC_PARSE_NEXT4 |
| 860 | #undef BC_PARSE_NEXT2 |
| 861 | #undef BC_PARSE_NEXT1 |
| 862 | }; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 863 | #endif // ENABLE_BC |
| 864 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 865 | #if ENABLE_DC |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 866 | static const //BcLexType - should be this type, but narrower type saves size: |
| 867 | uint8_t |
| 868 | dc_lex_regs[] = { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 869 | BC_LEX_OP_REL_EQ, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_NE, |
| 870 | BC_LEX_OP_REL_LT, BC_LEX_OP_REL_GT, BC_LEX_SCOLON, BC_LEX_COLON, |
| 871 | BC_LEX_ELSE, BC_LEX_LOAD, BC_LEX_LOAD_POP, BC_LEX_OP_ASSIGN, |
| 872 | BC_LEX_STORE_PUSH, |
| 873 | }; |
| 874 | |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 875 | static const //BcLexType - should be this type |
| 876 | uint8_t |
| 877 | dc_lex_tokens[] = { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 878 | BC_LEX_OP_MODULUS, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_LPAREN, |
| 879 | BC_LEX_INVALID, BC_LEX_OP_MULTIPLY, BC_LEX_OP_PLUS, BC_LEX_INVALID, |
| 880 | BC_LEX_OP_MINUS, BC_LEX_INVALID, BC_LEX_OP_DIVIDE, |
| 881 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, |
| 882 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, |
| 883 | BC_LEX_INVALID, BC_LEX_INVALID, |
| 884 | BC_LEX_COLON, BC_LEX_SCOLON, BC_LEX_OP_REL_GT, BC_LEX_OP_REL_EQ, |
| 885 | BC_LEX_OP_REL_LT, BC_LEX_KEY_READ, BC_LEX_INVALID, |
| 886 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, |
| 887 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_EQ_NO_REG, BC_LEX_INVALID, |
| 888 | BC_LEX_KEY_IBASE, BC_LEX_INVALID, BC_LEX_KEY_SCALE, BC_LEX_LOAD_POP, |
| 889 | BC_LEX_INVALID, BC_LEX_OP_BOOL_NOT, BC_LEX_KEY_OBASE, BC_LEX_PRINT_STREAM, |
| 890 | BC_LEX_NQUIT, BC_LEX_POP, BC_LEX_STORE_PUSH, BC_LEX_INVALID, BC_LEX_INVALID, |
| 891 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_SCALE_FACTOR, BC_LEX_INVALID, |
| 892 | BC_LEX_KEY_LENGTH, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, |
| 893 | BC_LEX_OP_POWER, BC_LEX_NEG, BC_LEX_INVALID, |
| 894 | BC_LEX_ASCIIFY, BC_LEX_INVALID, BC_LEX_CLEAR_STACK, BC_LEX_DUPLICATE, |
| 895 | BC_LEX_ELSE, BC_LEX_PRINT_STACK, BC_LEX_INVALID, BC_LEX_INVALID, |
| 896 | BC_LEX_STORE_IBASE, BC_LEX_INVALID, BC_LEX_STORE_SCALE, BC_LEX_LOAD, |
| 897 | BC_LEX_INVALID, BC_LEX_PRINT_POP, BC_LEX_STORE_OBASE, BC_LEX_KEY_PRINT, |
| 898 | BC_LEX_KEY_QUIT, BC_LEX_SWAP, BC_LEX_OP_ASSIGN, BC_LEX_INVALID, |
| 899 | BC_LEX_INVALID, BC_LEX_KEY_SQRT, BC_LEX_INVALID, BC_LEX_EXECUTE, |
| 900 | BC_LEX_INVALID, BC_LEX_STACK_LEVEL, |
| 901 | BC_LEX_LBRACE, BC_LEX_OP_MODEXP, BC_LEX_INVALID, BC_LEX_OP_DIVMOD, |
| 902 | BC_LEX_INVALID |
| 903 | }; |
| 904 | |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 905 | static const //BcInst - should be this type. Using signed narrow type since BC_INST_INVALID is -1 |
| 906 | int8_t |
| 907 | dc_parse_insts[] = { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 908 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE, |
| 909 | BC_INST_INVALID, BC_INST_POWER, BC_INST_MULTIPLY, BC_INST_DIVIDE, |
| 910 | BC_INST_MODULUS, BC_INST_PLUS, BC_INST_MINUS, |
| 911 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 912 | BC_INST_INVALID, BC_INST_INVALID, |
| 913 | BC_INST_BOOL_NOT, BC_INST_INVALID, BC_INST_INVALID, |
| 914 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 915 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 916 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GT, BC_INST_INVALID, |
| 917 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE, |
| 918 | BC_INST_INVALID, BC_INST_INVALID, |
| 919 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 920 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 921 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_IBASE, |
| 922 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_LENGTH, BC_INST_INVALID, |
| 923 | BC_INST_OBASE, BC_INST_PRINT, BC_INST_QUIT, BC_INST_INVALID, |
| 924 | BC_INST_INVALID, BC_INST_SCALE, BC_INST_SQRT, BC_INST_INVALID, |
| 925 | BC_INST_REL_EQ, BC_INST_MODEXP, BC_INST_DIVMOD, BC_INST_INVALID, |
| 926 | BC_INST_INVALID, BC_INST_EXECUTE, BC_INST_PRINT_STACK, BC_INST_CLEAR_STACK, |
| 927 | BC_INST_STACK_LEN, BC_INST_DUPLICATE, BC_INST_SWAP, BC_INST_POP, |
| 928 | BC_INST_ASCIIFY, BC_INST_PRINT_STREAM, BC_INST_INVALID, BC_INST_INVALID, |
| 929 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 930 | BC_INST_PRINT, BC_INST_NQUIT, BC_INST_SCALE_FUNC, |
| 931 | }; |
| 932 | #endif // ENABLE_DC |
| 933 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 934 | static const BcNumBinaryOp bc_program_ops[] = { |
| 935 | bc_num_pow, bc_num_mul, bc_num_div, bc_num_mod, bc_num_add, bc_num_sub, |
| 936 | }; |
| 937 | |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 938 | static void fflush_and_check(void) |
| 939 | { |
| 940 | fflush_all(); |
| 941 | if (ferror(stdout) || ferror(stderr)) |
| 942 | bb_perror_msg_and_die("output error"); |
| 943 | } |
| 944 | |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 945 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 946 | #define QUIT_OR_RETURN_TO_MAIN \ |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 947 | do { \ |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 948 | IF_FEATURE_BC_SIGNALS(G_ttyin = 0;) /* do not loop in main loop anymore */ \ |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 949 | G_exiting = 1; \ |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 950 | return BC_STATUS_FAILURE; \ |
| 951 | } while (0) |
| 952 | #else |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 953 | #define QUIT_OR_RETURN_TO_MAIN quit() |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 954 | #endif |
| 955 | |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 956 | static void quit(void) NORETURN; |
| 957 | static void quit(void) |
| 958 | { |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 959 | if (ferror(stdin)) |
| 960 | bb_perror_msg_and_die("input error"); |
| 961 | fflush_and_check(); |
| 962 | exit(0); |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 963 | } |
| 964 | |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 965 | static void bc_verror_msg(const char *fmt, va_list p) |
| 966 | { |
| 967 | const char *sv = sv; /* for compiler */ |
| 968 | if (G.prog.file) { |
| 969 | sv = applet_name; |
| 970 | applet_name = xasprintf("%s: %s:%u", applet_name, G.prog.file, G.err_line); |
| 971 | } |
| 972 | bb_verror_msg(fmt, p, NULL); |
| 973 | if (G.prog.file) { |
| 974 | free((char*)applet_name); |
| 975 | applet_name = sv; |
| 976 | } |
| 977 | } |
| 978 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 979 | static NOINLINE int bc_error_fmt(const char *fmt, ...) |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 980 | { |
| 981 | va_list p; |
| 982 | |
| 983 | va_start(p, fmt); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 984 | bc_verror_msg(fmt, p); |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 985 | va_end(p); |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 986 | |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 987 | if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin) |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 988 | exit(1); |
| 989 | return BC_STATUS_FAILURE; |
| 990 | } |
| 991 | |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 992 | #if ENABLE_BC |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 993 | static NOINLINE int bc_posix_error_fmt(const char *fmt, ...) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 994 | { |
| 995 | va_list p; |
| 996 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 997 | // Are non-POSIX constructs totally ok? |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 998 | if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W))) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 999 | return BC_STATUS_SUCCESS; // yes |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1000 | |
| 1001 | va_start(p, fmt); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 1002 | bc_verror_msg(fmt, p); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1003 | va_end(p); |
| 1004 | |
| 1005 | // Do we treat non-POSIX constructs as errors? |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 1006 | if (!(option_mask32 & BC_FLAG_S)) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1007 | return BC_STATUS_SUCCESS; // no, it's a warning |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 1008 | if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1009 | exit(1); |
| 1010 | return BC_STATUS_FAILURE; |
| 1011 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1012 | #endif |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1013 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1014 | // We use error functions with "return bc_error(FMT[, PARAMS])" idiom. |
| 1015 | // This idiom begs for tail-call optimization, but for it to work, |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1016 | // function must not have caller-cleaned parameters on stack. |
| 1017 | // Unfortunately, vararg function API does exactly that on most arches. |
| 1018 | // Thus, use these shims for the cases when we have no vararg PARAMS: |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1019 | static int bc_error(const char *msg) |
| 1020 | { |
| 1021 | return bc_error_fmt("%s", msg); |
| 1022 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1023 | #if ENABLE_BC |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 1024 | static int bc_POSIX_requires(const char *msg) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1025 | { |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 1026 | return bc_posix_error_fmt("POSIX requires %s", msg); |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1027 | } |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 1028 | static int bc_POSIX_does_not_allow(const char *msg) |
| 1029 | { |
| 1030 | return bc_posix_error_fmt("%s%s", "POSIX does not allow ", msg); |
| 1031 | } |
| 1032 | static int bc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg) |
| 1033 | { |
| 1034 | return bc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; the following is bad:", msg); |
| 1035 | } |
| 1036 | static int bc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg) |
| 1037 | { |
| 1038 | return bc_posix_error_fmt("%san empty %s expression in a for loop", "POSIX does not allow ", msg); |
| 1039 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1040 | #endif |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1041 | static int bc_error_bad_character(char c) |
| 1042 | { |
| 1043 | return bc_error_fmt("bad character '%c'", c); |
| 1044 | } |
| 1045 | static int bc_error_bad_expression(void) |
| 1046 | { |
| 1047 | return bc_error("bad expression"); |
| 1048 | } |
| 1049 | static int bc_error_bad_token(void) |
| 1050 | { |
| 1051 | return bc_error("bad token"); |
| 1052 | } |
| 1053 | static int bc_error_stack_has_too_few_elements(void) |
| 1054 | { |
| 1055 | return bc_error("stack has too few elements"); |
| 1056 | } |
| 1057 | static int bc_error_variable_is_wrong_type(void) |
| 1058 | { |
| 1059 | return bc_error("variable is wrong type"); |
| 1060 | } |
| 1061 | static int bc_error_nested_read_call(void) |
| 1062 | { |
| 1063 | return bc_error("read() call inside of a read() call"); |
| 1064 | } |
| 1065 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1066 | static void bc_vec_grow(BcVec *v, size_t n) |
| 1067 | { |
| 1068 | size_t cap = v->cap * 2; |
| 1069 | while (cap < v->len + n) cap *= 2; |
| 1070 | v->v = xrealloc(v->v, v->size * cap); |
| 1071 | v->cap = cap; |
| 1072 | } |
| 1073 | |
| 1074 | static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor) |
| 1075 | { |
| 1076 | v->size = esize; |
| 1077 | v->cap = BC_VEC_START_CAP; |
| 1078 | v->len = 0; |
| 1079 | v->dtor = dtor; |
| 1080 | v->v = xmalloc(esize * BC_VEC_START_CAP); |
| 1081 | } |
| 1082 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1083 | static void bc_char_vec_init(BcVec *v) |
| 1084 | { |
| 1085 | bc_vec_init(v, sizeof(char), NULL); |
| 1086 | } |
| 1087 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1088 | static void bc_vec_expand(BcVec *v, size_t req) |
| 1089 | { |
| 1090 | if (v->cap < req) { |
| 1091 | v->v = xrealloc(v->v, v->size * req); |
| 1092 | v->cap = req; |
| 1093 | } |
| 1094 | } |
| 1095 | |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 1096 | static void bc_vec_pop(BcVec *v) |
| 1097 | { |
| 1098 | v->len--; |
| 1099 | if (v->dtor) |
| 1100 | v->dtor(v->v + (v->size * v->len)); |
| 1101 | } |
Denys Vlasenko | 2fa11b6 | 2018-12-06 12:34:39 +0100 | [diff] [blame] | 1102 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1103 | static void bc_vec_npop(BcVec *v, size_t n) |
| 1104 | { |
| 1105 | if (!v->dtor) |
| 1106 | v->len -= n; |
| 1107 | else { |
| 1108 | size_t len = v->len - n; |
| 1109 | while (v->len > len) v->dtor(v->v + (v->size * --v->len)); |
| 1110 | } |
| 1111 | } |
| 1112 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1113 | static void bc_vec_pop_all(BcVec *v) |
| 1114 | { |
| 1115 | bc_vec_npop(v, v->len); |
| 1116 | } |
| 1117 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1118 | static void bc_vec_push(BcVec *v, const void *data) |
| 1119 | { |
| 1120 | if (v->len + 1 > v->cap) bc_vec_grow(v, 1); |
| 1121 | memmove(v->v + (v->size * v->len), data, v->size); |
| 1122 | v->len += 1; |
| 1123 | } |
| 1124 | |
| 1125 | static void bc_vec_pushByte(BcVec *v, char data) |
| 1126 | { |
| 1127 | bc_vec_push(v, &data); |
| 1128 | } |
| 1129 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1130 | static void bc_vec_pushZeroByte(BcVec *v) |
| 1131 | { |
| 1132 | //bc_vec_pushByte(v, '\0'); |
| 1133 | // better: |
| 1134 | bc_vec_push(v, &const_int_0); |
| 1135 | } |
| 1136 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1137 | static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx) |
| 1138 | { |
| 1139 | if (idx == v->len) |
| 1140 | bc_vec_push(v, data); |
| 1141 | else { |
| 1142 | |
| 1143 | char *ptr; |
| 1144 | |
| 1145 | if (v->len == v->cap) bc_vec_grow(v, 1); |
| 1146 | |
| 1147 | ptr = v->v + v->size * idx; |
| 1148 | |
| 1149 | memmove(ptr + v->size, ptr, v->size * (v->len++ - idx)); |
| 1150 | memmove(ptr, data, v->size); |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | static void bc_vec_string(BcVec *v, size_t len, const char *str) |
| 1155 | { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1156 | bc_vec_pop_all(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1157 | bc_vec_expand(v, len + 1); |
| 1158 | memcpy(v->v, str, len); |
| 1159 | v->len = len; |
| 1160 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1161 | bc_vec_pushZeroByte(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | static void bc_vec_concat(BcVec *v, const char *str) |
| 1165 | { |
Denys Vlasenko | 8b4cf0d | 2018-12-10 15:12:58 +0100 | [diff] [blame] | 1166 | size_t len, slen; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1167 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1168 | if (v->len == 0) bc_vec_pushZeroByte(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1169 | |
Denys Vlasenko | 8b4cf0d | 2018-12-10 15:12:58 +0100 | [diff] [blame] | 1170 | slen = strlen(str); |
| 1171 | len = v->len + slen; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1172 | |
Denys Vlasenko | 8b4cf0d | 2018-12-10 15:12:58 +0100 | [diff] [blame] | 1173 | if (v->cap < len) bc_vec_grow(v, slen); |
Denys Vlasenko | 1ff8862 | 2018-12-06 12:06:16 +0100 | [diff] [blame] | 1174 | strcpy(v->v + v->len - 1, str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1175 | |
| 1176 | v->len = len; |
| 1177 | } |
| 1178 | |
| 1179 | static void *bc_vec_item(const BcVec *v, size_t idx) |
| 1180 | { |
| 1181 | return v->v + v->size * idx; |
| 1182 | } |
| 1183 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 1184 | static char** bc_program_str(size_t idx) |
| 1185 | { |
| 1186 | return bc_vec_item(&G.prog.strs, idx); |
| 1187 | } |
| 1188 | |
| 1189 | static BcFunc* bc_program_func(size_t idx) |
| 1190 | { |
| 1191 | return bc_vec_item(&G.prog.fns, idx); |
| 1192 | } |
| 1193 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1194 | static void *bc_vec_item_rev(const BcVec *v, size_t idx) |
| 1195 | { |
| 1196 | return v->v + v->size * (v->len - idx - 1); |
| 1197 | } |
| 1198 | |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 1199 | static void *bc_vec_top(const BcVec *v) |
| 1200 | { |
| 1201 | return v->v + v->size * (v->len - 1); |
| 1202 | } |
| 1203 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 1204 | static FAST_FUNC void bc_vec_free(void *vec) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1205 | { |
| 1206 | BcVec *v = (BcVec *) vec; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1207 | bc_vec_pop_all(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1208 | free(v->v); |
| 1209 | } |
| 1210 | |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 1211 | static int bc_id_cmp(const void *e1, const void *e2) |
| 1212 | { |
| 1213 | return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name); |
| 1214 | } |
| 1215 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 1216 | static FAST_FUNC void bc_id_free(void *id) |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 1217 | { |
| 1218 | free(((BcId *) id)->name); |
| 1219 | } |
| 1220 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1221 | static size_t bc_map_find(const BcVec *v, const void *ptr) |
| 1222 | { |
| 1223 | size_t low = 0, high = v->len; |
| 1224 | |
| 1225 | while (low < high) { |
| 1226 | |
| 1227 | size_t mid = (low + high) / 2; |
| 1228 | BcId *id = bc_vec_item(v, mid); |
| 1229 | int result = bc_id_cmp(ptr, id); |
| 1230 | |
| 1231 | if (result == 0) |
| 1232 | return mid; |
| 1233 | else if (result < 0) |
| 1234 | high = mid; |
| 1235 | else |
| 1236 | low = mid + 1; |
| 1237 | } |
| 1238 | |
| 1239 | return low; |
| 1240 | } |
| 1241 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1242 | 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] | 1243 | { |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1244 | size_t n = *i = bc_map_find(v, ptr); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1245 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1246 | if (n == v->len) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1247 | bc_vec_push(v, ptr); |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1248 | else if (!bc_id_cmp(ptr, bc_vec_item(v, n))) |
| 1249 | return 0; // "was not inserted" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1250 | else |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1251 | bc_vec_pushAt(v, ptr, n); |
| 1252 | return 1; // "was inserted" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1253 | } |
| 1254 | |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1255 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1256 | static size_t bc_map_index(const BcVec *v, const void *ptr) |
| 1257 | { |
| 1258 | size_t i = bc_map_find(v, ptr); |
| 1259 | if (i >= v->len) return BC_VEC_INVALID_IDX; |
| 1260 | return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i; |
| 1261 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1262 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1263 | |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1264 | static int push_input_byte(BcVec *vec, char c) |
| 1265 | { |
| 1266 | if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'? |
| 1267 | || c > 0x7e |
| 1268 | ) { |
| 1269 | // Bad chars on this line, ignore entire line |
| 1270 | bc_error_fmt("illegal character 0x%02x", c); |
| 1271 | return 1; |
| 1272 | } |
| 1273 | bc_vec_pushByte(vec, (char)c); |
| 1274 | return 0; |
| 1275 | } |
| 1276 | |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 1277 | static BcStatus bc_read_line(BcVec *vec) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1278 | { |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 1279 | BcStatus s; |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1280 | bool bad_chars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1281 | |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 1282 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 1283 | do { |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1284 | int c; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1285 | |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1286 | bad_chars = 0; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1287 | bc_vec_pop_all(vec); |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1288 | |
| 1289 | fflush_and_check(); |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1290 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1291 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | b9c321d | 2018-12-07 12:41:42 +0100 | [diff] [blame] | 1292 | if (G_interrupt) { // ^C was pressed |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1293 | intr: |
Denys Vlasenko | b9c321d | 2018-12-07 12:41:42 +0100 | [diff] [blame] | 1294 | G_interrupt = 0; |
Denys Vlasenko | ac6ed11 | 2018-12-08 21:39:10 +0100 | [diff] [blame] | 1295 | // GNU bc says "interrupted execution." |
| 1296 | // GNU dc says "Interrupt!" |
| 1297 | fputs("\ninterrupted execution\n", stderr); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1298 | } |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1299 | # if ENABLE_FEATURE_EDITING |
| 1300 | if (G_ttyin) { |
| 1301 | int n, i; |
| 1302 | # define line_buf bb_common_bufsiz1 |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 1303 | n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE); |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1304 | if (n <= 0) { // read errors or EOF, or ^D, or ^C |
| 1305 | if (n == 0) // ^C |
| 1306 | goto intr; |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 1307 | s = BC_STATUS_EOF; |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1308 | break; |
| 1309 | } |
| 1310 | i = 0; |
| 1311 | for (;;) { |
| 1312 | c = line_buf[i++]; |
| 1313 | if (!c) break; |
| 1314 | bad_chars |= push_input_byte(vec, c); |
| 1315 | } |
| 1316 | # undef line_buf |
| 1317 | } else |
| 1318 | # endif |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1319 | #endif |
Denys Vlasenko | ed84935 | 2018-12-06 10:26:13 +0100 | [diff] [blame] | 1320 | { |
Denys Vlasenko | ed84935 | 2018-12-06 10:26:13 +0100 | [diff] [blame] | 1321 | IF_FEATURE_BC_SIGNALS(errno = 0;) |
| 1322 | do { |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1323 | c = fgetc(stdin); |
| 1324 | #if ENABLE_FEATURE_BC_SIGNALS && !ENABLE_FEATURE_EDITING |
| 1325 | // Both conditions appear simultaneously, check both just in case |
Denys Vlasenko | b9c321d | 2018-12-07 12:41:42 +0100 | [diff] [blame] | 1326 | if (errno == EINTR || G_interrupt) { |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1327 | // ^C was pressed |
| 1328 | clearerr(stdin); |
| 1329 | goto intr; |
| 1330 | } |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1331 | #endif |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1332 | if (c == EOF) { |
Denys Vlasenko | ed84935 | 2018-12-06 10:26:13 +0100 | [diff] [blame] | 1333 | if (ferror(stdin)) |
| 1334 | quit(); // this emits error message |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 1335 | s = BC_STATUS_EOF; |
Denys Vlasenko | ed84935 | 2018-12-06 10:26:13 +0100 | [diff] [blame] | 1336 | // Note: EOF does not append '\n', therefore: |
| 1337 | // printf 'print 123\n' | bc - works |
| 1338 | // printf 'print 123' | bc - fails (syntax error) |
| 1339 | break; |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1340 | } |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1341 | bad_chars |= push_input_byte(vec, c); |
| 1342 | } while (c != '\n'); |
Denys Vlasenko | ed84935 | 2018-12-06 10:26:13 +0100 | [diff] [blame] | 1343 | } |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1344 | } while (bad_chars); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1345 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1346 | bc_vec_pushZeroByte(vec); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1347 | |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 1348 | return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1349 | } |
| 1350 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 1351 | static char* bc_read_file(const char *path) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1352 | { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 1353 | char *buf; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 1354 | size_t size = ((size_t) -1); |
| 1355 | size_t i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1356 | |
Denys Vlasenko | 4c9455f | 2018-12-06 15:21:39 +0100 | [diff] [blame] | 1357 | // Never returns NULL (dies on errors) |
| 1358 | buf = xmalloc_xopen_read_close(path, &size); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1359 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 1360 | for (i = 0; i < size; ++i) { |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1361 | char c = buf[i]; |
| 1362 | if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'? |
| 1363 | || c > 0x7e |
| 1364 | ) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 1365 | free(buf); |
| 1366 | buf = NULL; |
| 1367 | break; |
| 1368 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1369 | } |
| 1370 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 1371 | return buf; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1372 | } |
| 1373 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1374 | static void bc_num_setToZero(BcNum *n, size_t scale) |
| 1375 | { |
| 1376 | n->len = 0; |
| 1377 | n->neg = false; |
| 1378 | n->rdx = scale; |
| 1379 | } |
| 1380 | |
| 1381 | static void bc_num_zero(BcNum *n) |
| 1382 | { |
| 1383 | bc_num_setToZero(n, 0); |
| 1384 | } |
| 1385 | |
| 1386 | static void bc_num_one(BcNum *n) |
| 1387 | { |
| 1388 | bc_num_setToZero(n, 0); |
| 1389 | n->len = 1; |
| 1390 | n->num[0] = 1; |
| 1391 | } |
| 1392 | |
| 1393 | static void bc_num_ten(BcNum *n) |
| 1394 | { |
| 1395 | bc_num_setToZero(n, 0); |
| 1396 | n->len = 2; |
| 1397 | n->num[0] = 0; |
| 1398 | n->num[1] = 1; |
| 1399 | } |
| 1400 | |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1401 | // Note: this also sets BcNum to zero |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1402 | static void bc_num_init(BcNum *n, size_t req) |
| 1403 | { |
| 1404 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1405 | //memset(n, 0, sizeof(BcNum)); - cleared by assignments below |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1406 | n->num = xmalloc(req); |
| 1407 | n->cap = req; |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1408 | n->rdx = 0; |
| 1409 | n->len = 0; |
| 1410 | n->neg = false; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1411 | } |
| 1412 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 1413 | static void bc_num_init_DEF_SIZE(BcNum *n) |
| 1414 | { |
| 1415 | bc_num_init(n, BC_NUM_DEF_SIZE); |
| 1416 | } |
| 1417 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1418 | static void bc_num_expand(BcNum *n, size_t req) |
| 1419 | { |
| 1420 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
| 1421 | if (req > n->cap) { |
| 1422 | n->num = xrealloc(n->num, req); |
| 1423 | n->cap = req; |
| 1424 | } |
| 1425 | } |
| 1426 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 1427 | static FAST_FUNC void bc_num_free(void *num) |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1428 | { |
| 1429 | free(((BcNum *) num)->num); |
| 1430 | } |
| 1431 | |
| 1432 | static void bc_num_copy(BcNum *d, BcNum *s) |
| 1433 | { |
| 1434 | if (d != s) { |
| 1435 | bc_num_expand(d, s->cap); |
| 1436 | d->len = s->len; |
| 1437 | d->neg = s->neg; |
| 1438 | d->rdx = s->rdx; |
| 1439 | memcpy(d->num, s->num, sizeof(BcDig) * d->len); |
| 1440 | } |
| 1441 | } |
| 1442 | |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1443 | static BcStatus bc_num_ulong(BcNum *n, unsigned long *result_p) |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1444 | { |
| 1445 | size_t i; |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1446 | unsigned long pow, result; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1447 | |
| 1448 | if (n->neg) return bc_error("negative number"); |
| 1449 | |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1450 | for (result = 0, pow = 1, i = n->rdx; i < n->len; ++i) { |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1451 | |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1452 | unsigned long prev = result, powprev = pow; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1453 | |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1454 | result += ((unsigned long) n->num[i]) * pow; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1455 | pow *= 10; |
| 1456 | |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1457 | if (result < prev || pow < powprev) |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1458 | return bc_error("overflow"); |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1459 | prev = result; |
| 1460 | powprev = pow; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1461 | } |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1462 | *result_p = result; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1463 | |
| 1464 | return BC_STATUS_SUCCESS; |
| 1465 | } |
| 1466 | |
| 1467 | static void bc_num_ulong2num(BcNum *n, unsigned long val) |
| 1468 | { |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1469 | BcDig *ptr; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1470 | |
| 1471 | bc_num_zero(n); |
| 1472 | |
| 1473 | if (val == 0) return; |
| 1474 | |
Denys Vlasenko | b696d9e | 2018-12-10 12:22:15 +0100 | [diff] [blame] | 1475 | if (ULONG_MAX == 0xffffffffUL) |
| 1476 | bc_num_expand(n, 10); // 10 digits: 4294967295 |
Denys Vlasenko | c665c18 | 2018-12-10 15:15:42 +0100 | [diff] [blame] | 1477 | if (ULONG_MAX == 0xffffffffffffffffULL) |
Denys Vlasenko | b696d9e | 2018-12-10 12:22:15 +0100 | [diff] [blame] | 1478 | bc_num_expand(n, 20); // 20 digits: 18446744073709551615 |
Denys Vlasenko | c665c18 | 2018-12-10 15:15:42 +0100 | [diff] [blame] | 1479 | BUILD_BUG_ON(ULONG_MAX > 0xffffffffffffffffULL); |
Denys Vlasenko | b696d9e | 2018-12-10 12:22:15 +0100 | [diff] [blame] | 1480 | |
| 1481 | ptr = n->num; |
| 1482 | for (;;) { |
| 1483 | n->len++; |
| 1484 | *ptr++ = val % 10; |
| 1485 | val /= 10; |
| 1486 | if (val == 0) break; |
| 1487 | } |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1488 | } |
| 1489 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1490 | static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1491 | size_t len) |
| 1492 | { |
| 1493 | size_t i, j; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1494 | for (i = 0; i < len; ++i) { |
| 1495 | for (a[i] -= b[i], j = 0; a[i + j] < 0;) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1496 | a[i + j++] += 10; |
| 1497 | a[i + j] -= 1; |
| 1498 | } |
| 1499 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1500 | } |
| 1501 | |
Denys Vlasenko | 2d615fe | 2018-12-07 16:22:45 +0100 | [diff] [blame] | 1502 | #define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg)) |
| 1503 | #define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1) |
| 1504 | #define BC_NUM_INT(n) ((n)->len - (n)->rdx) |
| 1505 | //#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1) |
| 1506 | static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b) |
| 1507 | { |
| 1508 | return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1; |
| 1509 | } |
| 1510 | //#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1) |
| 1511 | static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale) |
| 1512 | { |
| 1513 | return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1; |
| 1514 | } |
| 1515 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1516 | static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len) |
| 1517 | { |
| 1518 | size_t i; |
| 1519 | int c = 0; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1520 | for (i = len - 1; i < len && !(c = a[i] - b[i]); --i); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1521 | return BC_NUM_NEG(i + 1, c < 0); |
| 1522 | } |
| 1523 | |
| 1524 | static ssize_t bc_num_cmp(BcNum *a, BcNum *b) |
| 1525 | { |
| 1526 | size_t i, min, a_int, b_int, diff; |
| 1527 | BcDig *max_num, *min_num; |
| 1528 | bool a_max, neg = false; |
| 1529 | ssize_t cmp; |
| 1530 | |
| 1531 | if (a == b) return 0; |
| 1532 | if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg); |
| 1533 | if (b->len == 0) return BC_NUM_NEG(1, a->neg); |
| 1534 | if (a->neg) { |
| 1535 | if (b->neg) |
| 1536 | neg = true; |
| 1537 | else |
| 1538 | return -1; |
| 1539 | } |
| 1540 | else if (b->neg) |
| 1541 | return 1; |
| 1542 | |
| 1543 | a_int = BC_NUM_INT(a); |
| 1544 | b_int = BC_NUM_INT(b); |
| 1545 | a_int -= b_int; |
| 1546 | a_max = (a->rdx > b->rdx); |
| 1547 | |
| 1548 | if (a_int != 0) return (ssize_t) a_int; |
| 1549 | |
| 1550 | if (a_max) { |
| 1551 | min = b->rdx; |
| 1552 | diff = a->rdx - b->rdx; |
| 1553 | max_num = a->num + diff; |
| 1554 | min_num = b->num; |
| 1555 | } |
| 1556 | else { |
| 1557 | min = a->rdx; |
| 1558 | diff = b->rdx - a->rdx; |
| 1559 | max_num = b->num + diff; |
| 1560 | min_num = a->num; |
| 1561 | } |
| 1562 | |
| 1563 | cmp = bc_num_compare(max_num, min_num, b_int + min); |
| 1564 | if (cmp != 0) return BC_NUM_NEG(cmp, (!a_max) != neg); |
| 1565 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1566 | for (max_num -= diff, i = diff - 1; i < diff; --i) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1567 | if (max_num[i]) return BC_NUM_NEG(1, (!a_max) != neg); |
| 1568 | } |
| 1569 | |
| 1570 | return 0; |
| 1571 | } |
| 1572 | |
| 1573 | static void bc_num_truncate(BcNum *n, size_t places) |
| 1574 | { |
| 1575 | if (places == 0) return; |
| 1576 | |
| 1577 | n->rdx -= places; |
| 1578 | |
| 1579 | if (n->len != 0) { |
| 1580 | n->len -= places; |
| 1581 | memmove(n->num, n->num + places, n->len * sizeof(BcDig)); |
| 1582 | } |
| 1583 | } |
| 1584 | |
| 1585 | static void bc_num_extend(BcNum *n, size_t places) |
| 1586 | { |
| 1587 | size_t len = n->len + places; |
| 1588 | |
| 1589 | if (places != 0) { |
| 1590 | |
| 1591 | if (n->cap < len) bc_num_expand(n, len); |
| 1592 | |
| 1593 | memmove(n->num + places, n->num, sizeof(BcDig) * n->len); |
| 1594 | memset(n->num, 0, sizeof(BcDig) * places); |
| 1595 | |
| 1596 | n->len += places; |
| 1597 | n->rdx += places; |
| 1598 | } |
| 1599 | } |
| 1600 | |
| 1601 | static void bc_num_clean(BcNum *n) |
| 1602 | { |
| 1603 | while (n->len > 0 && n->num[n->len - 1] == 0) --n->len; |
| 1604 | if (n->len == 0) |
| 1605 | n->neg = false; |
| 1606 | else if (n->len < n->rdx) |
| 1607 | n->len = n->rdx; |
| 1608 | } |
| 1609 | |
| 1610 | static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2) |
| 1611 | { |
| 1612 | if (n->rdx < scale) |
| 1613 | bc_num_extend(n, scale - n->rdx); |
| 1614 | else |
| 1615 | bc_num_truncate(n, n->rdx - scale); |
| 1616 | |
| 1617 | bc_num_clean(n); |
| 1618 | if (n->len != 0) n->neg = !neg1 != !neg2; |
| 1619 | } |
| 1620 | |
| 1621 | static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a, |
| 1622 | BcNum *restrict b) |
| 1623 | { |
| 1624 | if (idx < n->len) { |
| 1625 | |
| 1626 | b->len = n->len - idx; |
| 1627 | a->len = idx; |
| 1628 | a->rdx = b->rdx = 0; |
| 1629 | |
| 1630 | memcpy(b->num, n->num + idx, b->len * sizeof(BcDig)); |
| 1631 | memcpy(a->num, n->num, idx * sizeof(BcDig)); |
| 1632 | } |
| 1633 | else { |
| 1634 | bc_num_zero(b); |
| 1635 | bc_num_copy(a, n); |
| 1636 | } |
| 1637 | |
| 1638 | bc_num_clean(a); |
| 1639 | bc_num_clean(b); |
| 1640 | } |
| 1641 | |
| 1642 | static BcStatus bc_num_shift(BcNum *n, size_t places) |
| 1643 | { |
| 1644 | if (places == 0 || n->len == 0) return BC_STATUS_SUCCESS; |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 1645 | |
| 1646 | // This check makes sense only if size_t is (much) larger than BC_MAX_NUM. |
| 1647 | if (SIZE_MAX > (BC_MAX_NUM | 0xff)) { |
| 1648 | if (places + n->len > BC_MAX_NUM) |
| 1649 | return bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"); |
| 1650 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1651 | |
| 1652 | if (n->rdx >= places) |
| 1653 | n->rdx -= places; |
| 1654 | else { |
| 1655 | bc_num_extend(n, places - n->rdx); |
| 1656 | n->rdx = 0; |
| 1657 | } |
| 1658 | |
| 1659 | bc_num_clean(n); |
| 1660 | |
| 1661 | return BC_STATUS_SUCCESS; |
| 1662 | } |
| 1663 | |
| 1664 | static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) |
| 1665 | { |
| 1666 | BcNum one; |
| 1667 | BcDig num[2]; |
| 1668 | |
| 1669 | one.cap = 2; |
| 1670 | one.num = num; |
| 1671 | bc_num_one(&one); |
| 1672 | |
| 1673 | return bc_num_div(&one, a, b, scale); |
| 1674 | } |
| 1675 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 1676 | static FAST_FUNC BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1677 | { |
| 1678 | BcDig *ptr, *ptr_a, *ptr_b, *ptr_c; |
| 1679 | size_t i, max, min_rdx, min_int, diff, a_int, b_int; |
| 1680 | int carry, in; |
| 1681 | |
| 1682 | // Because this function doesn't need to use scale (per the bc spec), |
| 1683 | // I am hijacking it to say whether it's doing an add or a subtract. |
| 1684 | |
| 1685 | if (a->len == 0) { |
| 1686 | bc_num_copy(c, b); |
| 1687 | if (sub && c->len) c->neg = !c->neg; |
| 1688 | return BC_STATUS_SUCCESS; |
| 1689 | } |
| 1690 | else if (b->len == 0) { |
| 1691 | bc_num_copy(c, a); |
| 1692 | return BC_STATUS_SUCCESS; |
| 1693 | } |
| 1694 | |
| 1695 | c->neg = a->neg; |
| 1696 | c->rdx = BC_MAX(a->rdx, b->rdx); |
| 1697 | min_rdx = BC_MIN(a->rdx, b->rdx); |
| 1698 | c->len = 0; |
| 1699 | |
| 1700 | if (a->rdx > b->rdx) { |
| 1701 | diff = a->rdx - b->rdx; |
| 1702 | ptr = a->num; |
| 1703 | ptr_a = a->num + diff; |
| 1704 | ptr_b = b->num; |
| 1705 | } |
| 1706 | else { |
| 1707 | diff = b->rdx - a->rdx; |
| 1708 | ptr = b->num; |
| 1709 | ptr_a = a->num; |
| 1710 | ptr_b = b->num + diff; |
| 1711 | } |
| 1712 | |
| 1713 | for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i]; |
| 1714 | |
| 1715 | ptr_c += diff; |
| 1716 | a_int = BC_NUM_INT(a); |
| 1717 | b_int = BC_NUM_INT(b); |
| 1718 | |
| 1719 | if (a_int > b_int) { |
| 1720 | min_int = b_int; |
| 1721 | max = a_int; |
| 1722 | ptr = ptr_a; |
| 1723 | } |
| 1724 | else { |
| 1725 | min_int = a_int; |
| 1726 | max = b_int; |
| 1727 | ptr = ptr_b; |
| 1728 | } |
| 1729 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1730 | 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] | 1731 | in = ((int) ptr_a[i]) + ((int) ptr_b[i]) + carry; |
| 1732 | carry = in / 10; |
| 1733 | ptr_c[i] = (BcDig)(in % 10); |
| 1734 | } |
| 1735 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1736 | for (; i < max + min_rdx; ++i, ++c->len) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1737 | in = ((int) ptr[i]) + carry; |
| 1738 | carry = in / 10; |
| 1739 | ptr_c[i] = (BcDig)(in % 10); |
| 1740 | } |
| 1741 | |
| 1742 | if (carry != 0) c->num[c->len++] = (BcDig) carry; |
| 1743 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1744 | return BC_STATUS_SUCCESS; // can't make void, see bc_num_binary() |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1745 | } |
| 1746 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 1747 | static FAST_FUNC BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1748 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1749 | ssize_t cmp; |
| 1750 | BcNum *minuend, *subtrahend; |
| 1751 | size_t start; |
| 1752 | bool aneg, bneg, neg; |
| 1753 | |
| 1754 | // Because this function doesn't need to use scale (per the bc spec), |
| 1755 | // I am hijacking it to say whether it's doing an add or a subtract. |
| 1756 | |
| 1757 | if (a->len == 0) { |
| 1758 | bc_num_copy(c, b); |
| 1759 | if (sub && c->len) c->neg = !c->neg; |
| 1760 | return BC_STATUS_SUCCESS; |
| 1761 | } |
| 1762 | else if (b->len == 0) { |
| 1763 | bc_num_copy(c, a); |
| 1764 | return BC_STATUS_SUCCESS; |
| 1765 | } |
| 1766 | |
| 1767 | aneg = a->neg; |
| 1768 | bneg = b->neg; |
| 1769 | a->neg = b->neg = false; |
| 1770 | |
| 1771 | cmp = bc_num_cmp(a, b); |
| 1772 | |
| 1773 | a->neg = aneg; |
| 1774 | b->neg = bneg; |
| 1775 | |
| 1776 | if (cmp == 0) { |
| 1777 | bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx)); |
| 1778 | return BC_STATUS_SUCCESS; |
| 1779 | } |
| 1780 | else if (cmp > 0) { |
| 1781 | neg = a->neg; |
| 1782 | minuend = a; |
| 1783 | subtrahend = b; |
| 1784 | } |
| 1785 | else { |
| 1786 | neg = b->neg; |
| 1787 | if (sub) neg = !neg; |
| 1788 | minuend = b; |
| 1789 | subtrahend = a; |
| 1790 | } |
| 1791 | |
| 1792 | bc_num_copy(c, minuend); |
| 1793 | c->neg = neg; |
| 1794 | |
| 1795 | if (c->rdx < subtrahend->rdx) { |
| 1796 | bc_num_extend(c, subtrahend->rdx - c->rdx); |
| 1797 | start = 0; |
| 1798 | } |
| 1799 | else |
| 1800 | start = c->rdx - subtrahend->rdx; |
| 1801 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1802 | bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1803 | |
| 1804 | bc_num_clean(c); |
| 1805 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1806 | return BC_STATUS_SUCCESS; // can't make void, see bc_num_binary() |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1807 | } |
| 1808 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 1809 | static FAST_FUNC BcStatus bc_num_k(BcNum *restrict a, BcNum *restrict b, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1810 | BcNum *restrict c) |
| 1811 | { |
| 1812 | BcStatus s; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1813 | 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] | 1814 | BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1815 | bool aone; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1816 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1817 | if (a->len == 0 || b->len == 0) { |
| 1818 | bc_num_zero(c); |
| 1819 | return BC_STATUS_SUCCESS; |
| 1820 | } |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1821 | aone = BC_NUM_ONE(a); |
| 1822 | if (aone || BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1823 | bc_num_copy(c, aone ? b : a); |
| 1824 | return BC_STATUS_SUCCESS; |
| 1825 | } |
| 1826 | |
| 1827 | if (a->len + b->len < BC_NUM_KARATSUBA_LEN || |
| 1828 | a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN) |
| 1829 | { |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1830 | size_t i, j, len; |
Denys Vlasenko | b3cb901 | 2018-12-05 19:05:32 +0100 | [diff] [blame] | 1831 | unsigned carry; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1832 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1833 | bc_num_expand(c, a->len + b->len + 1); |
| 1834 | |
| 1835 | memset(c->num, 0, sizeof(BcDig) * c->cap); |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1836 | c->len = len = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1837 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1838 | for (i = 0; i < b->len; ++i) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1839 | |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1840 | carry = 0; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1841 | for (j = 0; j < a->len; ++j) { |
Denys Vlasenko | b3cb901 | 2018-12-05 19:05:32 +0100 | [diff] [blame] | 1842 | unsigned in = c->num[i + j]; |
| 1843 | in += ((unsigned) a->num[j]) * ((unsigned) b->num[i]) + carry; |
| 1844 | // note: compilers prefer _unsigned_ div/const |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1845 | carry = in / 10; |
| 1846 | c->num[i + j] = (BcDig)(in % 10); |
| 1847 | } |
| 1848 | |
| 1849 | c->num[i + j] += (BcDig) carry; |
| 1850 | len = BC_MAX(len, i + j + !!carry); |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 1851 | |
| 1852 | // a=2^1000000 |
| 1853 | // a*a <- without check below, this will not be interruptible |
| 1854 | if (G_interrupt) return BC_STATUS_FAILURE; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | c->len = len; |
| 1858 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1859 | return BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1860 | } |
| 1861 | |
| 1862 | bc_num_init(&l1, max); |
| 1863 | bc_num_init(&h1, max); |
| 1864 | bc_num_init(&l2, max); |
| 1865 | bc_num_init(&h2, max); |
| 1866 | bc_num_init(&m1, max); |
| 1867 | bc_num_init(&m2, max); |
| 1868 | bc_num_init(&z0, max); |
| 1869 | bc_num_init(&z1, max); |
| 1870 | bc_num_init(&z2, max); |
| 1871 | bc_num_init(&temp, max + max); |
| 1872 | |
| 1873 | bc_num_split(a, max2, &l1, &h1); |
| 1874 | bc_num_split(b, max2, &l2, &h2); |
| 1875 | |
| 1876 | s = bc_num_add(&h1, &l1, &m1, 0); |
| 1877 | if (s) goto err; |
| 1878 | s = bc_num_add(&h2, &l2, &m2, 0); |
| 1879 | if (s) goto err; |
| 1880 | |
| 1881 | s = bc_num_k(&h1, &h2, &z0); |
| 1882 | if (s) goto err; |
| 1883 | s = bc_num_k(&m1, &m2, &z1); |
| 1884 | if (s) goto err; |
| 1885 | s = bc_num_k(&l1, &l2, &z2); |
| 1886 | if (s) goto err; |
| 1887 | |
| 1888 | s = bc_num_sub(&z1, &z0, &temp, 0); |
| 1889 | if (s) goto err; |
| 1890 | s = bc_num_sub(&temp, &z2, &z1, 0); |
| 1891 | if (s) goto err; |
| 1892 | |
| 1893 | s = bc_num_shift(&z0, max2 * 2); |
| 1894 | if (s) goto err; |
| 1895 | s = bc_num_shift(&z1, max2); |
| 1896 | if (s) goto err; |
| 1897 | s = bc_num_add(&z0, &z1, &temp, 0); |
| 1898 | if (s) goto err; |
| 1899 | s = bc_num_add(&temp, &z2, c, 0); |
| 1900 | |
| 1901 | err: |
| 1902 | bc_num_free(&temp); |
| 1903 | bc_num_free(&z2); |
| 1904 | bc_num_free(&z1); |
| 1905 | bc_num_free(&z0); |
| 1906 | bc_num_free(&m2); |
| 1907 | bc_num_free(&m1); |
| 1908 | bc_num_free(&h2); |
| 1909 | bc_num_free(&l2); |
| 1910 | bc_num_free(&h1); |
| 1911 | bc_num_free(&l1); |
| 1912 | return s; |
| 1913 | } |
| 1914 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 1915 | static FAST_FUNC BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1916 | { |
| 1917 | BcStatus s; |
| 1918 | BcNum cpa, cpb; |
| 1919 | size_t maxrdx = BC_MAX(a->rdx, b->rdx); |
| 1920 | |
| 1921 | scale = BC_MAX(scale, a->rdx); |
| 1922 | scale = BC_MAX(scale, b->rdx); |
| 1923 | scale = BC_MIN(a->rdx + b->rdx, scale); |
| 1924 | maxrdx = BC_MAX(maxrdx, scale); |
| 1925 | |
| 1926 | bc_num_init(&cpa, a->len); |
| 1927 | bc_num_init(&cpb, b->len); |
| 1928 | |
| 1929 | bc_num_copy(&cpa, a); |
| 1930 | bc_num_copy(&cpb, b); |
| 1931 | cpa.neg = cpb.neg = false; |
| 1932 | |
| 1933 | s = bc_num_shift(&cpa, maxrdx); |
| 1934 | if (s) goto err; |
| 1935 | s = bc_num_shift(&cpb, maxrdx); |
| 1936 | if (s) goto err; |
| 1937 | s = bc_num_k(&cpa, &cpb, c); |
| 1938 | if (s) goto err; |
| 1939 | |
| 1940 | maxrdx += scale; |
| 1941 | bc_num_expand(c, c->len + maxrdx); |
| 1942 | |
| 1943 | if (c->len < maxrdx) { |
| 1944 | memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig)); |
| 1945 | c->len += maxrdx; |
| 1946 | } |
| 1947 | |
| 1948 | c->rdx = maxrdx; |
| 1949 | bc_num_retireMul(c, scale, a->neg, b->neg); |
| 1950 | |
| 1951 | err: |
| 1952 | bc_num_free(&cpb); |
| 1953 | bc_num_free(&cpa); |
| 1954 | return s; |
| 1955 | } |
| 1956 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 1957 | static FAST_FUNC BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1958 | { |
| 1959 | BcStatus s = BC_STATUS_SUCCESS; |
| 1960 | BcDig *n, *p, q; |
| 1961 | size_t len, end, i; |
| 1962 | BcNum cp; |
| 1963 | bool zero = true; |
| 1964 | |
| 1965 | if (b->len == 0) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 1966 | return bc_error("divide by zero"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1967 | else if (a->len == 0) { |
| 1968 | bc_num_setToZero(c, scale); |
| 1969 | return BC_STATUS_SUCCESS; |
| 1970 | } |
| 1971 | else if (BC_NUM_ONE(b)) { |
| 1972 | bc_num_copy(c, a); |
| 1973 | bc_num_retireMul(c, scale, a->neg, b->neg); |
| 1974 | return BC_STATUS_SUCCESS; |
| 1975 | } |
| 1976 | |
| 1977 | bc_num_init(&cp, BC_NUM_MREQ(a, b, scale)); |
| 1978 | bc_num_copy(&cp, a); |
| 1979 | len = b->len; |
| 1980 | |
| 1981 | if (len > cp.len) { |
| 1982 | bc_num_expand(&cp, len + 2); |
| 1983 | bc_num_extend(&cp, len - cp.len); |
| 1984 | } |
| 1985 | |
| 1986 | if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx); |
| 1987 | cp.rdx -= b->rdx; |
| 1988 | if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx); |
| 1989 | |
| 1990 | if (b->rdx == b->len) { |
| 1991 | for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1]; |
| 1992 | len -= i - 1; |
| 1993 | } |
| 1994 | |
| 1995 | if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1); |
| 1996 | |
| 1997 | // We want an extra zero in front to make things simpler. |
| 1998 | cp.num[cp.len++] = 0; |
| 1999 | end = cp.len - len; |
| 2000 | |
| 2001 | bc_num_expand(c, cp.len); |
| 2002 | |
| 2003 | bc_num_zero(c); |
| 2004 | memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig)); |
| 2005 | c->rdx = cp.rdx; |
| 2006 | c->len = cp.len; |
| 2007 | p = b->num; |
| 2008 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2009 | for (i = end - 1; !s && i < end; --i) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2010 | n = cp.num + i; |
| 2011 | 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] | 2012 | bc_num_subArrays(n, p, len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2013 | c->num[i] = q; |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 2014 | // a=2^100000 |
| 2015 | // scale=40000 |
| 2016 | // 1/a <- without check below, this will not be interruptible |
| 2017 | if (G_interrupt) { |
| 2018 | s = BC_STATUS_FAILURE; |
| 2019 | break; |
| 2020 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2021 | } |
| 2022 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2023 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2024 | bc_num_free(&cp); |
| 2025 | |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 2026 | return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2027 | } |
| 2028 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 2029 | static FAST_FUNC BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2030 | BcNum *restrict d, size_t scale, size_t ts) |
| 2031 | { |
| 2032 | BcStatus s; |
| 2033 | BcNum temp; |
| 2034 | bool neg; |
| 2035 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2036 | if (b->len == 0) |
| 2037 | return bc_error("divide by zero"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2038 | |
| 2039 | if (a->len == 0) { |
| 2040 | bc_num_setToZero(d, ts); |
| 2041 | return BC_STATUS_SUCCESS; |
| 2042 | } |
| 2043 | |
| 2044 | bc_num_init(&temp, d->cap); |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 2045 | s = bc_num_d(a, b, c, scale); |
| 2046 | if (s) goto err; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2047 | |
| 2048 | if (scale != 0) scale = ts; |
| 2049 | |
| 2050 | s = bc_num_m(c, b, &temp, scale); |
| 2051 | if (s) goto err; |
| 2052 | s = bc_num_sub(a, &temp, d, scale); |
| 2053 | if (s) goto err; |
| 2054 | |
| 2055 | if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx); |
| 2056 | |
| 2057 | neg = d->neg; |
| 2058 | bc_num_retireMul(d, ts, a->neg, b->neg); |
| 2059 | d->neg = neg; |
| 2060 | |
| 2061 | err: |
| 2062 | bc_num_free(&temp); |
| 2063 | return s; |
| 2064 | } |
| 2065 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 2066 | static FAST_FUNC BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2067 | { |
| 2068 | BcStatus s; |
| 2069 | BcNum c1; |
| 2070 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 2071 | |
| 2072 | bc_num_init(&c1, len); |
| 2073 | s = bc_num_r(a, b, &c1, c, scale, ts); |
| 2074 | bc_num_free(&c1); |
| 2075 | |
| 2076 | return s; |
| 2077 | } |
| 2078 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 2079 | static FAST_FUNC BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2080 | { |
| 2081 | BcStatus s = BC_STATUS_SUCCESS; |
| 2082 | BcNum copy; |
| 2083 | unsigned long pow; |
| 2084 | size_t i, powrdx, resrdx; |
| 2085 | bool neg, zero; |
| 2086 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2087 | if (b->rdx) return bc_error("non integer number"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2088 | |
| 2089 | if (b->len == 0) { |
| 2090 | bc_num_one(c); |
| 2091 | return BC_STATUS_SUCCESS; |
| 2092 | } |
| 2093 | else if (a->len == 0) { |
| 2094 | bc_num_setToZero(c, scale); |
| 2095 | return BC_STATUS_SUCCESS; |
| 2096 | } |
| 2097 | else if (BC_NUM_ONE(b)) { |
| 2098 | if (!b->neg) |
| 2099 | bc_num_copy(c, a); |
| 2100 | else |
| 2101 | s = bc_num_inv(a, c, scale); |
| 2102 | return s; |
| 2103 | } |
| 2104 | |
| 2105 | neg = b->neg; |
| 2106 | b->neg = false; |
| 2107 | |
| 2108 | s = bc_num_ulong(b, &pow); |
| 2109 | if (s) return s; |
| 2110 | |
| 2111 | bc_num_init(©, a->len); |
| 2112 | bc_num_copy(©, a); |
| 2113 | |
Denys Vlasenko | 2d615fe | 2018-12-07 16:22:45 +0100 | [diff] [blame] | 2114 | if (!neg) { |
| 2115 | if (a->rdx > scale) |
| 2116 | scale = a->rdx; |
| 2117 | if (a->rdx * pow < scale) |
| 2118 | scale = a->rdx * pow; |
| 2119 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2120 | |
| 2121 | b->neg = neg; |
| 2122 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2123 | for (powrdx = a->rdx; !(pow & 1); pow >>= 1) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2124 | powrdx <<= 1; |
| 2125 | s = bc_num_mul(©, ©, ©, powrdx); |
| 2126 | if (s) goto err; |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 2127 | // Not needed: bc_num_mul() has a check for ^C: |
| 2128 | //if (G_interrupt) { |
| 2129 | // s = BC_STATUS_FAILURE; |
| 2130 | // goto err; |
| 2131 | //} |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2132 | } |
| 2133 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2134 | bc_num_copy(c, ©); |
| 2135 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2136 | for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2137 | |
| 2138 | powrdx <<= 1; |
| 2139 | s = bc_num_mul(©, ©, ©, powrdx); |
| 2140 | if (s) goto err; |
| 2141 | |
| 2142 | if (pow & 1) { |
| 2143 | resrdx += powrdx; |
| 2144 | s = bc_num_mul(c, ©, c, resrdx); |
| 2145 | if (s) goto err; |
| 2146 | } |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 2147 | // Not needed: bc_num_mul() has a check for ^C: |
| 2148 | //if (G_interrupt) { |
| 2149 | // s = BC_STATUS_FAILURE; |
| 2150 | // goto err; |
| 2151 | //} |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2152 | } |
| 2153 | |
| 2154 | if (neg) { |
| 2155 | s = bc_num_inv(c, c, scale); |
| 2156 | if (s) goto err; |
| 2157 | } |
| 2158 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2159 | if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale); |
| 2160 | |
| 2161 | // We can't use bc_num_clean() here. |
| 2162 | for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i]; |
| 2163 | if (zero) bc_num_setToZero(c, scale); |
| 2164 | |
| 2165 | err: |
| 2166 | bc_num_free(©); |
| 2167 | return s; |
| 2168 | } |
| 2169 | |
| 2170 | static BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale, |
| 2171 | BcNumBinaryOp op, size_t req) |
| 2172 | { |
| 2173 | BcStatus s; |
| 2174 | BcNum num2, *ptr_a, *ptr_b; |
| 2175 | bool init = false; |
| 2176 | |
| 2177 | if (c == a) { |
| 2178 | ptr_a = &num2; |
| 2179 | memcpy(ptr_a, c, sizeof(BcNum)); |
| 2180 | init = true; |
| 2181 | } |
| 2182 | else |
| 2183 | ptr_a = a; |
| 2184 | |
| 2185 | if (c == b) { |
| 2186 | ptr_b = &num2; |
| 2187 | if (c != a) { |
| 2188 | memcpy(ptr_b, c, sizeof(BcNum)); |
| 2189 | init = true; |
| 2190 | } |
| 2191 | } |
| 2192 | else |
| 2193 | ptr_b = b; |
| 2194 | |
| 2195 | if (init) |
| 2196 | bc_num_init(c, req); |
| 2197 | else |
| 2198 | bc_num_expand(c, req); |
| 2199 | |
| 2200 | s = op(ptr_a, ptr_b, c, scale); |
| 2201 | |
| 2202 | if (init) bc_num_free(&num2); |
| 2203 | |
| 2204 | return s; |
| 2205 | } |
| 2206 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2207 | static void bc_num_printNewline(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2208 | { |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2209 | if (G.prog.nchars == G.prog.len - 1) { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2210 | bb_putchar('\\'); |
| 2211 | bb_putchar('\n'); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2212 | G.prog.nchars = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2213 | } |
| 2214 | } |
| 2215 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2216 | #if ENABLE_DC |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 2217 | static FAST_FUNC void bc_num_printChar(size_t num, size_t width, bool radix) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2218 | { |
Denys Vlasenko | 2a8ad48 | 2018-12-08 21:56:37 +0100 | [diff] [blame] | 2219 | (void) radix; |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2220 | bb_putchar((char) num); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2221 | G.prog.nchars += width; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2222 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2223 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2224 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 2225 | static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2226 | { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2227 | size_t exp, pow; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2228 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2229 | bc_num_printNewline(); |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2230 | bb_putchar(radix ? '.' : ' '); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2231 | ++G.prog.nchars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2232 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2233 | bc_num_printNewline(); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2234 | for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10) |
| 2235 | continue; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2236 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2237 | for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2238 | size_t dig; |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2239 | bc_num_printNewline(); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2240 | dig = num / pow; |
| 2241 | num -= dig * pow; |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2242 | bb_putchar(((char) dig) + '0'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2243 | } |
| 2244 | } |
| 2245 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 2246 | static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2247 | { |
| 2248 | if (radix) { |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2249 | bc_num_printNewline(); |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2250 | bb_putchar('.'); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2251 | G.prog.nchars += 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2252 | } |
| 2253 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2254 | bc_num_printNewline(); |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2255 | bb_putchar(bb_hexdigits_upcase[num]); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2256 | G.prog.nchars += width; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2257 | } |
| 2258 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2259 | static void bc_num_printDecimal(BcNum *n) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2260 | { |
| 2261 | size_t i, rdx = n->rdx - 1; |
| 2262 | |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2263 | if (n->neg) bb_putchar('-'); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2264 | G.prog.nchars += n->neg; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2265 | |
| 2266 | for (i = n->len - 1; i < n->len; --i) |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2267 | bc_num_printHex((size_t) n->num[i], 1, i == rdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2268 | } |
| 2269 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2270 | static BcStatus bc_num_printNum(BcNum *n, BcNum *base, size_t width, BcNumDigitOp print) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2271 | { |
| 2272 | BcStatus s; |
| 2273 | BcVec stack; |
| 2274 | BcNum intp, fracp, digit, frac_len; |
| 2275 | unsigned long dig, *ptr; |
| 2276 | size_t i; |
| 2277 | bool radix; |
| 2278 | |
| 2279 | if (n->len == 0) { |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2280 | print(0, width, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2281 | return BC_STATUS_SUCCESS; |
| 2282 | } |
| 2283 | |
| 2284 | bc_vec_init(&stack, sizeof(long), NULL); |
| 2285 | bc_num_init(&intp, n->len); |
| 2286 | bc_num_init(&fracp, n->rdx); |
| 2287 | bc_num_init(&digit, width); |
| 2288 | bc_num_init(&frac_len, BC_NUM_INT(n)); |
| 2289 | bc_num_copy(&intp, n); |
| 2290 | bc_num_one(&frac_len); |
| 2291 | |
| 2292 | bc_num_truncate(&intp, intp.rdx); |
| 2293 | s = bc_num_sub(n, &intp, &fracp, 0); |
| 2294 | if (s) goto err; |
| 2295 | |
| 2296 | while (intp.len != 0) { |
| 2297 | s = bc_num_divmod(&intp, base, &intp, &digit, 0); |
| 2298 | if (s) goto err; |
| 2299 | s = bc_num_ulong(&digit, &dig); |
| 2300 | if (s) goto err; |
| 2301 | bc_vec_push(&stack, &dig); |
| 2302 | } |
| 2303 | |
| 2304 | for (i = 0; i < stack.len; ++i) { |
| 2305 | ptr = bc_vec_item_rev(&stack, i); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2306 | print(*ptr, width, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2307 | } |
| 2308 | |
| 2309 | if (!n->rdx) goto err; |
| 2310 | |
| 2311 | for (radix = true; frac_len.len <= n->rdx; radix = false) { |
| 2312 | s = bc_num_mul(&fracp, base, &fracp, n->rdx); |
| 2313 | if (s) goto err; |
| 2314 | s = bc_num_ulong(&fracp, &dig); |
| 2315 | if (s) goto err; |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 2316 | bc_num_ulong2num(&intp, dig); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2317 | s = bc_num_sub(&fracp, &intp, &fracp, 0); |
| 2318 | if (s) goto err; |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2319 | print(dig, width, radix); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2320 | s = bc_num_mul(&frac_len, base, &frac_len, 0); |
| 2321 | if (s) goto err; |
| 2322 | } |
| 2323 | |
| 2324 | err: |
| 2325 | bc_num_free(&frac_len); |
| 2326 | bc_num_free(&digit); |
| 2327 | bc_num_free(&fracp); |
| 2328 | bc_num_free(&intp); |
| 2329 | bc_vec_free(&stack); |
| 2330 | return s; |
| 2331 | } |
| 2332 | |
Denys Vlasenko | 0f37b32 | 2018-12-08 23:48:53 +0100 | [diff] [blame] | 2333 | static BcStatus bc_num_printBase(BcNum *n) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2334 | { |
| 2335 | BcStatus s; |
| 2336 | size_t width, i; |
| 2337 | BcNumDigitOp print; |
| 2338 | bool neg = n->neg; |
| 2339 | |
Denys Vlasenko | 0f37b32 | 2018-12-08 23:48:53 +0100 | [diff] [blame] | 2340 | if (neg) { |
| 2341 | bb_putchar('-'); |
| 2342 | G.prog.nchars++; |
| 2343 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2344 | |
| 2345 | n->neg = false; |
| 2346 | |
Denys Vlasenko | 0f37b32 | 2018-12-08 23:48:53 +0100 | [diff] [blame] | 2347 | if (G.prog.ob_t <= BC_NUM_MAX_IBASE) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2348 | width = 1; |
| 2349 | print = bc_num_printHex; |
| 2350 | } |
| 2351 | else { |
Denys Vlasenko | 0f37b32 | 2018-12-08 23:48:53 +0100 | [diff] [blame] | 2352 | for (i = G.prog.ob_t - 1, width = 0; i != 0; i /= 10, ++width) |
| 2353 | continue; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2354 | print = bc_num_printDigits; |
| 2355 | } |
| 2356 | |
Denys Vlasenko | 0f37b32 | 2018-12-08 23:48:53 +0100 | [diff] [blame] | 2357 | s = bc_num_printNum(n, &G.prog.ob, width, print); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2358 | n->neg = neg; |
| 2359 | |
| 2360 | return s; |
| 2361 | } |
| 2362 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2363 | #if ENABLE_DC |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2364 | static BcStatus bc_num_stream(BcNum *n, BcNum *base) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2365 | { |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2366 | return bc_num_printNum(n, base, 1, bc_num_printChar); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2367 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2368 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2369 | |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2370 | static bool bc_num_strValid(const char *val, size_t base) |
| 2371 | { |
| 2372 | BcDig b; |
| 2373 | bool radix; |
| 2374 | |
| 2375 | b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A'); |
| 2376 | radix = false; |
| 2377 | for (;;) { |
| 2378 | BcDig c = *val++; |
| 2379 | if (c == '\0') |
| 2380 | break; |
| 2381 | if (c == '.') { |
| 2382 | if (radix) return false; |
| 2383 | radix = true; |
| 2384 | continue; |
| 2385 | } |
| 2386 | if (c < '0' || c >= b || (c > '9' && c < 'A')) |
| 2387 | return false; |
| 2388 | } |
| 2389 | return true; |
| 2390 | } |
| 2391 | |
| 2392 | // Note: n is already "bc_num_zero()"ed, |
| 2393 | // leading zeroes in "val" are removed |
| 2394 | static void bc_num_parseDecimal(BcNum *n, const char *val) |
| 2395 | { |
| 2396 | size_t len, i; |
| 2397 | const char *ptr; |
| 2398 | bool zero; |
| 2399 | |
| 2400 | len = strlen(val); |
| 2401 | if (len == 0) |
| 2402 | return; |
| 2403 | |
| 2404 | zero = true; |
| 2405 | for (i = 0; val[i]; ++i) { |
| 2406 | if (val[i] != '0' && val[i] != '.') { |
| 2407 | zero = false; |
| 2408 | break; |
| 2409 | } |
| 2410 | } |
| 2411 | bc_num_expand(n, len); |
| 2412 | |
| 2413 | ptr = strchr(val, '.'); |
| 2414 | |
| 2415 | n->rdx = 0; |
| 2416 | if (ptr != NULL) |
| 2417 | n->rdx = (size_t)((val + len) - (ptr + 1)); |
| 2418 | |
| 2419 | if (!zero) { |
| 2420 | i = len - 1; |
| 2421 | for (;;) { |
| 2422 | n->num[n->len] = val[i] - '0'; |
| 2423 | ++n->len; |
| 2424 | skip_dot: |
| 2425 | if ((ssize_t)--i == (ssize_t)-1) break; |
| 2426 | if (val[i] == '.') goto skip_dot; |
| 2427 | } |
| 2428 | } |
| 2429 | } |
| 2430 | |
| 2431 | // Note: n is already "bc_num_zero()"ed, |
| 2432 | // leading zeroes in "val" are removed |
| 2433 | static void bc_num_parseBase(BcNum *n, const char *val, BcNum *base) |
| 2434 | { |
| 2435 | BcStatus s; |
| 2436 | BcNum temp, mult, result; |
| 2437 | BcDig c = '\0'; |
| 2438 | unsigned long v; |
| 2439 | size_t i, digits; |
| 2440 | |
| 2441 | for (i = 0; ; ++i) { |
| 2442 | if (val[i] == '\0') |
| 2443 | return; |
| 2444 | if (val[i] != '.' && val[i] != '0') |
| 2445 | break; |
| 2446 | } |
| 2447 | |
| 2448 | bc_num_init_DEF_SIZE(&temp); |
| 2449 | bc_num_init_DEF_SIZE(&mult); |
| 2450 | |
| 2451 | for (;;) { |
| 2452 | c = *val++; |
| 2453 | if (c == '\0') goto int_err; |
| 2454 | if (c == '.') break; |
| 2455 | |
| 2456 | v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10); |
| 2457 | |
| 2458 | s = bc_num_mul(n, base, &mult, 0); |
| 2459 | if (s) goto int_err; |
| 2460 | bc_num_ulong2num(&temp, v); |
| 2461 | s = bc_num_add(&mult, &temp, n, 0); |
| 2462 | if (s) goto int_err; |
| 2463 | } |
| 2464 | |
| 2465 | bc_num_init(&result, base->len); |
| 2466 | //bc_num_zero(&result); - already is |
| 2467 | bc_num_one(&mult); |
| 2468 | |
| 2469 | digits = 0; |
| 2470 | for (;;) { |
| 2471 | c = *val++; |
| 2472 | if (c == '\0') break; |
| 2473 | digits++; |
| 2474 | |
| 2475 | v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10); |
| 2476 | |
| 2477 | s = bc_num_mul(&result, base, &result, 0); |
| 2478 | if (s) goto err; |
| 2479 | bc_num_ulong2num(&temp, v); |
| 2480 | s = bc_num_add(&result, &temp, &result, 0); |
| 2481 | if (s) goto err; |
| 2482 | s = bc_num_mul(&mult, base, &mult, 0); |
| 2483 | if (s) goto err; |
| 2484 | } |
| 2485 | |
| 2486 | s = bc_num_div(&result, &mult, &result, digits); |
| 2487 | if (s) goto err; |
| 2488 | s = bc_num_add(n, &result, n, digits); |
| 2489 | if (s) goto err; |
| 2490 | |
| 2491 | if (n->len != 0) { |
| 2492 | if (n->rdx < digits) bc_num_extend(n, digits - n->rdx); |
| 2493 | } else |
| 2494 | bc_num_zero(n); |
| 2495 | |
| 2496 | err: |
| 2497 | bc_num_free(&result); |
| 2498 | int_err: |
| 2499 | bc_num_free(&mult); |
| 2500 | bc_num_free(&temp); |
| 2501 | } |
| 2502 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2503 | static BcStatus bc_num_parse(BcNum *n, const char *val, BcNum *base, |
| 2504 | size_t base_t) |
| 2505 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2506 | if (!bc_num_strValid(val, base_t)) |
| 2507 | return bc_error("bad number string"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2508 | |
Denys Vlasenko | 4a024c7 | 2018-12-09 13:21:54 +0100 | [diff] [blame] | 2509 | bc_num_zero(n); |
| 2510 | while (*val == '0') val++; |
| 2511 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2512 | if (base_t == 10) |
| 2513 | bc_num_parseDecimal(n, val); |
| 2514 | else |
| 2515 | bc_num_parseBase(n, val, base); |
| 2516 | |
| 2517 | return BC_STATUS_SUCCESS; |
| 2518 | } |
| 2519 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2520 | static BcStatus bc_num_print(BcNum *n, bool newline) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2521 | { |
| 2522 | BcStatus s = BC_STATUS_SUCCESS; |
| 2523 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2524 | bc_num_printNewline(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2525 | |
| 2526 | if (n->len == 0) { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2527 | bb_putchar('0'); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2528 | ++G.prog.nchars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2529 | } |
Denys Vlasenko | 0f37b32 | 2018-12-08 23:48:53 +0100 | [diff] [blame] | 2530 | else if (G.prog.ob_t == 10) |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2531 | bc_num_printDecimal(n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2532 | else |
Denys Vlasenko | 0f37b32 | 2018-12-08 23:48:53 +0100 | [diff] [blame] | 2533 | s = bc_num_printBase(n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2534 | |
| 2535 | if (newline) { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2536 | bb_putchar('\n'); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2537 | G.prog.nchars = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2538 | } |
| 2539 | |
| 2540 | return s; |
| 2541 | } |
| 2542 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 2543 | static FAST_FUNC BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2544 | { |
| 2545 | BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s; |
| 2546 | (void) scale; |
| 2547 | return bc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)); |
| 2548 | } |
| 2549 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 2550 | static FAST_FUNC BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2551 | { |
| 2552 | BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a; |
| 2553 | (void) scale; |
| 2554 | return bc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)); |
| 2555 | } |
| 2556 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 2557 | static FAST_FUNC BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2558 | { |
| 2559 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 2560 | return bc_num_binary(a, b, c, scale, bc_num_m, req); |
| 2561 | } |
| 2562 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 2563 | static FAST_FUNC BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2564 | { |
| 2565 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 2566 | return bc_num_binary(a, b, c, scale, bc_num_d, req); |
| 2567 | } |
| 2568 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 2569 | static FAST_FUNC BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2570 | { |
| 2571 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 2572 | return bc_num_binary(a, b, c, scale, bc_num_rem, req); |
| 2573 | } |
| 2574 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 2575 | static FAST_FUNC BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2576 | { |
| 2577 | return bc_num_binary(a, b, c, scale, bc_num_p, a->len * b->len + 1); |
| 2578 | } |
| 2579 | |
| 2580 | static BcStatus bc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale) |
| 2581 | { |
| 2582 | BcStatus s; |
| 2583 | BcNum num1, num2, half, f, fprime, *x0, *x1, *temp; |
| 2584 | size_t pow, len, digs, digs1, resrdx, req, times = 0; |
| 2585 | ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX; |
| 2586 | |
| 2587 | req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1; |
| 2588 | bc_num_expand(b, req); |
| 2589 | |
| 2590 | if (a->len == 0) { |
| 2591 | bc_num_setToZero(b, scale); |
| 2592 | return BC_STATUS_SUCCESS; |
| 2593 | } |
| 2594 | else if (a->neg) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2595 | return bc_error("negative number"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2596 | else if (BC_NUM_ONE(a)) { |
| 2597 | bc_num_one(b); |
| 2598 | bc_num_extend(b, scale); |
| 2599 | return BC_STATUS_SUCCESS; |
| 2600 | } |
| 2601 | |
| 2602 | scale = BC_MAX(scale, a->rdx) + 1; |
| 2603 | len = a->len + scale; |
| 2604 | |
| 2605 | bc_num_init(&num1, len); |
| 2606 | bc_num_init(&num2, len); |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 2607 | bc_num_init_DEF_SIZE(&half); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2608 | |
| 2609 | bc_num_one(&half); |
| 2610 | half.num[0] = 5; |
| 2611 | half.rdx = 1; |
| 2612 | |
| 2613 | bc_num_init(&f, len); |
| 2614 | bc_num_init(&fprime, len); |
| 2615 | |
| 2616 | x0 = &num1; |
| 2617 | x1 = &num2; |
| 2618 | |
| 2619 | bc_num_one(x0); |
| 2620 | pow = BC_NUM_INT(a); |
| 2621 | |
| 2622 | if (pow) { |
| 2623 | |
| 2624 | if (pow & 1) |
| 2625 | x0->num[0] = 2; |
| 2626 | else |
| 2627 | x0->num[0] = 6; |
| 2628 | |
| 2629 | pow -= 2 - (pow & 1); |
| 2630 | |
| 2631 | bc_num_extend(x0, pow); |
| 2632 | |
| 2633 | // Make sure to move the radix back. |
| 2634 | x0->rdx -= pow; |
| 2635 | } |
| 2636 | |
| 2637 | x0->rdx = digs = digs1 = 0; |
| 2638 | resrdx = scale + 2; |
| 2639 | len = BC_NUM_INT(x0) + resrdx - 1; |
| 2640 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2641 | while (cmp != 0 || digs < len) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2642 | |
| 2643 | s = bc_num_div(a, x0, &f, resrdx); |
| 2644 | if (s) goto err; |
| 2645 | s = bc_num_add(x0, &f, &fprime, resrdx); |
| 2646 | if (s) goto err; |
| 2647 | s = bc_num_mul(&fprime, &half, x1, resrdx); |
| 2648 | if (s) goto err; |
| 2649 | |
| 2650 | cmp = bc_num_cmp(x1, x0); |
| 2651 | digs = x1->len - (unsigned long long) llabs(cmp); |
| 2652 | |
| 2653 | if (cmp == cmp2 && digs == digs1) |
| 2654 | times += 1; |
| 2655 | else |
| 2656 | times = 0; |
| 2657 | |
| 2658 | resrdx += times > 4; |
| 2659 | |
| 2660 | cmp2 = cmp1; |
| 2661 | cmp1 = cmp; |
| 2662 | digs1 = digs; |
| 2663 | |
| 2664 | temp = x0; |
| 2665 | x0 = x1; |
| 2666 | x1 = temp; |
| 2667 | } |
| 2668 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2669 | bc_num_copy(b, x0); |
| 2670 | scale -= 1; |
| 2671 | if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale); |
| 2672 | |
| 2673 | err: |
| 2674 | bc_num_free(&fprime); |
| 2675 | bc_num_free(&f); |
| 2676 | bc_num_free(&half); |
| 2677 | bc_num_free(&num2); |
| 2678 | bc_num_free(&num1); |
| 2679 | return s; |
| 2680 | } |
| 2681 | |
| 2682 | static BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, |
| 2683 | size_t scale) |
| 2684 | { |
| 2685 | BcStatus s; |
| 2686 | BcNum num2, *ptr_a; |
| 2687 | bool init = false; |
| 2688 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 2689 | |
| 2690 | if (c == a) { |
| 2691 | memcpy(&num2, c, sizeof(BcNum)); |
| 2692 | ptr_a = &num2; |
| 2693 | bc_num_init(c, len); |
| 2694 | init = true; |
| 2695 | } |
| 2696 | else { |
| 2697 | ptr_a = a; |
| 2698 | bc_num_expand(c, len); |
| 2699 | } |
| 2700 | |
| 2701 | s = bc_num_r(ptr_a, b, c, d, scale, ts); |
| 2702 | |
| 2703 | if (init) bc_num_free(&num2); |
| 2704 | |
| 2705 | return s; |
| 2706 | } |
| 2707 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2708 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2709 | static BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) |
| 2710 | { |
| 2711 | BcStatus s; |
| 2712 | BcNum base, exp, two, temp; |
| 2713 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2714 | if (c->len == 0) |
| 2715 | return bc_error("divide by zero"); |
| 2716 | if (a->rdx || b->rdx || c->rdx) |
| 2717 | return bc_error("non integer number"); |
| 2718 | if (b->neg) |
| 2719 | return bc_error("negative number"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2720 | |
| 2721 | bc_num_expand(d, c->len); |
| 2722 | bc_num_init(&base, c->len); |
| 2723 | bc_num_init(&exp, b->len); |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 2724 | bc_num_init_DEF_SIZE(&two); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2725 | bc_num_init(&temp, b->len); |
| 2726 | |
| 2727 | bc_num_one(&two); |
| 2728 | two.num[0] = 2; |
| 2729 | bc_num_one(d); |
| 2730 | |
| 2731 | s = bc_num_rem(a, c, &base, 0); |
| 2732 | if (s) goto err; |
| 2733 | bc_num_copy(&exp, b); |
| 2734 | |
| 2735 | while (exp.len != 0) { |
| 2736 | |
| 2737 | s = bc_num_divmod(&exp, &two, &exp, &temp, 0); |
| 2738 | if (s) goto err; |
| 2739 | |
| 2740 | if (BC_NUM_ONE(&temp)) { |
| 2741 | s = bc_num_mul(d, &base, &temp, 0); |
| 2742 | if (s) goto err; |
| 2743 | s = bc_num_rem(&temp, c, d, 0); |
| 2744 | if (s) goto err; |
| 2745 | } |
| 2746 | |
| 2747 | s = bc_num_mul(&base, &base, &temp, 0); |
| 2748 | if (s) goto err; |
| 2749 | s = bc_num_rem(&temp, c, &base, 0); |
| 2750 | if (s) goto err; |
| 2751 | } |
| 2752 | |
| 2753 | err: |
| 2754 | bc_num_free(&temp); |
| 2755 | bc_num_free(&two); |
| 2756 | bc_num_free(&exp); |
| 2757 | bc_num_free(&base); |
| 2758 | return s; |
| 2759 | } |
| 2760 | #endif // ENABLE_DC |
| 2761 | |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 2762 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2763 | static BcStatus bc_func_insert(BcFunc *f, char *name, bool var) |
| 2764 | { |
| 2765 | BcId a; |
| 2766 | size_t i; |
| 2767 | |
| 2768 | for (i = 0; i < f->autos.len; ++i) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2769 | if (strcmp(name, ((BcId *) bc_vec_item(&f->autos, i))->name) == 0) |
| 2770 | return 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] | 2771 | } |
| 2772 | |
| 2773 | a.idx = var; |
| 2774 | a.name = name; |
| 2775 | |
| 2776 | bc_vec_push(&f->autos, &a); |
| 2777 | |
| 2778 | return BC_STATUS_SUCCESS; |
| 2779 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 2780 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2781 | |
| 2782 | static void bc_func_init(BcFunc *f) |
| 2783 | { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 2784 | bc_char_vec_init(&f->code); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2785 | bc_vec_init(&f->autos, sizeof(BcId), bc_id_free); |
| 2786 | bc_vec_init(&f->labels, sizeof(size_t), NULL); |
| 2787 | f->nparams = 0; |
| 2788 | } |
| 2789 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 2790 | static FAST_FUNC void bc_func_free(void *func) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2791 | { |
| 2792 | BcFunc *f = (BcFunc *) func; |
| 2793 | bc_vec_free(&f->code); |
| 2794 | bc_vec_free(&f->autos); |
| 2795 | bc_vec_free(&f->labels); |
| 2796 | } |
| 2797 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2798 | static void bc_array_expand(BcVec *a, size_t len); |
| 2799 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2800 | static void bc_array_init(BcVec *a, bool nums) |
| 2801 | { |
| 2802 | if (nums) |
| 2803 | bc_vec_init(a, sizeof(BcNum), bc_num_free); |
| 2804 | else |
| 2805 | bc_vec_init(a, sizeof(BcVec), bc_vec_free); |
| 2806 | bc_array_expand(a, 1); |
| 2807 | } |
| 2808 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2809 | static void bc_array_expand(BcVec *a, size_t len) |
| 2810 | { |
| 2811 | BcResultData data; |
| 2812 | |
| 2813 | if (a->size == sizeof(BcNum) && a->dtor == bc_num_free) { |
| 2814 | while (len > a->len) { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 2815 | bc_num_init_DEF_SIZE(&data.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2816 | bc_vec_push(a, &data.n); |
| 2817 | } |
| 2818 | } |
| 2819 | else { |
| 2820 | while (len > a->len) { |
| 2821 | bc_array_init(&data.v, true); |
| 2822 | bc_vec_push(a, &data.v); |
| 2823 | } |
| 2824 | } |
| 2825 | } |
| 2826 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2827 | static void bc_array_copy(BcVec *d, const BcVec *s) |
| 2828 | { |
| 2829 | size_t i; |
| 2830 | |
| 2831 | bc_vec_pop_all(d); |
| 2832 | bc_vec_expand(d, s->cap); |
| 2833 | d->len = s->len; |
| 2834 | |
| 2835 | for (i = 0; i < s->len; ++i) { |
| 2836 | BcNum *dnum = bc_vec_item(d, i), *snum = bc_vec_item(s, i); |
| 2837 | bc_num_init(dnum, snum->len); |
| 2838 | bc_num_copy(dnum, snum); |
| 2839 | } |
| 2840 | } |
| 2841 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 2842 | static FAST_FUNC void bc_string_free(void *string) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2843 | { |
| 2844 | free(*((char **) string)); |
| 2845 | } |
| 2846 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2847 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2848 | static void bc_result_copy(BcResult *d, BcResult *src) |
| 2849 | { |
| 2850 | d->t = src->t; |
| 2851 | |
| 2852 | switch (d->t) { |
| 2853 | |
| 2854 | case BC_RESULT_TEMP: |
| 2855 | case BC_RESULT_IBASE: |
| 2856 | case BC_RESULT_SCALE: |
| 2857 | case BC_RESULT_OBASE: |
| 2858 | { |
| 2859 | bc_num_init(&d->d.n, src->d.n.len); |
| 2860 | bc_num_copy(&d->d.n, &src->d.n); |
| 2861 | break; |
| 2862 | } |
| 2863 | |
| 2864 | case BC_RESULT_VAR: |
| 2865 | case BC_RESULT_ARRAY: |
| 2866 | case BC_RESULT_ARRAY_ELEM: |
| 2867 | { |
| 2868 | d->d.id.name = xstrdup(src->d.id.name); |
| 2869 | break; |
| 2870 | } |
| 2871 | |
| 2872 | case BC_RESULT_CONSTANT: |
| 2873 | case BC_RESULT_LAST: |
| 2874 | case BC_RESULT_ONE: |
| 2875 | case BC_RESULT_STR: |
| 2876 | { |
| 2877 | memcpy(&d->d.n, &src->d.n, sizeof(BcNum)); |
| 2878 | break; |
| 2879 | } |
| 2880 | } |
| 2881 | } |
| 2882 | #endif // ENABLE_DC |
| 2883 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 2884 | static FAST_FUNC void bc_result_free(void *result) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2885 | { |
| 2886 | BcResult *r = (BcResult *) result; |
| 2887 | |
| 2888 | switch (r->t) { |
| 2889 | |
| 2890 | case BC_RESULT_TEMP: |
| 2891 | case BC_RESULT_IBASE: |
| 2892 | case BC_RESULT_SCALE: |
| 2893 | case BC_RESULT_OBASE: |
| 2894 | { |
| 2895 | bc_num_free(&r->d.n); |
| 2896 | break; |
| 2897 | } |
| 2898 | |
| 2899 | case BC_RESULT_VAR: |
| 2900 | case BC_RESULT_ARRAY: |
| 2901 | case BC_RESULT_ARRAY_ELEM: |
| 2902 | { |
| 2903 | free(r->d.id.name); |
| 2904 | break; |
| 2905 | } |
| 2906 | |
| 2907 | default: |
| 2908 | { |
| 2909 | // Do nothing. |
| 2910 | break; |
| 2911 | } |
| 2912 | } |
| 2913 | } |
| 2914 | |
| 2915 | static void bc_lex_lineComment(BcLex *l) |
| 2916 | { |
| 2917 | l->t.t = BC_LEX_WHITESPACE; |
| 2918 | while (l->i < l->len && l->buf[l->i++] != '\n'); |
| 2919 | --l->i; |
| 2920 | } |
| 2921 | |
| 2922 | static void bc_lex_whitespace(BcLex *l) |
| 2923 | { |
| 2924 | char c; |
| 2925 | l->t.t = BC_LEX_WHITESPACE; |
| 2926 | for (c = l->buf[l->i]; c != '\n' && isspace(c); c = l->buf[++l->i]); |
| 2927 | } |
| 2928 | |
| 2929 | static BcStatus bc_lex_number(BcLex *l, char start) |
| 2930 | { |
| 2931 | const char *buf = l->buf + l->i; |
| 2932 | size_t len, hits = 0, bslashes = 0, i = 0, j; |
| 2933 | char c = buf[i]; |
| 2934 | bool last_pt, pt = start == '.'; |
| 2935 | |
| 2936 | last_pt = pt; |
| 2937 | l->t.t = BC_LEX_NUMBER; |
| 2938 | |
| 2939 | while (c != 0 && (isdigit(c) || (c >= 'A' && c <= 'F') || |
| 2940 | (c == '.' && !pt) || (c == '\\' && buf[i + 1] == '\n'))) |
| 2941 | { |
| 2942 | if (c != '\\') { |
| 2943 | last_pt = c == '.'; |
| 2944 | pt = pt || last_pt; |
| 2945 | } |
| 2946 | else { |
| 2947 | ++i; |
| 2948 | bslashes += 1; |
| 2949 | } |
| 2950 | |
| 2951 | c = buf[++i]; |
| 2952 | } |
| 2953 | |
Denys Vlasenko | d5f7703 | 2018-12-04 21:37:56 +0100 | [diff] [blame] | 2954 | len = i + !last_pt - bslashes * 2; |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 2955 | // This check makes sense only if size_t is (much) larger than BC_MAX_NUM. |
| 2956 | if (SIZE_MAX > (BC_MAX_NUM | 0xff)) { |
| 2957 | if (len > BC_MAX_NUM) |
| 2958 | return bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"); |
| 2959 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2960 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 2961 | bc_vec_pop_all(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2962 | bc_vec_expand(&l->t.v, len + 1); |
| 2963 | bc_vec_push(&l->t.v, &start); |
| 2964 | |
| 2965 | for (buf -= 1, j = 1; j < len + hits * 2; ++j) { |
| 2966 | |
| 2967 | c = buf[j]; |
| 2968 | |
| 2969 | // If we have hit a backslash, skip it. We don't have |
| 2970 | // to check for a newline because it's guaranteed. |
| 2971 | if (hits < bslashes && c == '\\') { |
| 2972 | ++hits; |
| 2973 | ++j; |
| 2974 | continue; |
| 2975 | } |
| 2976 | |
| 2977 | bc_vec_push(&l->t.v, &c); |
| 2978 | } |
| 2979 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 2980 | bc_vec_pushZeroByte(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2981 | l->i += i; |
| 2982 | |
| 2983 | return BC_STATUS_SUCCESS; |
| 2984 | } |
| 2985 | |
| 2986 | static BcStatus bc_lex_name(BcLex *l) |
| 2987 | { |
| 2988 | size_t i = 0; |
| 2989 | const char *buf = l->buf + l->i - 1; |
| 2990 | char c = buf[i]; |
| 2991 | |
| 2992 | l->t.t = BC_LEX_NAME; |
| 2993 | |
| 2994 | while ((c >= 'a' && c <= 'z') || isdigit(c) || c == '_') c = buf[++i]; |
| 2995 | |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 2996 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
| 2997 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
| 2998 | if (i > BC_MAX_STRING) |
| 2999 | return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]"); |
| 3000 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3001 | bc_vec_string(&l->t.v, i, buf); |
| 3002 | |
| 3003 | // Increment the index. We minus 1 because it has already been incremented. |
| 3004 | l->i += i - 1; |
| 3005 | |
| 3006 | return BC_STATUS_SUCCESS; |
| 3007 | } |
| 3008 | |
| 3009 | static void bc_lex_init(BcLex *l, BcLexNext next) |
| 3010 | { |
| 3011 | l->next = next; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3012 | bc_char_vec_init(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3013 | } |
| 3014 | |
| 3015 | static void bc_lex_free(BcLex *l) |
| 3016 | { |
| 3017 | bc_vec_free(&l->t.v); |
| 3018 | } |
| 3019 | |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 3020 | static void bc_lex_file(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3021 | { |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3022 | G.err_line = l->line = 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3023 | l->newline = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3024 | } |
| 3025 | |
| 3026 | static BcStatus bc_lex_next(BcLex *l) |
| 3027 | { |
| 3028 | BcStatus s; |
| 3029 | |
| 3030 | l->t.last = l->t.t; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3031 | 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] | 3032 | |
| 3033 | l->line += l->newline; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3034 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3035 | l->t.t = BC_LEX_EOF; |
| 3036 | |
| 3037 | l->newline = (l->i == l->len); |
| 3038 | if (l->newline) return BC_STATUS_SUCCESS; |
| 3039 | |
| 3040 | // Loop until failure or we don't have whitespace. This |
| 3041 | // is so the parser doesn't get inundated with whitespace. |
| 3042 | do { |
| 3043 | s = l->next(l); |
| 3044 | } while (!s && l->t.t == BC_LEX_WHITESPACE); |
| 3045 | |
| 3046 | return s; |
| 3047 | } |
| 3048 | |
| 3049 | static BcStatus bc_lex_text(BcLex *l, const char *text) |
| 3050 | { |
| 3051 | l->buf = text; |
| 3052 | l->i = 0; |
| 3053 | l->len = strlen(text); |
| 3054 | l->t.t = l->t.last = BC_LEX_INVALID; |
| 3055 | return bc_lex_next(l); |
| 3056 | } |
| 3057 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 3058 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3059 | static BcStatus bc_lex_identifier(BcLex *l) |
| 3060 | { |
| 3061 | BcStatus s; |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3062 | unsigned i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3063 | const char *buf = l->buf + l->i - 1; |
| 3064 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3065 | for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) { |
| 3066 | const char *keyword8 = bc_lex_kws[i].name8; |
| 3067 | unsigned j = 0; |
| 3068 | while (buf[j] != '\0' && buf[j] == keyword8[j]) { |
| 3069 | j++; |
| 3070 | if (j == 8) goto match; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3071 | } |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3072 | if (keyword8[j] != '\0') |
| 3073 | continue; |
| 3074 | match: |
| 3075 | // buf starts with keyword bc_lex_kws[i] |
| 3076 | l->t.t = BC_LEX_KEY_1st_keyword + i; |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 3077 | if (!bc_lex_kws_POSIX(i)) { |
Denys Vlasenko | 0d7e46b | 2018-12-05 18:31:19 +0100 | [diff] [blame] | 3078 | 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] | 3079 | if (s) return s; |
| 3080 | } |
| 3081 | |
| 3082 | // We minus 1 because the index has already been incremented. |
| 3083 | l->i += j - 1; |
| 3084 | return BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3085 | } |
| 3086 | |
| 3087 | s = bc_lex_name(l); |
| 3088 | if (s) return s; |
| 3089 | |
Denys Vlasenko | 0d7e46b | 2018-12-05 18:31:19 +0100 | [diff] [blame] | 3090 | if (l->t.v.len > 2) { |
| 3091 | // Prevent this: |
| 3092 | // >>> qwe=1 |
| 3093 | // bc: POSIX only allows one character names; the following is bad: 'qwe=1 |
| 3094 | // ' |
| 3095 | unsigned len = strchrnul(buf, '\n') - buf; |
| 3096 | s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf); |
| 3097 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3098 | |
| 3099 | return s; |
| 3100 | } |
| 3101 | |
| 3102 | static BcStatus bc_lex_string(BcLex *l) |
| 3103 | { |
| 3104 | size_t len, nls = 0, i = l->i; |
| 3105 | char c; |
| 3106 | |
| 3107 | l->t.t = BC_LEX_STR; |
| 3108 | |
| 3109 | for (c = l->buf[i]; c != 0 && c != '"'; c = l->buf[++i]) nls += (c == '\n'); |
| 3110 | |
| 3111 | if (c == '\0') { |
| 3112 | l->i = i; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3113 | return bc_error("string end could not be found"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3114 | } |
| 3115 | |
| 3116 | len = i - l->i; |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 3117 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
| 3118 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
| 3119 | if (len > BC_MAX_STRING) |
| 3120 | return bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"); |
| 3121 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3122 | bc_vec_string(&l->t.v, len, l->buf + l->i); |
| 3123 | |
| 3124 | l->i = i + 1; |
| 3125 | l->line += nls; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3126 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3127 | |
| 3128 | return BC_STATUS_SUCCESS; |
| 3129 | } |
| 3130 | |
| 3131 | static void bc_lex_assign(BcLex *l, BcLexType with, BcLexType without) |
| 3132 | { |
| 3133 | if (l->buf[l->i] == '=') { |
| 3134 | ++l->i; |
| 3135 | l->t.t = with; |
| 3136 | } |
| 3137 | else |
| 3138 | l->t.t = without; |
| 3139 | } |
| 3140 | |
| 3141 | static BcStatus bc_lex_comment(BcLex *l) |
| 3142 | { |
| 3143 | size_t i, nls = 0; |
| 3144 | const char *buf = l->buf; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3145 | |
| 3146 | l->t.t = BC_LEX_WHITESPACE; |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3147 | i = ++l->i; |
| 3148 | for (;;) { |
| 3149 | char c = buf[i]; |
| 3150 | check_star: |
| 3151 | if (c == '*') { |
| 3152 | c = buf[++i]; |
| 3153 | if (c == '/') |
| 3154 | break; |
| 3155 | goto check_star; |
| 3156 | } |
| 3157 | if (c == '\0') { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3158 | l->i = i; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3159 | return bc_error("comment end could not be found"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3160 | } |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3161 | nls += (c == '\n'); |
| 3162 | i++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3163 | } |
| 3164 | |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3165 | l->i = i + 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3166 | l->line += nls; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3167 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3168 | |
| 3169 | return BC_STATUS_SUCCESS; |
| 3170 | } |
| 3171 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 3172 | static FAST_FUNC BcStatus bc_lex_token(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3173 | { |
| 3174 | BcStatus s = BC_STATUS_SUCCESS; |
| 3175 | char c = l->buf[l->i++], c2; |
| 3176 | |
| 3177 | // This is the workhorse of the lexer. |
| 3178 | switch (c) { |
| 3179 | |
| 3180 | case '\0': |
| 3181 | case '\n': |
| 3182 | { |
| 3183 | l->newline = true; |
| 3184 | l->t.t = !c ? BC_LEX_EOF : BC_LEX_NLINE; |
| 3185 | break; |
| 3186 | } |
| 3187 | |
| 3188 | case '\t': |
| 3189 | case '\v': |
| 3190 | case '\f': |
| 3191 | case '\r': |
| 3192 | case ' ': |
| 3193 | { |
| 3194 | bc_lex_whitespace(l); |
| 3195 | break; |
| 3196 | } |
| 3197 | |
| 3198 | case '!': |
| 3199 | { |
| 3200 | bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT); |
| 3201 | |
| 3202 | if (l->t.t == BC_LEX_OP_BOOL_NOT) { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3203 | s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3204 | if (s) return s; |
| 3205 | } |
| 3206 | |
| 3207 | break; |
| 3208 | } |
| 3209 | |
| 3210 | case '"': |
| 3211 | { |
| 3212 | s = bc_lex_string(l); |
| 3213 | break; |
| 3214 | } |
| 3215 | |
| 3216 | case '#': |
| 3217 | { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3218 | s = bc_POSIX_does_not_allow("'#' script comments"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3219 | if (s) return s; |
| 3220 | |
| 3221 | bc_lex_lineComment(l); |
| 3222 | |
| 3223 | break; |
| 3224 | } |
| 3225 | |
| 3226 | case '%': |
| 3227 | { |
| 3228 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS); |
| 3229 | break; |
| 3230 | } |
| 3231 | |
| 3232 | case '&': |
| 3233 | { |
| 3234 | c2 = l->buf[l->i]; |
| 3235 | if (c2 == '&') { |
| 3236 | |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3237 | s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3238 | if (s) return s; |
| 3239 | |
| 3240 | ++l->i; |
| 3241 | l->t.t = BC_LEX_OP_BOOL_AND; |
| 3242 | } |
| 3243 | else { |
| 3244 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3245 | s = bc_error_bad_character('&'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3246 | } |
| 3247 | |
| 3248 | break; |
| 3249 | } |
| 3250 | |
| 3251 | case '(': |
| 3252 | case ')': |
| 3253 | { |
| 3254 | l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN); |
| 3255 | break; |
| 3256 | } |
| 3257 | |
| 3258 | case '*': |
| 3259 | { |
| 3260 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY); |
| 3261 | break; |
| 3262 | } |
| 3263 | |
| 3264 | case '+': |
| 3265 | { |
| 3266 | c2 = l->buf[l->i]; |
| 3267 | if (c2 == '+') { |
| 3268 | ++l->i; |
| 3269 | l->t.t = BC_LEX_OP_INC; |
| 3270 | } |
| 3271 | else |
| 3272 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS); |
| 3273 | break; |
| 3274 | } |
| 3275 | |
| 3276 | case ',': |
| 3277 | { |
| 3278 | l->t.t = BC_LEX_COMMA; |
| 3279 | break; |
| 3280 | } |
| 3281 | |
| 3282 | case '-': |
| 3283 | { |
| 3284 | c2 = l->buf[l->i]; |
| 3285 | if (c2 == '-') { |
| 3286 | ++l->i; |
| 3287 | l->t.t = BC_LEX_OP_DEC; |
| 3288 | } |
| 3289 | else |
| 3290 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS); |
| 3291 | break; |
| 3292 | } |
| 3293 | |
| 3294 | case '.': |
| 3295 | { |
| 3296 | if (isdigit(l->buf[l->i])) |
| 3297 | s = bc_lex_number(l, c); |
| 3298 | else { |
| 3299 | l->t.t = BC_LEX_KEY_LAST; |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3300 | 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] | 3301 | } |
| 3302 | break; |
| 3303 | } |
| 3304 | |
| 3305 | case '/': |
| 3306 | { |
| 3307 | c2 = l->buf[l->i]; |
| 3308 | if (c2 == '*') |
| 3309 | s = bc_lex_comment(l); |
| 3310 | else |
| 3311 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE); |
| 3312 | break; |
| 3313 | } |
| 3314 | |
| 3315 | case '0': |
| 3316 | case '1': |
| 3317 | case '2': |
| 3318 | case '3': |
| 3319 | case '4': |
| 3320 | case '5': |
| 3321 | case '6': |
| 3322 | case '7': |
| 3323 | case '8': |
| 3324 | case '9': |
| 3325 | case 'A': |
| 3326 | case 'B': |
| 3327 | case 'C': |
| 3328 | case 'D': |
| 3329 | case 'E': |
| 3330 | case 'F': |
| 3331 | { |
| 3332 | s = bc_lex_number(l, c); |
| 3333 | break; |
| 3334 | } |
| 3335 | |
| 3336 | case ';': |
| 3337 | { |
| 3338 | l->t.t = BC_LEX_SCOLON; |
| 3339 | break; |
| 3340 | } |
| 3341 | |
| 3342 | case '<': |
| 3343 | { |
| 3344 | bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT); |
| 3345 | break; |
| 3346 | } |
| 3347 | |
| 3348 | case '=': |
| 3349 | { |
| 3350 | bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN); |
| 3351 | break; |
| 3352 | } |
| 3353 | |
| 3354 | case '>': |
| 3355 | { |
| 3356 | bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT); |
| 3357 | break; |
| 3358 | } |
| 3359 | |
| 3360 | case '[': |
| 3361 | case ']': |
| 3362 | { |
| 3363 | l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET); |
| 3364 | break; |
| 3365 | } |
| 3366 | |
| 3367 | case '\\': |
| 3368 | { |
| 3369 | if (l->buf[l->i] == '\n') { |
| 3370 | l->t.t = BC_LEX_WHITESPACE; |
| 3371 | ++l->i; |
| 3372 | } |
| 3373 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3374 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3375 | break; |
| 3376 | } |
| 3377 | |
| 3378 | case '^': |
| 3379 | { |
| 3380 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER); |
| 3381 | break; |
| 3382 | } |
| 3383 | |
| 3384 | case 'a': |
| 3385 | case 'b': |
| 3386 | case 'c': |
| 3387 | case 'd': |
| 3388 | case 'e': |
| 3389 | case 'f': |
| 3390 | case 'g': |
| 3391 | case 'h': |
| 3392 | case 'i': |
| 3393 | case 'j': |
| 3394 | case 'k': |
| 3395 | case 'l': |
| 3396 | case 'm': |
| 3397 | case 'n': |
| 3398 | case 'o': |
| 3399 | case 'p': |
| 3400 | case 'q': |
| 3401 | case 'r': |
| 3402 | case 's': |
| 3403 | case 't': |
| 3404 | case 'u': |
| 3405 | case 'v': |
| 3406 | case 'w': |
| 3407 | case 'x': |
| 3408 | case 'y': |
| 3409 | case 'z': |
| 3410 | { |
| 3411 | s = bc_lex_identifier(l); |
| 3412 | break; |
| 3413 | } |
| 3414 | |
| 3415 | case '{': |
| 3416 | case '}': |
| 3417 | { |
| 3418 | l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE); |
| 3419 | break; |
| 3420 | } |
| 3421 | |
| 3422 | case '|': |
| 3423 | { |
| 3424 | c2 = l->buf[l->i]; |
| 3425 | |
| 3426 | if (c2 == '|') { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3427 | s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3428 | if (s) return s; |
| 3429 | |
| 3430 | ++l->i; |
| 3431 | l->t.t = BC_LEX_OP_BOOL_OR; |
| 3432 | } |
| 3433 | else { |
| 3434 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3435 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3436 | } |
| 3437 | |
| 3438 | break; |
| 3439 | } |
| 3440 | |
| 3441 | default: |
| 3442 | { |
| 3443 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3444 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3445 | break; |
| 3446 | } |
| 3447 | } |
| 3448 | |
| 3449 | return s; |
| 3450 | } |
| 3451 | #endif // ENABLE_BC |
| 3452 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 3453 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3454 | static BcStatus dc_lex_register(BcLex *l) |
| 3455 | { |
| 3456 | BcStatus s = BC_STATUS_SUCCESS; |
| 3457 | |
| 3458 | if (isspace(l->buf[l->i - 1])) { |
| 3459 | bc_lex_whitespace(l); |
| 3460 | ++l->i; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 3461 | if (!G_exreg) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3462 | s = bc_error("extended register"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3463 | else |
| 3464 | s = bc_lex_name(l); |
| 3465 | } |
| 3466 | else { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3467 | bc_vec_pop_all(&l->t.v); |
Denys Vlasenko | e55a572 | 2018-12-06 12:47:17 +0100 | [diff] [blame] | 3468 | bc_vec_push(&l->t.v, &l->buf[l->i - 1]); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 3469 | bc_vec_pushZeroByte(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3470 | l->t.t = BC_LEX_NAME; |
| 3471 | } |
| 3472 | |
| 3473 | return s; |
| 3474 | } |
| 3475 | |
| 3476 | static BcStatus dc_lex_string(BcLex *l) |
| 3477 | { |
| 3478 | size_t depth = 1, nls = 0, i = l->i; |
| 3479 | char c; |
| 3480 | |
| 3481 | l->t.t = BC_LEX_STR; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3482 | bc_vec_pop_all(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3483 | |
| 3484 | for (c = l->buf[i]; c != 0 && depth; c = l->buf[++i]) { |
| 3485 | |
| 3486 | depth += (c == '[' && (i == l->i || l->buf[i - 1] != '\\')); |
| 3487 | depth -= (c == ']' && (i == l->i || l->buf[i - 1] != '\\')); |
| 3488 | nls += (c == '\n'); |
| 3489 | |
| 3490 | if (depth) bc_vec_push(&l->t.v, &c); |
| 3491 | } |
| 3492 | |
| 3493 | if (c == '\0') { |
| 3494 | l->i = i; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3495 | return bc_error("string end could not be found"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3496 | } |
| 3497 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 3498 | bc_vec_pushZeroByte(&l->t.v); |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 3499 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
| 3500 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
| 3501 | if (i - l->i > BC_MAX_STRING) |
| 3502 | return bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"); |
| 3503 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3504 | |
| 3505 | l->i = i; |
| 3506 | l->line += nls; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3507 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3508 | |
| 3509 | return BC_STATUS_SUCCESS; |
| 3510 | } |
| 3511 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 3512 | static FAST_FUNC BcStatus dc_lex_token(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3513 | { |
| 3514 | BcStatus s = BC_STATUS_SUCCESS; |
| 3515 | char c = l->buf[l->i++], c2; |
| 3516 | size_t i; |
| 3517 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3518 | for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) { |
| 3519 | if (l->t.last == dc_lex_regs[i]) |
| 3520 | return dc_lex_register(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3521 | } |
| 3522 | |
| 3523 | if (c >= '%' && c <= '~' && |
| 3524 | (l->t.t = dc_lex_tokens[(c - '%')]) != BC_LEX_INVALID) |
| 3525 | { |
| 3526 | return s; |
| 3527 | } |
| 3528 | |
| 3529 | // This is the workhorse of the lexer. |
| 3530 | switch (c) { |
| 3531 | |
| 3532 | case '\0': |
| 3533 | { |
| 3534 | l->t.t = BC_LEX_EOF; |
| 3535 | break; |
| 3536 | } |
| 3537 | |
| 3538 | case '\n': |
| 3539 | case '\t': |
| 3540 | case '\v': |
| 3541 | case '\f': |
| 3542 | case '\r': |
| 3543 | case ' ': |
| 3544 | { |
| 3545 | l->newline = (c == '\n'); |
| 3546 | bc_lex_whitespace(l); |
| 3547 | break; |
| 3548 | } |
| 3549 | |
| 3550 | case '!': |
| 3551 | { |
| 3552 | c2 = l->buf[l->i]; |
| 3553 | |
| 3554 | if (c2 == '=') |
| 3555 | l->t.t = BC_LEX_OP_REL_NE; |
| 3556 | else if (c2 == '<') |
| 3557 | l->t.t = BC_LEX_OP_REL_LE; |
| 3558 | else if (c2 == '>') |
| 3559 | l->t.t = BC_LEX_OP_REL_GE; |
| 3560 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3561 | return bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3562 | |
| 3563 | ++l->i; |
| 3564 | break; |
| 3565 | } |
| 3566 | |
| 3567 | case '#': |
| 3568 | { |
| 3569 | bc_lex_lineComment(l); |
| 3570 | break; |
| 3571 | } |
| 3572 | |
| 3573 | case '.': |
| 3574 | { |
| 3575 | if (isdigit(l->buf[l->i])) |
| 3576 | s = bc_lex_number(l, c); |
| 3577 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3578 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3579 | break; |
| 3580 | } |
| 3581 | |
| 3582 | case '0': |
| 3583 | case '1': |
| 3584 | case '2': |
| 3585 | case '3': |
| 3586 | case '4': |
| 3587 | case '5': |
| 3588 | case '6': |
| 3589 | case '7': |
| 3590 | case '8': |
| 3591 | case '9': |
| 3592 | case 'A': |
| 3593 | case 'B': |
| 3594 | case 'C': |
| 3595 | case 'D': |
| 3596 | case 'E': |
| 3597 | case 'F': |
| 3598 | { |
| 3599 | s = bc_lex_number(l, c); |
| 3600 | break; |
| 3601 | } |
| 3602 | |
| 3603 | case '[': |
| 3604 | { |
| 3605 | s = dc_lex_string(l); |
| 3606 | break; |
| 3607 | } |
| 3608 | |
| 3609 | default: |
| 3610 | { |
| 3611 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3612 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3613 | break; |
| 3614 | } |
| 3615 | } |
| 3616 | |
| 3617 | return s; |
| 3618 | } |
| 3619 | #endif // ENABLE_DC |
| 3620 | |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3621 | static void bc_program_addFunc(char *name, size_t *idx); |
| 3622 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3623 | static void bc_parse_addFunc(BcParse *p, char *name, size_t *idx) |
| 3624 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3625 | bc_program_addFunc(name, idx); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 3626 | p->func = bc_program_func(p->fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3627 | } |
| 3628 | |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 3629 | #define bc_parse_push(p, i) bc_vec_pushByte(&(p)->func->code, (char) (i)) |
| 3630 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3631 | static void bc_parse_pushName(BcParse *p, char *name) |
| 3632 | { |
| 3633 | size_t i = 0, len = strlen(name); |
| 3634 | |
| 3635 | for (; i < len; ++i) bc_parse_push(p, name[i]); |
| 3636 | bc_parse_push(p, BC_PARSE_STREND); |
| 3637 | |
| 3638 | free(name); |
| 3639 | } |
| 3640 | |
| 3641 | static void bc_parse_pushIndex(BcParse *p, size_t idx) |
| 3642 | { |
| 3643 | unsigned char amt, i, nums[sizeof(size_t)]; |
| 3644 | |
| 3645 | for (amt = 0; idx; ++amt) { |
| 3646 | nums[amt] = (char) idx; |
| 3647 | idx = (idx & ((unsigned long) ~(UCHAR_MAX))) >> sizeof(char) * CHAR_BIT; |
| 3648 | } |
| 3649 | |
| 3650 | bc_parse_push(p, amt); |
| 3651 | for (i = 0; i < amt; ++i) bc_parse_push(p, nums[i]); |
| 3652 | } |
| 3653 | |
| 3654 | static void bc_parse_number(BcParse *p, BcInst *prev, size_t *nexs) |
| 3655 | { |
| 3656 | char *num = xstrdup(p->l.t.v.v); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3657 | size_t idx = G.prog.consts.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3658 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3659 | bc_vec_push(&G.prog.consts, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3660 | |
| 3661 | bc_parse_push(p, BC_INST_NUM); |
| 3662 | bc_parse_pushIndex(p, idx); |
| 3663 | |
| 3664 | ++(*nexs); |
| 3665 | (*prev) = BC_INST_NUM; |
| 3666 | } |
| 3667 | |
| 3668 | static BcStatus bc_parse_text(BcParse *p, const char *text) |
| 3669 | { |
| 3670 | BcStatus s; |
| 3671 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 3672 | p->func = bc_program_func(p->fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3673 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3674 | if (!text[0] && !BC_PARSE_CAN_EXEC(p)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3675 | p->l.t.t = BC_LEX_INVALID; |
| 3676 | s = p->parse(p); |
| 3677 | if (s) return s; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3678 | if (!BC_PARSE_CAN_EXEC(p)) |
| 3679 | return bc_error("file is not executable"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3680 | } |
| 3681 | |
| 3682 | return bc_lex_text(&p->l, text); |
| 3683 | } |
| 3684 | |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3685 | // Called when parsing or execution detects a failure, |
| 3686 | // resets execution structures. |
| 3687 | static void bc_program_reset(void) |
| 3688 | { |
| 3689 | BcFunc *f; |
| 3690 | BcInstPtr *ip; |
| 3691 | |
| 3692 | bc_vec_npop(&G.prog.stack, G.prog.stack.len - 1); |
| 3693 | bc_vec_pop_all(&G.prog.results); |
| 3694 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 3695 | f = bc_program_func(0); |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3696 | ip = bc_vec_top(&G.prog.stack); |
| 3697 | ip->idx = f->code.len; |
| 3698 | } |
| 3699 | |
Denys Vlasenko | e55a572 | 2018-12-06 12:47:17 +0100 | [diff] [blame] | 3700 | #define bc_parse_updateFunc(p, f) \ |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 3701 | ((p)->func = bc_program_func((p)->fidx = (f))) |
Denys Vlasenko | e55a572 | 2018-12-06 12:47:17 +0100 | [diff] [blame] | 3702 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 3703 | // Called when bc/dc_parse_parse() detects a failure, |
| 3704 | // resets parsing structures. |
| 3705 | static void bc_parse_reset(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3706 | { |
| 3707 | if (p->fidx != BC_PROG_MAIN) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3708 | p->func->nparams = 0; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3709 | bc_vec_pop_all(&p->func->code); |
| 3710 | bc_vec_pop_all(&p->func->autos); |
| 3711 | bc_vec_pop_all(&p->func->labels); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3712 | |
| 3713 | bc_parse_updateFunc(p, BC_PROG_MAIN); |
| 3714 | } |
| 3715 | |
| 3716 | p->l.i = p->l.len; |
| 3717 | p->l.t.t = BC_LEX_EOF; |
| 3718 | p->auto_part = (p->nbraces = 0); |
| 3719 | |
| 3720 | bc_vec_npop(&p->flags, p->flags.len - 1); |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3721 | bc_vec_pop_all(&p->exits); |
| 3722 | bc_vec_pop_all(&p->conds); |
| 3723 | bc_vec_pop_all(&p->ops); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3724 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 3725 | bc_program_reset(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3726 | } |
| 3727 | |
| 3728 | static void bc_parse_free(BcParse *p) |
| 3729 | { |
| 3730 | bc_vec_free(&p->flags); |
| 3731 | bc_vec_free(&p->exits); |
| 3732 | bc_vec_free(&p->conds); |
| 3733 | bc_vec_free(&p->ops); |
| 3734 | bc_lex_free(&p->l); |
| 3735 | } |
| 3736 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3737 | static void bc_parse_create(BcParse *p, size_t func, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3738 | BcParseParse parse, BcLexNext next) |
| 3739 | { |
| 3740 | memset(p, 0, sizeof(BcParse)); |
| 3741 | |
| 3742 | bc_lex_init(&p->l, next); |
| 3743 | bc_vec_init(&p->flags, sizeof(uint8_t), NULL); |
| 3744 | bc_vec_init(&p->exits, sizeof(BcInstPtr), NULL); |
| 3745 | bc_vec_init(&p->conds, sizeof(size_t), NULL); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 3746 | bc_vec_pushZeroByte(&p->flags); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3747 | bc_vec_init(&p->ops, sizeof(BcLexType), NULL); |
| 3748 | |
| 3749 | p->parse = parse; |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 3750 | // p->auto_part = p->nbraces = 0; - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3751 | bc_parse_updateFunc(p, func); |
| 3752 | } |
| 3753 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 3754 | #if ENABLE_BC |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 3755 | |
| 3756 | #define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops))) |
| 3757 | #define BC_PARSE_LEAF(p, rparen) \ |
| 3758 | (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \ |
| 3759 | (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST) |
| 3760 | |
| 3761 | // We can calculate the conversion between tokens and exprs by subtracting the |
| 3762 | // position of the first operator in the lex enum and adding the position of the |
| 3763 | // first in the expr enum. Note: This only works for binary operators. |
| 3764 | #define BC_PARSE_TOKEN_INST(t) ((char) ((t) -BC_LEX_NEG + BC_INST_NEG)) |
| 3765 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3766 | static BcStatus bc_parse_else(BcParse *p); |
| 3767 | static BcStatus bc_parse_stmt(BcParse *p); |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 3768 | static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next); |
Denys Vlasenko | 050b0fe | 2018-12-05 22:40:44 +0100 | [diff] [blame] | 3769 | 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] | 3770 | |
| 3771 | static BcStatus bc_parse_operator(BcParse *p, BcLexType type, size_t start, |
| 3772 | size_t *nexprs, bool next) |
| 3773 | { |
| 3774 | BcStatus s = BC_STATUS_SUCCESS; |
| 3775 | BcLexType t; |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 3776 | char l, r = bc_parse_op_PREC(type - BC_LEX_OP_INC); |
| 3777 | bool left = bc_parse_op_LEFT(type - BC_LEX_OP_INC); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3778 | |
| 3779 | while (p->ops.len > start) { |
| 3780 | |
| 3781 | t = BC_PARSE_TOP_OP(p); |
| 3782 | if (t == BC_LEX_LPAREN) break; |
| 3783 | |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 3784 | l = bc_parse_op_PREC(t - BC_LEX_OP_INC); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3785 | if (l >= r && (l != r || !left)) break; |
| 3786 | |
| 3787 | bc_parse_push(p, BC_PARSE_TOKEN_INST(t)); |
| 3788 | bc_vec_pop(&p->ops); |
| 3789 | *nexprs -= t != BC_LEX_OP_BOOL_NOT && t != BC_LEX_NEG; |
| 3790 | } |
| 3791 | |
| 3792 | bc_vec_push(&p->ops, &type); |
| 3793 | if (next) s = bc_lex_next(&p->l); |
| 3794 | |
| 3795 | return s; |
| 3796 | } |
| 3797 | |
| 3798 | static BcStatus bc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs) |
| 3799 | { |
| 3800 | BcLexType top; |
| 3801 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3802 | if (p->ops.len <= ops_bgn) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3803 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3804 | top = BC_PARSE_TOP_OP(p); |
| 3805 | |
| 3806 | while (top != BC_LEX_LPAREN) { |
| 3807 | |
| 3808 | bc_parse_push(p, BC_PARSE_TOKEN_INST(top)); |
| 3809 | |
| 3810 | bc_vec_pop(&p->ops); |
| 3811 | *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG; |
| 3812 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3813 | if (p->ops.len <= ops_bgn) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3814 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3815 | top = BC_PARSE_TOP_OP(p); |
| 3816 | } |
| 3817 | |
| 3818 | bc_vec_pop(&p->ops); |
| 3819 | |
| 3820 | return bc_lex_next(&p->l); |
| 3821 | } |
| 3822 | |
| 3823 | static BcStatus bc_parse_params(BcParse *p, uint8_t flags) |
| 3824 | { |
| 3825 | BcStatus s; |
| 3826 | bool comma = false; |
| 3827 | size_t nparams; |
| 3828 | |
| 3829 | s = bc_lex_next(&p->l); |
| 3830 | if (s) return s; |
| 3831 | |
| 3832 | for (nparams = 0; p->l.t.t != BC_LEX_RPAREN; ++nparams) { |
| 3833 | |
| 3834 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; |
| 3835 | s = bc_parse_expr(p, flags, bc_parse_next_param); |
| 3836 | if (s) return s; |
| 3837 | |
| 3838 | comma = p->l.t.t == BC_LEX_COMMA; |
| 3839 | if (comma) { |
| 3840 | s = bc_lex_next(&p->l); |
| 3841 | if (s) return s; |
| 3842 | } |
| 3843 | } |
| 3844 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3845 | if (comma) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3846 | bc_parse_push(p, BC_INST_CALL); |
| 3847 | bc_parse_pushIndex(p, nparams); |
| 3848 | |
| 3849 | return BC_STATUS_SUCCESS; |
| 3850 | } |
| 3851 | |
| 3852 | static BcStatus bc_parse_call(BcParse *p, char *name, uint8_t flags) |
| 3853 | { |
| 3854 | BcStatus s; |
| 3855 | BcId entry, *entry_ptr; |
| 3856 | size_t idx; |
| 3857 | |
| 3858 | entry.name = name; |
| 3859 | |
| 3860 | s = bc_parse_params(p, flags); |
| 3861 | if (s) goto err; |
| 3862 | |
| 3863 | if (p->l.t.t != BC_LEX_RPAREN) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3864 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3865 | goto err; |
| 3866 | } |
| 3867 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3868 | idx = bc_map_index(&G.prog.fn_map, &entry); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3869 | |
| 3870 | if (idx == BC_VEC_INVALID_IDX) { |
| 3871 | name = xstrdup(entry.name); |
| 3872 | bc_parse_addFunc(p, name, &idx); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3873 | idx = bc_map_index(&G.prog.fn_map, &entry); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3874 | free(entry.name); |
| 3875 | } |
| 3876 | else |
| 3877 | free(name); |
| 3878 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3879 | entry_ptr = bc_vec_item(&G.prog.fn_map, idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3880 | bc_parse_pushIndex(p, entry_ptr->idx); |
| 3881 | |
| 3882 | return bc_lex_next(&p->l); |
| 3883 | |
| 3884 | err: |
| 3885 | free(name); |
| 3886 | return s; |
| 3887 | } |
| 3888 | |
| 3889 | static BcStatus bc_parse_name(BcParse *p, BcInst *type, uint8_t flags) |
| 3890 | { |
| 3891 | BcStatus s; |
| 3892 | char *name; |
| 3893 | |
| 3894 | name = xstrdup(p->l.t.v.v); |
| 3895 | s = bc_lex_next(&p->l); |
| 3896 | if (s) goto err; |
| 3897 | |
| 3898 | if (p->l.t.t == BC_LEX_LBRACKET) { |
| 3899 | |
| 3900 | s = bc_lex_next(&p->l); |
| 3901 | if (s) goto err; |
| 3902 | |
| 3903 | if (p->l.t.t == BC_LEX_RBRACKET) { |
| 3904 | |
| 3905 | if (!(flags & BC_PARSE_ARRAY)) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3906 | s = bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3907 | goto err; |
| 3908 | } |
| 3909 | |
| 3910 | *type = BC_INST_ARRAY; |
| 3911 | } |
| 3912 | else { |
| 3913 | |
| 3914 | *type = BC_INST_ARRAY_ELEM; |
| 3915 | |
| 3916 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); |
| 3917 | s = bc_parse_expr(p, flags, bc_parse_next_elem); |
| 3918 | if (s) goto err; |
| 3919 | } |
| 3920 | |
| 3921 | s = bc_lex_next(&p->l); |
| 3922 | if (s) goto err; |
| 3923 | bc_parse_push(p, *type); |
| 3924 | bc_parse_pushName(p, name); |
| 3925 | } |
| 3926 | else if (p->l.t.t == BC_LEX_LPAREN) { |
| 3927 | |
| 3928 | if (flags & BC_PARSE_NOCALL) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3929 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3930 | goto err; |
| 3931 | } |
| 3932 | |
| 3933 | *type = BC_INST_CALL; |
| 3934 | s = bc_parse_call(p, name, flags); |
| 3935 | } |
| 3936 | else { |
| 3937 | *type = BC_INST_VAR; |
| 3938 | bc_parse_push(p, BC_INST_VAR); |
| 3939 | bc_parse_pushName(p, name); |
| 3940 | } |
| 3941 | |
| 3942 | return s; |
| 3943 | |
| 3944 | err: |
| 3945 | free(name); |
| 3946 | return s; |
| 3947 | } |
| 3948 | |
| 3949 | static BcStatus bc_parse_read(BcParse *p) |
| 3950 | { |
| 3951 | BcStatus s; |
| 3952 | |
| 3953 | s = bc_lex_next(&p->l); |
| 3954 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3955 | 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] | 3956 | |
| 3957 | s = bc_lex_next(&p->l); |
| 3958 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3959 | 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] | 3960 | |
| 3961 | bc_parse_push(p, BC_INST_READ); |
| 3962 | |
| 3963 | return bc_lex_next(&p->l); |
| 3964 | } |
| 3965 | |
| 3966 | static BcStatus bc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags, |
| 3967 | BcInst *prev) |
| 3968 | { |
| 3969 | BcStatus s; |
| 3970 | |
| 3971 | s = bc_lex_next(&p->l); |
| 3972 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3973 | 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] | 3974 | |
| 3975 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; |
| 3976 | |
| 3977 | s = bc_lex_next(&p->l); |
| 3978 | if (s) return s; |
| 3979 | |
| 3980 | s = bc_parse_expr(p, flags, bc_parse_next_rel); |
| 3981 | if (s) return s; |
| 3982 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3983 | 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] | 3984 | |
| 3985 | *prev = (type == BC_LEX_KEY_LENGTH) ? BC_INST_LENGTH : BC_INST_SQRT; |
| 3986 | bc_parse_push(p, *prev); |
| 3987 | |
| 3988 | return bc_lex_next(&p->l); |
| 3989 | } |
| 3990 | |
| 3991 | static BcStatus bc_parse_scale(BcParse *p, BcInst *type, uint8_t flags) |
| 3992 | { |
| 3993 | BcStatus s; |
| 3994 | |
| 3995 | s = bc_lex_next(&p->l); |
| 3996 | if (s) return s; |
| 3997 | |
| 3998 | if (p->l.t.t != BC_LEX_LPAREN) { |
| 3999 | *type = BC_INST_SCALE; |
| 4000 | bc_parse_push(p, BC_INST_SCALE); |
| 4001 | return BC_STATUS_SUCCESS; |
| 4002 | } |
| 4003 | |
| 4004 | *type = BC_INST_SCALE_FUNC; |
| 4005 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); |
| 4006 | |
| 4007 | s = bc_lex_next(&p->l); |
| 4008 | if (s) return s; |
| 4009 | |
| 4010 | s = bc_parse_expr(p, flags, bc_parse_next_rel); |
| 4011 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4012 | 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] | 4013 | bc_parse_push(p, BC_INST_SCALE_FUNC); |
| 4014 | |
| 4015 | return bc_lex_next(&p->l); |
| 4016 | } |
| 4017 | |
| 4018 | static BcStatus bc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr, |
| 4019 | size_t *nexprs, uint8_t flags) |
| 4020 | { |
| 4021 | BcStatus s; |
| 4022 | BcLexType type; |
| 4023 | char inst; |
| 4024 | BcInst etype = *prev; |
| 4025 | |
| 4026 | if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM || |
| 4027 | etype == BC_INST_SCALE || etype == BC_INST_LAST || |
| 4028 | etype == BC_INST_IBASE || etype == BC_INST_OBASE) |
| 4029 | { |
| 4030 | *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC); |
| 4031 | bc_parse_push(p, inst); |
| 4032 | s = bc_lex_next(&p->l); |
| 4033 | } |
| 4034 | else { |
| 4035 | |
| 4036 | *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC); |
| 4037 | *paren_expr = true; |
| 4038 | |
| 4039 | s = bc_lex_next(&p->l); |
| 4040 | if (s) return s; |
| 4041 | type = p->l.t.t; |
| 4042 | |
| 4043 | // Because we parse the next part of the expression |
| 4044 | // right here, we need to increment this. |
| 4045 | *nexprs = *nexprs + 1; |
| 4046 | |
| 4047 | switch (type) { |
| 4048 | |
| 4049 | case BC_LEX_NAME: |
| 4050 | { |
| 4051 | s = bc_parse_name(p, prev, flags | BC_PARSE_NOCALL); |
| 4052 | break; |
| 4053 | } |
| 4054 | |
| 4055 | case BC_LEX_KEY_IBASE: |
| 4056 | case BC_LEX_KEY_LAST: |
| 4057 | case BC_LEX_KEY_OBASE: |
| 4058 | { |
| 4059 | bc_parse_push(p, type - BC_LEX_KEY_IBASE + BC_INST_IBASE); |
| 4060 | s = bc_lex_next(&p->l); |
| 4061 | break; |
| 4062 | } |
| 4063 | |
| 4064 | case BC_LEX_KEY_SCALE: |
| 4065 | { |
| 4066 | s = bc_lex_next(&p->l); |
| 4067 | if (s) return s; |
| 4068 | if (p->l.t.t == BC_LEX_LPAREN) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4069 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4070 | else |
| 4071 | bc_parse_push(p, BC_INST_SCALE); |
| 4072 | break; |
| 4073 | } |
| 4074 | |
| 4075 | default: |
| 4076 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4077 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4078 | break; |
| 4079 | } |
| 4080 | } |
| 4081 | |
| 4082 | if (!s) bc_parse_push(p, inst); |
| 4083 | } |
| 4084 | |
| 4085 | return s; |
| 4086 | } |
| 4087 | |
| 4088 | static BcStatus bc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn, |
| 4089 | bool rparen, size_t *nexprs) |
| 4090 | { |
| 4091 | BcStatus s; |
| 4092 | BcLexType type; |
| 4093 | BcInst etype = *prev; |
| 4094 | |
| 4095 | s = bc_lex_next(&p->l); |
| 4096 | if (s) return s; |
| 4097 | |
| 4098 | type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST || |
| 4099 | (etype >= BC_INST_NUM && etype <= BC_INST_SQRT) ? |
| 4100 | BC_LEX_OP_MINUS : |
| 4101 | BC_LEX_NEG; |
| 4102 | *prev = BC_PARSE_TOKEN_INST(type); |
| 4103 | |
| 4104 | // We can just push onto the op stack because this is the largest |
| 4105 | // precedence operator that gets pushed. Inc/dec does not. |
| 4106 | if (type != BC_LEX_OP_MINUS) |
| 4107 | bc_vec_push(&p->ops, &type); |
| 4108 | else |
| 4109 | s = bc_parse_operator(p, type, ops_bgn, nexprs, false); |
| 4110 | |
| 4111 | return s; |
| 4112 | } |
| 4113 | |
| 4114 | static BcStatus bc_parse_string(BcParse *p, char inst) |
| 4115 | { |
| 4116 | char *str = xstrdup(p->l.t.v.v); |
| 4117 | |
| 4118 | bc_parse_push(p, BC_INST_STR); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 4119 | bc_parse_pushIndex(p, G.prog.strs.len); |
| 4120 | bc_vec_push(&G.prog.strs, &str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4121 | bc_parse_push(p, inst); |
| 4122 | |
| 4123 | return bc_lex_next(&p->l); |
| 4124 | } |
| 4125 | |
| 4126 | static BcStatus bc_parse_print(BcParse *p) |
| 4127 | { |
| 4128 | BcStatus s; |
| 4129 | BcLexType type; |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4130 | bool comma; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4131 | |
| 4132 | s = bc_lex_next(&p->l); |
| 4133 | if (s) return s; |
| 4134 | |
| 4135 | type = p->l.t.t; |
| 4136 | |
| 4137 | if (type == BC_LEX_SCOLON || type == BC_LEX_NLINE) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4138 | return bc_error("bad print statement"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4139 | |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4140 | comma = false; |
| 4141 | while (type != BC_LEX_SCOLON && type != BC_LEX_NLINE) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4142 | |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4143 | if (type == BC_LEX_STR) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4144 | s = bc_parse_string(p, BC_INST_PRINT_POP); |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4145 | if (s) return s; |
| 4146 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4147 | s = bc_parse_expr(p, 0, bc_parse_next_print); |
| 4148 | if (s) return s; |
| 4149 | bc_parse_push(p, BC_INST_PRINT_POP); |
| 4150 | } |
| 4151 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4152 | comma = p->l.t.t == BC_LEX_COMMA; |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4153 | if (comma) { |
| 4154 | s = bc_lex_next(&p->l); |
| 4155 | if (s) return s; |
| 4156 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4157 | type = p->l.t.t; |
| 4158 | } |
| 4159 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4160 | if (comma) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4161 | |
| 4162 | return bc_lex_next(&p->l); |
| 4163 | } |
| 4164 | |
| 4165 | static BcStatus bc_parse_return(BcParse *p) |
| 4166 | { |
| 4167 | BcStatus s; |
| 4168 | BcLexType t; |
| 4169 | bool paren; |
| 4170 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4171 | if (!BC_PARSE_FUNC(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4172 | |
| 4173 | s = bc_lex_next(&p->l); |
| 4174 | if (s) return s; |
| 4175 | |
| 4176 | t = p->l.t.t; |
| 4177 | paren = t == BC_LEX_LPAREN; |
| 4178 | |
| 4179 | if (t == BC_LEX_NLINE || t == BC_LEX_SCOLON) |
| 4180 | bc_parse_push(p, BC_INST_RET0); |
| 4181 | else { |
| 4182 | |
Denys Vlasenko | 050b0fe | 2018-12-05 22:40:44 +0100 | [diff] [blame] | 4183 | s = bc_parse_expr_empty_ok(p, 0, bc_parse_next_expr); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4184 | if (s == BC_STATUS_PARSE_EMPTY_EXP) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4185 | bc_parse_push(p, BC_INST_RET0); |
| 4186 | s = bc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4187 | } |
Denys Vlasenko | 452df92 | 2018-12-05 20:28:26 +0100 | [diff] [blame] | 4188 | if (s) return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4189 | |
| 4190 | if (!paren || p->l.t.last != BC_LEX_RPAREN) { |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 4191 | s = bc_POSIX_requires("parentheses around return expressions"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4192 | if (s) return s; |
| 4193 | } |
| 4194 | |
| 4195 | bc_parse_push(p, BC_INST_RET); |
| 4196 | } |
| 4197 | |
| 4198 | return s; |
| 4199 | } |
| 4200 | |
| 4201 | static BcStatus bc_parse_endBody(BcParse *p, bool brace) |
| 4202 | { |
| 4203 | BcStatus s = BC_STATUS_SUCCESS; |
| 4204 | |
| 4205 | if (p->flags.len <= 1 || (brace && p->nbraces == 0)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4206 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4207 | |
| 4208 | if (brace) { |
| 4209 | |
| 4210 | if (p->l.t.t == BC_LEX_RBRACE) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4211 | if (!p->nbraces) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4212 | --p->nbraces; |
| 4213 | s = bc_lex_next(&p->l); |
| 4214 | if (s) return s; |
| 4215 | } |
| 4216 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4217 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4218 | } |
| 4219 | |
| 4220 | if (BC_PARSE_IF(p)) { |
| 4221 | |
| 4222 | uint8_t *flag_ptr; |
| 4223 | |
| 4224 | while (p->l.t.t == BC_LEX_NLINE) { |
| 4225 | s = bc_lex_next(&p->l); |
| 4226 | if (s) return s; |
| 4227 | } |
| 4228 | |
| 4229 | bc_vec_pop(&p->flags); |
| 4230 | |
| 4231 | flag_ptr = BC_PARSE_TOP_FLAG_PTR(p); |
| 4232 | *flag_ptr = (*flag_ptr | BC_PARSE_FLAG_IF_END); |
| 4233 | |
| 4234 | if (p->l.t.t == BC_LEX_KEY_ELSE) s = bc_parse_else(p); |
| 4235 | } |
| 4236 | else if (BC_PARSE_ELSE(p)) { |
| 4237 | |
| 4238 | BcInstPtr *ip; |
| 4239 | size_t *label; |
| 4240 | |
| 4241 | bc_vec_pop(&p->flags); |
| 4242 | |
| 4243 | ip = bc_vec_top(&p->exits); |
| 4244 | label = bc_vec_item(&p->func->labels, ip->idx); |
| 4245 | *label = p->func->code.len; |
| 4246 | |
| 4247 | bc_vec_pop(&p->exits); |
| 4248 | } |
| 4249 | else if (BC_PARSE_FUNC_INNER(p)) { |
| 4250 | bc_parse_push(p, BC_INST_RET0); |
| 4251 | bc_parse_updateFunc(p, BC_PROG_MAIN); |
| 4252 | bc_vec_pop(&p->flags); |
| 4253 | } |
| 4254 | else { |
| 4255 | |
| 4256 | BcInstPtr *ip = bc_vec_top(&p->exits); |
| 4257 | size_t *label = bc_vec_top(&p->conds); |
| 4258 | |
| 4259 | bc_parse_push(p, BC_INST_JUMP); |
| 4260 | bc_parse_pushIndex(p, *label); |
| 4261 | |
| 4262 | label = bc_vec_item(&p->func->labels, ip->idx); |
| 4263 | *label = p->func->code.len; |
| 4264 | |
| 4265 | bc_vec_pop(&p->flags); |
| 4266 | bc_vec_pop(&p->exits); |
| 4267 | bc_vec_pop(&p->conds); |
| 4268 | } |
| 4269 | |
| 4270 | return s; |
| 4271 | } |
| 4272 | |
| 4273 | static void bc_parse_startBody(BcParse *p, uint8_t flags) |
| 4274 | { |
| 4275 | uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p); |
| 4276 | flags |= (*flag_ptr & (BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_LOOP)); |
| 4277 | flags |= BC_PARSE_FLAG_BODY; |
| 4278 | bc_vec_push(&p->flags, &flags); |
| 4279 | } |
| 4280 | |
| 4281 | static void bc_parse_noElse(BcParse *p) |
| 4282 | { |
| 4283 | BcInstPtr *ip; |
| 4284 | size_t *label; |
| 4285 | uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p); |
| 4286 | |
| 4287 | *flag_ptr = (*flag_ptr & ~(BC_PARSE_FLAG_IF_END)); |
| 4288 | |
| 4289 | ip = bc_vec_top(&p->exits); |
| 4290 | label = bc_vec_item(&p->func->labels, ip->idx); |
| 4291 | *label = p->func->code.len; |
| 4292 | |
| 4293 | bc_vec_pop(&p->exits); |
| 4294 | } |
| 4295 | |
| 4296 | static BcStatus bc_parse_if(BcParse *p) |
| 4297 | { |
| 4298 | BcStatus s; |
| 4299 | BcInstPtr ip; |
| 4300 | |
| 4301 | s = bc_lex_next(&p->l); |
| 4302 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4303 | 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] | 4304 | |
| 4305 | s = bc_lex_next(&p->l); |
| 4306 | if (s) return s; |
| 4307 | s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel); |
| 4308 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4309 | 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] | 4310 | |
| 4311 | s = bc_lex_next(&p->l); |
| 4312 | if (s) return s; |
| 4313 | bc_parse_push(p, BC_INST_JUMP_ZERO); |
| 4314 | |
| 4315 | ip.idx = p->func->labels.len; |
| 4316 | ip.func = ip.len = 0; |
| 4317 | |
| 4318 | bc_parse_pushIndex(p, ip.idx); |
| 4319 | bc_vec_push(&p->exits, &ip); |
| 4320 | bc_vec_push(&p->func->labels, &ip.idx); |
| 4321 | bc_parse_startBody(p, BC_PARSE_FLAG_IF); |
| 4322 | |
| 4323 | return BC_STATUS_SUCCESS; |
| 4324 | } |
| 4325 | |
| 4326 | static BcStatus bc_parse_else(BcParse *p) |
| 4327 | { |
| 4328 | BcInstPtr ip; |
| 4329 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4330 | if (!BC_PARSE_IF_END(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4331 | |
| 4332 | ip.idx = p->func->labels.len; |
| 4333 | ip.func = ip.len = 0; |
| 4334 | |
| 4335 | bc_parse_push(p, BC_INST_JUMP); |
| 4336 | bc_parse_pushIndex(p, ip.idx); |
| 4337 | |
| 4338 | bc_parse_noElse(p); |
| 4339 | |
| 4340 | bc_vec_push(&p->exits, &ip); |
| 4341 | bc_vec_push(&p->func->labels, &ip.idx); |
| 4342 | bc_parse_startBody(p, BC_PARSE_FLAG_ELSE); |
| 4343 | |
| 4344 | return bc_lex_next(&p->l); |
| 4345 | } |
| 4346 | |
| 4347 | static BcStatus bc_parse_while(BcParse *p) |
| 4348 | { |
| 4349 | BcStatus s; |
| 4350 | BcInstPtr ip; |
| 4351 | |
| 4352 | s = bc_lex_next(&p->l); |
| 4353 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4354 | 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] | 4355 | s = bc_lex_next(&p->l); |
| 4356 | if (s) return s; |
| 4357 | |
| 4358 | ip.idx = p->func->labels.len; |
| 4359 | |
| 4360 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4361 | bc_vec_push(&p->conds, &ip.idx); |
| 4362 | |
| 4363 | ip.idx = p->func->labels.len; |
| 4364 | ip.func = 1; |
| 4365 | ip.len = 0; |
| 4366 | |
| 4367 | bc_vec_push(&p->exits, &ip); |
| 4368 | bc_vec_push(&p->func->labels, &ip.idx); |
| 4369 | |
| 4370 | s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel); |
| 4371 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4372 | 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] | 4373 | s = bc_lex_next(&p->l); |
| 4374 | if (s) return s; |
| 4375 | |
| 4376 | bc_parse_push(p, BC_INST_JUMP_ZERO); |
| 4377 | bc_parse_pushIndex(p, ip.idx); |
| 4378 | bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER); |
| 4379 | |
| 4380 | return BC_STATUS_SUCCESS; |
| 4381 | } |
| 4382 | |
| 4383 | static BcStatus bc_parse_for(BcParse *p) |
| 4384 | { |
| 4385 | BcStatus s; |
| 4386 | BcInstPtr ip; |
| 4387 | size_t cond_idx, exit_idx, body_idx, update_idx; |
| 4388 | |
| 4389 | s = bc_lex_next(&p->l); |
| 4390 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4391 | 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] | 4392 | s = bc_lex_next(&p->l); |
| 4393 | if (s) return s; |
| 4394 | |
| 4395 | if (p->l.t.t != BC_LEX_SCOLON) |
| 4396 | s = bc_parse_expr(p, 0, bc_parse_next_for); |
| 4397 | else |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 4398 | s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4399 | |
| 4400 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4401 | 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] | 4402 | s = bc_lex_next(&p->l); |
| 4403 | if (s) return s; |
| 4404 | |
| 4405 | cond_idx = p->func->labels.len; |
| 4406 | update_idx = cond_idx + 1; |
| 4407 | body_idx = update_idx + 1; |
| 4408 | exit_idx = body_idx + 1; |
| 4409 | |
| 4410 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4411 | |
| 4412 | if (p->l.t.t != BC_LEX_SCOLON) |
| 4413 | s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_for); |
| 4414 | else |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 4415 | s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4416 | |
| 4417 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4418 | 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] | 4419 | |
| 4420 | s = bc_lex_next(&p->l); |
| 4421 | if (s) return s; |
| 4422 | |
| 4423 | bc_parse_push(p, BC_INST_JUMP_ZERO); |
| 4424 | bc_parse_pushIndex(p, exit_idx); |
| 4425 | bc_parse_push(p, BC_INST_JUMP); |
| 4426 | bc_parse_pushIndex(p, body_idx); |
| 4427 | |
| 4428 | ip.idx = p->func->labels.len; |
| 4429 | |
| 4430 | bc_vec_push(&p->conds, &update_idx); |
| 4431 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4432 | |
| 4433 | if (p->l.t.t != BC_LEX_RPAREN) |
| 4434 | s = bc_parse_expr(p, 0, bc_parse_next_rel); |
| 4435 | else |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 4436 | s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4437 | |
| 4438 | if (s) return s; |
| 4439 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4440 | 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] | 4441 | bc_parse_push(p, BC_INST_JUMP); |
| 4442 | bc_parse_pushIndex(p, cond_idx); |
| 4443 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4444 | |
| 4445 | ip.idx = exit_idx; |
| 4446 | ip.func = 1; |
| 4447 | ip.len = 0; |
| 4448 | |
| 4449 | bc_vec_push(&p->exits, &ip); |
| 4450 | bc_vec_push(&p->func->labels, &ip.idx); |
| 4451 | bc_lex_next(&p->l); |
| 4452 | bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER); |
| 4453 | |
| 4454 | return BC_STATUS_SUCCESS; |
| 4455 | } |
| 4456 | |
| 4457 | static BcStatus bc_parse_loopExit(BcParse *p, BcLexType type) |
| 4458 | { |
| 4459 | BcStatus s; |
| 4460 | size_t i; |
| 4461 | BcInstPtr *ip; |
| 4462 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4463 | if (!BC_PARSE_LOOP(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4464 | |
| 4465 | if (type == BC_LEX_KEY_BREAK) { |
| 4466 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4467 | if (p->exits.len == 0) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4468 | |
| 4469 | i = p->exits.len - 1; |
| 4470 | ip = bc_vec_item(&p->exits, i); |
| 4471 | |
| 4472 | 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] | 4473 | if (i >= p->exits.len && !ip->func) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4474 | |
| 4475 | i = ip->idx; |
| 4476 | } |
| 4477 | else |
| 4478 | i = *((size_t *) bc_vec_top(&p->conds)); |
| 4479 | |
| 4480 | bc_parse_push(p, BC_INST_JUMP); |
| 4481 | bc_parse_pushIndex(p, i); |
| 4482 | |
| 4483 | s = bc_lex_next(&p->l); |
| 4484 | if (s) return s; |
| 4485 | |
| 4486 | 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] | 4487 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4488 | |
| 4489 | return bc_lex_next(&p->l); |
| 4490 | } |
| 4491 | |
| 4492 | static BcStatus bc_parse_func(BcParse *p) |
| 4493 | { |
| 4494 | BcStatus s; |
| 4495 | bool var, comma = false; |
| 4496 | uint8_t flags; |
| 4497 | char *name; |
| 4498 | |
| 4499 | s = bc_lex_next(&p->l); |
| 4500 | if (s) return s; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4501 | if (p->l.t.t != BC_LEX_NAME) |
| 4502 | return bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4503 | |
| 4504 | name = xstrdup(p->l.t.v.v); |
| 4505 | bc_parse_addFunc(p, name, &p->fidx); |
| 4506 | |
| 4507 | s = bc_lex_next(&p->l); |
| 4508 | if (s) return s; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4509 | if (p->l.t.t != BC_LEX_LPAREN) |
| 4510 | return bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4511 | s = bc_lex_next(&p->l); |
| 4512 | if (s) return s; |
| 4513 | |
| 4514 | while (p->l.t.t != BC_LEX_RPAREN) { |
| 4515 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4516 | if (p->l.t.t != BC_LEX_NAME) |
| 4517 | return bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4518 | |
| 4519 | ++p->func->nparams; |
| 4520 | |
| 4521 | name = xstrdup(p->l.t.v.v); |
| 4522 | s = bc_lex_next(&p->l); |
| 4523 | if (s) goto err; |
| 4524 | |
| 4525 | var = p->l.t.t != BC_LEX_LBRACKET; |
| 4526 | |
| 4527 | if (!var) { |
| 4528 | |
| 4529 | s = bc_lex_next(&p->l); |
| 4530 | if (s) goto err; |
| 4531 | |
| 4532 | if (p->l.t.t != BC_LEX_RBRACKET) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4533 | s = bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4534 | goto err; |
| 4535 | } |
| 4536 | |
| 4537 | s = bc_lex_next(&p->l); |
| 4538 | if (s) goto err; |
| 4539 | } |
| 4540 | |
| 4541 | comma = p->l.t.t == BC_LEX_COMMA; |
| 4542 | if (comma) { |
| 4543 | s = bc_lex_next(&p->l); |
| 4544 | if (s) goto err; |
| 4545 | } |
| 4546 | |
| 4547 | s = bc_func_insert(p->func, name, var); |
| 4548 | if (s) goto err; |
| 4549 | } |
| 4550 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4551 | if (comma) return bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4552 | |
| 4553 | flags = BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_BODY; |
| 4554 | bc_parse_startBody(p, flags); |
| 4555 | |
| 4556 | s = bc_lex_next(&p->l); |
| 4557 | if (s) return s; |
| 4558 | |
| 4559 | if (p->l.t.t != BC_LEX_LBRACE) |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 4560 | 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] | 4561 | |
| 4562 | return s; |
| 4563 | |
| 4564 | err: |
| 4565 | free(name); |
| 4566 | return s; |
| 4567 | } |
| 4568 | |
| 4569 | static BcStatus bc_parse_auto(BcParse *p) |
| 4570 | { |
| 4571 | BcStatus s; |
| 4572 | bool comma, var, one; |
| 4573 | char *name; |
| 4574 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4575 | if (!p->auto_part) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4576 | s = bc_lex_next(&p->l); |
| 4577 | if (s) return s; |
| 4578 | |
| 4579 | p->auto_part = comma = false; |
| 4580 | one = p->l.t.t == BC_LEX_NAME; |
| 4581 | |
| 4582 | while (p->l.t.t == BC_LEX_NAME) { |
| 4583 | |
| 4584 | name = xstrdup(p->l.t.v.v); |
| 4585 | s = bc_lex_next(&p->l); |
| 4586 | if (s) goto err; |
| 4587 | |
| 4588 | var = p->l.t.t != BC_LEX_LBRACKET; |
| 4589 | if (!var) { |
| 4590 | |
| 4591 | s = bc_lex_next(&p->l); |
| 4592 | if (s) goto err; |
| 4593 | |
| 4594 | if (p->l.t.t != BC_LEX_RBRACKET) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4595 | s = bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4596 | goto err; |
| 4597 | } |
| 4598 | |
| 4599 | s = bc_lex_next(&p->l); |
| 4600 | if (s) goto err; |
| 4601 | } |
| 4602 | |
| 4603 | comma = p->l.t.t == BC_LEX_COMMA; |
| 4604 | if (comma) { |
| 4605 | s = bc_lex_next(&p->l); |
| 4606 | if (s) goto err; |
| 4607 | } |
| 4608 | |
| 4609 | s = bc_func_insert(p->func, name, var); |
| 4610 | if (s) goto err; |
| 4611 | } |
| 4612 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4613 | if (comma) return bc_error("bad function definition"); |
Denys Vlasenko | abbc433 | 2018-12-03 21:46:41 +0100 | [diff] [blame] | 4614 | if (!one) return bc_error("no auto variable found"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4615 | |
| 4616 | 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] | 4617 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4618 | |
| 4619 | return bc_lex_next(&p->l); |
| 4620 | |
| 4621 | err: |
| 4622 | free(name); |
| 4623 | return s; |
| 4624 | } |
| 4625 | |
| 4626 | static BcStatus bc_parse_body(BcParse *p, bool brace) |
| 4627 | { |
| 4628 | BcStatus s = BC_STATUS_SUCCESS; |
| 4629 | uint8_t *flag_ptr = bc_vec_top(&p->flags); |
| 4630 | |
| 4631 | *flag_ptr &= ~(BC_PARSE_FLAG_BODY); |
| 4632 | |
| 4633 | if (*flag_ptr & BC_PARSE_FLAG_FUNC_INNER) { |
| 4634 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4635 | if (!brace) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4636 | p->auto_part = p->l.t.t != BC_LEX_KEY_AUTO; |
| 4637 | |
| 4638 | if (!p->auto_part) { |
| 4639 | s = bc_parse_auto(p); |
| 4640 | if (s) return s; |
| 4641 | } |
| 4642 | |
| 4643 | if (p->l.t.t == BC_LEX_NLINE) s = bc_lex_next(&p->l); |
| 4644 | } |
| 4645 | else { |
| 4646 | s = bc_parse_stmt(p); |
| 4647 | if (!s && !brace) s = bc_parse_endBody(p, false); |
| 4648 | } |
| 4649 | |
| 4650 | return s; |
| 4651 | } |
| 4652 | |
| 4653 | static BcStatus bc_parse_stmt(BcParse *p) |
| 4654 | { |
| 4655 | BcStatus s = BC_STATUS_SUCCESS; |
| 4656 | |
| 4657 | switch (p->l.t.t) { |
| 4658 | |
| 4659 | case BC_LEX_NLINE: |
| 4660 | { |
| 4661 | return bc_lex_next(&p->l); |
| 4662 | } |
| 4663 | |
| 4664 | case BC_LEX_KEY_ELSE: |
| 4665 | { |
| 4666 | p->auto_part = false; |
| 4667 | break; |
| 4668 | } |
| 4669 | |
| 4670 | case BC_LEX_LBRACE: |
| 4671 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4672 | if (!BC_PARSE_BODY(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4673 | |
| 4674 | ++p->nbraces; |
| 4675 | s = bc_lex_next(&p->l); |
| 4676 | if (s) return s; |
| 4677 | |
| 4678 | return bc_parse_body(p, true); |
| 4679 | } |
| 4680 | |
| 4681 | case BC_LEX_KEY_AUTO: |
| 4682 | { |
| 4683 | return bc_parse_auto(p); |
| 4684 | } |
| 4685 | |
| 4686 | default: |
| 4687 | { |
| 4688 | p->auto_part = false; |
| 4689 | |
| 4690 | if (BC_PARSE_IF_END(p)) { |
| 4691 | bc_parse_noElse(p); |
| 4692 | return BC_STATUS_SUCCESS; |
| 4693 | } |
| 4694 | else if (BC_PARSE_BODY(p)) |
| 4695 | return bc_parse_body(p, false); |
| 4696 | |
| 4697 | break; |
| 4698 | } |
| 4699 | } |
| 4700 | |
| 4701 | switch (p->l.t.t) { |
| 4702 | |
| 4703 | case BC_LEX_OP_INC: |
| 4704 | case BC_LEX_OP_DEC: |
| 4705 | case BC_LEX_OP_MINUS: |
| 4706 | case BC_LEX_OP_BOOL_NOT: |
| 4707 | case BC_LEX_LPAREN: |
| 4708 | case BC_LEX_NAME: |
| 4709 | case BC_LEX_NUMBER: |
| 4710 | case BC_LEX_KEY_IBASE: |
| 4711 | case BC_LEX_KEY_LAST: |
| 4712 | case BC_LEX_KEY_LENGTH: |
| 4713 | case BC_LEX_KEY_OBASE: |
| 4714 | case BC_LEX_KEY_READ: |
| 4715 | case BC_LEX_KEY_SCALE: |
| 4716 | case BC_LEX_KEY_SQRT: |
| 4717 | { |
| 4718 | s = bc_parse_expr(p, BC_PARSE_PRINT, bc_parse_next_expr); |
| 4719 | break; |
| 4720 | } |
| 4721 | |
| 4722 | case BC_LEX_KEY_ELSE: |
| 4723 | { |
| 4724 | s = bc_parse_else(p); |
| 4725 | break; |
| 4726 | } |
| 4727 | |
| 4728 | case BC_LEX_SCOLON: |
| 4729 | { |
| 4730 | while (!s && p->l.t.t == BC_LEX_SCOLON) s = bc_lex_next(&p->l); |
| 4731 | break; |
| 4732 | } |
| 4733 | |
| 4734 | case BC_LEX_RBRACE: |
| 4735 | { |
| 4736 | s = bc_parse_endBody(p, true); |
| 4737 | break; |
| 4738 | } |
| 4739 | |
| 4740 | case BC_LEX_STR: |
| 4741 | { |
| 4742 | s = bc_parse_string(p, BC_INST_PRINT_STR); |
| 4743 | break; |
| 4744 | } |
| 4745 | |
| 4746 | case BC_LEX_KEY_BREAK: |
| 4747 | case BC_LEX_KEY_CONTINUE: |
| 4748 | { |
| 4749 | s = bc_parse_loopExit(p, p->l.t.t); |
| 4750 | break; |
| 4751 | } |
| 4752 | |
| 4753 | case BC_LEX_KEY_FOR: |
| 4754 | { |
| 4755 | s = bc_parse_for(p); |
| 4756 | break; |
| 4757 | } |
| 4758 | |
| 4759 | case BC_LEX_KEY_HALT: |
| 4760 | { |
| 4761 | bc_parse_push(p, BC_INST_HALT); |
| 4762 | s = bc_lex_next(&p->l); |
| 4763 | break; |
| 4764 | } |
| 4765 | |
| 4766 | case BC_LEX_KEY_IF: |
| 4767 | { |
| 4768 | s = bc_parse_if(p); |
| 4769 | break; |
| 4770 | } |
| 4771 | |
| 4772 | case BC_LEX_KEY_LIMITS: |
| 4773 | { |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 4774 | // "limits" is a compile-time command, |
| 4775 | // the output is produced at _parse time_. |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4776 | s = bc_lex_next(&p->l); |
| 4777 | if (s) return s; |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 4778 | printf( |
| 4779 | "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n" |
| 4780 | "BC_DIM_MAX = "BC_MAX_DIM_STR "\n" |
| 4781 | "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n" |
| 4782 | "BC_STRING_MAX = "BC_MAX_STRING_STR"\n" |
| 4783 | "BC_NAME_MAX = "BC_MAX_NAME_STR "\n" |
| 4784 | "BC_NUM_MAX = "BC_MAX_NUM_STR "\n" |
| 4785 | "MAX Exponent = "BC_MAX_EXP_STR "\n" |
| 4786 | "Number of vars = "BC_MAX_VARS_STR "\n" |
| 4787 | ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4788 | break; |
| 4789 | } |
| 4790 | |
| 4791 | case BC_LEX_KEY_PRINT: |
| 4792 | { |
| 4793 | s = bc_parse_print(p); |
| 4794 | break; |
| 4795 | } |
| 4796 | |
| 4797 | case BC_LEX_KEY_QUIT: |
| 4798 | { |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 4799 | // "quit" is a compile-time command. For example, |
| 4800 | // "if (0 == 1) quit" terminates when parsing the statement, |
| 4801 | // not when it is executed |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 4802 | QUIT_OR_RETURN_TO_MAIN; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4803 | } |
| 4804 | |
| 4805 | case BC_LEX_KEY_RETURN: |
| 4806 | { |
| 4807 | s = bc_parse_return(p); |
| 4808 | break; |
| 4809 | } |
| 4810 | |
| 4811 | case BC_LEX_KEY_WHILE: |
| 4812 | { |
| 4813 | s = bc_parse_while(p); |
| 4814 | break; |
| 4815 | } |
| 4816 | |
| 4817 | default: |
| 4818 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4819 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4820 | break; |
| 4821 | } |
| 4822 | } |
| 4823 | |
| 4824 | return s; |
| 4825 | } |
| 4826 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 4827 | static FAST_FUNC BcStatus bc_parse_parse(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4828 | { |
| 4829 | BcStatus s; |
| 4830 | |
| 4831 | if (p->l.t.t == BC_LEX_EOF) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4832 | 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] | 4833 | else if (p->l.t.t == BC_LEX_KEY_DEFINE) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4834 | if (!BC_PARSE_CAN_EXEC(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4835 | s = bc_parse_func(p); |
| 4836 | } |
| 4837 | else |
| 4838 | s = bc_parse_stmt(p); |
| 4839 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 4840 | if (s || G_interrupt) { |
| 4841 | bc_parse_reset(p); |
| 4842 | s = BC_STATUS_FAILURE; |
| 4843 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4844 | |
| 4845 | return s; |
| 4846 | } |
| 4847 | |
Denys Vlasenko | 050b0fe | 2018-12-05 22:40:44 +0100 | [diff] [blame] | 4848 | 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] | 4849 | { |
| 4850 | BcStatus s = BC_STATUS_SUCCESS; |
| 4851 | BcInst prev = BC_INST_PRINT; |
| 4852 | BcLexType top, t = p->l.t.t; |
| 4853 | size_t nexprs = 0, ops_bgn = p->ops.len; |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 4854 | unsigned nparens, nrelops; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4855 | bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last; |
| 4856 | |
| 4857 | paren_first = p->l.t.t == BC_LEX_LPAREN; |
| 4858 | nparens = nrelops = 0; |
| 4859 | paren_expr = rprn = done = get_token = assign = false; |
| 4860 | bin_last = true; |
| 4861 | |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 4862 | 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] | 4863 | switch (t) { |
| 4864 | |
| 4865 | case BC_LEX_OP_INC: |
| 4866 | case BC_LEX_OP_DEC: |
| 4867 | { |
| 4868 | s = bc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags); |
| 4869 | rprn = get_token = bin_last = false; |
| 4870 | break; |
| 4871 | } |
| 4872 | |
| 4873 | case BC_LEX_OP_MINUS: |
| 4874 | { |
| 4875 | s = bc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs); |
| 4876 | rprn = get_token = false; |
| 4877 | bin_last = prev == BC_INST_MINUS; |
| 4878 | break; |
| 4879 | } |
| 4880 | |
| 4881 | case BC_LEX_OP_ASSIGN_POWER: |
| 4882 | case BC_LEX_OP_ASSIGN_MULTIPLY: |
| 4883 | case BC_LEX_OP_ASSIGN_DIVIDE: |
| 4884 | case BC_LEX_OP_ASSIGN_MODULUS: |
| 4885 | case BC_LEX_OP_ASSIGN_PLUS: |
| 4886 | case BC_LEX_OP_ASSIGN_MINUS: |
| 4887 | case BC_LEX_OP_ASSIGN: |
| 4888 | { |
| 4889 | if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM && |
| 4890 | prev != BC_INST_SCALE && prev != BC_INST_IBASE && |
| 4891 | prev != BC_INST_OBASE && prev != BC_INST_LAST) |
| 4892 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4893 | s = bc_error("bad assignment:" |
| 4894 | " left side must be scale," |
| 4895 | " ibase, obase, last, var," |
| 4896 | " or array element" |
| 4897 | ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4898 | break; |
| 4899 | } |
| 4900 | } |
| 4901 | // Fallthrough. |
| 4902 | case BC_LEX_OP_POWER: |
| 4903 | case BC_LEX_OP_MULTIPLY: |
| 4904 | case BC_LEX_OP_DIVIDE: |
| 4905 | case BC_LEX_OP_MODULUS: |
| 4906 | case BC_LEX_OP_PLUS: |
| 4907 | case BC_LEX_OP_REL_EQ: |
| 4908 | case BC_LEX_OP_REL_LE: |
| 4909 | case BC_LEX_OP_REL_GE: |
| 4910 | case BC_LEX_OP_REL_NE: |
| 4911 | case BC_LEX_OP_REL_LT: |
| 4912 | case BC_LEX_OP_REL_GT: |
| 4913 | case BC_LEX_OP_BOOL_NOT: |
| 4914 | case BC_LEX_OP_BOOL_OR: |
| 4915 | case BC_LEX_OP_BOOL_AND: |
| 4916 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4917 | if (((t == BC_LEX_OP_BOOL_NOT) != bin_last) |
| 4918 | || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT) |
| 4919 | ) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4920 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4921 | } |
| 4922 | |
| 4923 | nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT; |
| 4924 | prev = BC_PARSE_TOKEN_INST(t); |
| 4925 | s = bc_parse_operator(p, t, ops_bgn, &nexprs, true); |
| 4926 | rprn = get_token = false; |
| 4927 | bin_last = t != BC_LEX_OP_BOOL_NOT; |
| 4928 | |
| 4929 | break; |
| 4930 | } |
| 4931 | |
| 4932 | case BC_LEX_LPAREN: |
| 4933 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4934 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4935 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4936 | ++nparens; |
| 4937 | paren_expr = rprn = bin_last = false; |
| 4938 | get_token = true; |
| 4939 | bc_vec_push(&p->ops, &t); |
| 4940 | |
| 4941 | break; |
| 4942 | } |
| 4943 | |
| 4944 | case BC_LEX_RPAREN: |
| 4945 | { |
| 4946 | if (bin_last || prev == BC_INST_BOOL_NOT) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4947 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4948 | |
| 4949 | if (nparens == 0) { |
| 4950 | s = BC_STATUS_SUCCESS; |
| 4951 | done = true; |
| 4952 | get_token = false; |
| 4953 | break; |
| 4954 | } |
| 4955 | else if (!paren_expr) |
| 4956 | return BC_STATUS_PARSE_EMPTY_EXP; |
| 4957 | |
| 4958 | --nparens; |
| 4959 | paren_expr = rprn = true; |
| 4960 | get_token = bin_last = false; |
| 4961 | |
| 4962 | s = bc_parse_rightParen(p, ops_bgn, &nexprs); |
| 4963 | |
| 4964 | break; |
| 4965 | } |
| 4966 | |
| 4967 | case BC_LEX_NAME: |
| 4968 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4969 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4970 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4971 | paren_expr = true; |
| 4972 | rprn = get_token = bin_last = false; |
| 4973 | s = bc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL); |
| 4974 | ++nexprs; |
| 4975 | |
| 4976 | break; |
| 4977 | } |
| 4978 | |
| 4979 | case BC_LEX_NUMBER: |
| 4980 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4981 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4982 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4983 | bc_parse_number(p, &prev, &nexprs); |
| 4984 | paren_expr = get_token = true; |
| 4985 | rprn = bin_last = false; |
| 4986 | |
| 4987 | break; |
| 4988 | } |
| 4989 | |
| 4990 | case BC_LEX_KEY_IBASE: |
| 4991 | case BC_LEX_KEY_LAST: |
| 4992 | case BC_LEX_KEY_OBASE: |
| 4993 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4994 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4995 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4996 | prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE); |
| 4997 | bc_parse_push(p, (char) prev); |
| 4998 | |
| 4999 | paren_expr = get_token = true; |
| 5000 | rprn = bin_last = false; |
| 5001 | ++nexprs; |
| 5002 | |
| 5003 | break; |
| 5004 | } |
| 5005 | |
| 5006 | case BC_LEX_KEY_LENGTH: |
| 5007 | case BC_LEX_KEY_SQRT: |
| 5008 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5009 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5010 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5011 | s = bc_parse_builtin(p, t, flags, &prev); |
| 5012 | paren_expr = true; |
| 5013 | rprn = get_token = bin_last = false; |
| 5014 | ++nexprs; |
| 5015 | |
| 5016 | break; |
| 5017 | } |
| 5018 | |
| 5019 | case BC_LEX_KEY_READ: |
| 5020 | { |
| 5021 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5022 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5023 | else if (flags & BC_PARSE_NOREAD) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5024 | s = bc_error_nested_read_call(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5025 | else |
| 5026 | s = bc_parse_read(p); |
| 5027 | |
| 5028 | paren_expr = true; |
| 5029 | rprn = get_token = bin_last = false; |
| 5030 | ++nexprs; |
| 5031 | prev = BC_INST_READ; |
| 5032 | |
| 5033 | break; |
| 5034 | } |
| 5035 | |
| 5036 | case BC_LEX_KEY_SCALE: |
| 5037 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5038 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5039 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5040 | s = bc_parse_scale(p, &prev, flags); |
| 5041 | paren_expr = true; |
| 5042 | rprn = get_token = bin_last = false; |
| 5043 | ++nexprs; |
| 5044 | prev = BC_INST_SCALE; |
| 5045 | |
| 5046 | break; |
| 5047 | } |
| 5048 | |
| 5049 | default: |
| 5050 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5051 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5052 | break; |
| 5053 | } |
| 5054 | } |
| 5055 | |
| 5056 | if (!s && get_token) s = bc_lex_next(&p->l); |
| 5057 | } |
| 5058 | |
| 5059 | if (s) return s; |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 5060 | if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5061 | |
| 5062 | while (p->ops.len > ops_bgn) { |
| 5063 | |
| 5064 | top = BC_PARSE_TOP_OP(p); |
| 5065 | assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN; |
| 5066 | |
| 5067 | if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5068 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5069 | |
| 5070 | bc_parse_push(p, BC_PARSE_TOKEN_INST(top)); |
| 5071 | |
| 5072 | nexprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG; |
| 5073 | bc_vec_pop(&p->ops); |
| 5074 | } |
| 5075 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5076 | if (prev == BC_INST_BOOL_NOT || nexprs != 1) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5077 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5078 | |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 5079 | // next is BcParseNext, byte array of up to 4 BC_LEX's, packed into 32-bit word |
| 5080 | for (;;) { |
| 5081 | if (t == (next & 0x7f)) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5082 | goto ok; |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 5083 | if (next & 0x80) // last element? |
| 5084 | break; |
| 5085 | next >>= 8; |
| 5086 | } |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5087 | return bc_error_bad_expression(); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5088 | ok: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5089 | |
| 5090 | if (!(flags & BC_PARSE_REL) && nrelops) { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 5091 | s = bc_POSIX_does_not_allow("comparison operators outside if or loops"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5092 | if (s) return s; |
| 5093 | } |
| 5094 | else if ((flags & BC_PARSE_REL) && nrelops > 1) { |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 5095 | s = bc_POSIX_requires("exactly one comparison operator per condition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5096 | if (s) return s; |
| 5097 | } |
| 5098 | |
| 5099 | if (flags & BC_PARSE_PRINT) { |
| 5100 | if (paren_first || !assign) bc_parse_push(p, BC_INST_PRINT); |
| 5101 | bc_parse_push(p, BC_INST_POP); |
| 5102 | } |
| 5103 | |
| 5104 | return s; |
| 5105 | } |
| 5106 | |
Denys Vlasenko | 050b0fe | 2018-12-05 22:40:44 +0100 | [diff] [blame] | 5107 | static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next) |
| 5108 | { |
| 5109 | BcStatus s; |
| 5110 | |
| 5111 | s = bc_parse_expr_empty_ok(p, flags, next); |
| 5112 | if (s == BC_STATUS_PARSE_EMPTY_EXP) |
| 5113 | return bc_error("empty expression"); |
| 5114 | return s; |
| 5115 | } |
| 5116 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5117 | static void bc_parse_init(BcParse *p, size_t func) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5118 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5119 | bc_parse_create(p, func, bc_parse_parse, bc_lex_token); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5120 | } |
| 5121 | |
| 5122 | static BcStatus bc_parse_expression(BcParse *p, uint8_t flags) |
| 5123 | { |
| 5124 | return bc_parse_expr(p, flags, bc_parse_next_read); |
| 5125 | } |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 5126 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5127 | #endif // ENABLE_BC |
| 5128 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5129 | #if ENABLE_DC |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 5130 | |
| 5131 | #define DC_PARSE_BUF_LEN ((int) (sizeof(uint32_t) * CHAR_BIT)) |
| 5132 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5133 | static BcStatus dc_parse_register(BcParse *p) |
| 5134 | { |
| 5135 | BcStatus s; |
| 5136 | char *name; |
| 5137 | |
| 5138 | s = bc_lex_next(&p->l); |
| 5139 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5140 | 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] | 5141 | |
| 5142 | name = xstrdup(p->l.t.v.v); |
| 5143 | bc_parse_pushName(p, name); |
| 5144 | |
| 5145 | return s; |
| 5146 | } |
| 5147 | |
| 5148 | static BcStatus dc_parse_string(BcParse *p) |
| 5149 | { |
| 5150 | char *str, *name, b[DC_PARSE_BUF_LEN + 1]; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5151 | size_t idx, len = G.prog.strs.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5152 | |
| 5153 | sprintf(b, "%0*zu", DC_PARSE_BUF_LEN, len); |
| 5154 | name = xstrdup(b); |
| 5155 | |
| 5156 | str = xstrdup(p->l.t.v.v); |
| 5157 | bc_parse_push(p, BC_INST_STR); |
| 5158 | bc_parse_pushIndex(p, len); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5159 | bc_vec_push(&G.prog.strs, &str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5160 | bc_parse_addFunc(p, name, &idx); |
| 5161 | |
| 5162 | return bc_lex_next(&p->l); |
| 5163 | } |
| 5164 | |
| 5165 | static BcStatus dc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store) |
| 5166 | { |
| 5167 | BcStatus s; |
| 5168 | |
| 5169 | bc_parse_push(p, inst); |
| 5170 | if (name) { |
| 5171 | s = dc_parse_register(p); |
| 5172 | if (s) return s; |
| 5173 | } |
| 5174 | |
| 5175 | if (store) { |
| 5176 | bc_parse_push(p, BC_INST_SWAP); |
| 5177 | bc_parse_push(p, BC_INST_ASSIGN); |
| 5178 | bc_parse_push(p, BC_INST_POP); |
| 5179 | } |
| 5180 | |
| 5181 | return bc_lex_next(&p->l); |
| 5182 | } |
| 5183 | |
| 5184 | static BcStatus dc_parse_cond(BcParse *p, uint8_t inst) |
| 5185 | { |
| 5186 | BcStatus s; |
| 5187 | |
| 5188 | bc_parse_push(p, inst); |
| 5189 | bc_parse_push(p, BC_INST_EXEC_COND); |
| 5190 | |
| 5191 | s = dc_parse_register(p); |
| 5192 | if (s) return s; |
| 5193 | |
| 5194 | s = bc_lex_next(&p->l); |
| 5195 | if (s) return s; |
| 5196 | |
| 5197 | if (p->l.t.t == BC_LEX_ELSE) { |
| 5198 | s = dc_parse_register(p); |
| 5199 | if (s) return s; |
| 5200 | s = bc_lex_next(&p->l); |
| 5201 | } |
| 5202 | else |
| 5203 | bc_parse_push(p, BC_PARSE_STREND); |
| 5204 | |
| 5205 | return s; |
| 5206 | } |
| 5207 | |
| 5208 | static BcStatus dc_parse_token(BcParse *p, BcLexType t, uint8_t flags) |
| 5209 | { |
| 5210 | BcStatus s = BC_STATUS_SUCCESS; |
| 5211 | BcInst prev; |
| 5212 | uint8_t inst; |
| 5213 | bool assign, get_token = false; |
| 5214 | |
| 5215 | switch (t) { |
| 5216 | |
| 5217 | case BC_LEX_OP_REL_EQ: |
| 5218 | case BC_LEX_OP_REL_LE: |
| 5219 | case BC_LEX_OP_REL_GE: |
| 5220 | case BC_LEX_OP_REL_NE: |
| 5221 | case BC_LEX_OP_REL_LT: |
| 5222 | case BC_LEX_OP_REL_GT: |
| 5223 | { |
| 5224 | s = dc_parse_cond(p, t - BC_LEX_OP_REL_EQ + BC_INST_REL_EQ); |
| 5225 | break; |
| 5226 | } |
| 5227 | |
| 5228 | case BC_LEX_SCOLON: |
| 5229 | case BC_LEX_COLON: |
| 5230 | { |
| 5231 | s = dc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON); |
| 5232 | break; |
| 5233 | } |
| 5234 | |
| 5235 | case BC_LEX_STR: |
| 5236 | { |
| 5237 | s = dc_parse_string(p); |
| 5238 | break; |
| 5239 | } |
| 5240 | |
| 5241 | case BC_LEX_NEG: |
| 5242 | case BC_LEX_NUMBER: |
| 5243 | { |
| 5244 | if (t == BC_LEX_NEG) { |
| 5245 | s = bc_lex_next(&p->l); |
| 5246 | if (s) return s; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5247 | if (p->l.t.t != BC_LEX_NUMBER) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5248 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5249 | } |
| 5250 | |
| 5251 | bc_parse_number(p, &prev, &p->nbraces); |
| 5252 | |
| 5253 | if (t == BC_LEX_NEG) bc_parse_push(p, BC_INST_NEG); |
| 5254 | get_token = true; |
| 5255 | |
| 5256 | break; |
| 5257 | } |
| 5258 | |
| 5259 | case BC_LEX_KEY_READ: |
| 5260 | { |
| 5261 | if (flags & BC_PARSE_NOREAD) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5262 | s = bc_error_nested_read_call(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5263 | else |
| 5264 | bc_parse_push(p, BC_INST_READ); |
| 5265 | get_token = true; |
| 5266 | break; |
| 5267 | } |
| 5268 | |
| 5269 | case BC_LEX_OP_ASSIGN: |
| 5270 | case BC_LEX_STORE_PUSH: |
| 5271 | { |
| 5272 | assign = t == BC_LEX_OP_ASSIGN; |
| 5273 | inst = assign ? BC_INST_VAR : BC_INST_PUSH_TO_VAR; |
| 5274 | s = dc_parse_mem(p, inst, true, assign); |
| 5275 | break; |
| 5276 | } |
| 5277 | |
| 5278 | case BC_LEX_LOAD: |
| 5279 | case BC_LEX_LOAD_POP: |
| 5280 | { |
| 5281 | inst = t == BC_LEX_LOAD_POP ? BC_INST_PUSH_VAR : BC_INST_LOAD; |
| 5282 | s = dc_parse_mem(p, inst, true, false); |
| 5283 | break; |
| 5284 | } |
| 5285 | |
| 5286 | case BC_LEX_STORE_IBASE: |
| 5287 | case BC_LEX_STORE_SCALE: |
| 5288 | case BC_LEX_STORE_OBASE: |
| 5289 | { |
| 5290 | inst = t - BC_LEX_STORE_IBASE + BC_INST_IBASE; |
| 5291 | s = dc_parse_mem(p, inst, false, true); |
| 5292 | break; |
| 5293 | } |
| 5294 | |
| 5295 | default: |
| 5296 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5297 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5298 | get_token = true; |
| 5299 | break; |
| 5300 | } |
| 5301 | } |
| 5302 | |
| 5303 | if (!s && get_token) s = bc_lex_next(&p->l); |
| 5304 | |
| 5305 | return s; |
| 5306 | } |
| 5307 | |
| 5308 | static BcStatus dc_parse_expr(BcParse *p, uint8_t flags) |
| 5309 | { |
| 5310 | BcStatus s = BC_STATUS_SUCCESS; |
| 5311 | BcInst inst; |
| 5312 | BcLexType t; |
| 5313 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5314 | if (flags & BC_PARSE_NOCALL) p->nbraces = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5315 | |
| 5316 | for (t = p->l.t.t; !s && t != BC_LEX_EOF; t = p->l.t.t) { |
| 5317 | |
| 5318 | inst = dc_parse_insts[t]; |
| 5319 | |
| 5320 | if (inst != BC_INST_INVALID) { |
| 5321 | bc_parse_push(p, inst); |
| 5322 | s = bc_lex_next(&p->l); |
| 5323 | } |
| 5324 | else |
| 5325 | s = dc_parse_token(p, t, flags); |
| 5326 | } |
| 5327 | |
| 5328 | if (!s && p->l.t.t == BC_LEX_EOF && (flags & BC_PARSE_NOCALL)) |
| 5329 | bc_parse_push(p, BC_INST_POP_EXEC); |
| 5330 | |
| 5331 | return s; |
| 5332 | } |
| 5333 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 5334 | static FAST_FUNC BcStatus dc_parse_parse(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5335 | { |
| 5336 | BcStatus s; |
| 5337 | |
| 5338 | if (p->l.t.t == BC_LEX_EOF) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5339 | s = bc_error("end of file"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5340 | else |
| 5341 | s = dc_parse_expr(p, 0); |
| 5342 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 5343 | if (s || G_interrupt) { |
| 5344 | bc_parse_reset(p); |
| 5345 | s = BC_STATUS_FAILURE; |
| 5346 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5347 | |
| 5348 | return s; |
| 5349 | } |
| 5350 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5351 | static void dc_parse_init(BcParse *p, size_t func) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5352 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5353 | bc_parse_create(p, func, dc_parse_parse, dc_lex_token); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5354 | } |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 5355 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5356 | #endif // ENABLE_DC |
| 5357 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5358 | static void common_parse_init(BcParse *p, size_t func) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5359 | { |
| 5360 | if (IS_BC) { |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 5361 | IF_BC(bc_parse_init(p, func);) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5362 | } else { |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 5363 | IF_DC(dc_parse_init(p, func);) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5364 | } |
| 5365 | } |
| 5366 | |
| 5367 | static BcStatus common_parse_expr(BcParse *p, uint8_t flags) |
| 5368 | { |
| 5369 | if (IS_BC) { |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 5370 | IF_BC(return bc_parse_expression(p, flags);) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5371 | } else { |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 5372 | IF_DC(return dc_parse_expr(p, flags);) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5373 | } |
| 5374 | } |
| 5375 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5376 | static BcVec* bc_program_search(char *id, bool var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5377 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5378 | BcId e, *ptr; |
| 5379 | BcVec *v, *map; |
| 5380 | size_t i; |
| 5381 | BcResultData data; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 5382 | int new; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5383 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5384 | v = var ? &G.prog.vars : &G.prog.arrs; |
| 5385 | map = var ? &G.prog.var_map : &G.prog.arr_map; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5386 | |
| 5387 | e.name = id; |
| 5388 | e.idx = v->len; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 5389 | new = bc_map_insert(map, &e, &i); // 1 if insertion was successful |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5390 | |
| 5391 | if (new) { |
| 5392 | bc_array_init(&data.v, var); |
| 5393 | bc_vec_push(v, &data.v); |
| 5394 | } |
| 5395 | |
| 5396 | ptr = bc_vec_item(map, i); |
| 5397 | if (new) ptr->name = xstrdup(e.name); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5398 | return bc_vec_item(v, ptr->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5399 | } |
| 5400 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5401 | static BcStatus bc_program_num(BcResult *r, BcNum **num, bool hex) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5402 | { |
| 5403 | BcStatus s = BC_STATUS_SUCCESS; |
| 5404 | |
| 5405 | switch (r->t) { |
| 5406 | |
| 5407 | case BC_RESULT_STR: |
| 5408 | case BC_RESULT_TEMP: |
| 5409 | case BC_RESULT_IBASE: |
| 5410 | case BC_RESULT_SCALE: |
| 5411 | case BC_RESULT_OBASE: |
| 5412 | { |
| 5413 | *num = &r->d.n; |
| 5414 | break; |
| 5415 | } |
| 5416 | |
| 5417 | case BC_RESULT_CONSTANT: |
| 5418 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5419 | char **str = bc_vec_item(&G.prog.consts, r->d.id.idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5420 | size_t base_t, len = strlen(*str); |
| 5421 | BcNum *base; |
| 5422 | |
| 5423 | bc_num_init(&r->d.n, len); |
| 5424 | |
| 5425 | hex = hex && len == 1; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5426 | base = hex ? &G.prog.hexb : &G.prog.ib; |
| 5427 | base_t = hex ? BC_NUM_MAX_IBASE : G.prog.ib_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5428 | s = bc_num_parse(&r->d.n, *str, base, base_t); |
| 5429 | |
| 5430 | if (s) { |
| 5431 | bc_num_free(&r->d.n); |
| 5432 | return s; |
| 5433 | } |
| 5434 | |
| 5435 | *num = &r->d.n; |
| 5436 | r->t = BC_RESULT_TEMP; |
| 5437 | |
| 5438 | break; |
| 5439 | } |
| 5440 | |
| 5441 | case BC_RESULT_VAR: |
| 5442 | case BC_RESULT_ARRAY: |
| 5443 | case BC_RESULT_ARRAY_ELEM: |
| 5444 | { |
| 5445 | BcVec *v; |
| 5446 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5447 | 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] | 5448 | |
| 5449 | if (r->t == BC_RESULT_ARRAY_ELEM) { |
| 5450 | v = bc_vec_top(v); |
| 5451 | if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1); |
| 5452 | *num = bc_vec_item(v, r->d.id.idx); |
| 5453 | } |
| 5454 | else |
| 5455 | *num = bc_vec_top(v); |
| 5456 | |
| 5457 | break; |
| 5458 | } |
| 5459 | |
| 5460 | case BC_RESULT_LAST: |
| 5461 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5462 | *num = &G.prog.last; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5463 | break; |
| 5464 | } |
| 5465 | |
| 5466 | case BC_RESULT_ONE: |
| 5467 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5468 | *num = &G.prog.one; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5469 | break; |
| 5470 | } |
| 5471 | } |
| 5472 | |
| 5473 | return s; |
| 5474 | } |
| 5475 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5476 | static BcStatus bc_program_binOpPrep(BcResult **l, BcNum **ln, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5477 | BcResult **r, BcNum **rn, bool assign) |
| 5478 | { |
| 5479 | BcStatus s; |
| 5480 | bool hex; |
| 5481 | BcResultType lt, rt; |
| 5482 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5483 | if (!BC_PROG_STACK(&G.prog.results, 2)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5484 | return bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5485 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5486 | *r = bc_vec_item_rev(&G.prog.results, 0); |
| 5487 | *l = bc_vec_item_rev(&G.prog.results, 1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5488 | |
| 5489 | lt = (*l)->t; |
| 5490 | rt = (*r)->t; |
| 5491 | hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE); |
| 5492 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5493 | s = bc_program_num(*l, ln, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5494 | if (s) return s; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5495 | s = bc_program_num(*r, rn, hex); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5496 | if (s) return s; |
| 5497 | |
| 5498 | // We run this again under these conditions in case any vector has been |
| 5499 | // reallocated out from under the BcNums or arrays we had. |
| 5500 | if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5501 | s = bc_program_num(*l, ln, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5502 | if (s) return s; |
| 5503 | } |
| 5504 | |
| 5505 | if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5506 | return bc_error_variable_is_wrong_type(); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5507 | if (!assign && !BC_PROG_NUM((*r), (*ln))) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5508 | return bc_error_variable_is_wrong_type(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5509 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5510 | return s; |
| 5511 | } |
| 5512 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5513 | static void bc_program_binOpRetire(BcResult *r) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5514 | { |
| 5515 | r->t = BC_RESULT_TEMP; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5516 | bc_vec_pop(&G.prog.results); |
| 5517 | bc_vec_pop(&G.prog.results); |
| 5518 | bc_vec_push(&G.prog.results, r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5519 | } |
| 5520 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5521 | static BcStatus bc_program_prep(BcResult **r, BcNum **n) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5522 | { |
| 5523 | BcStatus s; |
| 5524 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5525 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5526 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5527 | *r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5528 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5529 | s = bc_program_num(*r, n, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5530 | if (s) return s; |
| 5531 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5532 | if (!BC_PROG_NUM((*r), (*n))) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5533 | return bc_error_variable_is_wrong_type(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5534 | |
| 5535 | return s; |
| 5536 | } |
| 5537 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5538 | static void bc_program_retire(BcResult *r, BcResultType t) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5539 | { |
| 5540 | r->t = t; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5541 | bc_vec_pop(&G.prog.results); |
| 5542 | bc_vec_push(&G.prog.results, r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5543 | } |
| 5544 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5545 | static BcStatus bc_program_op(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5546 | { |
| 5547 | BcStatus s; |
| 5548 | BcResult *opd1, *opd2, res; |
| 5549 | BcNum *n1, *n2 = NULL; |
| 5550 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5551 | s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5552 | if (s) return s; |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5553 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5554 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5555 | s = bc_program_ops[inst - BC_INST_POWER](n1, n2, &res.d.n, G.prog.scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5556 | if (s) goto err; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5557 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5558 | |
| 5559 | return s; |
| 5560 | |
| 5561 | err: |
| 5562 | bc_num_free(&res.d.n); |
| 5563 | return s; |
| 5564 | } |
| 5565 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 5566 | static BcStatus bc_program_read(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5567 | { |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5568 | const char *sv_file; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5569 | BcStatus s; |
| 5570 | BcParse parse; |
| 5571 | BcVec buf; |
| 5572 | BcInstPtr ip; |
| 5573 | size_t i; |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 5574 | BcFunc *f = bc_program_func(BC_PROG_READ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5575 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5576 | for (i = 0; i < G.prog.stack.len; ++i) { |
| 5577 | BcInstPtr *ip_ptr = bc_vec_item(&G.prog.stack, i); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5578 | if (ip_ptr->func == BC_PROG_READ) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5579 | return bc_error_nested_read_call(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5580 | } |
| 5581 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 5582 | bc_vec_pop_all(&f->code); |
| 5583 | bc_char_vec_init(&buf); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5584 | |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5585 | sv_file = G.prog.file; |
| 5586 | G.prog.file = NULL; |
| 5587 | |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 5588 | s = bc_read_line(&buf); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5589 | if (s) goto io_err; |
| 5590 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5591 | common_parse_init(&parse, BC_PROG_READ); |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5592 | bc_lex_file(&parse.l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5593 | |
| 5594 | s = bc_parse_text(&parse, buf.v); |
| 5595 | if (s) goto exec_err; |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5596 | s = common_parse_expr(&parse, BC_PARSE_NOREAD); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5597 | if (s) goto exec_err; |
| 5598 | |
| 5599 | 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] | 5600 | s = bc_error("bad read() expression"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5601 | goto exec_err; |
| 5602 | } |
| 5603 | |
| 5604 | ip.func = BC_PROG_READ; |
| 5605 | ip.idx = 0; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5606 | ip.len = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5607 | |
| 5608 | // Update this pointer, just in case. |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 5609 | f = bc_program_func(BC_PROG_READ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5610 | |
| 5611 | bc_vec_pushByte(&f->code, BC_INST_POP_EXEC); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5612 | bc_vec_push(&G.prog.stack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5613 | |
| 5614 | exec_err: |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5615 | G.prog.file = sv_file; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5616 | bc_parse_free(&parse); |
| 5617 | io_err: |
| 5618 | bc_vec_free(&buf); |
| 5619 | return s; |
| 5620 | } |
| 5621 | |
| 5622 | static size_t bc_program_index(char *code, size_t *bgn) |
| 5623 | { |
| 5624 | char amt = code[(*bgn)++], i = 0; |
| 5625 | size_t res = 0; |
| 5626 | |
| 5627 | for (; i < amt; ++i, ++(*bgn)) |
| 5628 | res |= (((size_t)((int) code[*bgn]) & UCHAR_MAX) << (i * CHAR_BIT)); |
| 5629 | |
| 5630 | return res; |
| 5631 | } |
| 5632 | |
| 5633 | static char *bc_program_name(char *code, size_t *bgn) |
| 5634 | { |
| 5635 | size_t i; |
| 5636 | char c, *s, *str = code + *bgn, *ptr = strchr(str, BC_PARSE_STREND); |
| 5637 | |
| 5638 | s = xmalloc(ptr - str + 1); |
| 5639 | c = code[(*bgn)++]; |
| 5640 | |
| 5641 | for (i = 0; c != 0 && c != BC_PARSE_STREND; c = code[(*bgn)++], ++i) |
| 5642 | s[i] = c; |
| 5643 | |
| 5644 | s[i] = '\0'; |
| 5645 | |
| 5646 | return s; |
| 5647 | } |
| 5648 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 5649 | static void bc_program_printString(const char *str) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5650 | { |
| 5651 | size_t i, len = strlen(str); |
| 5652 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5653 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5654 | if (len == 0) { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5655 | bb_putchar('\0'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5656 | return; |
| 5657 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5658 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5659 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 5660 | for (i = 0; i < len; ++i, ++G.prog.nchars) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5661 | |
| 5662 | int c = str[i]; |
| 5663 | |
| 5664 | if (c != '\\' || i == len - 1) |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5665 | bb_putchar(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5666 | else { |
| 5667 | |
| 5668 | c = str[++i]; |
| 5669 | |
| 5670 | switch (c) { |
| 5671 | |
| 5672 | case 'a': |
| 5673 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5674 | bb_putchar('\a'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5675 | break; |
| 5676 | } |
| 5677 | |
| 5678 | case 'b': |
| 5679 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5680 | bb_putchar('\b'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5681 | break; |
| 5682 | } |
| 5683 | |
| 5684 | case '\\': |
| 5685 | case 'e': |
| 5686 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5687 | bb_putchar('\\'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5688 | break; |
| 5689 | } |
| 5690 | |
| 5691 | case 'f': |
| 5692 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5693 | bb_putchar('\f'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5694 | break; |
| 5695 | } |
| 5696 | |
| 5697 | case 'n': |
| 5698 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5699 | bb_putchar('\n'); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 5700 | G.prog.nchars = SIZE_MAX; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5701 | break; |
| 5702 | } |
| 5703 | |
| 5704 | case 'r': |
| 5705 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5706 | bb_putchar('\r'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5707 | break; |
| 5708 | } |
| 5709 | |
| 5710 | case 'q': |
| 5711 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5712 | bb_putchar('"'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5713 | break; |
| 5714 | } |
| 5715 | |
| 5716 | case 't': |
| 5717 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5718 | bb_putchar('\t'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5719 | break; |
| 5720 | } |
| 5721 | |
| 5722 | default: |
| 5723 | { |
| 5724 | // Just print the backslash and following character. |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5725 | bb_putchar('\\'); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 5726 | ++G.prog.nchars; |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5727 | bb_putchar(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5728 | break; |
| 5729 | } |
| 5730 | } |
| 5731 | } |
| 5732 | } |
| 5733 | } |
| 5734 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5735 | static BcStatus bc_program_print(char inst, size_t idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5736 | { |
| 5737 | BcStatus s = BC_STATUS_SUCCESS; |
| 5738 | BcResult *r; |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5739 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5740 | bool pop = inst != BC_INST_PRINT; |
| 5741 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5742 | if (!BC_PROG_STACK(&G.prog.results, idx + 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5743 | return bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5744 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5745 | r = bc_vec_item_rev(&G.prog.results, idx); |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5746 | num = NULL; // is this NULL necessary? |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5747 | s = bc_program_num(r, &num, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5748 | if (s) return s; |
| 5749 | |
| 5750 | if (BC_PROG_NUM(r, num)) { |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 5751 | s = bc_num_print(num, !pop); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5752 | if (!s) bc_num_copy(&G.prog.last, num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5753 | } |
| 5754 | else { |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5755 | char *str; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5756 | |
| 5757 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx; |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 5758 | str = *bc_program_str(idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5759 | |
| 5760 | if (inst == BC_INST_PRINT_STR) { |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5761 | for (;;) { |
| 5762 | char c = *str++; |
| 5763 | if (c == '\0') break; |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5764 | bb_putchar(c); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5765 | ++G.prog.nchars; |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5766 | if (c == '\n') G.prog.nchars = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5767 | } |
| 5768 | } |
| 5769 | else { |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 5770 | bc_program_printString(str); |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5771 | if (inst == BC_INST_PRINT) bb_putchar('\n'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5772 | } |
| 5773 | } |
| 5774 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5775 | if (!s && pop) bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5776 | |
| 5777 | return s; |
| 5778 | } |
| 5779 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5780 | static BcStatus bc_program_negate(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5781 | { |
| 5782 | BcStatus s; |
| 5783 | BcResult res, *ptr; |
| 5784 | BcNum *num = NULL; |
| 5785 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5786 | s = bc_program_prep(&ptr, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5787 | if (s) return s; |
| 5788 | |
| 5789 | bc_num_init(&res.d.n, num->len); |
| 5790 | bc_num_copy(&res.d.n, num); |
| 5791 | if (res.d.n.len) res.d.n.neg = !res.d.n.neg; |
| 5792 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5793 | bc_program_retire(&res, BC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5794 | |
| 5795 | return s; |
| 5796 | } |
| 5797 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5798 | static BcStatus bc_program_logical(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5799 | { |
| 5800 | BcStatus s; |
| 5801 | BcResult *opd1, *opd2, res; |
| 5802 | BcNum *n1, *n2; |
| 5803 | bool cond = 0; |
| 5804 | ssize_t cmp; |
| 5805 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5806 | s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5807 | if (s) return s; |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5808 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5809 | |
| 5810 | if (inst == BC_INST_BOOL_AND) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5811 | 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] | 5812 | else if (inst == BC_INST_BOOL_OR) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5813 | 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] | 5814 | else { |
| 5815 | |
| 5816 | cmp = bc_num_cmp(n1, n2); |
| 5817 | |
| 5818 | switch (inst) { |
| 5819 | |
| 5820 | case BC_INST_REL_EQ: |
| 5821 | { |
| 5822 | cond = cmp == 0; |
| 5823 | break; |
| 5824 | } |
| 5825 | |
| 5826 | case BC_INST_REL_LE: |
| 5827 | { |
| 5828 | cond = cmp <= 0; |
| 5829 | break; |
| 5830 | } |
| 5831 | |
| 5832 | case BC_INST_REL_GE: |
| 5833 | { |
| 5834 | cond = cmp >= 0; |
| 5835 | break; |
| 5836 | } |
| 5837 | |
| 5838 | case BC_INST_REL_NE: |
| 5839 | { |
| 5840 | cond = cmp != 0; |
| 5841 | break; |
| 5842 | } |
| 5843 | |
| 5844 | case BC_INST_REL_LT: |
| 5845 | { |
| 5846 | cond = cmp < 0; |
| 5847 | break; |
| 5848 | } |
| 5849 | |
| 5850 | case BC_INST_REL_GT: |
| 5851 | { |
| 5852 | cond = cmp > 0; |
| 5853 | break; |
| 5854 | } |
| 5855 | } |
| 5856 | } |
| 5857 | |
| 5858 | (cond ? bc_num_one : bc_num_zero)(&res.d.n); |
| 5859 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5860 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5861 | |
| 5862 | return s; |
| 5863 | } |
| 5864 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5865 | #if ENABLE_DC |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5866 | static BcStatus bc_program_assignStr(BcResult *r, BcVec *v, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5867 | bool push) |
| 5868 | { |
| 5869 | BcNum n2; |
| 5870 | BcResult res; |
| 5871 | |
| 5872 | memset(&n2, 0, sizeof(BcNum)); |
| 5873 | n2.rdx = res.d.id.idx = r->d.id.idx; |
| 5874 | res.t = BC_RESULT_STR; |
| 5875 | |
| 5876 | if (!push) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5877 | if (!BC_PROG_STACK(&G.prog.results, 2)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5878 | return bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5879 | bc_vec_pop(v); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5880 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5881 | } |
| 5882 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5883 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5884 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5885 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5886 | bc_vec_push(v, &n2); |
| 5887 | |
| 5888 | return BC_STATUS_SUCCESS; |
| 5889 | } |
| 5890 | #endif // ENABLE_DC |
| 5891 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5892 | static BcStatus bc_program_copyToVar(char *name, bool var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5893 | { |
| 5894 | BcStatus s; |
| 5895 | BcResult *ptr, r; |
| 5896 | BcVec *v; |
| 5897 | BcNum *n; |
| 5898 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5899 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5900 | return bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5901 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5902 | ptr = bc_vec_top(&G.prog.results); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5903 | if ((ptr->t == BC_RESULT_ARRAY) != !var) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5904 | return bc_error_variable_is_wrong_type(); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5905 | v = bc_program_search(name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5906 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5907 | #if ENABLE_DC |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5908 | if (ptr->t == BC_RESULT_STR && !var) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5909 | return bc_error_variable_is_wrong_type(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5910 | if (ptr->t == BC_RESULT_STR) return bc_program_assignStr(ptr, v, true); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5911 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5912 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5913 | s = bc_program_num(ptr, &n, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5914 | if (s) return s; |
| 5915 | |
| 5916 | // Do this once more to make sure that pointers were not invalidated. |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5917 | v = bc_program_search(name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5918 | |
| 5919 | if (var) { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5920 | bc_num_init_DEF_SIZE(&r.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5921 | bc_num_copy(&r.d.n, n); |
| 5922 | } |
| 5923 | else { |
| 5924 | bc_array_init(&r.d.v, true); |
| 5925 | bc_array_copy(&r.d.v, (BcVec *) n); |
| 5926 | } |
| 5927 | |
| 5928 | bc_vec_push(v, &r.d); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5929 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5930 | |
| 5931 | return s; |
| 5932 | } |
| 5933 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5934 | static BcStatus bc_program_assign(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5935 | { |
| 5936 | BcStatus s; |
| 5937 | BcResult *left, *right, res; |
| 5938 | BcNum *l = NULL, *r = NULL; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5939 | bool assign = inst == BC_INST_ASSIGN, ib, sc; |
| 5940 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5941 | s = bc_program_binOpPrep(&left, &l, &right, &r, assign); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5942 | if (s) return s; |
| 5943 | |
| 5944 | ib = left->t == BC_RESULT_IBASE; |
| 5945 | sc = left->t == BC_RESULT_SCALE; |
| 5946 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5947 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5948 | |
| 5949 | if (right->t == BC_RESULT_STR) { |
| 5950 | |
| 5951 | BcVec *v; |
| 5952 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5953 | if (left->t != BC_RESULT_VAR) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5954 | return bc_error_variable_is_wrong_type(); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5955 | v = bc_program_search(left->d.id.name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5956 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5957 | return bc_program_assignStr(right, v, false); |
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 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5960 | |
| 5961 | if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5962 | return bc_error("bad assignment:" |
| 5963 | " left side must be scale," |
| 5964 | " ibase, obase, last, var," |
| 5965 | " or array element" |
| 5966 | ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5967 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5968 | #if ENABLE_BC |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5969 | if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero)) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5970 | return bc_error("divide by zero"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5971 | |
| 5972 | if (assign) |
| 5973 | bc_num_copy(l, r); |
| 5974 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5975 | s = bc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5976 | |
| 5977 | if (s) return s; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5978 | #else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5979 | bc_num_copy(l, r); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5980 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5981 | |
| 5982 | if (ib || sc || left->t == BC_RESULT_OBASE) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5983 | static const char *const msg[] = { |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 5984 | "bad ibase; must be [2,16]", //BC_RESULT_IBASE |
| 5985 | "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE |
| 5986 | NULL, //can't happen //BC_RESULT_LAST |
| 5987 | NULL, //can't happen //BC_RESULT_CONSTANT |
| 5988 | NULL, //can't happen //BC_RESULT_ONE |
| 5989 | "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5990 | }; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5991 | size_t *ptr; |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 5992 | unsigned long val, max; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5993 | |
| 5994 | s = bc_num_ulong(l, &val); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5995 | if (s) |
| 5996 | return s; |
| 5997 | s = left->t - BC_RESULT_IBASE; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5998 | if (sc) { |
| 5999 | max = BC_MAX_SCALE; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6000 | ptr = &G.prog.scale; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6001 | } |
| 6002 | else { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6003 | if (val < BC_NUM_MIN_BASE) |
| 6004 | return bc_error(msg[s]); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6005 | max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6006 | ptr = ib ? &G.prog.ib_t : &G.prog.ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6007 | } |
| 6008 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6009 | if (val > max) |
| 6010 | return bc_error(msg[s]); |
| 6011 | if (!sc) |
| 6012 | bc_num_copy(ib ? &G.prog.ib : &G.prog.ob, l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6013 | |
| 6014 | *ptr = (size_t) val; |
| 6015 | s = BC_STATUS_SUCCESS; |
| 6016 | } |
| 6017 | |
| 6018 | bc_num_init(&res.d.n, l->len); |
| 6019 | bc_num_copy(&res.d.n, l); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6020 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6021 | |
| 6022 | return s; |
| 6023 | } |
| 6024 | |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6025 | #if !ENABLE_DC |
| 6026 | #define bc_program_pushVar(code, bgn, pop, copy) \ |
| 6027 | bc_program_pushVar(code, bgn) |
| 6028 | // for bc, 'pop' and 'copy' are always false |
| 6029 | #endif |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6030 | static BcStatus bc_program_pushVar(char *code, size_t *bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6031 | bool pop, bool copy) |
| 6032 | { |
| 6033 | BcStatus s = BC_STATUS_SUCCESS; |
| 6034 | BcResult r; |
| 6035 | char *name = bc_program_name(code, bgn); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6036 | |
| 6037 | r.t = BC_RESULT_VAR; |
| 6038 | r.d.id.name = name; |
| 6039 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6040 | #if ENABLE_DC |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6041 | { |
| 6042 | BcVec *v = bc_program_search(name, true); |
| 6043 | BcNum *num = bc_vec_top(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6044 | |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6045 | if (pop || copy) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6046 | |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6047 | if (!BC_PROG_STACK(v, 2 - copy)) { |
| 6048 | free(name); |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6049 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6050 | } |
| 6051 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6052 | free(name); |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6053 | name = NULL; |
| 6054 | |
| 6055 | if (!BC_PROG_STR(num)) { |
| 6056 | |
| 6057 | r.t = BC_RESULT_TEMP; |
| 6058 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6059 | bc_num_init_DEF_SIZE(&r.d.n); |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6060 | bc_num_copy(&r.d.n, num); |
| 6061 | } |
| 6062 | else { |
| 6063 | r.t = BC_RESULT_STR; |
| 6064 | r.d.id.idx = num->rdx; |
| 6065 | } |
| 6066 | |
| 6067 | if (!copy) bc_vec_pop(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6068 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6069 | } |
| 6070 | #endif // ENABLE_DC |
| 6071 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6072 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6073 | |
| 6074 | return s; |
| 6075 | } |
| 6076 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6077 | static BcStatus bc_program_pushArray(char *code, size_t *bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6078 | char inst) |
| 6079 | { |
| 6080 | BcStatus s = BC_STATUS_SUCCESS; |
| 6081 | BcResult r; |
| 6082 | BcNum *num; |
| 6083 | |
| 6084 | r.d.id.name = bc_program_name(code, bgn); |
| 6085 | |
| 6086 | if (inst == BC_INST_ARRAY) { |
| 6087 | r.t = BC_RESULT_ARRAY; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6088 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6089 | } |
| 6090 | else { |
| 6091 | |
| 6092 | BcResult *operand; |
| 6093 | unsigned long temp; |
| 6094 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6095 | s = bc_program_prep(&operand, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6096 | if (s) goto err; |
| 6097 | s = bc_num_ulong(num, &temp); |
| 6098 | if (s) goto err; |
| 6099 | |
| 6100 | if (temp > BC_MAX_DIM) { |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 6101 | 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] | 6102 | goto err; |
| 6103 | } |
| 6104 | |
| 6105 | r.d.id.idx = (size_t) temp; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6106 | bc_program_retire(&r, BC_RESULT_ARRAY_ELEM); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6107 | } |
| 6108 | |
| 6109 | err: |
| 6110 | if (s) free(r.d.id.name); |
| 6111 | return s; |
| 6112 | } |
| 6113 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6114 | #if ENABLE_BC |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6115 | static BcStatus bc_program_incdec(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6116 | { |
| 6117 | BcStatus s; |
| 6118 | BcResult *ptr, res, copy; |
| 6119 | BcNum *num = NULL; |
| 6120 | char inst2 = inst; |
| 6121 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6122 | s = bc_program_prep(&ptr, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6123 | if (s) return s; |
| 6124 | |
| 6125 | if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) { |
| 6126 | copy.t = BC_RESULT_TEMP; |
| 6127 | bc_num_init(©.d.n, num->len); |
| 6128 | bc_num_copy(©.d.n, num); |
| 6129 | } |
| 6130 | |
| 6131 | res.t = BC_RESULT_ONE; |
| 6132 | inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ? |
| 6133 | BC_INST_ASSIGN_PLUS : |
| 6134 | BC_INST_ASSIGN_MINUS; |
| 6135 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6136 | bc_vec_push(&G.prog.results, &res); |
| 6137 | bc_program_assign(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6138 | |
| 6139 | if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6140 | bc_vec_pop(&G.prog.results); |
| 6141 | bc_vec_push(&G.prog.results, ©); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6142 | } |
| 6143 | |
| 6144 | return s; |
| 6145 | } |
| 6146 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6147 | static BcStatus bc_program_call(char *code, size_t *idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6148 | { |
| 6149 | BcStatus s = BC_STATUS_SUCCESS; |
| 6150 | BcInstPtr ip; |
| 6151 | size_t i, nparams = bc_program_index(code, idx); |
| 6152 | BcFunc *func; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6153 | BcId *a; |
| 6154 | BcResultData param; |
| 6155 | BcResult *arg; |
| 6156 | |
| 6157 | ip.idx = 0; |
| 6158 | ip.func = bc_program_index(code, idx); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6159 | func = bc_program_func(ip.func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6160 | |
Denys Vlasenko | 04a1c76 | 2018-12-03 21:10:57 +0100 | [diff] [blame] | 6161 | if (func->code.len == 0) { |
| 6162 | return bc_error("undefined function"); |
| 6163 | } |
| 6164 | if (nparams != func->nparams) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6165 | return 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] | 6166 | } |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6167 | ip.len = G.prog.results.len - nparams; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6168 | |
| 6169 | for (i = 0; i < nparams; ++i) { |
| 6170 | |
| 6171 | a = bc_vec_item(&func->autos, nparams - 1 - i); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6172 | arg = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6173 | |
| 6174 | if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6175 | return bc_error_variable_is_wrong_type(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6176 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6177 | s = bc_program_copyToVar(a->name, a->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6178 | if (s) return s; |
| 6179 | } |
| 6180 | |
| 6181 | for (; i < func->autos.len; ++i) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6182 | BcVec *v; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6183 | |
| 6184 | a = bc_vec_item(&func->autos, i); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6185 | v = bc_program_search(a->name, a->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6186 | |
| 6187 | if (a->idx) { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6188 | bc_num_init_DEF_SIZE(¶m.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6189 | bc_vec_push(v, ¶m.n); |
| 6190 | } |
| 6191 | else { |
| 6192 | bc_array_init(¶m.v, true); |
| 6193 | bc_vec_push(v, ¶m.v); |
| 6194 | } |
| 6195 | } |
| 6196 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6197 | bc_vec_push(&G.prog.stack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6198 | |
| 6199 | return BC_STATUS_SUCCESS; |
| 6200 | } |
| 6201 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6202 | static BcStatus bc_program_return(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6203 | { |
| 6204 | BcStatus s; |
| 6205 | BcResult res; |
| 6206 | BcFunc *f; |
| 6207 | size_t i; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6208 | BcInstPtr *ip = bc_vec_top(&G.prog.stack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6209 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6210 | if (!BC_PROG_STACK(&G.prog.results, ip->len + inst == BC_INST_RET)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6211 | return bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6212 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6213 | f = bc_program_func(ip->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6214 | res.t = BC_RESULT_TEMP; |
| 6215 | |
| 6216 | if (inst == BC_INST_RET) { |
| 6217 | |
| 6218 | BcNum *num; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6219 | BcResult *operand = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6220 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6221 | s = bc_program_num(operand, &num, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6222 | if (s) return s; |
| 6223 | bc_num_init(&res.d.n, num->len); |
| 6224 | bc_num_copy(&res.d.n, num); |
| 6225 | } |
| 6226 | else { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6227 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 6228 | //bc_num_zero(&res.d.n); - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6229 | } |
| 6230 | |
| 6231 | // We need to pop arguments as well, so this takes that into account. |
| 6232 | for (i = 0; i < f->autos.len; ++i) { |
| 6233 | |
| 6234 | BcVec *v; |
| 6235 | BcId *a = bc_vec_item(&f->autos, i); |
| 6236 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6237 | v = bc_program_search(a->name, a->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6238 | bc_vec_pop(v); |
| 6239 | } |
| 6240 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6241 | bc_vec_npop(&G.prog.results, G.prog.results.len - ip->len); |
| 6242 | bc_vec_push(&G.prog.results, &res); |
| 6243 | bc_vec_pop(&G.prog.stack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6244 | |
| 6245 | return BC_STATUS_SUCCESS; |
| 6246 | } |
| 6247 | #endif // ENABLE_BC |
| 6248 | |
| 6249 | static unsigned long bc_program_scale(BcNum *n) |
| 6250 | { |
| 6251 | return (unsigned long) n->rdx; |
| 6252 | } |
| 6253 | |
| 6254 | static unsigned long bc_program_len(BcNum *n) |
| 6255 | { |
Denys Vlasenko | a7f1a36 | 2018-12-10 12:57:01 +0100 | [diff] [blame] | 6256 | size_t len = n->len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6257 | |
Denys Vlasenko | a7f1a36 | 2018-12-10 12:57:01 +0100 | [diff] [blame] | 6258 | if (n->rdx != len) return len; |
| 6259 | for (;;) { |
| 6260 | if (len == 0) break; |
| 6261 | len--; |
| 6262 | if (n->num[len] != 0) break; |
| 6263 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6264 | return len; |
| 6265 | } |
| 6266 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6267 | static BcStatus bc_program_builtin(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6268 | { |
| 6269 | BcStatus s; |
| 6270 | BcResult *opnd; |
| 6271 | BcNum *num = NULL; |
| 6272 | BcResult res; |
| 6273 | bool len = inst == BC_INST_LENGTH; |
| 6274 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6275 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6276 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6277 | opnd = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6278 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6279 | s = bc_program_num(opnd, &num, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6280 | if (s) return s; |
| 6281 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6282 | #if ENABLE_DC |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6283 | if (!BC_PROG_NUM(opnd, num) && !len) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6284 | return bc_error_variable_is_wrong_type(); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6285 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6286 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6287 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6288 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6289 | if (inst == BC_INST_SQRT) s = bc_num_sqrt(num, &res.d.n, G.prog.scale); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6290 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6291 | else if (len != 0 && opnd->t == BC_RESULT_ARRAY) { |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6292 | bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6293 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6294 | #endif |
| 6295 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6296 | else if (len != 0 && !BC_PROG_NUM(opnd, num)) { |
| 6297 | |
| 6298 | char **str; |
| 6299 | size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx; |
| 6300 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6301 | str = bc_program_str(idx); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6302 | bc_num_ulong2num(&res.d.n, strlen(*str)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6303 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6304 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6305 | else { |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame^] | 6306 | 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] | 6307 | } |
| 6308 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6309 | bc_program_retire(&res, BC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6310 | |
| 6311 | return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6312 | } |
| 6313 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6314 | #if ENABLE_DC |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6315 | static BcStatus bc_program_divmod(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6316 | { |
| 6317 | BcStatus s; |
| 6318 | BcResult *opd1, *opd2, res, res2; |
| 6319 | BcNum *n1, *n2 = NULL; |
| 6320 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6321 | s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6322 | if (s) return s; |
| 6323 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6324 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6325 | bc_num_init(&res2.d.n, n2->len); |
| 6326 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6327 | s = bc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6328 | if (s) goto err; |
| 6329 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6330 | bc_program_binOpRetire(&res2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6331 | res.t = BC_RESULT_TEMP; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6332 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6333 | |
| 6334 | return s; |
| 6335 | |
| 6336 | err: |
| 6337 | bc_num_free(&res2.d.n); |
| 6338 | bc_num_free(&res.d.n); |
| 6339 | return s; |
| 6340 | } |
| 6341 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6342 | static BcStatus bc_program_modexp(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6343 | { |
| 6344 | BcStatus s; |
| 6345 | BcResult *r1, *r2, *r3, res; |
| 6346 | BcNum *n1, *n2, *n3; |
| 6347 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6348 | if (!BC_PROG_STACK(&G.prog.results, 3)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6349 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6350 | s = bc_program_binOpPrep(&r2, &n2, &r3, &n3, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6351 | if (s) return s; |
| 6352 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6353 | r1 = bc_vec_item_rev(&G.prog.results, 2); |
| 6354 | s = bc_program_num(r1, &n1, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6355 | if (s) return s; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6356 | if (!BC_PROG_NUM(r1, n1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6357 | return bc_error_variable_is_wrong_type(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6358 | |
| 6359 | // Make sure that the values have their pointers updated, if necessary. |
| 6360 | if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) { |
| 6361 | |
| 6362 | if (r1->t == r2->t) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6363 | s = bc_program_num(r2, &n2, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6364 | if (s) return s; |
| 6365 | } |
| 6366 | |
| 6367 | if (r1->t == r3->t) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6368 | s = bc_program_num(r3, &n3, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6369 | if (s) return s; |
| 6370 | } |
| 6371 | } |
| 6372 | |
| 6373 | bc_num_init(&res.d.n, n3->len); |
| 6374 | s = bc_num_modexp(n1, n2, n3, &res.d.n); |
| 6375 | if (s) goto err; |
| 6376 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6377 | bc_vec_pop(&G.prog.results); |
| 6378 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6379 | |
| 6380 | return s; |
| 6381 | |
| 6382 | err: |
| 6383 | bc_num_free(&res.d.n); |
| 6384 | return s; |
| 6385 | } |
| 6386 | |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6387 | static void bc_program_stackLen(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6388 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6389 | BcResult res; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6390 | size_t len = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6391 | |
| 6392 | res.t = BC_RESULT_TEMP; |
| 6393 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6394 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6395 | bc_num_ulong2num(&res.d.n, len); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6396 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6397 | } |
| 6398 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6399 | static BcStatus bc_program_asciify(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6400 | { |
| 6401 | BcStatus s; |
| 6402 | BcResult *r, res; |
Denys Vlasenko | 4a024c7 | 2018-12-09 13:21:54 +0100 | [diff] [blame] | 6403 | BcNum *num, n; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6404 | char *str, *str2, c; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6405 | size_t len = G.prog.strs.len, idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6406 | unsigned long val; |
| 6407 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6408 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6409 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6410 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6411 | |
Denys Vlasenko | 4a024c7 | 2018-12-09 13:21:54 +0100 | [diff] [blame] | 6412 | num = NULL; // TODO: is this NULL needed? |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6413 | s = bc_program_num(r, &num, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6414 | if (s) return s; |
| 6415 | |
| 6416 | if (BC_PROG_NUM(r, num)) { |
| 6417 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6418 | bc_num_init_DEF_SIZE(&n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6419 | bc_num_copy(&n, num); |
| 6420 | bc_num_truncate(&n, n.rdx); |
| 6421 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6422 | s = bc_num_mod(&n, &G.prog.strmb, &n, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6423 | if (s) goto num_err; |
| 6424 | s = bc_num_ulong(&n, &val); |
| 6425 | if (s) goto num_err; |
| 6426 | |
| 6427 | c = (char) val; |
| 6428 | |
| 6429 | bc_num_free(&n); |
| 6430 | } |
| 6431 | else { |
| 6432 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx; |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6433 | str2 = *bc_program_str(idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6434 | c = str2[0]; |
| 6435 | } |
| 6436 | |
| 6437 | str = xmalloc(2); |
| 6438 | str[0] = c; |
| 6439 | str[1] = '\0'; |
| 6440 | |
| 6441 | str2 = xstrdup(str); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6442 | bc_program_addFunc(str2, &idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6443 | |
| 6444 | if (idx != len + BC_PROG_REQ_FUNCS) { |
| 6445 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6446 | for (idx = 0; idx < G.prog.strs.len; ++idx) { |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6447 | if (strcmp(*bc_program_str(idx), str) == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6448 | len = idx; |
| 6449 | break; |
| 6450 | } |
| 6451 | } |
| 6452 | |
| 6453 | free(str); |
| 6454 | } |
| 6455 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6456 | bc_vec_push(&G.prog.strs, &str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6457 | |
| 6458 | res.t = BC_RESULT_STR; |
| 6459 | res.d.id.idx = len; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6460 | bc_vec_pop(&G.prog.results); |
| 6461 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6462 | |
| 6463 | return BC_STATUS_SUCCESS; |
| 6464 | |
| 6465 | num_err: |
| 6466 | bc_num_free(&n); |
| 6467 | return s; |
| 6468 | } |
| 6469 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6470 | static BcStatus bc_program_printStream(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6471 | { |
| 6472 | BcStatus s; |
| 6473 | BcResult *r; |
| 6474 | BcNum *n = NULL; |
| 6475 | size_t idx; |
| 6476 | char *str; |
| 6477 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6478 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6479 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6480 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6481 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6482 | s = bc_program_num(r, &n, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6483 | if (s) return s; |
| 6484 | |
| 6485 | if (BC_PROG_NUM(r, n)) |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 6486 | s = bc_num_stream(n, &G.prog.strmb); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6487 | else { |
| 6488 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx; |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6489 | str = *bc_program_str(idx); |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 6490 | printf("%s", str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6491 | } |
| 6492 | |
| 6493 | return s; |
| 6494 | } |
| 6495 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6496 | static BcStatus bc_program_nquit(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6497 | { |
| 6498 | BcStatus s; |
| 6499 | BcResult *opnd; |
| 6500 | BcNum *num = NULL; |
| 6501 | unsigned long val; |
| 6502 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6503 | s = bc_program_prep(&opnd, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6504 | if (s) return s; |
| 6505 | s = bc_num_ulong(num, &val); |
| 6506 | if (s) return s; |
| 6507 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6508 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6509 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6510 | if (G.prog.stack.len < val) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6511 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 6512 | if (G.prog.stack.len == val) { |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 6513 | QUIT_OR_RETURN_TO_MAIN; |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 6514 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6515 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6516 | bc_vec_npop(&G.prog.stack, val); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6517 | |
| 6518 | return s; |
| 6519 | } |
| 6520 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6521 | static BcStatus bc_program_execStr(char *code, size_t *bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6522 | bool cond) |
| 6523 | { |
| 6524 | BcStatus s = BC_STATUS_SUCCESS; |
| 6525 | BcResult *r; |
| 6526 | char **str; |
| 6527 | BcFunc *f; |
| 6528 | BcParse prs; |
| 6529 | BcInstPtr ip; |
| 6530 | size_t fidx, sidx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6531 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6532 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6533 | return bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6534 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6535 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6536 | |
| 6537 | if (cond) { |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6538 | BcNum *n = n; // for compiler |
| 6539 | bool exec; |
| 6540 | char *name; |
| 6541 | char *then_name = bc_program_name(code, bgn); |
| 6542 | char *else_name = NULL; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6543 | |
| 6544 | if (code[*bgn] == BC_PARSE_STREND) |
| 6545 | (*bgn) += 1; |
| 6546 | else |
| 6547 | else_name = bc_program_name(code, bgn); |
| 6548 | |
| 6549 | exec = r->d.n.len != 0; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6550 | name = then_name; |
| 6551 | if (!exec && else_name != NULL) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6552 | exec = true; |
| 6553 | name = else_name; |
| 6554 | } |
| 6555 | |
| 6556 | if (exec) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6557 | BcVec *v; |
| 6558 | v = bc_program_search(name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6559 | n = bc_vec_top(v); |
| 6560 | } |
| 6561 | |
| 6562 | free(then_name); |
| 6563 | free(else_name); |
| 6564 | |
| 6565 | if (!exec) goto exit; |
| 6566 | if (!BC_PROG_STR(n)) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6567 | s = bc_error_variable_is_wrong_type(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6568 | goto exit; |
| 6569 | } |
| 6570 | |
| 6571 | sidx = n->rdx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6572 | } else { |
| 6573 | if (r->t == BC_RESULT_STR) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6574 | sidx = r->d.id.idx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6575 | } else if (r->t == BC_RESULT_VAR) { |
| 6576 | BcNum *n; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6577 | s = bc_program_num(r, &n, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6578 | if (s || !BC_PROG_STR(n)) goto exit; |
| 6579 | sidx = n->rdx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6580 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6581 | goto exit; |
| 6582 | } |
| 6583 | |
| 6584 | fidx = sidx + BC_PROG_REQ_FUNCS; |
| 6585 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6586 | str = bc_program_str(sidx); |
| 6587 | f = bc_program_func(fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6588 | |
| 6589 | if (f->code.len == 0) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6590 | common_parse_init(&prs, fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6591 | s = bc_parse_text(&prs, *str); |
| 6592 | if (s) goto err; |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 6593 | s = common_parse_expr(&prs, BC_PARSE_NOCALL); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6594 | if (s) goto err; |
| 6595 | |
| 6596 | if (prs.l.t.t != BC_LEX_EOF) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6597 | s = bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6598 | goto err; |
| 6599 | } |
| 6600 | |
| 6601 | bc_parse_free(&prs); |
| 6602 | } |
| 6603 | |
| 6604 | ip.idx = 0; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6605 | ip.len = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6606 | ip.func = fidx; |
| 6607 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6608 | bc_vec_pop(&G.prog.results); |
| 6609 | bc_vec_push(&G.prog.stack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6610 | |
| 6611 | return BC_STATUS_SUCCESS; |
| 6612 | |
| 6613 | err: |
| 6614 | bc_parse_free(&prs); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6615 | f = bc_program_func(fidx); |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 6616 | bc_vec_pop_all(&f->code); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6617 | exit: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6618 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6619 | return s; |
| 6620 | } |
| 6621 | #endif // ENABLE_DC |
| 6622 | |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6623 | static void bc_program_pushGlobal(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6624 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6625 | BcResult res; |
| 6626 | unsigned long val; |
| 6627 | |
| 6628 | res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE; |
| 6629 | if (inst == BC_INST_IBASE) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6630 | val = (unsigned long) G.prog.ib_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6631 | else if (inst == BC_INST_SCALE) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6632 | val = (unsigned long) G.prog.scale; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6633 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6634 | val = (unsigned long) G.prog.ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6635 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6636 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6637 | bc_num_ulong2num(&res.d.n, val); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6638 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6639 | } |
| 6640 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6641 | static void bc_program_addFunc(char *name, size_t *idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6642 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6643 | BcId entry, *entry_ptr; |
| 6644 | BcFunc f; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 6645 | int inserted; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6646 | |
| 6647 | entry.name = name; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6648 | entry.idx = G.prog.fns.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6649 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 6650 | inserted = bc_map_insert(&G.prog.fn_map, &entry, idx); |
| 6651 | if (!inserted) free(name); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6652 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6653 | entry_ptr = bc_vec_item(&G.prog.fn_map, *idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6654 | *idx = entry_ptr->idx; |
| 6655 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 6656 | if (!inserted) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6657 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6658 | BcFunc *func = bc_program_func(entry_ptr->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6659 | |
| 6660 | // We need to reset these, so the function can be repopulated. |
| 6661 | func->nparams = 0; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 6662 | bc_vec_pop_all(&func->autos); |
| 6663 | bc_vec_pop_all(&func->code); |
| 6664 | bc_vec_pop_all(&func->labels); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6665 | } |
| 6666 | else { |
| 6667 | bc_func_init(&f); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6668 | bc_vec_push(&G.prog.fns, &f); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6669 | } |
| 6670 | } |
| 6671 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6672 | static BcStatus bc_program_exec(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6673 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6674 | BcResult r, *ptr; |
| 6675 | BcNum *num; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6676 | BcInstPtr *ip = bc_vec_top(&G.prog.stack); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6677 | BcFunc *func = bc_program_func(ip->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6678 | char *code = func->code.v; |
| 6679 | bool cond = false; |
| 6680 | |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6681 | while (ip->idx < func->code.len) { |
| 6682 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6683 | char inst = code[(ip->idx)++]; |
| 6684 | |
| 6685 | switch (inst) { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6686 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6687 | case BC_INST_JUMP_ZERO: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6688 | s = bc_program_prep(&ptr, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6689 | if (s) return s; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6690 | cond = !bc_num_cmp(num, &G.prog.zero); |
| 6691 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6692 | // Fallthrough. |
| 6693 | case BC_INST_JUMP: { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6694 | size_t *addr; |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6695 | size_t idx = bc_program_index(code, &ip->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6696 | addr = bc_vec_item(&func->labels, idx); |
| 6697 | if (inst == BC_INST_JUMP || cond) ip->idx = *addr; |
| 6698 | break; |
| 6699 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6700 | case BC_INST_CALL: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6701 | s = bc_program_call(code, &ip->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6702 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6703 | case BC_INST_INC_PRE: |
| 6704 | case BC_INST_DEC_PRE: |
| 6705 | case BC_INST_INC_POST: |
| 6706 | case BC_INST_DEC_POST: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6707 | s = bc_program_incdec(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6708 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6709 | case BC_INST_HALT: |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 6710 | QUIT_OR_RETURN_TO_MAIN; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6711 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6712 | case BC_INST_RET: |
| 6713 | case BC_INST_RET0: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6714 | s = bc_program_return(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6715 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6716 | case BC_INST_BOOL_OR: |
| 6717 | case BC_INST_BOOL_AND: |
| 6718 | #endif // ENABLE_BC |
| 6719 | case BC_INST_REL_EQ: |
| 6720 | case BC_INST_REL_LE: |
| 6721 | case BC_INST_REL_GE: |
| 6722 | case BC_INST_REL_NE: |
| 6723 | case BC_INST_REL_LT: |
| 6724 | case BC_INST_REL_GT: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6725 | s = bc_program_logical(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6726 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6727 | case BC_INST_READ: |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6728 | s = bc_program_read(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6729 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6730 | case BC_INST_VAR: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6731 | s = bc_program_pushVar(code, &ip->idx, false, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6732 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6733 | case BC_INST_ARRAY_ELEM: |
| 6734 | case BC_INST_ARRAY: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6735 | s = bc_program_pushArray(code, &ip->idx, inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6736 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6737 | case BC_INST_LAST: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6738 | r.t = BC_RESULT_LAST; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6739 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6740 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6741 | case BC_INST_IBASE: |
| 6742 | case BC_INST_SCALE: |
| 6743 | case BC_INST_OBASE: |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6744 | bc_program_pushGlobal(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6745 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6746 | case BC_INST_SCALE_FUNC: |
| 6747 | case BC_INST_LENGTH: |
| 6748 | case BC_INST_SQRT: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6749 | s = bc_program_builtin(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6750 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6751 | case BC_INST_NUM: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6752 | r.t = BC_RESULT_CONSTANT; |
| 6753 | r.d.id.idx = bc_program_index(code, &ip->idx); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6754 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6755 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6756 | case BC_INST_POP: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6757 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6758 | s = bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6759 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6760 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6761 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6762 | case BC_INST_POP_EXEC: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6763 | bc_vec_pop(&G.prog.stack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6764 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6765 | case BC_INST_PRINT: |
| 6766 | case BC_INST_PRINT_POP: |
| 6767 | case BC_INST_PRINT_STR: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6768 | s = bc_program_print(inst, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6769 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6770 | case BC_INST_STR: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6771 | r.t = BC_RESULT_STR; |
| 6772 | r.d.id.idx = bc_program_index(code, &ip->idx); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6773 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6774 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6775 | case BC_INST_POWER: |
| 6776 | case BC_INST_MULTIPLY: |
| 6777 | case BC_INST_DIVIDE: |
| 6778 | case BC_INST_MODULUS: |
| 6779 | case BC_INST_PLUS: |
| 6780 | case BC_INST_MINUS: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6781 | s = bc_program_op(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6782 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6783 | case BC_INST_BOOL_NOT: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6784 | s = bc_program_prep(&ptr, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6785 | if (s) return s; |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6786 | bc_num_init_DEF_SIZE(&r.d.n); |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6787 | if (!bc_num_cmp(num, &G.prog.zero)) |
| 6788 | bc_num_one(&r.d.n); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 6789 | //else bc_num_zero(&r.d.n); - already is |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6790 | bc_program_retire(&r, BC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6791 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6792 | case BC_INST_NEG: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6793 | s = bc_program_negate(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6794 | break; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6795 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6796 | case BC_INST_ASSIGN_POWER: |
| 6797 | case BC_INST_ASSIGN_MULTIPLY: |
| 6798 | case BC_INST_ASSIGN_DIVIDE: |
| 6799 | case BC_INST_ASSIGN_MODULUS: |
| 6800 | case BC_INST_ASSIGN_PLUS: |
| 6801 | case BC_INST_ASSIGN_MINUS: |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6802 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6803 | case BC_INST_ASSIGN: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6804 | s = bc_program_assign(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6805 | break; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6806 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6807 | case BC_INST_MODEXP: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6808 | s = bc_program_modexp(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6809 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6810 | case BC_INST_DIVMOD: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6811 | s = bc_program_divmod(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6812 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6813 | case BC_INST_EXECUTE: |
| 6814 | case BC_INST_EXEC_COND: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6815 | cond = inst == BC_INST_EXEC_COND; |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6816 | s = bc_program_execStr(code, &ip->idx, cond); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6817 | break; |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6818 | case BC_INST_PRINT_STACK: { |
| 6819 | size_t idx; |
| 6820 | for (idx = 0; idx < G.prog.results.len; ++idx) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6821 | s = bc_program_print(BC_INST_PRINT, idx); |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6822 | if (s) break; |
| 6823 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6824 | break; |
| 6825 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6826 | case BC_INST_CLEAR_STACK: |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 6827 | bc_vec_pop_all(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6828 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6829 | case BC_INST_STACK_LEN: |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6830 | bc_program_stackLen(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6831 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6832 | case BC_INST_DUPLICATE: |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6833 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6834 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6835 | ptr = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6836 | bc_result_copy(&r, ptr); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6837 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6838 | break; |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6839 | case BC_INST_SWAP: { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6840 | BcResult *ptr2; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6841 | if (!BC_PROG_STACK(&G.prog.results, 2)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6842 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6843 | ptr = bc_vec_item_rev(&G.prog.results, 0); |
| 6844 | ptr2 = bc_vec_item_rev(&G.prog.results, 1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6845 | memcpy(&r, ptr, sizeof(BcResult)); |
| 6846 | memcpy(ptr, ptr2, sizeof(BcResult)); |
| 6847 | memcpy(ptr2, &r, sizeof(BcResult)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6848 | break; |
| 6849 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6850 | case BC_INST_ASCIIFY: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6851 | s = bc_program_asciify(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6852 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6853 | case BC_INST_PRINT_STREAM: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6854 | s = bc_program_printStream(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6855 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6856 | case BC_INST_LOAD: |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6857 | case BC_INST_PUSH_VAR: { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6858 | bool copy = inst == BC_INST_LOAD; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6859 | s = bc_program_pushVar(code, &ip->idx, true, copy); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6860 | break; |
| 6861 | } |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6862 | case BC_INST_PUSH_TO_VAR: { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6863 | char *name = bc_program_name(code, &ip->idx); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6864 | s = bc_program_copyToVar(name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6865 | free(name); |
| 6866 | break; |
| 6867 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6868 | case BC_INST_QUIT: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6869 | if (G.prog.stack.len <= 2) |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 6870 | QUIT_OR_RETURN_TO_MAIN; |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 6871 | bc_vec_npop(&G.prog.stack, 2); |
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_NQUIT: |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6874 | s = bc_program_nquit(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6875 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6876 | #endif // ENABLE_DC |
| 6877 | } |
| 6878 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6879 | if (s || G_interrupt) { |
| 6880 | bc_program_reset(); |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6881 | return s; |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6882 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6883 | |
| 6884 | // If the stack has changed, pointers may be invalid. |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6885 | ip = bc_vec_top(&G.prog.stack); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6886 | func = bc_program_func(ip->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6887 | code = func->code.v; |
| 6888 | } |
| 6889 | |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6890 | return BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6891 | } |
| 6892 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 6893 | #if ENABLE_BC |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 6894 | static void bc_vm_info(void) |
| 6895 | { |
| 6896 | printf("%s "BB_VER"\n" |
| 6897 | "Copyright (c) 2018 Gavin D. Howard and contributors\n" |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 6898 | , applet_name); |
| 6899 | } |
| 6900 | |
| 6901 | static void bc_args(char **argv) |
| 6902 | { |
| 6903 | unsigned opts; |
| 6904 | int i; |
| 6905 | |
| 6906 | GETOPT_RESET(); |
| 6907 | #if ENABLE_FEATURE_BC_LONG_OPTIONS |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 6908 | opts = option_mask32 |= getopt32long(argv, "wvsqli", |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 6909 | "warn\0" No_argument "w" |
| 6910 | "version\0" No_argument "v" |
| 6911 | "standard\0" No_argument "s" |
| 6912 | "quiet\0" No_argument "q" |
| 6913 | "mathlib\0" No_argument "l" |
| 6914 | "interactive\0" No_argument "i" |
| 6915 | ); |
| 6916 | #else |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 6917 | opts = option_mask32 |= getopt32(argv, "wvsqli"); |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 6918 | #endif |
| 6919 | if (getenv("POSIXLY_CORRECT")) |
| 6920 | option_mask32 |= BC_FLAG_S; |
| 6921 | |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 6922 | if (opts & BC_FLAG_V) { |
| 6923 | bc_vm_info(); |
| 6924 | exit(0); |
| 6925 | } |
| 6926 | |
| 6927 | for (i = optind; argv[i]; ++i) |
| 6928 | bc_vec_push(&G.files, argv + i); |
| 6929 | } |
| 6930 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6931 | static void bc_vm_envArgs(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6932 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6933 | BcVec v; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6934 | char *buf; |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 6935 | char *env_args = getenv("BC_ENV_ARGS"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6936 | |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 6937 | if (!env_args) return; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6938 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6939 | G.env_args = xstrdup(env_args); |
| 6940 | buf = G.env_args; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6941 | |
| 6942 | bc_vec_init(&v, sizeof(char *), NULL); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6943 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6944 | while (*(buf = skip_whitespace(buf)) != '\0') { |
| 6945 | bc_vec_push(&v, &buf); |
| 6946 | buf = skip_non_whitespace(buf); |
| 6947 | if (!*buf) |
| 6948 | break; |
| 6949 | *buf++ = '\0'; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6950 | } |
| 6951 | |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 6952 | // NULL terminate, and pass argv[] so that first arg is argv[1] |
| 6953 | if (sizeof(int) == sizeof(char*)) { |
| 6954 | bc_vec_push(&v, &const_int_0); |
| 6955 | } else { |
| 6956 | static char *const nullptr = NULL; |
| 6957 | bc_vec_push(&v, &nullptr); |
| 6958 | } |
| 6959 | bc_args(((char **)v.v) - 1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6960 | |
| 6961 | bc_vec_free(&v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6962 | } |
| 6963 | #endif // ENABLE_BC |
| 6964 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6965 | static unsigned bc_vm_envLen(const char *var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6966 | { |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6967 | char *lenv; |
| 6968 | unsigned len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6969 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6970 | lenv = getenv(var); |
| 6971 | len = BC_NUM_PRINT_WIDTH; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6972 | if (!lenv) return len; |
| 6973 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6974 | len = bb_strtou(lenv, NULL, 10) - 1; |
| 6975 | if (errno || len < 2 || len >= INT_MAX) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6976 | len = BC_NUM_PRINT_WIDTH; |
| 6977 | |
| 6978 | return len; |
| 6979 | } |
| 6980 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6981 | static BcStatus bc_vm_process(const char *text) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6982 | { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6983 | BcStatus s = bc_parse_text(&G.prs, text); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6984 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6985 | if (s) return s; |
| 6986 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6987 | while (G.prs.l.t.t != BC_LEX_EOF) { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6988 | s = G.prs.parse(&G.prs); |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 6989 | if (s) return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6990 | } |
| 6991 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6992 | if (BC_PARSE_CAN_EXEC(&G.prs)) { |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6993 | s = bc_program_exec(); |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 6994 | fflush_and_check(); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 6995 | if (s) |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6996 | bc_program_reset(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6997 | } |
| 6998 | |
| 6999 | return s; |
| 7000 | } |
| 7001 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7002 | static BcStatus bc_vm_file(const char *file) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7003 | { |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7004 | const char *sv_file; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7005 | char *data; |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7006 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7007 | BcFunc *main_func; |
| 7008 | BcInstPtr *ip; |
| 7009 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 7010 | data = bc_read_file(file); |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 7011 | if (!data) return bc_error_fmt("file '%s' is not text", file); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7012 | |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7013 | sv_file = G.prog.file; |
| 7014 | G.prog.file = file; |
| 7015 | bc_lex_file(&G.prs.l); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7016 | s = bc_vm_process(data); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7017 | if (s) goto err; |
| 7018 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 7019 | main_func = bc_program_func(BC_PROG_MAIN); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7020 | ip = bc_vec_item(&G.prog.stack, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7021 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 7022 | if (main_func->code.len < ip->idx) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 7023 | s = bc_error_fmt("file '%s' is not executable", file); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7024 | |
| 7025 | err: |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7026 | G.prog.file = sv_file; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7027 | free(data); |
| 7028 | return s; |
| 7029 | } |
| 7030 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7031 | static BcStatus bc_vm_stdin(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7032 | { |
Denys Vlasenko | a0c421c | 2018-12-02 20:16:52 +0100 | [diff] [blame] | 7033 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7034 | BcVec buf, buffer; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7035 | size_t len, i, str = 0; |
Denys Vlasenko | a0c421c | 2018-12-02 20:16:52 +0100 | [diff] [blame] | 7036 | bool comment = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7037 | |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7038 | G.prog.file = NULL; |
| 7039 | bc_lex_file(&G.prs.l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7040 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 7041 | bc_char_vec_init(&buffer); |
| 7042 | bc_char_vec_init(&buf); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 7043 | bc_vec_pushZeroByte(&buffer); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7044 | |
| 7045 | // This loop is complex because the vm tries not to send any lines that end |
| 7046 | // with a backslash to the parser. The reason for that is because the parser |
| 7047 | // treats a backslash+newline combo as whitespace, per the bc spec. In that |
| 7048 | // case, and for strings and comments, the parser will expect more stuff. |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 7049 | while ((s = bc_read_line(&buf)) == BC_STATUS_SUCCESS) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7050 | |
| 7051 | char *string = buf.v; |
| 7052 | |
| 7053 | len = buf.len - 1; |
| 7054 | |
| 7055 | if (len == 1) { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7056 | if (str && buf.v[0] == G.send) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7057 | str -= 1; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7058 | else if (buf.v[0] == G.sbgn) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7059 | str += 1; |
| 7060 | } |
| 7061 | else if (len > 1 || comment) { |
| 7062 | |
| 7063 | for (i = 0; i < len; ++i) { |
| 7064 | |
Denys Vlasenko | a0c421c | 2018-12-02 20:16:52 +0100 | [diff] [blame] | 7065 | bool notend = len > i + 1; |
| 7066 | char c = string[i]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7067 | |
| 7068 | if (i - 1 > len || string[i - 1] != '\\') { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7069 | if (G.sbgn == G.send) |
| 7070 | str ^= c == G.sbgn; |
| 7071 | else if (c == G.send) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7072 | str -= 1; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7073 | else if (c == G.sbgn) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7074 | str += 1; |
| 7075 | } |
| 7076 | |
| 7077 | if (c == '/' && notend && !comment && string[i + 1] == '*') { |
| 7078 | comment = true; |
| 7079 | break; |
| 7080 | } |
| 7081 | else if (c == '*' && notend && comment && string[i + 1] == '/') |
| 7082 | comment = false; |
| 7083 | } |
| 7084 | |
| 7085 | if (str || comment || string[len - 2] == '\\') { |
| 7086 | bc_vec_concat(&buffer, buf.v); |
| 7087 | continue; |
| 7088 | } |
| 7089 | } |
| 7090 | |
| 7091 | bc_vec_concat(&buffer, buf.v); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7092 | s = bc_vm_process(buffer.v); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7093 | if (s) { |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7094 | if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin) { |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 7095 | // Debug config, non-interactive mode: |
| 7096 | // return all the way back to main. |
| 7097 | // Non-debug builds do not come here, they exit. |
| 7098 | break; |
| 7099 | } |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7100 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7101 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 7102 | bc_vec_pop_all(&buffer); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7103 | } |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 7104 | if (s == BC_STATUS_EOF) // input EOF (^D) is not an error |
| 7105 | s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7106 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 7107 | if (str) { |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7108 | s = bc_error("string end could not be found"); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 7109 | } |
| 7110 | else if (comment) { |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7111 | s = bc_error("comment end could not be found"); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 7112 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7113 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7114 | bc_vec_free(&buf); |
| 7115 | bc_vec_free(&buffer); |
| 7116 | return s; |
| 7117 | } |
| 7118 | |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7119 | #if ENABLE_BC |
| 7120 | static const char bc_lib[] = { |
| 7121 | "scale=20" |
| 7122 | "\n" "define e(x){" |
| 7123 | "\n" "auto b,s,n,r,d,i,p,f,v" |
| 7124 | "\n" "b=ibase" |
| 7125 | "\n" "ibase=A" |
| 7126 | "\n" "if(x<0){" |
| 7127 | "\n" "n=1" |
| 7128 | "\n" "x=-x" |
| 7129 | "\n" "}" |
| 7130 | "\n" "s=scale" |
| 7131 | "\n" "r=6+s+0.44*x" |
| 7132 | "\n" "scale=scale(x)+1" |
| 7133 | "\n" "while(x>1){" |
| 7134 | "\n" "d+=1" |
| 7135 | "\n" "x/=2" |
| 7136 | "\n" "scale+=1" |
| 7137 | "\n" "}" |
| 7138 | "\n" "scale=r" |
| 7139 | "\n" "r=x+1" |
| 7140 | "\n" "p=x" |
| 7141 | "\n" "f=v=1" |
| 7142 | "\n" "for(i=2;v!=0;++i){" |
| 7143 | "\n" "p*=x" |
| 7144 | "\n" "f*=i" |
| 7145 | "\n" "v=p/f" |
| 7146 | "\n" "r+=v" |
| 7147 | "\n" "}" |
| 7148 | "\n" "while((d--)!=0)r*=r" |
| 7149 | "\n" "scale=s" |
| 7150 | "\n" "ibase=b" |
| 7151 | "\n" "if(n!=0)return(1/r)" |
| 7152 | "\n" "return(r/1)" |
| 7153 | "\n" "}" |
| 7154 | "\n" "define l(x){" |
| 7155 | "\n" "auto b,s,r,p,a,q,i,v" |
| 7156 | "\n" "b=ibase" |
| 7157 | "\n" "ibase=A" |
| 7158 | "\n" "if(x<=0){" |
| 7159 | "\n" "r=(1-10^scale)/1" |
| 7160 | "\n" "ibase=b" |
| 7161 | "\n" "return(r)" |
| 7162 | "\n" "}" |
| 7163 | "\n" "s=scale" |
| 7164 | "\n" "scale+=6" |
| 7165 | "\n" "p=2" |
| 7166 | "\n" "while(x>=2){" |
| 7167 | "\n" "p*=2" |
| 7168 | "\n" "x=sqrt(x)" |
| 7169 | "\n" "}" |
| 7170 | "\n" "while(x<=0.5){" |
| 7171 | "\n" "p*=2" |
| 7172 | "\n" "x=sqrt(x)" |
| 7173 | "\n" "}" |
| 7174 | "\n" "r=a=(x-1)/(x+1)" |
| 7175 | "\n" "q=a*a" |
| 7176 | "\n" "v=1" |
| 7177 | "\n" "for(i=3;v!=0;i+=2){" |
| 7178 | "\n" "a*=q" |
| 7179 | "\n" "v=a/i" |
| 7180 | "\n" "r+=v" |
| 7181 | "\n" "}" |
| 7182 | "\n" "r*=p" |
| 7183 | "\n" "scale=s" |
| 7184 | "\n" "ibase=b" |
| 7185 | "\n" "return(r/1)" |
| 7186 | "\n" "}" |
| 7187 | "\n" "define s(x){" |
| 7188 | "\n" "auto b,s,r,n,a,q,i" |
| 7189 | "\n" "b=ibase" |
| 7190 | "\n" "ibase=A" |
| 7191 | "\n" "s=scale" |
| 7192 | "\n" "scale=1.1*s+2" |
| 7193 | "\n" "a=a(1)" |
| 7194 | "\n" "if(x<0){" |
| 7195 | "\n" "n=1" |
| 7196 | "\n" "x=-x" |
| 7197 | "\n" "}" |
| 7198 | "\n" "scale=0" |
| 7199 | "\n" "q=(x/a+2)/4" |
| 7200 | "\n" "x=x-4*q*a" |
| 7201 | "\n" "if(q%2!=0)x=-x" |
| 7202 | "\n" "scale=s+2" |
| 7203 | "\n" "r=a=x" |
| 7204 | "\n" "q=-x*x" |
| 7205 | "\n" "for(i=3;a!=0;i+=2){" |
| 7206 | "\n" "a*=q/(i*(i-1))" |
| 7207 | "\n" "r+=a" |
| 7208 | "\n" "}" |
| 7209 | "\n" "scale=s" |
| 7210 | "\n" "ibase=b" |
| 7211 | "\n" "if(n!=0)return(-r/1)" |
| 7212 | "\n" "return(r/1)" |
| 7213 | "\n" "}" |
| 7214 | "\n" "define c(x){" |
| 7215 | "\n" "auto b,s" |
| 7216 | "\n" "b=ibase" |
| 7217 | "\n" "ibase=A" |
| 7218 | "\n" "s=scale" |
| 7219 | "\n" "scale*=1.2" |
| 7220 | "\n" "x=s(2*a(1)+x)" |
| 7221 | "\n" "scale=s" |
| 7222 | "\n" "ibase=b" |
| 7223 | "\n" "return(x/1)" |
| 7224 | "\n" "}" |
| 7225 | "\n" "define a(x){" |
| 7226 | "\n" "auto b,s,r,n,a,m,t,f,i,u" |
| 7227 | "\n" "b=ibase" |
| 7228 | "\n" "ibase=A" |
| 7229 | "\n" "n=1" |
| 7230 | "\n" "if(x<0){" |
| 7231 | "\n" "n=-1" |
| 7232 | "\n" "x=-x" |
| 7233 | "\n" "}" |
| 7234 | "\n" "if(x==1){" |
| 7235 | "\n" "if(scale<65){" |
| 7236 | "\n" "return(.7853981633974483096156608458198757210492923498437764552437361480/n)" |
| 7237 | "\n" "}" |
| 7238 | "\n" "}" |
| 7239 | "\n" "if(x==.2){" |
| 7240 | "\n" "if(scale<65){" |
| 7241 | "\n" "return(.1973955598498807583700497651947902934475851037878521015176889402/n)" |
| 7242 | "\n" "}" |
| 7243 | "\n" "}" |
| 7244 | "\n" "s=scale" |
| 7245 | "\n" "if(x>.2){" |
| 7246 | "\n" "scale+=5" |
| 7247 | "\n" "a=a(.2)" |
| 7248 | "\n" "}" |
| 7249 | "\n" "scale=s+3" |
| 7250 | "\n" "while(x>.2){" |
| 7251 | "\n" "m+=1" |
| 7252 | "\n" "x=(x-.2)/(1+.2*x)" |
| 7253 | "\n" "}" |
| 7254 | "\n" "r=u=x" |
| 7255 | "\n" "f=-x*x" |
| 7256 | "\n" "t=1" |
| 7257 | "\n" "for(i=3;t!=0;i+=2){" |
| 7258 | "\n" "u*=f" |
| 7259 | "\n" "t=u/i" |
| 7260 | "\n" "r+=t" |
| 7261 | "\n" "}" |
| 7262 | "\n" "scale=s" |
| 7263 | "\n" "ibase=b" |
| 7264 | "\n" "return((m*a+r)/n)" |
| 7265 | "\n" "}" |
| 7266 | "\n" "define j(n,x){" |
| 7267 | "\n" "auto b,s,o,a,i,v,f" |
| 7268 | "\n" "b=ibase" |
| 7269 | "\n" "ibase=A" |
| 7270 | "\n" "s=scale" |
| 7271 | "\n" "scale=0" |
| 7272 | "\n" "n/=1" |
| 7273 | "\n" "if(n<0){" |
| 7274 | "\n" "n=-n" |
| 7275 | "\n" "if(n%2==1)o=1" |
| 7276 | "\n" "}" |
| 7277 | "\n" "a=1" |
| 7278 | "\n" "for(i=2;i<=n;++i)a*=i" |
| 7279 | "\n" "scale=1.5*s" |
| 7280 | "\n" "a=(x^n)/2^n/a" |
| 7281 | "\n" "r=v=1" |
| 7282 | "\n" "f=-x*x/4" |
| 7283 | "\n" "scale=scale+length(a)-scale(a)" |
| 7284 | "\n" "for(i=1;v!=0;++i){" |
| 7285 | "\n" "v=v*f/i/(n+i)" |
| 7286 | "\n" "r+=v" |
| 7287 | "\n" "}" |
| 7288 | "\n" "scale=s" |
| 7289 | "\n" "ibase=b" |
| 7290 | "\n" "if(o!=0)a=-a" |
| 7291 | "\n" "return(a*r/1)" |
| 7292 | "\n" "}" |
| 7293 | }; |
| 7294 | #endif // ENABLE_BC |
| 7295 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7296 | static BcStatus bc_vm_exec(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7297 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7298 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7299 | size_t i; |
| 7300 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7301 | #if ENABLE_BC |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 7302 | if (option_mask32 & BC_FLAG_L) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7303 | |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7304 | // We know that internal library is not buggy, |
| 7305 | // thus error checking is normally disabled. |
| 7306 | # define DEBUG_LIB 0 |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7307 | bc_lex_file(&G.prs.l); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7308 | s = bc_parse_text(&G.prs, bc_lib); |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7309 | if (DEBUG_LIB && s) return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7310 | |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7311 | while (G.prs.l.t.t != BC_LEX_EOF) { |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7312 | s = G.prs.parse(&G.prs); |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7313 | if (DEBUG_LIB && s) return s; |
| 7314 | } |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7315 | s = bc_program_exec(); |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7316 | if (DEBUG_LIB && s) return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7317 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7318 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7319 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7320 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7321 | for (i = 0; !s && i < G.files.len; ++i) |
| 7322 | s = bc_vm_file(*((char **) bc_vec_item(&G.files, i))); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7323 | if (ENABLE_FEATURE_CLEAN_UP && s && !G_ttyin) { |
| 7324 | // Debug config, non-interactive mode: |
| 7325 | // return all the way back to main. |
| 7326 | // Non-debug builds do not come here, they exit. |
| 7327 | return s; |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7328 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7329 | |
Denys Vlasenko | ac6ed11 | 2018-12-08 21:39:10 +0100 | [diff] [blame] | 7330 | if (IS_BC || (option_mask32 & BC_FLAG_I)) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7331 | s = bc_vm_stdin(); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7332 | |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7333 | if (!s && !BC_PARSE_CAN_EXEC(&G.prs)) |
| 7334 | s = bc_vm_process(""); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7335 | |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 7336 | return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7337 | } |
| 7338 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7339 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 7340 | static void bc_program_free(void) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7341 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7342 | bc_num_free(&G.prog.ib); |
| 7343 | bc_num_free(&G.prog.ob); |
| 7344 | bc_num_free(&G.prog.hexb); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7345 | # if ENABLE_DC |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7346 | bc_num_free(&G.prog.strmb); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7347 | # endif |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7348 | bc_vec_free(&G.prog.fns); |
| 7349 | bc_vec_free(&G.prog.fn_map); |
| 7350 | bc_vec_free(&G.prog.vars); |
| 7351 | bc_vec_free(&G.prog.var_map); |
| 7352 | bc_vec_free(&G.prog.arrs); |
| 7353 | bc_vec_free(&G.prog.arr_map); |
| 7354 | bc_vec_free(&G.prog.strs); |
| 7355 | bc_vec_free(&G.prog.consts); |
| 7356 | bc_vec_free(&G.prog.results); |
| 7357 | bc_vec_free(&G.prog.stack); |
| 7358 | bc_num_free(&G.prog.last); |
| 7359 | bc_num_free(&G.prog.zero); |
| 7360 | bc_num_free(&G.prog.one); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7361 | } |
| 7362 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7363 | static void bc_vm_free(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7364 | { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7365 | bc_vec_free(&G.files); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7366 | bc_program_free(); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7367 | bc_parse_free(&G.prs); |
| 7368 | free(G.env_args); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7369 | } |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7370 | #endif |
| 7371 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7372 | static void bc_program_init(void) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7373 | { |
| 7374 | size_t idx; |
| 7375 | BcInstPtr ip; |
| 7376 | |
| 7377 | /* memset(&G.prog, 0, sizeof(G.prog)); - already is */ |
| 7378 | memset(&ip, 0, sizeof(BcInstPtr)); |
| 7379 | |
| 7380 | /* G.prog.nchars = G.prog.scale = 0; - already is */ |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7381 | bc_num_init_DEF_SIZE(&G.prog.ib); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7382 | bc_num_ten(&G.prog.ib); |
| 7383 | G.prog.ib_t = 10; |
| 7384 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7385 | bc_num_init_DEF_SIZE(&G.prog.ob); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7386 | bc_num_ten(&G.prog.ob); |
| 7387 | G.prog.ob_t = 10; |
| 7388 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7389 | bc_num_init_DEF_SIZE(&G.prog.hexb); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7390 | bc_num_ten(&G.prog.hexb); |
| 7391 | G.prog.hexb.num[0] = 6; |
| 7392 | |
| 7393 | #if ENABLE_DC |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7394 | bc_num_init_DEF_SIZE(&G.prog.strmb); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7395 | bc_num_ulong2num(&G.prog.strmb, UCHAR_MAX + 1); |
| 7396 | #endif |
| 7397 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7398 | bc_num_init_DEF_SIZE(&G.prog.last); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 7399 | //bc_num_zero(&G.prog.last); - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7400 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7401 | bc_num_init_DEF_SIZE(&G.prog.zero); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 7402 | //bc_num_zero(&G.prog.zero); - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7403 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7404 | bc_num_init_DEF_SIZE(&G.prog.one); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7405 | bc_num_one(&G.prog.one); |
| 7406 | |
| 7407 | bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7408 | bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7409 | |
Denys Vlasenko | 1f67e93 | 2018-12-03 00:08:59 +0100 | [diff] [blame] | 7410 | bc_program_addFunc(xstrdup("(main)"), &idx); |
| 7411 | bc_program_addFunc(xstrdup("(read)"), &idx); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7412 | |
| 7413 | bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7414 | bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7415 | |
| 7416 | bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7417 | bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7418 | |
| 7419 | bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free); |
| 7420 | bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free); |
| 7421 | bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free); |
| 7422 | bc_vec_init(&G.prog.stack, sizeof(BcInstPtr), NULL); |
| 7423 | bc_vec_push(&G.prog.stack, &ip); |
| 7424 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7425 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7426 | static int bc_vm_init(const char *env_len) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7427 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7428 | #if ENABLE_FEATURE_EDITING |
| 7429 | G.line_input_state = new_line_input_t(DO_HISTORY); |
| 7430 | #endif |
| 7431 | G.prog.len = bc_vm_envLen(env_len); |
| 7432 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7433 | bc_vec_init(&G.files, sizeof(char *), NULL); |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7434 | if (IS_BC) |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 7435 | IF_BC(bc_vm_envArgs();) |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7436 | bc_program_init(); |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 7437 | if (IS_BC) { |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 7438 | IF_BC(bc_parse_init(&G.prs, BC_PROG_MAIN);) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 7439 | } else { |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 7440 | IF_DC(dc_parse_init(&G.prs, BC_PROG_MAIN);) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 7441 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7442 | |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7443 | if (isatty(0)) { |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7444 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7445 | G_ttyin = 1; |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7446 | // With SA_RESTART, most system calls will restart |
| 7447 | // (IOW: they won't fail with EINTR). |
| 7448 | // In particular, this means ^C won't cause |
| 7449 | // stdout to get into "error state" if SIGINT hits |
| 7450 | // within write() syscall. |
| 7451 | // The downside is that ^C while line input is taken |
| 7452 | // will only be handled after [Enter] since read() |
| 7453 | // from stdin is not interrupted by ^C either, |
| 7454 | // it restarts, thus fgetc() does not return on ^C. |
| 7455 | signal_SA_RESTART_empty_mask(SIGINT, record_signo); |
| 7456 | |
| 7457 | // Without SA_RESTART, this exhibits a bug: |
| 7458 | // "while (1) print 1" and try ^C-ing it. |
| 7459 | // Intermittently, instead of returning to input line, |
| 7460 | // you'll get "output error: Interrupted system call" |
| 7461 | // and exit. |
| 7462 | //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo); |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7463 | #endif |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7464 | return 1; // "tty" |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7465 | } |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7466 | return 0; // "not a tty" |
| 7467 | } |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7468 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7469 | static BcStatus bc_vm_run(void) |
| 7470 | { |
| 7471 | BcStatus st = bc_vm_exec(); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7472 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 7473 | if (G_exiting) // it was actually "halt" or "quit" |
| 7474 | st = EXIT_SUCCESS; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7475 | bc_vm_free(); |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 7476 | # if ENABLE_FEATURE_EDITING |
| 7477 | free_line_input_t(G.line_input_state); |
| 7478 | # endif |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 7479 | FREE_G(); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7480 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7481 | return st; |
| 7482 | } |
| 7483 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7484 | #if ENABLE_BC |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7485 | int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7486 | int bc_main(int argc UNUSED_PARAM, char **argv) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7487 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7488 | int is_tty; |
| 7489 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7490 | INIT_G(); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7491 | G.sbgn = G.send = '"'; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7492 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7493 | is_tty = bc_vm_init("BC_LINE_LENGTH"); |
| 7494 | |
| 7495 | bc_args(argv); |
| 7496 | |
| 7497 | if (is_tty && !(option_mask32 & BC_FLAG_Q)) |
| 7498 | bc_vm_info(); |
| 7499 | |
| 7500 | return bc_vm_run(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7501 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7502 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7503 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7504 | #if ENABLE_DC |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7505 | int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7506 | int dc_main(int argc UNUSED_PARAM, char **argv) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7507 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7508 | int noscript; |
| 7509 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7510 | INIT_G(); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7511 | G.sbgn = '['; |
| 7512 | G.send = ']'; |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 7513 | /* |
| 7514 | * TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width |
| 7515 | * 1 char wider than bc from the same package. |
| 7516 | * Both default width, and xC_LINE_LENGTH=N are wider: |
| 7517 | * "DC_LINE_LENGTH=5 dc -e'123456 p'" prints: |
| 7518 | * 1234\ |
| 7519 | * 56 |
| 7520 | * "echo '123456' | BC_LINE_LENGTH=5 bc" prints: |
| 7521 | * 123\ |
| 7522 | * 456 |
| 7523 | * Do the same, or it's a bug? |
| 7524 | */ |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7525 | bc_vm_init("DC_LINE_LENGTH"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7526 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7527 | // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs |
| 7528 | noscript = BC_FLAG_I; |
| 7529 | for (;;) { |
| 7530 | int n = getopt(argc, argv, "e:f:x"); |
| 7531 | if (n <= 0) |
| 7532 | break; |
| 7533 | switch (n) { |
| 7534 | case 'e': |
| 7535 | noscript = 0; |
| 7536 | n = bc_vm_process(optarg); |
| 7537 | if (n) return n; |
| 7538 | break; |
| 7539 | case 'f': |
| 7540 | noscript = 0; |
| 7541 | bc_vm_file(optarg); |
| 7542 | break; |
| 7543 | case 'x': |
| 7544 | option_mask32 |= DC_FLAG_X; |
| 7545 | break; |
| 7546 | default: |
| 7547 | bb_show_usage(); |
| 7548 | } |
| 7549 | } |
| 7550 | argv += optind; |
| 7551 | |
| 7552 | while (*argv) { |
| 7553 | noscript = 0; |
| 7554 | bc_vec_push(&G.files, argv++); |
| 7555 | } |
| 7556 | |
| 7557 | option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin |
| 7558 | |
| 7559 | return bc_vm_run(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7560 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7561 | #endif |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 7562 | |
| 7563 | #endif // not DC_SMALL |