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. |
| 5 | * |
| 6 | * ** Automatically generated from https://github.com/gavinhoward/bc ** |
| 7 | * ** Do not edit unless you know what you are doing. ** |
| 8 | */ |
| 9 | //config:config BC |
| 10 | //config: bool "bc (45 kb; 49 kb when combined with dc)" |
| 11 | //config: default y |
| 12 | //config: help |
| 13 | //config: bc is a command-line, arbitrary-precision calculator with a |
| 14 | //config: Turing-complete language. See the GNU bc manual |
| 15 | //config: (https://www.gnu.org/software/bc/manual/bc.html) and bc spec |
| 16 | //config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html) |
| 17 | //config: for details. |
| 18 | //config: |
| 19 | //config: This bc has four differences to the GNU bc: |
| 20 | //config: |
| 21 | //config: 1) The period (.) can also be used as a shortcut for "last", as in |
| 22 | //config: the BSD bc. |
| 23 | //config: 2) Arrays are copied before being passed as arguments to |
| 24 | //config: functions. This behavior is required by the bc spec. |
| 25 | //config: 3) Arrays can be passed to the builtin "length" function to get |
| 26 | //config: the number of elements currently in the array. The following |
| 27 | //config: example prints "1": |
| 28 | //config: |
| 29 | //config: a[0] = 0 |
| 30 | //config: length(a[]) |
| 31 | //config: |
| 32 | //config: 4) The precedence of the boolean "not" operator (!) is equal to |
| 33 | //config: that of the unary minus (-), or negation, operator. This still |
| 34 | //config: allows POSIX-compliant scripts to work while somewhat |
| 35 | //config: preserving expected behavior (versus C) and making parsing |
| 36 | //config: easier. |
| 37 | //config: |
| 38 | //config: Options: |
| 39 | //config: |
| 40 | //config: -i --interactive force interactive mode |
| 41 | //config: -l --mathlib use predefined math routines: |
| 42 | //config: |
| 43 | //config: s(expr) = sine of expr in radians |
| 44 | //config: c(expr) = cosine of expr in radians |
| 45 | //config: a(expr) = arctangent of expr, returning |
| 46 | //config: radians |
| 47 | //config: l(expr) = natural log of expr |
| 48 | //config: e(expr) = raises e to the power of expr |
| 49 | //config: j(n, x) = Bessel function of integer order |
| 50 | //config: n of x |
| 51 | //config: |
| 52 | //config: -q --quiet don't print version and copyright. |
| 53 | //config: -s --standard error if any non-POSIX extensions are used. |
| 54 | //config: -w --warn warn if any non-POSIX extensions are used. |
| 55 | //config: -v --version print version and copyright and exit. |
| 56 | //config: |
| 57 | //config: Long options are only available if FEATURE_BC_LONG_OPTIONS is |
| 58 | //config: enabled. |
| 59 | //config: |
| 60 | //config:config DC |
| 61 | //config: bool "dc (38 kb; 49 kb when combined with bc)" |
| 62 | //config: default y |
| 63 | //config: help |
| 64 | //config: dc is a reverse-polish notation command-line calculator which |
| 65 | //config: supports unlimited precision arithmetic. See the FreeBSD man page |
| 66 | //config: (https://www.unix.com/man-page/FreeBSD/1/dc/) and GNU dc manual |
| 67 | //config: (https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html) |
| 68 | //config: for details. |
| 69 | //config: |
| 70 | //config: This dc has a few differences from the two above: |
| 71 | //config: |
| 72 | //config: 1) When printing a byte stream (command "P"), this bc follows what |
| 73 | //config: the FreeBSD dc does. |
| 74 | //config: 2) This dc implements the GNU extensions for divmod ("~") and |
| 75 | //config: modular exponentiation ("|"). |
| 76 | //config: 3) This dc implements all FreeBSD extensions, except for "J" and |
| 77 | //config: "M". |
| 78 | //config: 4) Like the FreeBSD dc, this dc supports extended registers. |
| 79 | //config: However, they are implemented differently. When it encounters |
| 80 | //config: whitespace where a register should be, it skips the whitespace. |
| 81 | //config: If the character following is not a lowercase letter, an error |
| 82 | //config: is issued. Otherwise, the register name is parsed by the |
| 83 | //config: following regex: |
| 84 | //config: |
| 85 | //config: [a-z][a-z0-9_]* |
| 86 | //config: |
| 87 | //config: This generally means that register names will be surrounded by |
| 88 | //config: whitespace. |
| 89 | //config: |
| 90 | //config: Examples: |
| 91 | //config: |
| 92 | //config: l idx s temp L index S temp2 < do_thing |
| 93 | //config: |
| 94 | //config: Also note that, like the FreeBSD dc, extended registers are not |
| 95 | //config: allowed unless the "-x" option is given. |
| 96 | //config: |
| 97 | //config:config FEATURE_BC_SIGNALS |
| 98 | //config: bool "Enable bc/dc signal handling" |
| 99 | //config: default y |
| 100 | //config: depends on BC || DC |
| 101 | //config: help |
| 102 | //config: Enable signal handling for bc and dc. |
| 103 | //config: |
| 104 | //config:config FEATURE_BC_LONG_OPTIONS |
| 105 | //config: bool "Enable bc/dc long options" |
| 106 | //config: default y |
| 107 | //config: depends on BC || DC |
| 108 | //config: help |
| 109 | //config: Enable long options for bc and dc. |
| 110 | |
| 111 | //applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 112 | //applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 113 | |
| 114 | //kbuild:lib-$(CONFIG_BC) += bc.o |
| 115 | //kbuild:lib-$(CONFIG_DC) += bc.o |
| 116 | |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 117 | //See www.gnu.org/software/bc/manual/bc.html |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 118 | //usage:#define bc_trivial_usage |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 119 | //usage: "[-sqli] FILE..." |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 120 | //usage: |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 121 | //usage:#define bc_full_usage "\n" |
| 122 | //usage: "\nArbitrary precision calculator" |
| 123 | //usage: "\n" |
| 124 | //usage: "\n -i Interactive" |
| 125 | //usage: "\n -l Load standard math library" |
| 126 | //usage: "\n -s Be POSIX compatible" |
| 127 | //usage: "\n -q Quiet" |
| 128 | //usage: "\n -w Warn if extensions are used" |
| 129 | ///////: "\n -v Version" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 130 | //usage: |
| 131 | //usage:#define bc_example_usage |
| 132 | //usage: "3 + 4.129\n" |
| 133 | //usage: "1903 - 2893\n" |
| 134 | //usage: "-129 * 213.28935\n" |
| 135 | //usage: "12 / -1932\n" |
| 136 | //usage: "12 % 12\n" |
| 137 | //usage: "34 ^ 189\n" |
| 138 | //usage: "scale = 13\n" |
| 139 | //usage: "ibase = 2\n" |
| 140 | //usage: "obase = A\n" |
| 141 | //usage: |
| 142 | //usage:#define dc_trivial_usage |
| 143 | //usage: "EXPRESSION..." |
| 144 | //usage: |
| 145 | //usage:#define dc_full_usage "\n\n" |
| 146 | //usage: "Tiny RPN calculator. Operations:\n" |
| 147 | //usage: "+, add, -, sub, *, mul, /, div, %, mod, ^, exp, ~, divmod, |, " |
| 148 | //usage: "modular exponentiation,\n" |
| 149 | //usage: "p - print top of the stack (without popping),\n" |
| 150 | //usage: "f - print entire stack,\n" |
| 151 | //usage: "k - pop the value and set the precision.\n" |
| 152 | //usage: "i - pop the value and set input radix.\n" |
| 153 | //usage: "o - pop the value and set output radix.\n" |
| 154 | //usage: "Examples: 'dc 2 2 add p' -> 4, 'dc 8 8 mul 2 2 + / p' -> 16" |
| 155 | //usage: |
| 156 | //usage:#define dc_example_usage |
| 157 | //usage: "$ dc 2 2 + p\n" |
| 158 | //usage: "4\n" |
| 159 | //usage: "$ dc 8 8 \\* 2 2 + / p\n" |
| 160 | //usage: "16\n" |
| 161 | //usage: "$ dc 0 1 and p\n" |
| 162 | //usage: "0\n" |
| 163 | //usage: "$ dc 0 1 or p\n" |
| 164 | //usage: "1\n" |
| 165 | //usage: "$ echo 72 9 div 8 mul p | dc\n" |
| 166 | //usage: "64\n" |
| 167 | |
| 168 | #include "libbb.h" |
| 169 | |
| 170 | typedef enum BcStatus { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 171 | BC_STATUS_SUCCESS = 0, |
| 172 | BC_STATUS_FAILURE = 1, |
| 173 | BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr() uses this |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 174 | } BcStatus; |
| 175 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 176 | #define BC_VEC_INVALID_IDX ((size_t) -1) |
| 177 | #define BC_VEC_START_CAP (1 << 5) |
| 178 | |
| 179 | typedef void (*BcVecFree)(void *); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 180 | |
| 181 | typedef struct BcVec { |
| 182 | char *v; |
| 183 | size_t len; |
| 184 | size_t cap; |
| 185 | size_t size; |
| 186 | BcVecFree dtor; |
| 187 | } BcVec; |
| 188 | |
| 189 | #define bc_vec_pop(v) (bc_vec_npop((v), 1)) |
| 190 | #define bc_vec_top(v) (bc_vec_item_rev((v), 0)) |
| 191 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 192 | typedef signed char BcDig; |
| 193 | |
| 194 | typedef struct BcNum { |
| 195 | BcDig *restrict num; |
| 196 | size_t rdx; |
| 197 | size_t len; |
| 198 | size_t cap; |
| 199 | bool neg; |
| 200 | } BcNum; |
| 201 | |
| 202 | #define BC_NUM_MIN_BASE ((unsigned long) 2) |
| 203 | #define BC_NUM_MAX_IBASE ((unsigned long) 16) |
| 204 | #define BC_NUM_DEF_SIZE (16) |
| 205 | #define BC_NUM_PRINT_WIDTH (69) |
| 206 | |
| 207 | #define BC_NUM_KARATSUBA_LEN (32) |
| 208 | |
| 209 | #define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg)) |
| 210 | #define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1) |
| 211 | #define BC_NUM_INT(n) ((n)->len - (n)->rdx) |
| 212 | #define BC_NUM_AREQ(a, b) \ |
| 213 | (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1) |
| 214 | #define BC_NUM_MREQ(a, b, scale) \ |
| 215 | (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1) |
| 216 | |
| 217 | typedef BcStatus (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t); |
| 218 | typedef void (*BcNumDigitOp)(size_t, size_t, bool, size_t *, size_t); |
| 219 | |
| 220 | static void bc_num_init(BcNum *n, size_t req); |
| 221 | static void bc_num_expand(BcNum *n, size_t req); |
| 222 | static void bc_num_copy(BcNum *d, BcNum *s); |
| 223 | static void bc_num_free(void *num); |
| 224 | |
| 225 | static BcStatus bc_num_ulong(BcNum *n, unsigned long *result); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 226 | static void bc_num_ulong2num(BcNum *n, unsigned long val); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 227 | |
| 228 | static BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale); |
| 229 | static BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale); |
| 230 | static BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale); |
| 231 | static BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale); |
| 232 | static BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale); |
| 233 | static BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale); |
| 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 | |
| 390 | static void bc_array_expand(BcVec *a, size_t len); |
| 391 | static int bc_id_cmp(const void *e1, const void *e2); |
| 392 | |
| 393 | // BC_LEX_NEG is not used in lexing; it is only for parsing. |
| 394 | typedef enum BcLexType { |
| 395 | |
| 396 | BC_LEX_EOF, |
| 397 | BC_LEX_INVALID, |
| 398 | |
| 399 | BC_LEX_OP_INC, |
| 400 | BC_LEX_OP_DEC, |
| 401 | |
| 402 | BC_LEX_NEG, |
| 403 | |
| 404 | BC_LEX_OP_POWER, |
| 405 | BC_LEX_OP_MULTIPLY, |
| 406 | BC_LEX_OP_DIVIDE, |
| 407 | BC_LEX_OP_MODULUS, |
| 408 | BC_LEX_OP_PLUS, |
| 409 | BC_LEX_OP_MINUS, |
| 410 | |
| 411 | BC_LEX_OP_REL_EQ, |
| 412 | BC_LEX_OP_REL_LE, |
| 413 | BC_LEX_OP_REL_GE, |
| 414 | BC_LEX_OP_REL_NE, |
| 415 | BC_LEX_OP_REL_LT, |
| 416 | BC_LEX_OP_REL_GT, |
| 417 | |
| 418 | BC_LEX_OP_BOOL_NOT, |
| 419 | BC_LEX_OP_BOOL_OR, |
| 420 | BC_LEX_OP_BOOL_AND, |
| 421 | |
| 422 | BC_LEX_OP_ASSIGN_POWER, |
| 423 | BC_LEX_OP_ASSIGN_MULTIPLY, |
| 424 | BC_LEX_OP_ASSIGN_DIVIDE, |
| 425 | BC_LEX_OP_ASSIGN_MODULUS, |
| 426 | BC_LEX_OP_ASSIGN_PLUS, |
| 427 | BC_LEX_OP_ASSIGN_MINUS, |
| 428 | BC_LEX_OP_ASSIGN, |
| 429 | |
| 430 | BC_LEX_NLINE, |
| 431 | BC_LEX_WHITESPACE, |
| 432 | |
| 433 | BC_LEX_LPAREN, |
| 434 | BC_LEX_RPAREN, |
| 435 | |
| 436 | BC_LEX_LBRACKET, |
| 437 | BC_LEX_COMMA, |
| 438 | BC_LEX_RBRACKET, |
| 439 | |
| 440 | BC_LEX_LBRACE, |
| 441 | BC_LEX_SCOLON, |
| 442 | BC_LEX_RBRACE, |
| 443 | |
| 444 | BC_LEX_STR, |
| 445 | BC_LEX_NAME, |
| 446 | BC_LEX_NUMBER, |
| 447 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 448 | BC_LEX_KEY_1st_keyword, |
| 449 | BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 450 | BC_LEX_KEY_BREAK, |
| 451 | BC_LEX_KEY_CONTINUE, |
| 452 | BC_LEX_KEY_DEFINE, |
| 453 | BC_LEX_KEY_ELSE, |
| 454 | BC_LEX_KEY_FOR, |
| 455 | BC_LEX_KEY_HALT, |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 456 | // code uses "type - BC_LEX_KEY_IBASE + BC_INST_IBASE" construct, |
| 457 | BC_LEX_KEY_IBASE, // relative order should match for: BC_INST_IBASE |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 458 | BC_LEX_KEY_IF, |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 459 | BC_LEX_KEY_LAST, // relative order should match for: BC_INST_LAST |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 460 | BC_LEX_KEY_LENGTH, |
| 461 | BC_LEX_KEY_LIMITS, |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 462 | BC_LEX_KEY_OBASE, // relative order should match for: BC_INST_OBASE |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 463 | BC_LEX_KEY_PRINT, |
| 464 | BC_LEX_KEY_QUIT, |
| 465 | BC_LEX_KEY_READ, |
| 466 | BC_LEX_KEY_RETURN, |
| 467 | BC_LEX_KEY_SCALE, |
| 468 | BC_LEX_KEY_SQRT, |
| 469 | BC_LEX_KEY_WHILE, |
| 470 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 471 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 472 | BC_LEX_EQ_NO_REG, |
| 473 | BC_LEX_OP_MODEXP, |
| 474 | BC_LEX_OP_DIVMOD, |
| 475 | |
| 476 | BC_LEX_COLON, |
| 477 | BC_LEX_ELSE, |
| 478 | BC_LEX_EXECUTE, |
| 479 | BC_LEX_PRINT_STACK, |
| 480 | BC_LEX_CLEAR_STACK, |
| 481 | BC_LEX_STACK_LEVEL, |
| 482 | BC_LEX_DUPLICATE, |
| 483 | BC_LEX_SWAP, |
| 484 | BC_LEX_POP, |
| 485 | |
| 486 | BC_LEX_ASCIIFY, |
| 487 | BC_LEX_PRINT_STREAM, |
| 488 | |
| 489 | BC_LEX_STORE_IBASE, |
| 490 | BC_LEX_STORE_SCALE, |
| 491 | BC_LEX_LOAD, |
| 492 | BC_LEX_LOAD_POP, |
| 493 | BC_LEX_STORE_PUSH, |
| 494 | BC_LEX_STORE_OBASE, |
| 495 | BC_LEX_PRINT_POP, |
| 496 | BC_LEX_NQUIT, |
| 497 | BC_LEX_SCALE_FACTOR, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 498 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 499 | } BcLexType; |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 500 | // must match order of BC_LEX_KEY_foo etc above |
| 501 | #if ENABLE_BC |
| 502 | struct BcLexKeyword { |
| 503 | char name8[8]; |
| 504 | }; |
| 505 | #define BC_LEX_KW_ENTRY(a, b, c) \ |
| 506 | { .name8 = a /*, .len = b, .posix = c*/ } |
| 507 | static const struct BcLexKeyword bc_lex_kws[20] = { |
| 508 | BC_LEX_KW_ENTRY("auto" , 4, 1), // 0 |
| 509 | BC_LEX_KW_ENTRY("break" , 5, 1), // 1 |
| 510 | BC_LEX_KW_ENTRY("continue", 8, 0), // 2 note: this one has no terminating NUL |
| 511 | BC_LEX_KW_ENTRY("define" , 6, 1), // 3 |
| 512 | |
| 513 | BC_LEX_KW_ENTRY("else" , 4, 0), // 4 |
| 514 | BC_LEX_KW_ENTRY("for" , 3, 1), // 5 |
| 515 | BC_LEX_KW_ENTRY("halt" , 4, 0), // 6 |
| 516 | BC_LEX_KW_ENTRY("ibase" , 5, 1), // 7 |
| 517 | |
| 518 | BC_LEX_KW_ENTRY("if" , 2, 1), // 8 |
| 519 | BC_LEX_KW_ENTRY("last" , 4, 0), // 9 |
| 520 | BC_LEX_KW_ENTRY("length" , 6, 1), // 10 |
| 521 | BC_LEX_KW_ENTRY("limits" , 6, 0), // 11 |
| 522 | |
| 523 | BC_LEX_KW_ENTRY("obase" , 5, 1), // 12 |
| 524 | BC_LEX_KW_ENTRY("print" , 5, 0), // 13 |
| 525 | BC_LEX_KW_ENTRY("quit" , 4, 1), // 14 |
| 526 | BC_LEX_KW_ENTRY("read" , 4, 0), // 15 |
| 527 | |
| 528 | BC_LEX_KW_ENTRY("return" , 6, 1), // 16 |
| 529 | BC_LEX_KW_ENTRY("scale" , 5, 1), // 17 |
| 530 | BC_LEX_KW_ENTRY("sqrt" , 4, 1), // 18 |
| 531 | BC_LEX_KW_ENTRY("while" , 5, 1), // 19 |
| 532 | }; |
| 533 | enum { |
| 534 | POSIX_KWORD_MASK = 0 |
| 535 | | (1 << 0) |
| 536 | | (1 << 1) |
| 537 | | (0 << 2) |
| 538 | | (1 << 3) |
| 539 | \ |
| 540 | | (0 << 4) |
| 541 | | (1 << 5) |
| 542 | | (0 << 6) |
| 543 | | (1 << 7) |
| 544 | \ |
| 545 | | (1 << 8) |
| 546 | | (0 << 9) |
| 547 | | (1 << 10) |
| 548 | | (0 << 11) |
| 549 | \ |
| 550 | | (1 << 12) |
| 551 | | (0 << 13) |
| 552 | | (1 << 14) |
| 553 | | (0 << 15) |
| 554 | \ |
| 555 | | (1 << 16) |
| 556 | | (1 << 17) |
| 557 | | (1 << 18) |
| 558 | | (1 << 19) |
| 559 | }; |
| 560 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 561 | |
| 562 | struct BcLex; |
| 563 | typedef BcStatus (*BcLexNext)(struct BcLex *); |
| 564 | |
| 565 | typedef struct BcLex { |
| 566 | |
| 567 | const char *buf; |
| 568 | size_t i; |
| 569 | size_t line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 570 | size_t len; |
| 571 | bool newline; |
| 572 | |
| 573 | struct { |
| 574 | BcLexType t; |
| 575 | BcLexType last; |
| 576 | BcVec v; |
| 577 | } t; |
| 578 | |
| 579 | BcLexNext next; |
| 580 | |
| 581 | } BcLex; |
| 582 | |
| 583 | #define BC_PARSE_STREND ((char) UCHAR_MAX) |
| 584 | |
| 585 | #define bc_parse_push(p, i) (bc_vec_pushByte(&(p)->func->code, (char) (i))) |
| 586 | #define bc_parse_updateFunc(p, f) \ |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 587 | ((p)->func = bc_vec_item(&G.prog.fns, ((p)->fidx = (f)))) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 588 | |
| 589 | #define BC_PARSE_REL (1 << 0) |
| 590 | #define BC_PARSE_PRINT (1 << 1) |
| 591 | #define BC_PARSE_NOCALL (1 << 2) |
| 592 | #define BC_PARSE_NOREAD (1 << 3) |
| 593 | #define BC_PARSE_ARRAY (1 << 4) |
| 594 | |
| 595 | #define BC_PARSE_TOP_FLAG_PTR(parse) ((uint8_t *) bc_vec_top(&(parse)->flags)) |
| 596 | #define BC_PARSE_TOP_FLAG(parse) (*(BC_PARSE_TOP_FLAG_PTR(parse))) |
| 597 | |
| 598 | #define BC_PARSE_FLAG_FUNC_INNER (1 << 0) |
| 599 | #define BC_PARSE_FUNC_INNER(parse) \ |
| 600 | (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC_INNER) |
| 601 | |
| 602 | #define BC_PARSE_FLAG_FUNC (1 << 1) |
| 603 | #define BC_PARSE_FUNC(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC) |
| 604 | |
| 605 | #define BC_PARSE_FLAG_BODY (1 << 2) |
| 606 | #define BC_PARSE_BODY(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_BODY) |
| 607 | |
| 608 | #define BC_PARSE_FLAG_LOOP (1 << 3) |
| 609 | #define BC_PARSE_LOOP(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP) |
| 610 | |
| 611 | #define BC_PARSE_FLAG_LOOP_INNER (1 << 4) |
| 612 | #define BC_PARSE_LOOP_INNER(parse) \ |
| 613 | (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP_INNER) |
| 614 | |
| 615 | #define BC_PARSE_FLAG_IF (1 << 5) |
| 616 | #define BC_PARSE_IF(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF) |
| 617 | |
| 618 | #define BC_PARSE_FLAG_ELSE (1 << 6) |
| 619 | #define BC_PARSE_ELSE(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_ELSE) |
| 620 | |
| 621 | #define BC_PARSE_FLAG_IF_END (1 << 7) |
| 622 | #define BC_PARSE_IF_END(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF_END) |
| 623 | |
| 624 | #define BC_PARSE_CAN_EXEC(parse) \ |
| 625 | (!(BC_PARSE_TOP_FLAG(parse) & \ |
| 626 | (BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_BODY | \ |
| 627 | BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER | BC_PARSE_FLAG_IF | \ |
| 628 | BC_PARSE_FLAG_ELSE | BC_PARSE_FLAG_IF_END))) |
| 629 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 630 | typedef struct BcParseNext { |
| 631 | uint32_t len; |
| 632 | BcLexType tokens[4]; |
| 633 | } BcParseNext; |
| 634 | |
| 635 | #define BC_PARSE_NEXT_TOKENS(...) .tokens = { __VA_ARGS__ } |
| 636 | #define BC_PARSE_NEXT(a, ...) \ |
| 637 | { \ |
| 638 | .len = (a), BC_PARSE_NEXT_TOKENS(__VA_ARGS__) \ |
| 639 | } |
| 640 | |
| 641 | struct BcParse; |
| 642 | |
| 643 | struct BcProgram; |
| 644 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 645 | typedef BcStatus (*BcParseParse)(struct BcParse *); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 646 | |
| 647 | typedef struct BcParse { |
| 648 | |
| 649 | BcParseParse parse; |
| 650 | |
| 651 | BcLex l; |
| 652 | |
| 653 | BcVec flags; |
| 654 | |
| 655 | BcVec exits; |
| 656 | BcVec conds; |
| 657 | |
| 658 | BcVec ops; |
| 659 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 660 | BcFunc *func; |
| 661 | size_t fidx; |
| 662 | |
| 663 | size_t nbraces; |
| 664 | bool auto_part; |
| 665 | |
| 666 | } BcParse; |
| 667 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 668 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 669 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 670 | static BcStatus bc_lex_token(BcLex *l); |
| 671 | |
| 672 | #define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops))) |
| 673 | #define BC_PARSE_LEAF(p, rparen) \ |
| 674 | (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \ |
| 675 | (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST) |
| 676 | |
| 677 | // We can calculate the conversion between tokens and exprs by subtracting the |
| 678 | // position of the first operator in the lex enum and adding the position of the |
| 679 | // first in the expr enum. Note: This only works for binary operators. |
| 680 | #define BC_PARSE_TOKEN_INST(t) ((char) ((t) -BC_LEX_NEG + BC_INST_NEG)) |
| 681 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 682 | static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next); |
| 683 | |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 684 | #endif // ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 685 | |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 686 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 687 | |
| 688 | #define DC_PARSE_BUF_LEN ((int) (sizeof(uint32_t) * CHAR_BIT)) |
| 689 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 690 | static BcStatus dc_lex_token(BcLex *l); |
| 691 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 692 | static BcStatus dc_parse_expr(BcParse *p, uint8_t flags); |
| 693 | |
| 694 | #endif // ENABLE_DC |
| 695 | |
| 696 | typedef struct BcProgram { |
| 697 | |
| 698 | size_t len; |
| 699 | size_t scale; |
| 700 | |
| 701 | BcNum ib; |
| 702 | size_t ib_t; |
| 703 | BcNum ob; |
| 704 | size_t ob_t; |
| 705 | |
| 706 | BcNum hexb; |
| 707 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 708 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 709 | BcNum strmb; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 710 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 711 | |
| 712 | BcVec results; |
| 713 | BcVec stack; |
| 714 | |
| 715 | BcVec fns; |
| 716 | BcVec fn_map; |
| 717 | |
| 718 | BcVec vars; |
| 719 | BcVec var_map; |
| 720 | |
| 721 | BcVec arrs; |
| 722 | BcVec arr_map; |
| 723 | |
| 724 | BcVec strs; |
| 725 | BcVec consts; |
| 726 | |
| 727 | const char *file; |
| 728 | |
| 729 | BcNum last; |
| 730 | BcNum zero; |
| 731 | BcNum one; |
| 732 | |
| 733 | size_t nchars; |
| 734 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 735 | } BcProgram; |
| 736 | |
| 737 | #define BC_PROG_STACK(s, n) ((s)->len >= ((size_t) n)) |
| 738 | |
| 739 | #define BC_PROG_MAIN (0) |
| 740 | #define BC_PROG_READ (1) |
| 741 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 742 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 743 | #define BC_PROG_REQ_FUNCS (2) |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 744 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 745 | |
| 746 | #define BC_PROG_STR(n) (!(n)->num && !(n)->cap) |
| 747 | #define BC_PROG_NUM(r, n) \ |
| 748 | ((r)->t != BC_RESULT_ARRAY && (r)->t != BC_RESULT_STR && !BC_PROG_STR(n)) |
| 749 | |
| 750 | typedef unsigned long (*BcProgramBuiltIn)(BcNum *); |
| 751 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 752 | static void bc_program_addFunc(char *name, size_t *idx); |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 753 | static void bc_program_reset(void); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 754 | |
| 755 | #define BC_FLAG_X (1 << 0) |
| 756 | #define BC_FLAG_W (1 << 1) |
| 757 | #define BC_FLAG_V (1 << 2) |
| 758 | #define BC_FLAG_S (1 << 3) |
| 759 | #define BC_FLAG_Q (1 << 4) |
| 760 | #define BC_FLAG_L (1 << 5) |
| 761 | #define BC_FLAG_I (1 << 6) |
| 762 | |
| 763 | #define BC_MAX(a, b) ((a) > (b) ? (a) : (b)) |
| 764 | #define BC_MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 765 | |
Denys Vlasenko | d9d6655 | 2018-12-02 21:02:54 +0100 | [diff] [blame] | 766 | #define BC_MAX_OBASE ((unsigned) 999) |
| 767 | #define BC_MAX_DIM ((unsigned) INT_MAX) |
| 768 | #define BC_MAX_SCALE ((unsigned) UINT_MAX) |
| 769 | #define BC_MAX_STRING ((unsigned) UINT_MAX - 1) |
| 770 | #define BC_MAX_NAME BC_MAX_STRING |
| 771 | #define BC_MAX_NUM BC_MAX_STRING |
| 772 | #define BC_MAX_EXP ((unsigned long) LONG_MAX) |
| 773 | #define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 774 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 775 | struct globals { |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 776 | smallint ttyin; |
| 777 | smallint eof; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 778 | char sbgn; |
| 779 | char send; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 780 | |
| 781 | BcParse prs; |
| 782 | BcProgram prog; |
| 783 | |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 784 | // For error messages. Can be set to current parsed line, |
| 785 | // or [TODO] to current executing line (can be before last parsed one) |
| 786 | unsigned err_line; |
| 787 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 788 | BcVec files; |
| 789 | |
| 790 | char *env_args; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 791 | } FIX_ALIASING; |
| 792 | #define G (*ptr_to_globals) |
| 793 | #define INIT_G() do { \ |
| 794 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 795 | } while (0) |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 796 | #define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S)) |
| 797 | #define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W)) |
| 798 | #define G_exreg (ENABLE_DC && (option_mask32 & BC_FLAG_X)) |
Denys Vlasenko | ab3c568 | 2018-12-02 16:32:36 +0100 | [diff] [blame] | 799 | #define G_interrupt (ENABLE_FEATURE_BC_SIGNALS ? bb_got_signal : 0) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 800 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 801 | |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 802 | #define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b')) |
| 803 | |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 804 | static void bc_vm_info(void); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 805 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 806 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 807 | |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 808 | // 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] | 809 | // true if the token is valid in an expression, false otherwise. |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 810 | enum { |
| 811 | BC_PARSE_EXPRS_BITS = 0 |
| 812 | + ((uint64_t)((0 << 0)+(0 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (0*8)) |
| 813 | + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (1*8)) |
| 814 | + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (2*8)) |
| 815 | + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(0 << 3)+(0 << 4)+(1 << 5)+(1 << 6)+(0 << 7)) << (3*8)) |
| 816 | + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(1 << 6)+(1 << 7)) << (4*8)) |
| 817 | + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (5*8)) |
| 818 | + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (6*8)) |
| 819 | + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(0 << 3) ) << (7*8)) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 820 | }; |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 821 | static ALWAYS_INLINE long bc_parse_exprs(unsigned i) |
| 822 | { |
| 823 | #if ULONG_MAX > 0xffffffff |
| 824 | // 64-bit version (will not work correctly for 32-bit longs!) |
| 825 | return BC_PARSE_EXPRS_BITS & (1UL << i); |
| 826 | #else |
| 827 | // 32-bit version |
| 828 | unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS; |
| 829 | if (i >= 32) { |
| 830 | m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32); |
| 831 | i &= 31; |
| 832 | } |
| 833 | return m & (1UL << i); |
| 834 | #endif |
| 835 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 836 | |
| 837 | // 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] | 838 | static const uint8_t bc_parse_ops[] = { |
| 839 | #define OP(p,l) ((int)(l) * 0x10 + (p)) |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 840 | OP(0, false), OP( 0, false ), // inc dec |
| 841 | OP(1, false), // neg |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 842 | OP(2, false), |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 843 | OP(3, true ), OP( 3, true ), OP( 3, true ), // pow mul div |
| 844 | OP(4, true ), OP( 4, true ), // mod + - |
| 845 | OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < > |
| 846 | OP(1, false), // not |
| 847 | OP(7, true ), OP( 7, true ), // or and |
| 848 | OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= += |
| 849 | OP(5, false), OP( 5, false ), // -= = |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 850 | #undef OP |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 851 | }; |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 852 | #define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f) |
| 853 | #define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 854 | |
| 855 | // These identify what tokens can come after expressions in certain cases. |
| 856 | static const BcParseNext bc_parse_next_expr = |
| 857 | BC_PARSE_NEXT(4, BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_RBRACE, BC_LEX_EOF); |
| 858 | static const BcParseNext bc_parse_next_param = |
| 859 | BC_PARSE_NEXT(2, BC_LEX_RPAREN, BC_LEX_COMMA); |
| 860 | static const BcParseNext bc_parse_next_print = |
| 861 | BC_PARSE_NEXT(4, BC_LEX_COMMA, BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_EOF); |
| 862 | static const BcParseNext bc_parse_next_rel = BC_PARSE_NEXT(1, BC_LEX_RPAREN); |
| 863 | static const BcParseNext bc_parse_next_elem = BC_PARSE_NEXT(1, BC_LEX_RBRACKET); |
| 864 | static const BcParseNext bc_parse_next_for = BC_PARSE_NEXT(1, BC_LEX_SCOLON); |
| 865 | static const BcParseNext bc_parse_next_read = |
| 866 | BC_PARSE_NEXT(2, BC_LEX_NLINE, BC_LEX_EOF); |
| 867 | #endif // ENABLE_BC |
| 868 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 869 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 870 | static const BcLexType dc_lex_regs[] = { |
| 871 | BC_LEX_OP_REL_EQ, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_NE, |
| 872 | BC_LEX_OP_REL_LT, BC_LEX_OP_REL_GT, BC_LEX_SCOLON, BC_LEX_COLON, |
| 873 | BC_LEX_ELSE, BC_LEX_LOAD, BC_LEX_LOAD_POP, BC_LEX_OP_ASSIGN, |
| 874 | BC_LEX_STORE_PUSH, |
| 875 | }; |
| 876 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 877 | static const BcLexType dc_lex_tokens[] = { |
| 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 | |
| 905 | static const BcInst dc_parse_insts[] = { |
| 906 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE, |
| 907 | BC_INST_INVALID, BC_INST_POWER, BC_INST_MULTIPLY, BC_INST_DIVIDE, |
| 908 | BC_INST_MODULUS, BC_INST_PLUS, BC_INST_MINUS, |
| 909 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 910 | BC_INST_INVALID, BC_INST_INVALID, |
| 911 | BC_INST_BOOL_NOT, BC_INST_INVALID, BC_INST_INVALID, |
| 912 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 913 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 914 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GT, BC_INST_INVALID, |
| 915 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE, |
| 916 | BC_INST_INVALID, BC_INST_INVALID, |
| 917 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 918 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 919 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_IBASE, |
| 920 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_LENGTH, BC_INST_INVALID, |
| 921 | BC_INST_OBASE, BC_INST_PRINT, BC_INST_QUIT, BC_INST_INVALID, |
| 922 | BC_INST_INVALID, BC_INST_SCALE, BC_INST_SQRT, BC_INST_INVALID, |
| 923 | BC_INST_REL_EQ, BC_INST_MODEXP, BC_INST_DIVMOD, BC_INST_INVALID, |
| 924 | BC_INST_INVALID, BC_INST_EXECUTE, BC_INST_PRINT_STACK, BC_INST_CLEAR_STACK, |
| 925 | BC_INST_STACK_LEN, BC_INST_DUPLICATE, BC_INST_SWAP, BC_INST_POP, |
| 926 | BC_INST_ASCIIFY, BC_INST_PRINT_STREAM, BC_INST_INVALID, BC_INST_INVALID, |
| 927 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 928 | BC_INST_PRINT, BC_INST_NQUIT, BC_INST_SCALE_FUNC, |
| 929 | }; |
| 930 | #endif // ENABLE_DC |
| 931 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 932 | static const BcNumBinaryOp bc_program_ops[] = { |
| 933 | bc_num_pow, bc_num_mul, bc_num_div, bc_num_mod, bc_num_add, bc_num_sub, |
| 934 | }; |
| 935 | |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 936 | static void fflush_and_check(void) |
| 937 | { |
| 938 | fflush_all(); |
| 939 | if (ferror(stdout) || ferror(stderr)) |
| 940 | bb_perror_msg_and_die("output error"); |
| 941 | } |
| 942 | |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 943 | static void quit(void) NORETURN; |
| 944 | static void quit(void) |
| 945 | { |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 946 | if (ferror(stdin)) |
| 947 | bb_perror_msg_and_die("input error"); |
| 948 | fflush_and_check(); |
| 949 | exit(0); |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 950 | } |
| 951 | |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 952 | static void bc_verror_msg(const char *fmt, va_list p) |
| 953 | { |
| 954 | const char *sv = sv; /* for compiler */ |
| 955 | if (G.prog.file) { |
| 956 | sv = applet_name; |
| 957 | applet_name = xasprintf("%s: %s:%u", applet_name, G.prog.file, G.err_line); |
| 958 | } |
| 959 | bb_verror_msg(fmt, p, NULL); |
| 960 | if (G.prog.file) { |
| 961 | free((char*)applet_name); |
| 962 | applet_name = sv; |
| 963 | } |
| 964 | } |
| 965 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 966 | static NOINLINE int bc_error_fmt(const char *fmt, ...) |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 967 | { |
| 968 | va_list p; |
| 969 | |
| 970 | va_start(p, fmt); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 971 | bc_verror_msg(fmt, p); |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 972 | va_end(p); |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 973 | |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 974 | if (!G.ttyin) |
| 975 | exit(1); |
| 976 | return BC_STATUS_FAILURE; |
| 977 | } |
| 978 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 979 | static NOINLINE int bc_posix_error_fmt(const char *fmt, ...) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 980 | { |
| 981 | va_list p; |
| 982 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 983 | // Are non-POSIX constructs totally ok? |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 984 | if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W))) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 985 | return BC_STATUS_SUCCESS; // yes |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 986 | |
| 987 | va_start(p, fmt); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 988 | bc_verror_msg(fmt, p); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 989 | va_end(p); |
| 990 | |
| 991 | // Do we treat non-POSIX constructs as errors? |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 992 | if (!(option_mask32 & BC_FLAG_S)) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 993 | return BC_STATUS_SUCCESS; // no, it's a warning |
| 994 | if (!G.ttyin) |
| 995 | exit(1); |
| 996 | return BC_STATUS_FAILURE; |
| 997 | } |
| 998 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 999 | // We use error functions with "return bc_error(FMT[, PARAMS])" idiom. |
| 1000 | // This idiom begs for tail-call optimization, but for it to work, |
| 1001 | // function must not have calller-cleaned parameters on stack. |
| 1002 | // Unfortunately, vararg functions do exactly that on most arches. |
| 1003 | // Thus, these shims for the cases when we have no PARAMS: |
| 1004 | static int bc_error(const char *msg) |
| 1005 | { |
| 1006 | return bc_error_fmt("%s", msg); |
| 1007 | } |
| 1008 | static int bc_posix_error(const char *msg) |
| 1009 | { |
| 1010 | return bc_posix_error_fmt("%s", msg); |
| 1011 | } |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 1012 | static int bc_POSIX_does_not_allow(const char *msg) |
| 1013 | { |
| 1014 | return bc_posix_error_fmt("%s%s", "POSIX does not allow ", msg); |
| 1015 | } |
| 1016 | static int bc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg) |
| 1017 | { |
| 1018 | return bc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; the following is bad:", msg); |
| 1019 | } |
| 1020 | static int bc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg) |
| 1021 | { |
| 1022 | return bc_posix_error_fmt("%san empty %s expression in a for loop", "POSIX does not allow ", msg); |
| 1023 | } |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1024 | static int bc_error_bad_character(char c) |
| 1025 | { |
| 1026 | return bc_error_fmt("bad character '%c'", c); |
| 1027 | } |
| 1028 | static int bc_error_bad_expression(void) |
| 1029 | { |
| 1030 | return bc_error("bad expression"); |
| 1031 | } |
| 1032 | static int bc_error_bad_token(void) |
| 1033 | { |
| 1034 | return bc_error("bad token"); |
| 1035 | } |
| 1036 | static int bc_error_stack_has_too_few_elements(void) |
| 1037 | { |
| 1038 | return bc_error("stack has too few elements"); |
| 1039 | } |
| 1040 | static int bc_error_variable_is_wrong_type(void) |
| 1041 | { |
| 1042 | return bc_error("variable is wrong type"); |
| 1043 | } |
| 1044 | static int bc_error_nested_read_call(void) |
| 1045 | { |
| 1046 | return bc_error("read() call inside of a read() call"); |
| 1047 | } |
| 1048 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1049 | static void bc_vec_grow(BcVec *v, size_t n) |
| 1050 | { |
| 1051 | size_t cap = v->cap * 2; |
| 1052 | while (cap < v->len + n) cap *= 2; |
| 1053 | v->v = xrealloc(v->v, v->size * cap); |
| 1054 | v->cap = cap; |
| 1055 | } |
| 1056 | |
| 1057 | static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor) |
| 1058 | { |
| 1059 | v->size = esize; |
| 1060 | v->cap = BC_VEC_START_CAP; |
| 1061 | v->len = 0; |
| 1062 | v->dtor = dtor; |
| 1063 | v->v = xmalloc(esize * BC_VEC_START_CAP); |
| 1064 | } |
| 1065 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1066 | static void bc_char_vec_init(BcVec *v) |
| 1067 | { |
| 1068 | bc_vec_init(v, sizeof(char), NULL); |
| 1069 | } |
| 1070 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1071 | static void bc_vec_expand(BcVec *v, size_t req) |
| 1072 | { |
| 1073 | if (v->cap < req) { |
| 1074 | v->v = xrealloc(v->v, v->size * req); |
| 1075 | v->cap = req; |
| 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | static void bc_vec_npop(BcVec *v, size_t n) |
| 1080 | { |
| 1081 | if (!v->dtor) |
| 1082 | v->len -= n; |
| 1083 | else { |
| 1084 | size_t len = v->len - n; |
| 1085 | while (v->len > len) v->dtor(v->v + (v->size * --v->len)); |
| 1086 | } |
| 1087 | } |
| 1088 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1089 | static void bc_vec_pop_all(BcVec *v) |
| 1090 | { |
| 1091 | bc_vec_npop(v, v->len); |
| 1092 | } |
| 1093 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1094 | static void bc_vec_push(BcVec *v, const void *data) |
| 1095 | { |
| 1096 | if (v->len + 1 > v->cap) bc_vec_grow(v, 1); |
| 1097 | memmove(v->v + (v->size * v->len), data, v->size); |
| 1098 | v->len += 1; |
| 1099 | } |
| 1100 | |
| 1101 | static void bc_vec_pushByte(BcVec *v, char data) |
| 1102 | { |
| 1103 | bc_vec_push(v, &data); |
| 1104 | } |
| 1105 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1106 | static void bc_vec_pushZeroByte(BcVec *v) |
| 1107 | { |
| 1108 | //bc_vec_pushByte(v, '\0'); |
| 1109 | // better: |
| 1110 | bc_vec_push(v, &const_int_0); |
| 1111 | } |
| 1112 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1113 | static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx) |
| 1114 | { |
| 1115 | if (idx == v->len) |
| 1116 | bc_vec_push(v, data); |
| 1117 | else { |
| 1118 | |
| 1119 | char *ptr; |
| 1120 | |
| 1121 | if (v->len == v->cap) bc_vec_grow(v, 1); |
| 1122 | |
| 1123 | ptr = v->v + v->size * idx; |
| 1124 | |
| 1125 | memmove(ptr + v->size, ptr, v->size * (v->len++ - idx)); |
| 1126 | memmove(ptr, data, v->size); |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | static void bc_vec_string(BcVec *v, size_t len, const char *str) |
| 1131 | { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1132 | bc_vec_pop_all(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1133 | bc_vec_expand(v, len + 1); |
| 1134 | memcpy(v->v, str, len); |
| 1135 | v->len = len; |
| 1136 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1137 | bc_vec_pushZeroByte(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | static void bc_vec_concat(BcVec *v, const char *str) |
| 1141 | { |
| 1142 | size_t len; |
| 1143 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1144 | if (v->len == 0) bc_vec_pushZeroByte(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1145 | |
| 1146 | len = v->len + strlen(str); |
| 1147 | |
| 1148 | if (v->cap < len) bc_vec_grow(v, len - v->len); |
| 1149 | strcat(v->v, str); |
| 1150 | |
| 1151 | v->len = len; |
| 1152 | } |
| 1153 | |
| 1154 | static void *bc_vec_item(const BcVec *v, size_t idx) |
| 1155 | { |
| 1156 | return v->v + v->size * idx; |
| 1157 | } |
| 1158 | |
| 1159 | static void *bc_vec_item_rev(const BcVec *v, size_t idx) |
| 1160 | { |
| 1161 | return v->v + v->size * (v->len - idx - 1); |
| 1162 | } |
| 1163 | |
| 1164 | static void bc_vec_free(void *vec) |
| 1165 | { |
| 1166 | BcVec *v = (BcVec *) vec; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1167 | bc_vec_pop_all(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1168 | free(v->v); |
| 1169 | } |
| 1170 | |
| 1171 | static size_t bc_map_find(const BcVec *v, const void *ptr) |
| 1172 | { |
| 1173 | size_t low = 0, high = v->len; |
| 1174 | |
| 1175 | while (low < high) { |
| 1176 | |
| 1177 | size_t mid = (low + high) / 2; |
| 1178 | BcId *id = bc_vec_item(v, mid); |
| 1179 | int result = bc_id_cmp(ptr, id); |
| 1180 | |
| 1181 | if (result == 0) |
| 1182 | return mid; |
| 1183 | else if (result < 0) |
| 1184 | high = mid; |
| 1185 | else |
| 1186 | low = mid + 1; |
| 1187 | } |
| 1188 | |
| 1189 | return low; |
| 1190 | } |
| 1191 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1192 | 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] | 1193 | { |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1194 | size_t n = *i = bc_map_find(v, ptr); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1195 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1196 | if (n == v->len) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1197 | bc_vec_push(v, ptr); |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1198 | else if (!bc_id_cmp(ptr, bc_vec_item(v, n))) |
| 1199 | return 0; // "was not inserted" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1200 | else |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1201 | bc_vec_pushAt(v, ptr, n); |
| 1202 | return 1; // "was inserted" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | static size_t bc_map_index(const BcVec *v, const void *ptr) |
| 1206 | { |
| 1207 | size_t i = bc_map_find(v, ptr); |
| 1208 | if (i >= v->len) return BC_VEC_INVALID_IDX; |
| 1209 | return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i; |
| 1210 | } |
| 1211 | |
| 1212 | static BcStatus bc_read_line(BcVec *vec, const char *prompt) |
| 1213 | { |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1214 | bool bad_chars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1215 | |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 1216 | do { |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1217 | int i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1218 | |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1219 | bad_chars = 0; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1220 | bc_vec_pop_all(vec); |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1221 | |
| 1222 | fflush_and_check(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1223 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1224 | if (bb_got_signal) { // ^C was pressed |
| 1225 | intr: |
| 1226 | bb_got_signal = 0; // resets G_interrupt to zero |
| 1227 | fputs(IS_BC |
| 1228 | ? "\ninterrupt (type \"quit\" to exit)\n" |
| 1229 | : "\ninterrupt (type \"q\" to exit)\n" |
| 1230 | , stderr); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1231 | } |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1232 | #endif |
| 1233 | if (G.ttyin && !G_posix) |
| 1234 | fputs(prompt, stderr); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1235 | |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1236 | #if ENABLE_FEATURE_BC_SIGNALS |
| 1237 | errno = 0; |
| 1238 | #endif |
| 1239 | do { |
| 1240 | i = fgetc(stdin); |
| 1241 | if (i == EOF) { |
| 1242 | #if ENABLE_FEATURE_BC_SIGNALS |
| 1243 | // Both conditions appear simultaneously, check both just in case |
| 1244 | if (errno == EINTR || bb_got_signal) { |
| 1245 | // ^C was pressed |
| 1246 | clearerr(stdin); |
| 1247 | goto intr; |
| 1248 | } |
| 1249 | #endif |
| 1250 | if (ferror(stdin)) |
| 1251 | quit(); // this emits error message |
| 1252 | G.eof = 1; |
| 1253 | // Note: EOF does not append '\n', therefore: |
| 1254 | // printf 'print 123\n' | bc - works |
| 1255 | // printf 'print 123' | bc - fails (syntax error) |
| 1256 | break; |
| 1257 | } |
| 1258 | |
| 1259 | if ((i < ' ' && i != '\t' && i != '\r' && i != '\n') // also allow '\v' '\f'? |
| 1260 | || i > 0x7e |
| 1261 | ) { |
| 1262 | // Bad chars on this line, ignore entire line |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1263 | bc_error_fmt("illegal character 0x%02x", i); |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1264 | bad_chars = 1; |
| 1265 | } |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1266 | bc_vec_pushByte(vec, (char)i); |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1267 | } while (i != '\n'); |
| 1268 | } while (bad_chars); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1269 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1270 | bc_vec_pushZeroByte(vec); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1271 | |
| 1272 | return BC_STATUS_SUCCESS; |
| 1273 | } |
| 1274 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 1275 | static char* bc_read_file(const char *path) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1276 | { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 1277 | char *buf; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 1278 | size_t size = ((size_t) -1); |
| 1279 | size_t i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1280 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 1281 | buf = xmalloc_open_read_close(path, &size); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1282 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 1283 | for (i = 0; i < size; ++i) { |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1284 | char c = buf[i]; |
| 1285 | if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'? |
| 1286 | || c > 0x7e |
| 1287 | ) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 1288 | free(buf); |
| 1289 | buf = NULL; |
| 1290 | break; |
| 1291 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1292 | } |
| 1293 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 1294 | return buf; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1295 | } |
| 1296 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 1297 | static void bc_args(int argc, char **argv) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1298 | { |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 1299 | unsigned opts; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1300 | int i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1301 | |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 1302 | GETOPT_RESET(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1303 | #if ENABLE_FEATURE_BC_LONG_OPTIONS |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 1304 | opts = getopt32long(argv, "xwvsqli", |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 1305 | "extended-register\0" No_argument "x" |
| 1306 | "warn\0" No_argument "w" |
| 1307 | "version\0" No_argument "v" |
| 1308 | "standard\0" No_argument "s" |
| 1309 | "quiet\0" No_argument "q" |
| 1310 | "mathlib\0" No_argument "l" |
| 1311 | "interactive\0" No_argument "i" |
| 1312 | ); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 1313 | #else |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 1314 | opts = getopt32(argv, "xwvsqli"); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 1315 | #endif |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 1316 | if (getenv("POSIXLY_CORRECT")) |
| 1317 | option_mask32 |= BC_FLAG_S; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1318 | |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 1319 | if (opts & BC_FLAG_V) bc_vm_info(); |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 1320 | // should not be necessary, getopt32() handles this?? |
| 1321 | //if (argv[optind] && !strcmp(argv[optind], "--")) ++optind; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1322 | |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 1323 | for (i = optind; i < argc; ++i) |
| 1324 | bc_vec_push(&G.files, argv + i); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | static void bc_num_setToZero(BcNum *n, size_t scale) |
| 1328 | { |
| 1329 | n->len = 0; |
| 1330 | n->neg = false; |
| 1331 | n->rdx = scale; |
| 1332 | } |
| 1333 | |
| 1334 | static void bc_num_zero(BcNum *n) |
| 1335 | { |
| 1336 | bc_num_setToZero(n, 0); |
| 1337 | } |
| 1338 | |
| 1339 | static void bc_num_one(BcNum *n) |
| 1340 | { |
| 1341 | bc_num_setToZero(n, 0); |
| 1342 | n->len = 1; |
| 1343 | n->num[0] = 1; |
| 1344 | } |
| 1345 | |
| 1346 | static void bc_num_ten(BcNum *n) |
| 1347 | { |
| 1348 | bc_num_setToZero(n, 0); |
| 1349 | n->len = 2; |
| 1350 | n->num[0] = 0; |
| 1351 | n->num[1] = 1; |
| 1352 | } |
| 1353 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1354 | static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1355 | size_t len) |
| 1356 | { |
| 1357 | size_t i, j; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1358 | for (i = 0; i < len; ++i) { |
| 1359 | for (a[i] -= b[i], j = 0; a[i + j] < 0;) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1360 | a[i + j++] += 10; |
| 1361 | a[i + j] -= 1; |
| 1362 | } |
| 1363 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len) |
| 1367 | { |
| 1368 | size_t i; |
| 1369 | int c = 0; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1370 | for (i = len - 1; i < len && !(c = a[i] - b[i]); --i); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1371 | return BC_NUM_NEG(i + 1, c < 0); |
| 1372 | } |
| 1373 | |
| 1374 | static ssize_t bc_num_cmp(BcNum *a, BcNum *b) |
| 1375 | { |
| 1376 | size_t i, min, a_int, b_int, diff; |
| 1377 | BcDig *max_num, *min_num; |
| 1378 | bool a_max, neg = false; |
| 1379 | ssize_t cmp; |
| 1380 | |
| 1381 | if (a == b) return 0; |
| 1382 | if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg); |
| 1383 | if (b->len == 0) return BC_NUM_NEG(1, a->neg); |
| 1384 | if (a->neg) { |
| 1385 | if (b->neg) |
| 1386 | neg = true; |
| 1387 | else |
| 1388 | return -1; |
| 1389 | } |
| 1390 | else if (b->neg) |
| 1391 | return 1; |
| 1392 | |
| 1393 | a_int = BC_NUM_INT(a); |
| 1394 | b_int = BC_NUM_INT(b); |
| 1395 | a_int -= b_int; |
| 1396 | a_max = (a->rdx > b->rdx); |
| 1397 | |
| 1398 | if (a_int != 0) return (ssize_t) a_int; |
| 1399 | |
| 1400 | if (a_max) { |
| 1401 | min = b->rdx; |
| 1402 | diff = a->rdx - b->rdx; |
| 1403 | max_num = a->num + diff; |
| 1404 | min_num = b->num; |
| 1405 | } |
| 1406 | else { |
| 1407 | min = a->rdx; |
| 1408 | diff = b->rdx - a->rdx; |
| 1409 | max_num = b->num + diff; |
| 1410 | min_num = a->num; |
| 1411 | } |
| 1412 | |
| 1413 | cmp = bc_num_compare(max_num, min_num, b_int + min); |
| 1414 | if (cmp != 0) return BC_NUM_NEG(cmp, (!a_max) != neg); |
| 1415 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1416 | for (max_num -= diff, i = diff - 1; i < diff; --i) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1417 | if (max_num[i]) return BC_NUM_NEG(1, (!a_max) != neg); |
| 1418 | } |
| 1419 | |
| 1420 | return 0; |
| 1421 | } |
| 1422 | |
| 1423 | static void bc_num_truncate(BcNum *n, size_t places) |
| 1424 | { |
| 1425 | if (places == 0) return; |
| 1426 | |
| 1427 | n->rdx -= places; |
| 1428 | |
| 1429 | if (n->len != 0) { |
| 1430 | n->len -= places; |
| 1431 | memmove(n->num, n->num + places, n->len * sizeof(BcDig)); |
| 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | static void bc_num_extend(BcNum *n, size_t places) |
| 1436 | { |
| 1437 | size_t len = n->len + places; |
| 1438 | |
| 1439 | if (places != 0) { |
| 1440 | |
| 1441 | if (n->cap < len) bc_num_expand(n, len); |
| 1442 | |
| 1443 | memmove(n->num + places, n->num, sizeof(BcDig) * n->len); |
| 1444 | memset(n->num, 0, sizeof(BcDig) * places); |
| 1445 | |
| 1446 | n->len += places; |
| 1447 | n->rdx += places; |
| 1448 | } |
| 1449 | } |
| 1450 | |
| 1451 | static void bc_num_clean(BcNum *n) |
| 1452 | { |
| 1453 | while (n->len > 0 && n->num[n->len - 1] == 0) --n->len; |
| 1454 | if (n->len == 0) |
| 1455 | n->neg = false; |
| 1456 | else if (n->len < n->rdx) |
| 1457 | n->len = n->rdx; |
| 1458 | } |
| 1459 | |
| 1460 | static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2) |
| 1461 | { |
| 1462 | if (n->rdx < scale) |
| 1463 | bc_num_extend(n, scale - n->rdx); |
| 1464 | else |
| 1465 | bc_num_truncate(n, n->rdx - scale); |
| 1466 | |
| 1467 | bc_num_clean(n); |
| 1468 | if (n->len != 0) n->neg = !neg1 != !neg2; |
| 1469 | } |
| 1470 | |
| 1471 | static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a, |
| 1472 | BcNum *restrict b) |
| 1473 | { |
| 1474 | if (idx < n->len) { |
| 1475 | |
| 1476 | b->len = n->len - idx; |
| 1477 | a->len = idx; |
| 1478 | a->rdx = b->rdx = 0; |
| 1479 | |
| 1480 | memcpy(b->num, n->num + idx, b->len * sizeof(BcDig)); |
| 1481 | memcpy(a->num, n->num, idx * sizeof(BcDig)); |
| 1482 | } |
| 1483 | else { |
| 1484 | bc_num_zero(b); |
| 1485 | bc_num_copy(a, n); |
| 1486 | } |
| 1487 | |
| 1488 | bc_num_clean(a); |
| 1489 | bc_num_clean(b); |
| 1490 | } |
| 1491 | |
| 1492 | static BcStatus bc_num_shift(BcNum *n, size_t places) |
| 1493 | { |
| 1494 | if (places == 0 || n->len == 0) return BC_STATUS_SUCCESS; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 1495 | if (places + n->len > BC_MAX_NUM) |
| 1496 | return bc_error("number too long: must be [1, BC_NUM_MAX]"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1497 | |
| 1498 | if (n->rdx >= places) |
| 1499 | n->rdx -= places; |
| 1500 | else { |
| 1501 | bc_num_extend(n, places - n->rdx); |
| 1502 | n->rdx = 0; |
| 1503 | } |
| 1504 | |
| 1505 | bc_num_clean(n); |
| 1506 | |
| 1507 | return BC_STATUS_SUCCESS; |
| 1508 | } |
| 1509 | |
| 1510 | static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) |
| 1511 | { |
| 1512 | BcNum one; |
| 1513 | BcDig num[2]; |
| 1514 | |
| 1515 | one.cap = 2; |
| 1516 | one.num = num; |
| 1517 | bc_num_one(&one); |
| 1518 | |
| 1519 | return bc_num_div(&one, a, b, scale); |
| 1520 | } |
| 1521 | |
| 1522 | static BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) |
| 1523 | { |
| 1524 | BcDig *ptr, *ptr_a, *ptr_b, *ptr_c; |
| 1525 | size_t i, max, min_rdx, min_int, diff, a_int, b_int; |
| 1526 | int carry, in; |
| 1527 | |
| 1528 | // Because this function doesn't need to use scale (per the bc spec), |
| 1529 | // I am hijacking it to say whether it's doing an add or a subtract. |
| 1530 | |
| 1531 | if (a->len == 0) { |
| 1532 | bc_num_copy(c, b); |
| 1533 | if (sub && c->len) c->neg = !c->neg; |
| 1534 | return BC_STATUS_SUCCESS; |
| 1535 | } |
| 1536 | else if (b->len == 0) { |
| 1537 | bc_num_copy(c, a); |
| 1538 | return BC_STATUS_SUCCESS; |
| 1539 | } |
| 1540 | |
| 1541 | c->neg = a->neg; |
| 1542 | c->rdx = BC_MAX(a->rdx, b->rdx); |
| 1543 | min_rdx = BC_MIN(a->rdx, b->rdx); |
| 1544 | c->len = 0; |
| 1545 | |
| 1546 | if (a->rdx > b->rdx) { |
| 1547 | diff = a->rdx - b->rdx; |
| 1548 | ptr = a->num; |
| 1549 | ptr_a = a->num + diff; |
| 1550 | ptr_b = b->num; |
| 1551 | } |
| 1552 | else { |
| 1553 | diff = b->rdx - a->rdx; |
| 1554 | ptr = b->num; |
| 1555 | ptr_a = a->num; |
| 1556 | ptr_b = b->num + diff; |
| 1557 | } |
| 1558 | |
| 1559 | for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i]; |
| 1560 | |
| 1561 | ptr_c += diff; |
| 1562 | a_int = BC_NUM_INT(a); |
| 1563 | b_int = BC_NUM_INT(b); |
| 1564 | |
| 1565 | if (a_int > b_int) { |
| 1566 | min_int = b_int; |
| 1567 | max = a_int; |
| 1568 | ptr = ptr_a; |
| 1569 | } |
| 1570 | else { |
| 1571 | min_int = a_int; |
| 1572 | max = b_int; |
| 1573 | ptr = ptr_b; |
| 1574 | } |
| 1575 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1576 | 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] | 1577 | in = ((int) ptr_a[i]) + ((int) ptr_b[i]) + carry; |
| 1578 | carry = in / 10; |
| 1579 | ptr_c[i] = (BcDig)(in % 10); |
| 1580 | } |
| 1581 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1582 | for (; i < max + min_rdx; ++i, ++c->len) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1583 | in = ((int) ptr[i]) + carry; |
| 1584 | carry = in / 10; |
| 1585 | ptr_c[i] = (BcDig)(in % 10); |
| 1586 | } |
| 1587 | |
| 1588 | if (carry != 0) c->num[c->len++] = (BcDig) carry; |
| 1589 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1590 | return BC_STATUS_SUCCESS; // can't make void, see bc_num_binary() |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | static BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) |
| 1594 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1595 | ssize_t cmp; |
| 1596 | BcNum *minuend, *subtrahend; |
| 1597 | size_t start; |
| 1598 | bool aneg, bneg, neg; |
| 1599 | |
| 1600 | // Because this function doesn't need to use scale (per the bc spec), |
| 1601 | // I am hijacking it to say whether it's doing an add or a subtract. |
| 1602 | |
| 1603 | if (a->len == 0) { |
| 1604 | bc_num_copy(c, b); |
| 1605 | if (sub && c->len) c->neg = !c->neg; |
| 1606 | return BC_STATUS_SUCCESS; |
| 1607 | } |
| 1608 | else if (b->len == 0) { |
| 1609 | bc_num_copy(c, a); |
| 1610 | return BC_STATUS_SUCCESS; |
| 1611 | } |
| 1612 | |
| 1613 | aneg = a->neg; |
| 1614 | bneg = b->neg; |
| 1615 | a->neg = b->neg = false; |
| 1616 | |
| 1617 | cmp = bc_num_cmp(a, b); |
| 1618 | |
| 1619 | a->neg = aneg; |
| 1620 | b->neg = bneg; |
| 1621 | |
| 1622 | if (cmp == 0) { |
| 1623 | bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx)); |
| 1624 | return BC_STATUS_SUCCESS; |
| 1625 | } |
| 1626 | else if (cmp > 0) { |
| 1627 | neg = a->neg; |
| 1628 | minuend = a; |
| 1629 | subtrahend = b; |
| 1630 | } |
| 1631 | else { |
| 1632 | neg = b->neg; |
| 1633 | if (sub) neg = !neg; |
| 1634 | minuend = b; |
| 1635 | subtrahend = a; |
| 1636 | } |
| 1637 | |
| 1638 | bc_num_copy(c, minuend); |
| 1639 | c->neg = neg; |
| 1640 | |
| 1641 | if (c->rdx < subtrahend->rdx) { |
| 1642 | bc_num_extend(c, subtrahend->rdx - c->rdx); |
| 1643 | start = 0; |
| 1644 | } |
| 1645 | else |
| 1646 | start = c->rdx - subtrahend->rdx; |
| 1647 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1648 | bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1649 | |
| 1650 | bc_num_clean(c); |
| 1651 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1652 | return BC_STATUS_SUCCESS; // can't make void, see bc_num_binary() |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | static BcStatus bc_num_k(BcNum *restrict a, BcNum *restrict b, |
| 1656 | BcNum *restrict c) |
| 1657 | { |
| 1658 | BcStatus s; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1659 | 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] | 1660 | BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1661 | bool aone; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1662 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1663 | if (a->len == 0 || b->len == 0) { |
| 1664 | bc_num_zero(c); |
| 1665 | return BC_STATUS_SUCCESS; |
| 1666 | } |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1667 | aone = BC_NUM_ONE(a); |
| 1668 | if (aone || BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1669 | bc_num_copy(c, aone ? b : a); |
| 1670 | return BC_STATUS_SUCCESS; |
| 1671 | } |
| 1672 | |
| 1673 | if (a->len + b->len < BC_NUM_KARATSUBA_LEN || |
| 1674 | a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN) |
| 1675 | { |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1676 | size_t i, j, len; |
Denys Vlasenko | b3cb901 | 2018-12-05 19:05:32 +0100 | [diff] [blame] | 1677 | unsigned carry; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1678 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1679 | bc_num_expand(c, a->len + b->len + 1); |
| 1680 | |
| 1681 | memset(c->num, 0, sizeof(BcDig) * c->cap); |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1682 | c->len = len = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1683 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1684 | for (i = 0; i < b->len; ++i) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1685 | |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1686 | carry = 0; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1687 | for (j = 0; j < a->len; ++j) { |
Denys Vlasenko | b3cb901 | 2018-12-05 19:05:32 +0100 | [diff] [blame] | 1688 | unsigned in = c->num[i + j]; |
| 1689 | in += ((unsigned) a->num[j]) * ((unsigned) b->num[i]) + carry; |
| 1690 | // note: compilers prefer _unsigned_ div/const |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1691 | carry = in / 10; |
| 1692 | c->num[i + j] = (BcDig)(in % 10); |
| 1693 | } |
| 1694 | |
| 1695 | c->num[i + j] += (BcDig) carry; |
| 1696 | len = BC_MAX(len, i + j + !!carry); |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 1697 | |
| 1698 | // a=2^1000000 |
| 1699 | // a*a <- without check below, this will not be interruptible |
| 1700 | if (G_interrupt) return BC_STATUS_FAILURE; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1701 | } |
| 1702 | |
| 1703 | c->len = len; |
| 1704 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1705 | return BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | bc_num_init(&l1, max); |
| 1709 | bc_num_init(&h1, max); |
| 1710 | bc_num_init(&l2, max); |
| 1711 | bc_num_init(&h2, max); |
| 1712 | bc_num_init(&m1, max); |
| 1713 | bc_num_init(&m2, max); |
| 1714 | bc_num_init(&z0, max); |
| 1715 | bc_num_init(&z1, max); |
| 1716 | bc_num_init(&z2, max); |
| 1717 | bc_num_init(&temp, max + max); |
| 1718 | |
| 1719 | bc_num_split(a, max2, &l1, &h1); |
| 1720 | bc_num_split(b, max2, &l2, &h2); |
| 1721 | |
| 1722 | s = bc_num_add(&h1, &l1, &m1, 0); |
| 1723 | if (s) goto err; |
| 1724 | s = bc_num_add(&h2, &l2, &m2, 0); |
| 1725 | if (s) goto err; |
| 1726 | |
| 1727 | s = bc_num_k(&h1, &h2, &z0); |
| 1728 | if (s) goto err; |
| 1729 | s = bc_num_k(&m1, &m2, &z1); |
| 1730 | if (s) goto err; |
| 1731 | s = bc_num_k(&l1, &l2, &z2); |
| 1732 | if (s) goto err; |
| 1733 | |
| 1734 | s = bc_num_sub(&z1, &z0, &temp, 0); |
| 1735 | if (s) goto err; |
| 1736 | s = bc_num_sub(&temp, &z2, &z1, 0); |
| 1737 | if (s) goto err; |
| 1738 | |
| 1739 | s = bc_num_shift(&z0, max2 * 2); |
| 1740 | if (s) goto err; |
| 1741 | s = bc_num_shift(&z1, max2); |
| 1742 | if (s) goto err; |
| 1743 | s = bc_num_add(&z0, &z1, &temp, 0); |
| 1744 | if (s) goto err; |
| 1745 | s = bc_num_add(&temp, &z2, c, 0); |
| 1746 | |
| 1747 | err: |
| 1748 | bc_num_free(&temp); |
| 1749 | bc_num_free(&z2); |
| 1750 | bc_num_free(&z1); |
| 1751 | bc_num_free(&z0); |
| 1752 | bc_num_free(&m2); |
| 1753 | bc_num_free(&m1); |
| 1754 | bc_num_free(&h2); |
| 1755 | bc_num_free(&l2); |
| 1756 | bc_num_free(&h1); |
| 1757 | bc_num_free(&l1); |
| 1758 | return s; |
| 1759 | } |
| 1760 | |
| 1761 | static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
| 1762 | { |
| 1763 | BcStatus s; |
| 1764 | BcNum cpa, cpb; |
| 1765 | size_t maxrdx = BC_MAX(a->rdx, b->rdx); |
| 1766 | |
| 1767 | scale = BC_MAX(scale, a->rdx); |
| 1768 | scale = BC_MAX(scale, b->rdx); |
| 1769 | scale = BC_MIN(a->rdx + b->rdx, scale); |
| 1770 | maxrdx = BC_MAX(maxrdx, scale); |
| 1771 | |
| 1772 | bc_num_init(&cpa, a->len); |
| 1773 | bc_num_init(&cpb, b->len); |
| 1774 | |
| 1775 | bc_num_copy(&cpa, a); |
| 1776 | bc_num_copy(&cpb, b); |
| 1777 | cpa.neg = cpb.neg = false; |
| 1778 | |
| 1779 | s = bc_num_shift(&cpa, maxrdx); |
| 1780 | if (s) goto err; |
| 1781 | s = bc_num_shift(&cpb, maxrdx); |
| 1782 | if (s) goto err; |
| 1783 | s = bc_num_k(&cpa, &cpb, c); |
| 1784 | if (s) goto err; |
| 1785 | |
| 1786 | maxrdx += scale; |
| 1787 | bc_num_expand(c, c->len + maxrdx); |
| 1788 | |
| 1789 | if (c->len < maxrdx) { |
| 1790 | memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig)); |
| 1791 | c->len += maxrdx; |
| 1792 | } |
| 1793 | |
| 1794 | c->rdx = maxrdx; |
| 1795 | bc_num_retireMul(c, scale, a->neg, b->neg); |
| 1796 | |
| 1797 | err: |
| 1798 | bc_num_free(&cpb); |
| 1799 | bc_num_free(&cpa); |
| 1800 | return s; |
| 1801 | } |
| 1802 | |
| 1803 | static BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
| 1804 | { |
| 1805 | BcStatus s = BC_STATUS_SUCCESS; |
| 1806 | BcDig *n, *p, q; |
| 1807 | size_t len, end, i; |
| 1808 | BcNum cp; |
| 1809 | bool zero = true; |
| 1810 | |
| 1811 | if (b->len == 0) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 1812 | return bc_error("divide by zero"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1813 | else if (a->len == 0) { |
| 1814 | bc_num_setToZero(c, scale); |
| 1815 | return BC_STATUS_SUCCESS; |
| 1816 | } |
| 1817 | else if (BC_NUM_ONE(b)) { |
| 1818 | bc_num_copy(c, a); |
| 1819 | bc_num_retireMul(c, scale, a->neg, b->neg); |
| 1820 | return BC_STATUS_SUCCESS; |
| 1821 | } |
| 1822 | |
| 1823 | bc_num_init(&cp, BC_NUM_MREQ(a, b, scale)); |
| 1824 | bc_num_copy(&cp, a); |
| 1825 | len = b->len; |
| 1826 | |
| 1827 | if (len > cp.len) { |
| 1828 | bc_num_expand(&cp, len + 2); |
| 1829 | bc_num_extend(&cp, len - cp.len); |
| 1830 | } |
| 1831 | |
| 1832 | if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx); |
| 1833 | cp.rdx -= b->rdx; |
| 1834 | if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx); |
| 1835 | |
| 1836 | if (b->rdx == b->len) { |
| 1837 | for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1]; |
| 1838 | len -= i - 1; |
| 1839 | } |
| 1840 | |
| 1841 | if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1); |
| 1842 | |
| 1843 | // We want an extra zero in front to make things simpler. |
| 1844 | cp.num[cp.len++] = 0; |
| 1845 | end = cp.len - len; |
| 1846 | |
| 1847 | bc_num_expand(c, cp.len); |
| 1848 | |
| 1849 | bc_num_zero(c); |
| 1850 | memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig)); |
| 1851 | c->rdx = cp.rdx; |
| 1852 | c->len = cp.len; |
| 1853 | p = b->num; |
| 1854 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1855 | for (i = end - 1; !s && i < end; --i) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1856 | n = cp.num + i; |
| 1857 | 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] | 1858 | bc_num_subArrays(n, p, len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1859 | c->num[i] = q; |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 1860 | // a=2^100000 |
| 1861 | // scale=40000 |
| 1862 | // 1/a <- without check below, this will not be interruptible |
| 1863 | if (G_interrupt) { |
| 1864 | s = BC_STATUS_FAILURE; |
| 1865 | break; |
| 1866 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1867 | } |
| 1868 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1869 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1870 | bc_num_free(&cp); |
| 1871 | |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 1872 | return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1873 | } |
| 1874 | |
| 1875 | static BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c, |
| 1876 | BcNum *restrict d, size_t scale, size_t ts) |
| 1877 | { |
| 1878 | BcStatus s; |
| 1879 | BcNum temp; |
| 1880 | bool neg; |
| 1881 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 1882 | if (b->len == 0) |
| 1883 | return bc_error("divide by zero"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1884 | |
| 1885 | if (a->len == 0) { |
| 1886 | bc_num_setToZero(d, ts); |
| 1887 | return BC_STATUS_SUCCESS; |
| 1888 | } |
| 1889 | |
| 1890 | bc_num_init(&temp, d->cap); |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 1891 | s = bc_num_d(a, b, c, scale); |
| 1892 | if (s) goto err; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1893 | |
| 1894 | if (scale != 0) scale = ts; |
| 1895 | |
| 1896 | s = bc_num_m(c, b, &temp, scale); |
| 1897 | if (s) goto err; |
| 1898 | s = bc_num_sub(a, &temp, d, scale); |
| 1899 | if (s) goto err; |
| 1900 | |
| 1901 | if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx); |
| 1902 | |
| 1903 | neg = d->neg; |
| 1904 | bc_num_retireMul(d, ts, a->neg, b->neg); |
| 1905 | d->neg = neg; |
| 1906 | |
| 1907 | err: |
| 1908 | bc_num_free(&temp); |
| 1909 | return s; |
| 1910 | } |
| 1911 | |
| 1912 | static BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
| 1913 | { |
| 1914 | BcStatus s; |
| 1915 | BcNum c1; |
| 1916 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 1917 | |
| 1918 | bc_num_init(&c1, len); |
| 1919 | s = bc_num_r(a, b, &c1, c, scale, ts); |
| 1920 | bc_num_free(&c1); |
| 1921 | |
| 1922 | return s; |
| 1923 | } |
| 1924 | |
| 1925 | static BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
| 1926 | { |
| 1927 | BcStatus s = BC_STATUS_SUCCESS; |
| 1928 | BcNum copy; |
| 1929 | unsigned long pow; |
| 1930 | size_t i, powrdx, resrdx; |
| 1931 | bool neg, zero; |
| 1932 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 1933 | if (b->rdx) return bc_error("non integer number"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1934 | |
| 1935 | if (b->len == 0) { |
| 1936 | bc_num_one(c); |
| 1937 | return BC_STATUS_SUCCESS; |
| 1938 | } |
| 1939 | else if (a->len == 0) { |
| 1940 | bc_num_setToZero(c, scale); |
| 1941 | return BC_STATUS_SUCCESS; |
| 1942 | } |
| 1943 | else if (BC_NUM_ONE(b)) { |
| 1944 | if (!b->neg) |
| 1945 | bc_num_copy(c, a); |
| 1946 | else |
| 1947 | s = bc_num_inv(a, c, scale); |
| 1948 | return s; |
| 1949 | } |
| 1950 | |
| 1951 | neg = b->neg; |
| 1952 | b->neg = false; |
| 1953 | |
| 1954 | s = bc_num_ulong(b, &pow); |
| 1955 | if (s) return s; |
| 1956 | |
| 1957 | bc_num_init(©, a->len); |
| 1958 | bc_num_copy(©, a); |
| 1959 | |
| 1960 | if (!neg) scale = BC_MIN(a->rdx * pow, BC_MAX(scale, a->rdx)); |
| 1961 | |
| 1962 | b->neg = neg; |
| 1963 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1964 | for (powrdx = a->rdx; !(pow & 1); pow >>= 1) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1965 | powrdx <<= 1; |
| 1966 | s = bc_num_mul(©, ©, ©, powrdx); |
| 1967 | if (s) goto err; |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 1968 | // Not needed: bc_num_mul() has a check for ^C: |
| 1969 | //if (G_interrupt) { |
| 1970 | // s = BC_STATUS_FAILURE; |
| 1971 | // goto err; |
| 1972 | //} |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1973 | } |
| 1974 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1975 | bc_num_copy(c, ©); |
| 1976 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1977 | for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1978 | |
| 1979 | powrdx <<= 1; |
| 1980 | s = bc_num_mul(©, ©, ©, powrdx); |
| 1981 | if (s) goto err; |
| 1982 | |
| 1983 | if (pow & 1) { |
| 1984 | resrdx += powrdx; |
| 1985 | s = bc_num_mul(c, ©, c, resrdx); |
| 1986 | if (s) goto err; |
| 1987 | } |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 1988 | // Not needed: bc_num_mul() has a check for ^C: |
| 1989 | //if (G_interrupt) { |
| 1990 | // s = BC_STATUS_FAILURE; |
| 1991 | // goto err; |
| 1992 | //} |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1993 | } |
| 1994 | |
| 1995 | if (neg) { |
| 1996 | s = bc_num_inv(c, c, scale); |
| 1997 | if (s) goto err; |
| 1998 | } |
| 1999 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2000 | if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale); |
| 2001 | |
| 2002 | // We can't use bc_num_clean() here. |
| 2003 | for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i]; |
| 2004 | if (zero) bc_num_setToZero(c, scale); |
| 2005 | |
| 2006 | err: |
| 2007 | bc_num_free(©); |
| 2008 | return s; |
| 2009 | } |
| 2010 | |
| 2011 | static BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale, |
| 2012 | BcNumBinaryOp op, size_t req) |
| 2013 | { |
| 2014 | BcStatus s; |
| 2015 | BcNum num2, *ptr_a, *ptr_b; |
| 2016 | bool init = false; |
| 2017 | |
| 2018 | if (c == a) { |
| 2019 | ptr_a = &num2; |
| 2020 | memcpy(ptr_a, c, sizeof(BcNum)); |
| 2021 | init = true; |
| 2022 | } |
| 2023 | else |
| 2024 | ptr_a = a; |
| 2025 | |
| 2026 | if (c == b) { |
| 2027 | ptr_b = &num2; |
| 2028 | if (c != a) { |
| 2029 | memcpy(ptr_b, c, sizeof(BcNum)); |
| 2030 | init = true; |
| 2031 | } |
| 2032 | } |
| 2033 | else |
| 2034 | ptr_b = b; |
| 2035 | |
| 2036 | if (init) |
| 2037 | bc_num_init(c, req); |
| 2038 | else |
| 2039 | bc_num_expand(c, req); |
| 2040 | |
| 2041 | s = op(ptr_a, ptr_b, c, scale); |
| 2042 | |
| 2043 | if (init) bc_num_free(&num2); |
| 2044 | |
| 2045 | return s; |
| 2046 | } |
| 2047 | |
| 2048 | static bool bc_num_strValid(const char *val, size_t base) |
| 2049 | { |
| 2050 | BcDig b; |
| 2051 | bool small, radix = false; |
| 2052 | size_t i, len = strlen(val); |
| 2053 | |
| 2054 | if (!len) return true; |
| 2055 | |
| 2056 | small = base <= 10; |
| 2057 | b = (BcDig)(small ? base + '0' : base - 10 + 'A'); |
| 2058 | |
| 2059 | for (i = 0; i < len; ++i) { |
| 2060 | |
| 2061 | BcDig c = val[i]; |
| 2062 | |
| 2063 | if (c == '.') { |
| 2064 | |
| 2065 | if (radix) return false; |
| 2066 | |
| 2067 | radix = true; |
| 2068 | continue; |
| 2069 | } |
| 2070 | |
| 2071 | if (c < '0' || (small && c >= b) || (c > '9' && (c < 'A' || c >= b))) |
| 2072 | return false; |
| 2073 | } |
| 2074 | |
| 2075 | return true; |
| 2076 | } |
| 2077 | |
| 2078 | static void bc_num_parseDecimal(BcNum *n, const char *val) |
| 2079 | { |
| 2080 | size_t len, i; |
| 2081 | const char *ptr; |
| 2082 | bool zero = true; |
| 2083 | |
| 2084 | for (i = 0; val[i] == '0'; ++i); |
| 2085 | |
| 2086 | val += i; |
| 2087 | len = strlen(val); |
| 2088 | bc_num_zero(n); |
| 2089 | |
| 2090 | if (len != 0) { |
| 2091 | for (i = 0; zero && i < len; ++i) zero = val[i] == '0' || val[i] == '.'; |
| 2092 | bc_num_expand(n, len); |
| 2093 | } |
| 2094 | |
| 2095 | ptr = strchr(val, '.'); |
| 2096 | |
Denys Vlasenko | d5f7703 | 2018-12-04 21:37:56 +0100 | [diff] [blame] | 2097 | n->rdx = 0; |
| 2098 | if (ptr != NULL) |
| 2099 | n->rdx = (size_t)((val + len) - (ptr + 1)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2100 | |
| 2101 | if (!zero) { |
| 2102 | for (i = len - 1; i < len; ++n->len, i -= 1 + (i && val[i - 1] == '.')) |
| 2103 | n->num[n->len] = val[i] - '0'; |
| 2104 | } |
| 2105 | } |
| 2106 | |
| 2107 | static void bc_num_parseBase(BcNum *n, const char *val, BcNum *base) |
| 2108 | { |
| 2109 | BcStatus s; |
| 2110 | BcNum temp, mult, result; |
| 2111 | BcDig c = '\0'; |
| 2112 | bool zero = true; |
| 2113 | unsigned long v; |
| 2114 | size_t i, digits, len = strlen(val); |
| 2115 | |
| 2116 | bc_num_zero(n); |
| 2117 | |
| 2118 | for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0'); |
| 2119 | if (zero) return; |
| 2120 | |
| 2121 | bc_num_init(&temp, BC_NUM_DEF_SIZE); |
| 2122 | bc_num_init(&mult, BC_NUM_DEF_SIZE); |
| 2123 | |
| 2124 | for (i = 0; i < len; ++i) { |
| 2125 | |
| 2126 | c = val[i]; |
| 2127 | if (c == '.') break; |
| 2128 | |
| 2129 | v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10); |
| 2130 | |
| 2131 | s = bc_num_mul(n, base, &mult, 0); |
| 2132 | if (s) goto int_err; |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 2133 | bc_num_ulong2num(&temp, v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2134 | s = bc_num_add(&mult, &temp, n, 0); |
| 2135 | if (s) goto int_err; |
| 2136 | } |
| 2137 | |
| 2138 | if (i == len) { |
| 2139 | c = val[i]; |
| 2140 | if (c == 0) goto int_err; |
| 2141 | } |
| 2142 | |
| 2143 | bc_num_init(&result, base->len); |
| 2144 | bc_num_zero(&result); |
| 2145 | bc_num_one(&mult); |
| 2146 | |
| 2147 | for (i += 1, digits = 0; i < len; ++i, ++digits) { |
| 2148 | |
| 2149 | c = val[i]; |
| 2150 | if (c == 0) break; |
| 2151 | |
| 2152 | v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10); |
| 2153 | |
| 2154 | s = bc_num_mul(&result, base, &result, 0); |
| 2155 | if (s) goto err; |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 2156 | bc_num_ulong2num(&temp, v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2157 | s = bc_num_add(&result, &temp, &result, 0); |
| 2158 | if (s) goto err; |
| 2159 | s = bc_num_mul(&mult, base, &mult, 0); |
| 2160 | if (s) goto err; |
| 2161 | } |
| 2162 | |
| 2163 | s = bc_num_div(&result, &mult, &result, digits); |
| 2164 | if (s) goto err; |
| 2165 | s = bc_num_add(n, &result, n, digits); |
| 2166 | if (s) goto err; |
| 2167 | |
| 2168 | if (n->len != 0) { |
| 2169 | if (n->rdx < digits) bc_num_extend(n, digits - n->rdx); |
| 2170 | } |
| 2171 | else |
| 2172 | bc_num_zero(n); |
| 2173 | |
| 2174 | err: |
| 2175 | bc_num_free(&result); |
| 2176 | int_err: |
| 2177 | bc_num_free(&mult); |
| 2178 | bc_num_free(&temp); |
| 2179 | } |
| 2180 | |
| 2181 | static void bc_num_printNewline(size_t *nchars, size_t line_len) |
| 2182 | { |
| 2183 | if (*nchars == line_len - 1) { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2184 | bb_putchar('\\'); |
| 2185 | bb_putchar('\n'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2186 | *nchars = 0; |
| 2187 | } |
| 2188 | } |
| 2189 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2190 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2191 | static void bc_num_printChar(size_t num, size_t width, bool radix, |
| 2192 | size_t *nchars, size_t line_len) |
| 2193 | { |
| 2194 | (void) radix, (void) line_len; |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2195 | bb_putchar((char) num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2196 | *nchars = *nchars + width; |
| 2197 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2198 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2199 | |
| 2200 | static void bc_num_printDigits(size_t num, size_t width, bool radix, |
| 2201 | size_t *nchars, size_t line_len) |
| 2202 | { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2203 | size_t exp, pow; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2204 | |
| 2205 | bc_num_printNewline(nchars, line_len); |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2206 | bb_putchar(radix ? '.' : ' '); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2207 | ++(*nchars); |
| 2208 | |
| 2209 | bc_num_printNewline(nchars, line_len); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2210 | for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10) |
| 2211 | continue; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2212 | |
| 2213 | for (exp = 0; exp < width; pow /= 10, ++(*nchars), ++exp) { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2214 | size_t dig; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2215 | bc_num_printNewline(nchars, line_len); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2216 | dig = num / pow; |
| 2217 | num -= dig * pow; |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2218 | bb_putchar(((char) dig) + '0'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2219 | } |
| 2220 | } |
| 2221 | |
| 2222 | static void bc_num_printHex(size_t num, size_t width, bool radix, |
| 2223 | size_t *nchars, size_t line_len) |
| 2224 | { |
| 2225 | if (radix) { |
| 2226 | bc_num_printNewline(nchars, line_len); |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2227 | bb_putchar('.'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2228 | *nchars += 1; |
| 2229 | } |
| 2230 | |
| 2231 | bc_num_printNewline(nchars, line_len); |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2232 | bb_putchar(bb_hexdigits_upcase[num]); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2233 | *nchars = *nchars + width; |
| 2234 | } |
| 2235 | |
| 2236 | static void bc_num_printDecimal(BcNum *n, size_t *nchars, size_t len) |
| 2237 | { |
| 2238 | size_t i, rdx = n->rdx - 1; |
| 2239 | |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2240 | if (n->neg) bb_putchar('-'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2241 | (*nchars) += n->neg; |
| 2242 | |
| 2243 | for (i = n->len - 1; i < n->len; --i) |
| 2244 | bc_num_printHex((size_t) n->num[i], 1, i == rdx, nchars, len); |
| 2245 | } |
| 2246 | |
| 2247 | static BcStatus bc_num_printNum(BcNum *n, BcNum *base, size_t width, |
| 2248 | size_t *nchars, size_t len, BcNumDigitOp print) |
| 2249 | { |
| 2250 | BcStatus s; |
| 2251 | BcVec stack; |
| 2252 | BcNum intp, fracp, digit, frac_len; |
| 2253 | unsigned long dig, *ptr; |
| 2254 | size_t i; |
| 2255 | bool radix; |
| 2256 | |
| 2257 | if (n->len == 0) { |
| 2258 | print(0, width, false, nchars, len); |
| 2259 | return BC_STATUS_SUCCESS; |
| 2260 | } |
| 2261 | |
| 2262 | bc_vec_init(&stack, sizeof(long), NULL); |
| 2263 | bc_num_init(&intp, n->len); |
| 2264 | bc_num_init(&fracp, n->rdx); |
| 2265 | bc_num_init(&digit, width); |
| 2266 | bc_num_init(&frac_len, BC_NUM_INT(n)); |
| 2267 | bc_num_copy(&intp, n); |
| 2268 | bc_num_one(&frac_len); |
| 2269 | |
| 2270 | bc_num_truncate(&intp, intp.rdx); |
| 2271 | s = bc_num_sub(n, &intp, &fracp, 0); |
| 2272 | if (s) goto err; |
| 2273 | |
| 2274 | while (intp.len != 0) { |
| 2275 | s = bc_num_divmod(&intp, base, &intp, &digit, 0); |
| 2276 | if (s) goto err; |
| 2277 | s = bc_num_ulong(&digit, &dig); |
| 2278 | if (s) goto err; |
| 2279 | bc_vec_push(&stack, &dig); |
| 2280 | } |
| 2281 | |
| 2282 | for (i = 0; i < stack.len; ++i) { |
| 2283 | ptr = bc_vec_item_rev(&stack, i); |
| 2284 | print(*ptr, width, false, nchars, len); |
| 2285 | } |
| 2286 | |
| 2287 | if (!n->rdx) goto err; |
| 2288 | |
| 2289 | for (radix = true; frac_len.len <= n->rdx; radix = false) { |
| 2290 | s = bc_num_mul(&fracp, base, &fracp, n->rdx); |
| 2291 | if (s) goto err; |
| 2292 | s = bc_num_ulong(&fracp, &dig); |
| 2293 | if (s) goto err; |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 2294 | bc_num_ulong2num(&intp, dig); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2295 | s = bc_num_sub(&fracp, &intp, &fracp, 0); |
| 2296 | if (s) goto err; |
| 2297 | print(dig, width, radix, nchars, len); |
| 2298 | s = bc_num_mul(&frac_len, base, &frac_len, 0); |
| 2299 | if (s) goto err; |
| 2300 | } |
| 2301 | |
| 2302 | err: |
| 2303 | bc_num_free(&frac_len); |
| 2304 | bc_num_free(&digit); |
| 2305 | bc_num_free(&fracp); |
| 2306 | bc_num_free(&intp); |
| 2307 | bc_vec_free(&stack); |
| 2308 | return s; |
| 2309 | } |
| 2310 | |
| 2311 | static BcStatus bc_num_printBase(BcNum *n, BcNum *base, size_t base_t, |
| 2312 | size_t *nchars, size_t line_len) |
| 2313 | { |
| 2314 | BcStatus s; |
| 2315 | size_t width, i; |
| 2316 | BcNumDigitOp print; |
| 2317 | bool neg = n->neg; |
| 2318 | |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2319 | if (neg) bb_putchar('-'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2320 | (*nchars) += neg; |
| 2321 | |
| 2322 | n->neg = false; |
| 2323 | |
| 2324 | if (base_t <= BC_NUM_MAX_IBASE) { |
| 2325 | width = 1; |
| 2326 | print = bc_num_printHex; |
| 2327 | } |
| 2328 | else { |
| 2329 | for (i = base_t - 1, width = 0; i != 0; i /= 10, ++width); |
| 2330 | print = bc_num_printDigits; |
| 2331 | } |
| 2332 | |
| 2333 | s = bc_num_printNum(n, base, width, nchars, line_len, print); |
| 2334 | n->neg = neg; |
| 2335 | |
| 2336 | return s; |
| 2337 | } |
| 2338 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2339 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2340 | static BcStatus bc_num_stream(BcNum *n, BcNum *base, size_t *nchars, size_t len) |
| 2341 | { |
| 2342 | return bc_num_printNum(n, base, 1, nchars, len, bc_num_printChar); |
| 2343 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2344 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2345 | |
| 2346 | static void bc_num_init(BcNum *n, size_t req) |
| 2347 | { |
| 2348 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
| 2349 | memset(n, 0, sizeof(BcNum)); |
| 2350 | n->num = xmalloc(req); |
| 2351 | n->cap = req; |
| 2352 | } |
| 2353 | |
| 2354 | static void bc_num_expand(BcNum *n, size_t req) |
| 2355 | { |
| 2356 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
| 2357 | if (req > n->cap) { |
| 2358 | n->num = xrealloc(n->num, req); |
| 2359 | n->cap = req; |
| 2360 | } |
| 2361 | } |
| 2362 | |
| 2363 | static void bc_num_free(void *num) |
| 2364 | { |
| 2365 | free(((BcNum *) num)->num); |
| 2366 | } |
| 2367 | |
| 2368 | static void bc_num_copy(BcNum *d, BcNum *s) |
| 2369 | { |
| 2370 | if (d != s) { |
| 2371 | bc_num_expand(d, s->cap); |
| 2372 | d->len = s->len; |
| 2373 | d->neg = s->neg; |
| 2374 | d->rdx = s->rdx; |
| 2375 | memcpy(d->num, s->num, sizeof(BcDig) * d->len); |
| 2376 | } |
| 2377 | } |
| 2378 | |
| 2379 | static BcStatus bc_num_parse(BcNum *n, const char *val, BcNum *base, |
| 2380 | size_t base_t) |
| 2381 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2382 | if (!bc_num_strValid(val, base_t)) |
| 2383 | return bc_error("bad number string"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2384 | |
| 2385 | if (base_t == 10) |
| 2386 | bc_num_parseDecimal(n, val); |
| 2387 | else |
| 2388 | bc_num_parseBase(n, val, base); |
| 2389 | |
| 2390 | return BC_STATUS_SUCCESS; |
| 2391 | } |
| 2392 | |
| 2393 | static BcStatus bc_num_print(BcNum *n, BcNum *base, size_t base_t, bool newline, |
| 2394 | size_t *nchars, size_t line_len) |
| 2395 | { |
| 2396 | BcStatus s = BC_STATUS_SUCCESS; |
| 2397 | |
| 2398 | bc_num_printNewline(nchars, line_len); |
| 2399 | |
| 2400 | if (n->len == 0) { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2401 | bb_putchar('0'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2402 | ++(*nchars); |
| 2403 | } |
| 2404 | else if (base_t == 10) |
| 2405 | bc_num_printDecimal(n, nchars, line_len); |
| 2406 | else |
| 2407 | s = bc_num_printBase(n, base, base_t, nchars, line_len); |
| 2408 | |
| 2409 | if (newline) { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2410 | bb_putchar('\n'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2411 | *nchars = 0; |
| 2412 | } |
| 2413 | |
| 2414 | return s; |
| 2415 | } |
| 2416 | |
| 2417 | static BcStatus bc_num_ulong(BcNum *n, unsigned long *result) |
| 2418 | { |
| 2419 | size_t i; |
| 2420 | unsigned long pow; |
| 2421 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2422 | if (n->neg) return bc_error("negative number"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2423 | |
| 2424 | for (*result = 0, pow = 1, i = n->rdx; i < n->len; ++i) { |
| 2425 | |
| 2426 | unsigned long prev = *result, powprev = pow; |
| 2427 | |
| 2428 | *result += ((unsigned long) n->num[i]) * pow; |
| 2429 | pow *= 10; |
| 2430 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2431 | if (*result < prev || pow < powprev) |
| 2432 | return bc_error("overflow"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2433 | } |
| 2434 | |
| 2435 | return BC_STATUS_SUCCESS; |
| 2436 | } |
| 2437 | |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 2438 | static void bc_num_ulong2num(BcNum *n, unsigned long val) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2439 | { |
| 2440 | size_t len; |
| 2441 | BcDig *ptr; |
| 2442 | unsigned long i; |
| 2443 | |
| 2444 | bc_num_zero(n); |
| 2445 | |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 2446 | if (val == 0) return; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2447 | |
| 2448 | for (len = 1, i = ULONG_MAX; i != 0; i /= 10, ++len) bc_num_expand(n, len); |
| 2449 | for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= 10) ptr[i] = val % 10; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2450 | } |
| 2451 | |
| 2452 | static BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 2453 | { |
| 2454 | BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s; |
| 2455 | (void) scale; |
| 2456 | return bc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)); |
| 2457 | } |
| 2458 | |
| 2459 | static BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 2460 | { |
| 2461 | BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a; |
| 2462 | (void) scale; |
| 2463 | return bc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)); |
| 2464 | } |
| 2465 | |
| 2466 | static BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 2467 | { |
| 2468 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 2469 | return bc_num_binary(a, b, c, scale, bc_num_m, req); |
| 2470 | } |
| 2471 | |
| 2472 | static BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 2473 | { |
| 2474 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 2475 | return bc_num_binary(a, b, c, scale, bc_num_d, req); |
| 2476 | } |
| 2477 | |
| 2478 | static BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 2479 | { |
| 2480 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 2481 | return bc_num_binary(a, b, c, scale, bc_num_rem, req); |
| 2482 | } |
| 2483 | |
| 2484 | static BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 2485 | { |
| 2486 | return bc_num_binary(a, b, c, scale, bc_num_p, a->len * b->len + 1); |
| 2487 | } |
| 2488 | |
| 2489 | static BcStatus bc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale) |
| 2490 | { |
| 2491 | BcStatus s; |
| 2492 | BcNum num1, num2, half, f, fprime, *x0, *x1, *temp; |
| 2493 | size_t pow, len, digs, digs1, resrdx, req, times = 0; |
| 2494 | ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX; |
| 2495 | |
| 2496 | req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1; |
| 2497 | bc_num_expand(b, req); |
| 2498 | |
| 2499 | if (a->len == 0) { |
| 2500 | bc_num_setToZero(b, scale); |
| 2501 | return BC_STATUS_SUCCESS; |
| 2502 | } |
| 2503 | else if (a->neg) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2504 | return bc_error("negative number"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2505 | else if (BC_NUM_ONE(a)) { |
| 2506 | bc_num_one(b); |
| 2507 | bc_num_extend(b, scale); |
| 2508 | return BC_STATUS_SUCCESS; |
| 2509 | } |
| 2510 | |
| 2511 | scale = BC_MAX(scale, a->rdx) + 1; |
| 2512 | len = a->len + scale; |
| 2513 | |
| 2514 | bc_num_init(&num1, len); |
| 2515 | bc_num_init(&num2, len); |
| 2516 | bc_num_init(&half, BC_NUM_DEF_SIZE); |
| 2517 | |
| 2518 | bc_num_one(&half); |
| 2519 | half.num[0] = 5; |
| 2520 | half.rdx = 1; |
| 2521 | |
| 2522 | bc_num_init(&f, len); |
| 2523 | bc_num_init(&fprime, len); |
| 2524 | |
| 2525 | x0 = &num1; |
| 2526 | x1 = &num2; |
| 2527 | |
| 2528 | bc_num_one(x0); |
| 2529 | pow = BC_NUM_INT(a); |
| 2530 | |
| 2531 | if (pow) { |
| 2532 | |
| 2533 | if (pow & 1) |
| 2534 | x0->num[0] = 2; |
| 2535 | else |
| 2536 | x0->num[0] = 6; |
| 2537 | |
| 2538 | pow -= 2 - (pow & 1); |
| 2539 | |
| 2540 | bc_num_extend(x0, pow); |
| 2541 | |
| 2542 | // Make sure to move the radix back. |
| 2543 | x0->rdx -= pow; |
| 2544 | } |
| 2545 | |
| 2546 | x0->rdx = digs = digs1 = 0; |
| 2547 | resrdx = scale + 2; |
| 2548 | len = BC_NUM_INT(x0) + resrdx - 1; |
| 2549 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2550 | while (cmp != 0 || digs < len) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2551 | |
| 2552 | s = bc_num_div(a, x0, &f, resrdx); |
| 2553 | if (s) goto err; |
| 2554 | s = bc_num_add(x0, &f, &fprime, resrdx); |
| 2555 | if (s) goto err; |
| 2556 | s = bc_num_mul(&fprime, &half, x1, resrdx); |
| 2557 | if (s) goto err; |
| 2558 | |
| 2559 | cmp = bc_num_cmp(x1, x0); |
| 2560 | digs = x1->len - (unsigned long long) llabs(cmp); |
| 2561 | |
| 2562 | if (cmp == cmp2 && digs == digs1) |
| 2563 | times += 1; |
| 2564 | else |
| 2565 | times = 0; |
| 2566 | |
| 2567 | resrdx += times > 4; |
| 2568 | |
| 2569 | cmp2 = cmp1; |
| 2570 | cmp1 = cmp; |
| 2571 | digs1 = digs; |
| 2572 | |
| 2573 | temp = x0; |
| 2574 | x0 = x1; |
| 2575 | x1 = temp; |
| 2576 | } |
| 2577 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2578 | bc_num_copy(b, x0); |
| 2579 | scale -= 1; |
| 2580 | if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale); |
| 2581 | |
| 2582 | err: |
| 2583 | bc_num_free(&fprime); |
| 2584 | bc_num_free(&f); |
| 2585 | bc_num_free(&half); |
| 2586 | bc_num_free(&num2); |
| 2587 | bc_num_free(&num1); |
| 2588 | return s; |
| 2589 | } |
| 2590 | |
| 2591 | static BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, |
| 2592 | size_t scale) |
| 2593 | { |
| 2594 | BcStatus s; |
| 2595 | BcNum num2, *ptr_a; |
| 2596 | bool init = false; |
| 2597 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 2598 | |
| 2599 | if (c == a) { |
| 2600 | memcpy(&num2, c, sizeof(BcNum)); |
| 2601 | ptr_a = &num2; |
| 2602 | bc_num_init(c, len); |
| 2603 | init = true; |
| 2604 | } |
| 2605 | else { |
| 2606 | ptr_a = a; |
| 2607 | bc_num_expand(c, len); |
| 2608 | } |
| 2609 | |
| 2610 | s = bc_num_r(ptr_a, b, c, d, scale, ts); |
| 2611 | |
| 2612 | if (init) bc_num_free(&num2); |
| 2613 | |
| 2614 | return s; |
| 2615 | } |
| 2616 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2617 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2618 | static BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) |
| 2619 | { |
| 2620 | BcStatus s; |
| 2621 | BcNum base, exp, two, temp; |
| 2622 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2623 | if (c->len == 0) |
| 2624 | return bc_error("divide by zero"); |
| 2625 | if (a->rdx || b->rdx || c->rdx) |
| 2626 | return bc_error("non integer number"); |
| 2627 | if (b->neg) |
| 2628 | return bc_error("negative number"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2629 | |
| 2630 | bc_num_expand(d, c->len); |
| 2631 | bc_num_init(&base, c->len); |
| 2632 | bc_num_init(&exp, b->len); |
| 2633 | bc_num_init(&two, BC_NUM_DEF_SIZE); |
| 2634 | bc_num_init(&temp, b->len); |
| 2635 | |
| 2636 | bc_num_one(&two); |
| 2637 | two.num[0] = 2; |
| 2638 | bc_num_one(d); |
| 2639 | |
| 2640 | s = bc_num_rem(a, c, &base, 0); |
| 2641 | if (s) goto err; |
| 2642 | bc_num_copy(&exp, b); |
| 2643 | |
| 2644 | while (exp.len != 0) { |
| 2645 | |
| 2646 | s = bc_num_divmod(&exp, &two, &exp, &temp, 0); |
| 2647 | if (s) goto err; |
| 2648 | |
| 2649 | if (BC_NUM_ONE(&temp)) { |
| 2650 | s = bc_num_mul(d, &base, &temp, 0); |
| 2651 | if (s) goto err; |
| 2652 | s = bc_num_rem(&temp, c, d, 0); |
| 2653 | if (s) goto err; |
| 2654 | } |
| 2655 | |
| 2656 | s = bc_num_mul(&base, &base, &temp, 0); |
| 2657 | if (s) goto err; |
| 2658 | s = bc_num_rem(&temp, c, &base, 0); |
| 2659 | if (s) goto err; |
| 2660 | } |
| 2661 | |
| 2662 | err: |
| 2663 | bc_num_free(&temp); |
| 2664 | bc_num_free(&two); |
| 2665 | bc_num_free(&exp); |
| 2666 | bc_num_free(&base); |
| 2667 | return s; |
| 2668 | } |
| 2669 | #endif // ENABLE_DC |
| 2670 | |
| 2671 | static int bc_id_cmp(const void *e1, const void *e2) |
| 2672 | { |
| 2673 | return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name); |
| 2674 | } |
| 2675 | |
| 2676 | static void bc_id_free(void *id) |
| 2677 | { |
| 2678 | free(((BcId *) id)->name); |
| 2679 | } |
| 2680 | |
| 2681 | static BcStatus bc_func_insert(BcFunc *f, char *name, bool var) |
| 2682 | { |
| 2683 | BcId a; |
| 2684 | size_t i; |
| 2685 | |
| 2686 | for (i = 0; i < f->autos.len; ++i) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2687 | if (strcmp(name, ((BcId *) bc_vec_item(&f->autos, i))->name) == 0) |
| 2688 | 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] | 2689 | } |
| 2690 | |
| 2691 | a.idx = var; |
| 2692 | a.name = name; |
| 2693 | |
| 2694 | bc_vec_push(&f->autos, &a); |
| 2695 | |
| 2696 | return BC_STATUS_SUCCESS; |
| 2697 | } |
| 2698 | |
| 2699 | static void bc_func_init(BcFunc *f) |
| 2700 | { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 2701 | bc_char_vec_init(&f->code); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2702 | bc_vec_init(&f->autos, sizeof(BcId), bc_id_free); |
| 2703 | bc_vec_init(&f->labels, sizeof(size_t), NULL); |
| 2704 | f->nparams = 0; |
| 2705 | } |
| 2706 | |
| 2707 | static void bc_func_free(void *func) |
| 2708 | { |
| 2709 | BcFunc *f = (BcFunc *) func; |
| 2710 | bc_vec_free(&f->code); |
| 2711 | bc_vec_free(&f->autos); |
| 2712 | bc_vec_free(&f->labels); |
| 2713 | } |
| 2714 | |
| 2715 | static void bc_array_init(BcVec *a, bool nums) |
| 2716 | { |
| 2717 | if (nums) |
| 2718 | bc_vec_init(a, sizeof(BcNum), bc_num_free); |
| 2719 | else |
| 2720 | bc_vec_init(a, sizeof(BcVec), bc_vec_free); |
| 2721 | bc_array_expand(a, 1); |
| 2722 | } |
| 2723 | |
| 2724 | static void bc_array_copy(BcVec *d, const BcVec *s) |
| 2725 | { |
| 2726 | size_t i; |
| 2727 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 2728 | bc_vec_pop_all(d); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2729 | bc_vec_expand(d, s->cap); |
| 2730 | d->len = s->len; |
| 2731 | |
| 2732 | for (i = 0; i < s->len; ++i) { |
| 2733 | BcNum *dnum = bc_vec_item(d, i), *snum = bc_vec_item(s, i); |
| 2734 | bc_num_init(dnum, snum->len); |
| 2735 | bc_num_copy(dnum, snum); |
| 2736 | } |
| 2737 | } |
| 2738 | |
| 2739 | static void bc_array_expand(BcVec *a, size_t len) |
| 2740 | { |
| 2741 | BcResultData data; |
| 2742 | |
| 2743 | if (a->size == sizeof(BcNum) && a->dtor == bc_num_free) { |
| 2744 | while (len > a->len) { |
| 2745 | bc_num_init(&data.n, BC_NUM_DEF_SIZE); |
| 2746 | bc_vec_push(a, &data.n); |
| 2747 | } |
| 2748 | } |
| 2749 | else { |
| 2750 | while (len > a->len) { |
| 2751 | bc_array_init(&data.v, true); |
| 2752 | bc_vec_push(a, &data.v); |
| 2753 | } |
| 2754 | } |
| 2755 | } |
| 2756 | |
| 2757 | static void bc_string_free(void *string) |
| 2758 | { |
| 2759 | free(*((char **) string)); |
| 2760 | } |
| 2761 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2762 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2763 | static void bc_result_copy(BcResult *d, BcResult *src) |
| 2764 | { |
| 2765 | d->t = src->t; |
| 2766 | |
| 2767 | switch (d->t) { |
| 2768 | |
| 2769 | case BC_RESULT_TEMP: |
| 2770 | case BC_RESULT_IBASE: |
| 2771 | case BC_RESULT_SCALE: |
| 2772 | case BC_RESULT_OBASE: |
| 2773 | { |
| 2774 | bc_num_init(&d->d.n, src->d.n.len); |
| 2775 | bc_num_copy(&d->d.n, &src->d.n); |
| 2776 | break; |
| 2777 | } |
| 2778 | |
| 2779 | case BC_RESULT_VAR: |
| 2780 | case BC_RESULT_ARRAY: |
| 2781 | case BC_RESULT_ARRAY_ELEM: |
| 2782 | { |
| 2783 | d->d.id.name = xstrdup(src->d.id.name); |
| 2784 | break; |
| 2785 | } |
| 2786 | |
| 2787 | case BC_RESULT_CONSTANT: |
| 2788 | case BC_RESULT_LAST: |
| 2789 | case BC_RESULT_ONE: |
| 2790 | case BC_RESULT_STR: |
| 2791 | { |
| 2792 | memcpy(&d->d.n, &src->d.n, sizeof(BcNum)); |
| 2793 | break; |
| 2794 | } |
| 2795 | } |
| 2796 | } |
| 2797 | #endif // ENABLE_DC |
| 2798 | |
| 2799 | static void bc_result_free(void *result) |
| 2800 | { |
| 2801 | BcResult *r = (BcResult *) result; |
| 2802 | |
| 2803 | switch (r->t) { |
| 2804 | |
| 2805 | case BC_RESULT_TEMP: |
| 2806 | case BC_RESULT_IBASE: |
| 2807 | case BC_RESULT_SCALE: |
| 2808 | case BC_RESULT_OBASE: |
| 2809 | { |
| 2810 | bc_num_free(&r->d.n); |
| 2811 | break; |
| 2812 | } |
| 2813 | |
| 2814 | case BC_RESULT_VAR: |
| 2815 | case BC_RESULT_ARRAY: |
| 2816 | case BC_RESULT_ARRAY_ELEM: |
| 2817 | { |
| 2818 | free(r->d.id.name); |
| 2819 | break; |
| 2820 | } |
| 2821 | |
| 2822 | default: |
| 2823 | { |
| 2824 | // Do nothing. |
| 2825 | break; |
| 2826 | } |
| 2827 | } |
| 2828 | } |
| 2829 | |
| 2830 | static void bc_lex_lineComment(BcLex *l) |
| 2831 | { |
| 2832 | l->t.t = BC_LEX_WHITESPACE; |
| 2833 | while (l->i < l->len && l->buf[l->i++] != '\n'); |
| 2834 | --l->i; |
| 2835 | } |
| 2836 | |
| 2837 | static void bc_lex_whitespace(BcLex *l) |
| 2838 | { |
| 2839 | char c; |
| 2840 | l->t.t = BC_LEX_WHITESPACE; |
| 2841 | for (c = l->buf[l->i]; c != '\n' && isspace(c); c = l->buf[++l->i]); |
| 2842 | } |
| 2843 | |
| 2844 | static BcStatus bc_lex_number(BcLex *l, char start) |
| 2845 | { |
| 2846 | const char *buf = l->buf + l->i; |
| 2847 | size_t len, hits = 0, bslashes = 0, i = 0, j; |
| 2848 | char c = buf[i]; |
| 2849 | bool last_pt, pt = start == '.'; |
| 2850 | |
| 2851 | last_pt = pt; |
| 2852 | l->t.t = BC_LEX_NUMBER; |
| 2853 | |
| 2854 | while (c != 0 && (isdigit(c) || (c >= 'A' && c <= 'F') || |
| 2855 | (c == '.' && !pt) || (c == '\\' && buf[i + 1] == '\n'))) |
| 2856 | { |
| 2857 | if (c != '\\') { |
| 2858 | last_pt = c == '.'; |
| 2859 | pt = pt || last_pt; |
| 2860 | } |
| 2861 | else { |
| 2862 | ++i; |
| 2863 | bslashes += 1; |
| 2864 | } |
| 2865 | |
| 2866 | c = buf[++i]; |
| 2867 | } |
| 2868 | |
Denys Vlasenko | d5f7703 | 2018-12-04 21:37:56 +0100 | [diff] [blame] | 2869 | len = i + !last_pt - bslashes * 2; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2870 | if (len > BC_MAX_NUM) |
| 2871 | return bc_error("number too long: must be [1, BC_NUM_MAX]"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2872 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 2873 | bc_vec_pop_all(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2874 | bc_vec_expand(&l->t.v, len + 1); |
| 2875 | bc_vec_push(&l->t.v, &start); |
| 2876 | |
| 2877 | for (buf -= 1, j = 1; j < len + hits * 2; ++j) { |
| 2878 | |
| 2879 | c = buf[j]; |
| 2880 | |
| 2881 | // If we have hit a backslash, skip it. We don't have |
| 2882 | // to check for a newline because it's guaranteed. |
| 2883 | if (hits < bslashes && c == '\\') { |
| 2884 | ++hits; |
| 2885 | ++j; |
| 2886 | continue; |
| 2887 | } |
| 2888 | |
| 2889 | bc_vec_push(&l->t.v, &c); |
| 2890 | } |
| 2891 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 2892 | bc_vec_pushZeroByte(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2893 | l->i += i; |
| 2894 | |
| 2895 | return BC_STATUS_SUCCESS; |
| 2896 | } |
| 2897 | |
| 2898 | static BcStatus bc_lex_name(BcLex *l) |
| 2899 | { |
| 2900 | size_t i = 0; |
| 2901 | const char *buf = l->buf + l->i - 1; |
| 2902 | char c = buf[i]; |
| 2903 | |
| 2904 | l->t.t = BC_LEX_NAME; |
| 2905 | |
| 2906 | while ((c >= 'a' && c <= 'z') || isdigit(c) || c == '_') c = buf[++i]; |
| 2907 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2908 | if (i > BC_MAX_STRING) |
| 2909 | return bc_error("name too long: must be [1, BC_NAME_MAX]"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2910 | bc_vec_string(&l->t.v, i, buf); |
| 2911 | |
| 2912 | // Increment the index. We minus 1 because it has already been incremented. |
| 2913 | l->i += i - 1; |
| 2914 | |
| 2915 | return BC_STATUS_SUCCESS; |
| 2916 | } |
| 2917 | |
| 2918 | static void bc_lex_init(BcLex *l, BcLexNext next) |
| 2919 | { |
| 2920 | l->next = next; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 2921 | bc_char_vec_init(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2922 | } |
| 2923 | |
| 2924 | static void bc_lex_free(BcLex *l) |
| 2925 | { |
| 2926 | bc_vec_free(&l->t.v); |
| 2927 | } |
| 2928 | |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 2929 | static void bc_lex_file(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2930 | { |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 2931 | G.err_line = l->line = 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2932 | l->newline = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2933 | } |
| 2934 | |
| 2935 | static BcStatus bc_lex_next(BcLex *l) |
| 2936 | { |
| 2937 | BcStatus s; |
| 2938 | |
| 2939 | l->t.last = l->t.t; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2940 | 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] | 2941 | |
| 2942 | l->line += l->newline; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 2943 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2944 | l->t.t = BC_LEX_EOF; |
| 2945 | |
| 2946 | l->newline = (l->i == l->len); |
| 2947 | if (l->newline) return BC_STATUS_SUCCESS; |
| 2948 | |
| 2949 | // Loop until failure or we don't have whitespace. This |
| 2950 | // is so the parser doesn't get inundated with whitespace. |
| 2951 | do { |
| 2952 | s = l->next(l); |
| 2953 | } while (!s && l->t.t == BC_LEX_WHITESPACE); |
| 2954 | |
| 2955 | return s; |
| 2956 | } |
| 2957 | |
| 2958 | static BcStatus bc_lex_text(BcLex *l, const char *text) |
| 2959 | { |
| 2960 | l->buf = text; |
| 2961 | l->i = 0; |
| 2962 | l->len = strlen(text); |
| 2963 | l->t.t = l->t.last = BC_LEX_INVALID; |
| 2964 | return bc_lex_next(l); |
| 2965 | } |
| 2966 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2967 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2968 | static BcStatus bc_lex_identifier(BcLex *l) |
| 2969 | { |
| 2970 | BcStatus s; |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 2971 | unsigned i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2972 | const char *buf = l->buf + l->i - 1; |
| 2973 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 2974 | for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) { |
| 2975 | const char *keyword8 = bc_lex_kws[i].name8; |
| 2976 | unsigned j = 0; |
| 2977 | while (buf[j] != '\0' && buf[j] == keyword8[j]) { |
| 2978 | j++; |
| 2979 | if (j == 8) goto match; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2980 | } |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 2981 | if (keyword8[j] != '\0') |
| 2982 | continue; |
| 2983 | match: |
| 2984 | // buf starts with keyword bc_lex_kws[i] |
| 2985 | l->t.t = BC_LEX_KEY_1st_keyword + i; |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 2986 | if (!((1 << i) & POSIX_KWORD_MASK)) { |
Denys Vlasenko | 0d7e46b | 2018-12-05 18:31:19 +0100 | [diff] [blame] | 2987 | 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] | 2988 | if (s) return s; |
| 2989 | } |
| 2990 | |
| 2991 | // We minus 1 because the index has already been incremented. |
| 2992 | l->i += j - 1; |
| 2993 | return BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2994 | } |
| 2995 | |
| 2996 | s = bc_lex_name(l); |
| 2997 | if (s) return s; |
| 2998 | |
Denys Vlasenko | 0d7e46b | 2018-12-05 18:31:19 +0100 | [diff] [blame] | 2999 | if (l->t.v.len > 2) { |
| 3000 | // Prevent this: |
| 3001 | // >>> qwe=1 |
| 3002 | // bc: POSIX only allows one character names; the following is bad: 'qwe=1 |
| 3003 | // ' |
| 3004 | unsigned len = strchrnul(buf, '\n') - buf; |
| 3005 | s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf); |
| 3006 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3007 | |
| 3008 | return s; |
| 3009 | } |
| 3010 | |
| 3011 | static BcStatus bc_lex_string(BcLex *l) |
| 3012 | { |
| 3013 | size_t len, nls = 0, i = l->i; |
| 3014 | char c; |
| 3015 | |
| 3016 | l->t.t = BC_LEX_STR; |
| 3017 | |
| 3018 | for (c = l->buf[i]; c != 0 && c != '"'; c = l->buf[++i]) nls += (c == '\n'); |
| 3019 | |
| 3020 | if (c == '\0') { |
| 3021 | l->i = i; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3022 | return bc_error("string end could not be found"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3023 | } |
| 3024 | |
| 3025 | len = i - l->i; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3026 | if (len > BC_MAX_STRING) |
| 3027 | return bc_error("string too long: must be [1, BC_STRING_MAX]"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3028 | bc_vec_string(&l->t.v, len, l->buf + l->i); |
| 3029 | |
| 3030 | l->i = i + 1; |
| 3031 | l->line += nls; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3032 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3033 | |
| 3034 | return BC_STATUS_SUCCESS; |
| 3035 | } |
| 3036 | |
| 3037 | static void bc_lex_assign(BcLex *l, BcLexType with, BcLexType without) |
| 3038 | { |
| 3039 | if (l->buf[l->i] == '=') { |
| 3040 | ++l->i; |
| 3041 | l->t.t = with; |
| 3042 | } |
| 3043 | else |
| 3044 | l->t.t = without; |
| 3045 | } |
| 3046 | |
| 3047 | static BcStatus bc_lex_comment(BcLex *l) |
| 3048 | { |
| 3049 | size_t i, nls = 0; |
| 3050 | const char *buf = l->buf; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3051 | |
| 3052 | l->t.t = BC_LEX_WHITESPACE; |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3053 | i = ++l->i; |
| 3054 | for (;;) { |
| 3055 | char c = buf[i]; |
| 3056 | check_star: |
| 3057 | if (c == '*') { |
| 3058 | c = buf[++i]; |
| 3059 | if (c == '/') |
| 3060 | break; |
| 3061 | goto check_star; |
| 3062 | } |
| 3063 | if (c == '\0') { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3064 | l->i = i; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3065 | return bc_error("comment end could not be found"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3066 | } |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3067 | nls += (c == '\n'); |
| 3068 | i++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3069 | } |
| 3070 | |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3071 | l->i = i + 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3072 | l->line += nls; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3073 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3074 | |
| 3075 | return BC_STATUS_SUCCESS; |
| 3076 | } |
| 3077 | |
| 3078 | static BcStatus bc_lex_token(BcLex *l) |
| 3079 | { |
| 3080 | BcStatus s = BC_STATUS_SUCCESS; |
| 3081 | char c = l->buf[l->i++], c2; |
| 3082 | |
| 3083 | // This is the workhorse of the lexer. |
| 3084 | switch (c) { |
| 3085 | |
| 3086 | case '\0': |
| 3087 | case '\n': |
| 3088 | { |
| 3089 | l->newline = true; |
| 3090 | l->t.t = !c ? BC_LEX_EOF : BC_LEX_NLINE; |
| 3091 | break; |
| 3092 | } |
| 3093 | |
| 3094 | case '\t': |
| 3095 | case '\v': |
| 3096 | case '\f': |
| 3097 | case '\r': |
| 3098 | case ' ': |
| 3099 | { |
| 3100 | bc_lex_whitespace(l); |
| 3101 | break; |
| 3102 | } |
| 3103 | |
| 3104 | case '!': |
| 3105 | { |
| 3106 | bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT); |
| 3107 | |
| 3108 | if (l->t.t == BC_LEX_OP_BOOL_NOT) { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3109 | s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3110 | if (s) return s; |
| 3111 | } |
| 3112 | |
| 3113 | break; |
| 3114 | } |
| 3115 | |
| 3116 | case '"': |
| 3117 | { |
| 3118 | s = bc_lex_string(l); |
| 3119 | break; |
| 3120 | } |
| 3121 | |
| 3122 | case '#': |
| 3123 | { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3124 | s = bc_POSIX_does_not_allow("'#' script comments"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3125 | if (s) return s; |
| 3126 | |
| 3127 | bc_lex_lineComment(l); |
| 3128 | |
| 3129 | break; |
| 3130 | } |
| 3131 | |
| 3132 | case '%': |
| 3133 | { |
| 3134 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS); |
| 3135 | break; |
| 3136 | } |
| 3137 | |
| 3138 | case '&': |
| 3139 | { |
| 3140 | c2 = l->buf[l->i]; |
| 3141 | if (c2 == '&') { |
| 3142 | |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3143 | s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3144 | if (s) return s; |
| 3145 | |
| 3146 | ++l->i; |
| 3147 | l->t.t = BC_LEX_OP_BOOL_AND; |
| 3148 | } |
| 3149 | else { |
| 3150 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3151 | s = bc_error_bad_character('&'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3152 | } |
| 3153 | |
| 3154 | break; |
| 3155 | } |
| 3156 | |
| 3157 | case '(': |
| 3158 | case ')': |
| 3159 | { |
| 3160 | l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN); |
| 3161 | break; |
| 3162 | } |
| 3163 | |
| 3164 | case '*': |
| 3165 | { |
| 3166 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY); |
| 3167 | break; |
| 3168 | } |
| 3169 | |
| 3170 | case '+': |
| 3171 | { |
| 3172 | c2 = l->buf[l->i]; |
| 3173 | if (c2 == '+') { |
| 3174 | ++l->i; |
| 3175 | l->t.t = BC_LEX_OP_INC; |
| 3176 | } |
| 3177 | else |
| 3178 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS); |
| 3179 | break; |
| 3180 | } |
| 3181 | |
| 3182 | case ',': |
| 3183 | { |
| 3184 | l->t.t = BC_LEX_COMMA; |
| 3185 | break; |
| 3186 | } |
| 3187 | |
| 3188 | case '-': |
| 3189 | { |
| 3190 | c2 = l->buf[l->i]; |
| 3191 | if (c2 == '-') { |
| 3192 | ++l->i; |
| 3193 | l->t.t = BC_LEX_OP_DEC; |
| 3194 | } |
| 3195 | else |
| 3196 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS); |
| 3197 | break; |
| 3198 | } |
| 3199 | |
| 3200 | case '.': |
| 3201 | { |
| 3202 | if (isdigit(l->buf[l->i])) |
| 3203 | s = bc_lex_number(l, c); |
| 3204 | else { |
| 3205 | l->t.t = BC_LEX_KEY_LAST; |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3206 | 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] | 3207 | } |
| 3208 | break; |
| 3209 | } |
| 3210 | |
| 3211 | case '/': |
| 3212 | { |
| 3213 | c2 = l->buf[l->i]; |
| 3214 | if (c2 == '*') |
| 3215 | s = bc_lex_comment(l); |
| 3216 | else |
| 3217 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE); |
| 3218 | break; |
| 3219 | } |
| 3220 | |
| 3221 | case '0': |
| 3222 | case '1': |
| 3223 | case '2': |
| 3224 | case '3': |
| 3225 | case '4': |
| 3226 | case '5': |
| 3227 | case '6': |
| 3228 | case '7': |
| 3229 | case '8': |
| 3230 | case '9': |
| 3231 | case 'A': |
| 3232 | case 'B': |
| 3233 | case 'C': |
| 3234 | case 'D': |
| 3235 | case 'E': |
| 3236 | case 'F': |
| 3237 | { |
| 3238 | s = bc_lex_number(l, c); |
| 3239 | break; |
| 3240 | } |
| 3241 | |
| 3242 | case ';': |
| 3243 | { |
| 3244 | l->t.t = BC_LEX_SCOLON; |
| 3245 | break; |
| 3246 | } |
| 3247 | |
| 3248 | case '<': |
| 3249 | { |
| 3250 | bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT); |
| 3251 | break; |
| 3252 | } |
| 3253 | |
| 3254 | case '=': |
| 3255 | { |
| 3256 | bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN); |
| 3257 | break; |
| 3258 | } |
| 3259 | |
| 3260 | case '>': |
| 3261 | { |
| 3262 | bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT); |
| 3263 | break; |
| 3264 | } |
| 3265 | |
| 3266 | case '[': |
| 3267 | case ']': |
| 3268 | { |
| 3269 | l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET); |
| 3270 | break; |
| 3271 | } |
| 3272 | |
| 3273 | case '\\': |
| 3274 | { |
| 3275 | if (l->buf[l->i] == '\n') { |
| 3276 | l->t.t = BC_LEX_WHITESPACE; |
| 3277 | ++l->i; |
| 3278 | } |
| 3279 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3280 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3281 | break; |
| 3282 | } |
| 3283 | |
| 3284 | case '^': |
| 3285 | { |
| 3286 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER); |
| 3287 | break; |
| 3288 | } |
| 3289 | |
| 3290 | case 'a': |
| 3291 | case 'b': |
| 3292 | case 'c': |
| 3293 | case 'd': |
| 3294 | case 'e': |
| 3295 | case 'f': |
| 3296 | case 'g': |
| 3297 | case 'h': |
| 3298 | case 'i': |
| 3299 | case 'j': |
| 3300 | case 'k': |
| 3301 | case 'l': |
| 3302 | case 'm': |
| 3303 | case 'n': |
| 3304 | case 'o': |
| 3305 | case 'p': |
| 3306 | case 'q': |
| 3307 | case 'r': |
| 3308 | case 's': |
| 3309 | case 't': |
| 3310 | case 'u': |
| 3311 | case 'v': |
| 3312 | case 'w': |
| 3313 | case 'x': |
| 3314 | case 'y': |
| 3315 | case 'z': |
| 3316 | { |
| 3317 | s = bc_lex_identifier(l); |
| 3318 | break; |
| 3319 | } |
| 3320 | |
| 3321 | case '{': |
| 3322 | case '}': |
| 3323 | { |
| 3324 | l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE); |
| 3325 | break; |
| 3326 | } |
| 3327 | |
| 3328 | case '|': |
| 3329 | { |
| 3330 | c2 = l->buf[l->i]; |
| 3331 | |
| 3332 | if (c2 == '|') { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3333 | s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3334 | if (s) return s; |
| 3335 | |
| 3336 | ++l->i; |
| 3337 | l->t.t = BC_LEX_OP_BOOL_OR; |
| 3338 | } |
| 3339 | else { |
| 3340 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3341 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3342 | } |
| 3343 | |
| 3344 | break; |
| 3345 | } |
| 3346 | |
| 3347 | default: |
| 3348 | { |
| 3349 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3350 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3351 | break; |
| 3352 | } |
| 3353 | } |
| 3354 | |
| 3355 | return s; |
| 3356 | } |
| 3357 | #endif // ENABLE_BC |
| 3358 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 3359 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3360 | static BcStatus dc_lex_register(BcLex *l) |
| 3361 | { |
| 3362 | BcStatus s = BC_STATUS_SUCCESS; |
| 3363 | |
| 3364 | if (isspace(l->buf[l->i - 1])) { |
| 3365 | bc_lex_whitespace(l); |
| 3366 | ++l->i; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 3367 | if (!G_exreg) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3368 | s = bc_error("extended register"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3369 | else |
| 3370 | s = bc_lex_name(l); |
| 3371 | } |
| 3372 | else { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3373 | bc_vec_pop_all(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3374 | bc_vec_pushByte(&l->t.v, l->buf[l->i - 1]); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 3375 | bc_vec_pushZeroByte(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3376 | l->t.t = BC_LEX_NAME; |
| 3377 | } |
| 3378 | |
| 3379 | return s; |
| 3380 | } |
| 3381 | |
| 3382 | static BcStatus dc_lex_string(BcLex *l) |
| 3383 | { |
| 3384 | size_t depth = 1, nls = 0, i = l->i; |
| 3385 | char c; |
| 3386 | |
| 3387 | l->t.t = BC_LEX_STR; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3388 | bc_vec_pop_all(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3389 | |
| 3390 | for (c = l->buf[i]; c != 0 && depth; c = l->buf[++i]) { |
| 3391 | |
| 3392 | depth += (c == '[' && (i == l->i || l->buf[i - 1] != '\\')); |
| 3393 | depth -= (c == ']' && (i == l->i || l->buf[i - 1] != '\\')); |
| 3394 | nls += (c == '\n'); |
| 3395 | |
| 3396 | if (depth) bc_vec_push(&l->t.v, &c); |
| 3397 | } |
| 3398 | |
| 3399 | if (c == '\0') { |
| 3400 | l->i = i; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3401 | return bc_error("string end could not be found"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3402 | } |
| 3403 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 3404 | bc_vec_pushZeroByte(&l->t.v); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3405 | if (i - l->i > BC_MAX_STRING) |
| 3406 | return bc_error("string too long: must be [1, BC_STRING_MAX]"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3407 | |
| 3408 | l->i = i; |
| 3409 | l->line += nls; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3410 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3411 | |
| 3412 | return BC_STATUS_SUCCESS; |
| 3413 | } |
| 3414 | |
| 3415 | static BcStatus dc_lex_token(BcLex *l) |
| 3416 | { |
| 3417 | BcStatus s = BC_STATUS_SUCCESS; |
| 3418 | char c = l->buf[l->i++], c2; |
| 3419 | size_t i; |
| 3420 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3421 | for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) { |
| 3422 | if (l->t.last == dc_lex_regs[i]) |
| 3423 | return dc_lex_register(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3424 | } |
| 3425 | |
| 3426 | if (c >= '%' && c <= '~' && |
| 3427 | (l->t.t = dc_lex_tokens[(c - '%')]) != BC_LEX_INVALID) |
| 3428 | { |
| 3429 | return s; |
| 3430 | } |
| 3431 | |
| 3432 | // This is the workhorse of the lexer. |
| 3433 | switch (c) { |
| 3434 | |
| 3435 | case '\0': |
| 3436 | { |
| 3437 | l->t.t = BC_LEX_EOF; |
| 3438 | break; |
| 3439 | } |
| 3440 | |
| 3441 | case '\n': |
| 3442 | case '\t': |
| 3443 | case '\v': |
| 3444 | case '\f': |
| 3445 | case '\r': |
| 3446 | case ' ': |
| 3447 | { |
| 3448 | l->newline = (c == '\n'); |
| 3449 | bc_lex_whitespace(l); |
| 3450 | break; |
| 3451 | } |
| 3452 | |
| 3453 | case '!': |
| 3454 | { |
| 3455 | c2 = l->buf[l->i]; |
| 3456 | |
| 3457 | if (c2 == '=') |
| 3458 | l->t.t = BC_LEX_OP_REL_NE; |
| 3459 | else if (c2 == '<') |
| 3460 | l->t.t = BC_LEX_OP_REL_LE; |
| 3461 | else if (c2 == '>') |
| 3462 | l->t.t = BC_LEX_OP_REL_GE; |
| 3463 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3464 | return bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3465 | |
| 3466 | ++l->i; |
| 3467 | break; |
| 3468 | } |
| 3469 | |
| 3470 | case '#': |
| 3471 | { |
| 3472 | bc_lex_lineComment(l); |
| 3473 | break; |
| 3474 | } |
| 3475 | |
| 3476 | case '.': |
| 3477 | { |
| 3478 | if (isdigit(l->buf[l->i])) |
| 3479 | s = bc_lex_number(l, c); |
| 3480 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3481 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3482 | break; |
| 3483 | } |
| 3484 | |
| 3485 | case '0': |
| 3486 | case '1': |
| 3487 | case '2': |
| 3488 | case '3': |
| 3489 | case '4': |
| 3490 | case '5': |
| 3491 | case '6': |
| 3492 | case '7': |
| 3493 | case '8': |
| 3494 | case '9': |
| 3495 | case 'A': |
| 3496 | case 'B': |
| 3497 | case 'C': |
| 3498 | case 'D': |
| 3499 | case 'E': |
| 3500 | case 'F': |
| 3501 | { |
| 3502 | s = bc_lex_number(l, c); |
| 3503 | break; |
| 3504 | } |
| 3505 | |
| 3506 | case '[': |
| 3507 | { |
| 3508 | s = dc_lex_string(l); |
| 3509 | break; |
| 3510 | } |
| 3511 | |
| 3512 | default: |
| 3513 | { |
| 3514 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3515 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3516 | break; |
| 3517 | } |
| 3518 | } |
| 3519 | |
| 3520 | return s; |
| 3521 | } |
| 3522 | #endif // ENABLE_DC |
| 3523 | |
| 3524 | static void bc_parse_addFunc(BcParse *p, char *name, size_t *idx) |
| 3525 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3526 | bc_program_addFunc(name, idx); |
| 3527 | p->func = bc_vec_item(&G.prog.fns, p->fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3528 | } |
| 3529 | |
| 3530 | static void bc_parse_pushName(BcParse *p, char *name) |
| 3531 | { |
| 3532 | size_t i = 0, len = strlen(name); |
| 3533 | |
| 3534 | for (; i < len; ++i) bc_parse_push(p, name[i]); |
| 3535 | bc_parse_push(p, BC_PARSE_STREND); |
| 3536 | |
| 3537 | free(name); |
| 3538 | } |
| 3539 | |
| 3540 | static void bc_parse_pushIndex(BcParse *p, size_t idx) |
| 3541 | { |
| 3542 | unsigned char amt, i, nums[sizeof(size_t)]; |
| 3543 | |
| 3544 | for (amt = 0; idx; ++amt) { |
| 3545 | nums[amt] = (char) idx; |
| 3546 | idx = (idx & ((unsigned long) ~(UCHAR_MAX))) >> sizeof(char) * CHAR_BIT; |
| 3547 | } |
| 3548 | |
| 3549 | bc_parse_push(p, amt); |
| 3550 | for (i = 0; i < amt; ++i) bc_parse_push(p, nums[i]); |
| 3551 | } |
| 3552 | |
| 3553 | static void bc_parse_number(BcParse *p, BcInst *prev, size_t *nexs) |
| 3554 | { |
| 3555 | char *num = xstrdup(p->l.t.v.v); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3556 | size_t idx = G.prog.consts.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3557 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3558 | bc_vec_push(&G.prog.consts, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3559 | |
| 3560 | bc_parse_push(p, BC_INST_NUM); |
| 3561 | bc_parse_pushIndex(p, idx); |
| 3562 | |
| 3563 | ++(*nexs); |
| 3564 | (*prev) = BC_INST_NUM; |
| 3565 | } |
| 3566 | |
| 3567 | static BcStatus bc_parse_text(BcParse *p, const char *text) |
| 3568 | { |
| 3569 | BcStatus s; |
| 3570 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3571 | p->func = bc_vec_item(&G.prog.fns, p->fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3572 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3573 | if (!text[0] && !BC_PARSE_CAN_EXEC(p)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3574 | p->l.t.t = BC_LEX_INVALID; |
| 3575 | s = p->parse(p); |
| 3576 | if (s) return s; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3577 | if (!BC_PARSE_CAN_EXEC(p)) |
| 3578 | return bc_error("file is not executable"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3579 | } |
| 3580 | |
| 3581 | return bc_lex_text(&p->l, text); |
| 3582 | } |
| 3583 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 3584 | // Called when bc/dc_parse_parse() detects a failure, |
| 3585 | // resets parsing structures. |
| 3586 | static void bc_parse_reset(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3587 | { |
| 3588 | if (p->fidx != BC_PROG_MAIN) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3589 | p->func->nparams = 0; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3590 | bc_vec_pop_all(&p->func->code); |
| 3591 | bc_vec_pop_all(&p->func->autos); |
| 3592 | bc_vec_pop_all(&p->func->labels); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3593 | |
| 3594 | bc_parse_updateFunc(p, BC_PROG_MAIN); |
| 3595 | } |
| 3596 | |
| 3597 | p->l.i = p->l.len; |
| 3598 | p->l.t.t = BC_LEX_EOF; |
| 3599 | p->auto_part = (p->nbraces = 0); |
| 3600 | |
| 3601 | bc_vec_npop(&p->flags, p->flags.len - 1); |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3602 | bc_vec_pop_all(&p->exits); |
| 3603 | bc_vec_pop_all(&p->conds); |
| 3604 | bc_vec_pop_all(&p->ops); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3605 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 3606 | bc_program_reset(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3607 | } |
| 3608 | |
| 3609 | static void bc_parse_free(BcParse *p) |
| 3610 | { |
| 3611 | bc_vec_free(&p->flags); |
| 3612 | bc_vec_free(&p->exits); |
| 3613 | bc_vec_free(&p->conds); |
| 3614 | bc_vec_free(&p->ops); |
| 3615 | bc_lex_free(&p->l); |
| 3616 | } |
| 3617 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3618 | static void bc_parse_create(BcParse *p, size_t func, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3619 | BcParseParse parse, BcLexNext next) |
| 3620 | { |
| 3621 | memset(p, 0, sizeof(BcParse)); |
| 3622 | |
| 3623 | bc_lex_init(&p->l, next); |
| 3624 | bc_vec_init(&p->flags, sizeof(uint8_t), NULL); |
| 3625 | bc_vec_init(&p->exits, sizeof(BcInstPtr), NULL); |
| 3626 | bc_vec_init(&p->conds, sizeof(size_t), NULL); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 3627 | bc_vec_pushZeroByte(&p->flags); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3628 | bc_vec_init(&p->ops, sizeof(BcLexType), NULL); |
| 3629 | |
| 3630 | p->parse = parse; |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 3631 | // p->auto_part = p->nbraces = 0; - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3632 | bc_parse_updateFunc(p, func); |
| 3633 | } |
| 3634 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 3635 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3636 | static BcStatus bc_parse_else(BcParse *p); |
| 3637 | static BcStatus bc_parse_stmt(BcParse *p); |
| 3638 | |
| 3639 | static BcStatus bc_parse_operator(BcParse *p, BcLexType type, size_t start, |
| 3640 | size_t *nexprs, bool next) |
| 3641 | { |
| 3642 | BcStatus s = BC_STATUS_SUCCESS; |
| 3643 | BcLexType t; |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 3644 | char l, r = bc_parse_op_PREC(type - BC_LEX_OP_INC); |
| 3645 | bool left = bc_parse_op_LEFT(type - BC_LEX_OP_INC); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3646 | |
| 3647 | while (p->ops.len > start) { |
| 3648 | |
| 3649 | t = BC_PARSE_TOP_OP(p); |
| 3650 | if (t == BC_LEX_LPAREN) break; |
| 3651 | |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 3652 | l = bc_parse_op_PREC(t - BC_LEX_OP_INC); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3653 | if (l >= r && (l != r || !left)) break; |
| 3654 | |
| 3655 | bc_parse_push(p, BC_PARSE_TOKEN_INST(t)); |
| 3656 | bc_vec_pop(&p->ops); |
| 3657 | *nexprs -= t != BC_LEX_OP_BOOL_NOT && t != BC_LEX_NEG; |
| 3658 | } |
| 3659 | |
| 3660 | bc_vec_push(&p->ops, &type); |
| 3661 | if (next) s = bc_lex_next(&p->l); |
| 3662 | |
| 3663 | return s; |
| 3664 | } |
| 3665 | |
| 3666 | static BcStatus bc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs) |
| 3667 | { |
| 3668 | BcLexType top; |
| 3669 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3670 | if (p->ops.len <= ops_bgn) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3671 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3672 | top = BC_PARSE_TOP_OP(p); |
| 3673 | |
| 3674 | while (top != BC_LEX_LPAREN) { |
| 3675 | |
| 3676 | bc_parse_push(p, BC_PARSE_TOKEN_INST(top)); |
| 3677 | |
| 3678 | bc_vec_pop(&p->ops); |
| 3679 | *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG; |
| 3680 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3681 | if (p->ops.len <= ops_bgn) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3682 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3683 | top = BC_PARSE_TOP_OP(p); |
| 3684 | } |
| 3685 | |
| 3686 | bc_vec_pop(&p->ops); |
| 3687 | |
| 3688 | return bc_lex_next(&p->l); |
| 3689 | } |
| 3690 | |
| 3691 | static BcStatus bc_parse_params(BcParse *p, uint8_t flags) |
| 3692 | { |
| 3693 | BcStatus s; |
| 3694 | bool comma = false; |
| 3695 | size_t nparams; |
| 3696 | |
| 3697 | s = bc_lex_next(&p->l); |
| 3698 | if (s) return s; |
| 3699 | |
| 3700 | for (nparams = 0; p->l.t.t != BC_LEX_RPAREN; ++nparams) { |
| 3701 | |
| 3702 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; |
| 3703 | s = bc_parse_expr(p, flags, bc_parse_next_param); |
| 3704 | if (s) return s; |
| 3705 | |
| 3706 | comma = p->l.t.t == BC_LEX_COMMA; |
| 3707 | if (comma) { |
| 3708 | s = bc_lex_next(&p->l); |
| 3709 | if (s) return s; |
| 3710 | } |
| 3711 | } |
| 3712 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3713 | if (comma) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3714 | bc_parse_push(p, BC_INST_CALL); |
| 3715 | bc_parse_pushIndex(p, nparams); |
| 3716 | |
| 3717 | return BC_STATUS_SUCCESS; |
| 3718 | } |
| 3719 | |
| 3720 | static BcStatus bc_parse_call(BcParse *p, char *name, uint8_t flags) |
| 3721 | { |
| 3722 | BcStatus s; |
| 3723 | BcId entry, *entry_ptr; |
| 3724 | size_t idx; |
| 3725 | |
| 3726 | entry.name = name; |
| 3727 | |
| 3728 | s = bc_parse_params(p, flags); |
| 3729 | if (s) goto err; |
| 3730 | |
| 3731 | if (p->l.t.t != BC_LEX_RPAREN) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3732 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3733 | goto err; |
| 3734 | } |
| 3735 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3736 | idx = bc_map_index(&G.prog.fn_map, &entry); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3737 | |
| 3738 | if (idx == BC_VEC_INVALID_IDX) { |
| 3739 | name = xstrdup(entry.name); |
| 3740 | bc_parse_addFunc(p, name, &idx); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3741 | idx = bc_map_index(&G.prog.fn_map, &entry); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3742 | free(entry.name); |
| 3743 | } |
| 3744 | else |
| 3745 | free(name); |
| 3746 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3747 | entry_ptr = bc_vec_item(&G.prog.fn_map, idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3748 | bc_parse_pushIndex(p, entry_ptr->idx); |
| 3749 | |
| 3750 | return bc_lex_next(&p->l); |
| 3751 | |
| 3752 | err: |
| 3753 | free(name); |
| 3754 | return s; |
| 3755 | } |
| 3756 | |
| 3757 | static BcStatus bc_parse_name(BcParse *p, BcInst *type, uint8_t flags) |
| 3758 | { |
| 3759 | BcStatus s; |
| 3760 | char *name; |
| 3761 | |
| 3762 | name = xstrdup(p->l.t.v.v); |
| 3763 | s = bc_lex_next(&p->l); |
| 3764 | if (s) goto err; |
| 3765 | |
| 3766 | if (p->l.t.t == BC_LEX_LBRACKET) { |
| 3767 | |
| 3768 | s = bc_lex_next(&p->l); |
| 3769 | if (s) goto err; |
| 3770 | |
| 3771 | if (p->l.t.t == BC_LEX_RBRACKET) { |
| 3772 | |
| 3773 | if (!(flags & BC_PARSE_ARRAY)) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3774 | s = bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3775 | goto err; |
| 3776 | } |
| 3777 | |
| 3778 | *type = BC_INST_ARRAY; |
| 3779 | } |
| 3780 | else { |
| 3781 | |
| 3782 | *type = BC_INST_ARRAY_ELEM; |
| 3783 | |
| 3784 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); |
| 3785 | s = bc_parse_expr(p, flags, bc_parse_next_elem); |
| 3786 | if (s) goto err; |
| 3787 | } |
| 3788 | |
| 3789 | s = bc_lex_next(&p->l); |
| 3790 | if (s) goto err; |
| 3791 | bc_parse_push(p, *type); |
| 3792 | bc_parse_pushName(p, name); |
| 3793 | } |
| 3794 | else if (p->l.t.t == BC_LEX_LPAREN) { |
| 3795 | |
| 3796 | if (flags & BC_PARSE_NOCALL) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3797 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3798 | goto err; |
| 3799 | } |
| 3800 | |
| 3801 | *type = BC_INST_CALL; |
| 3802 | s = bc_parse_call(p, name, flags); |
| 3803 | } |
| 3804 | else { |
| 3805 | *type = BC_INST_VAR; |
| 3806 | bc_parse_push(p, BC_INST_VAR); |
| 3807 | bc_parse_pushName(p, name); |
| 3808 | } |
| 3809 | |
| 3810 | return s; |
| 3811 | |
| 3812 | err: |
| 3813 | free(name); |
| 3814 | return s; |
| 3815 | } |
| 3816 | |
| 3817 | static BcStatus bc_parse_read(BcParse *p) |
| 3818 | { |
| 3819 | BcStatus s; |
| 3820 | |
| 3821 | s = bc_lex_next(&p->l); |
| 3822 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3823 | 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] | 3824 | |
| 3825 | s = bc_lex_next(&p->l); |
| 3826 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3827 | 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] | 3828 | |
| 3829 | bc_parse_push(p, BC_INST_READ); |
| 3830 | |
| 3831 | return bc_lex_next(&p->l); |
| 3832 | } |
| 3833 | |
| 3834 | static BcStatus bc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags, |
| 3835 | BcInst *prev) |
| 3836 | { |
| 3837 | BcStatus s; |
| 3838 | |
| 3839 | s = bc_lex_next(&p->l); |
| 3840 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3841 | 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] | 3842 | |
| 3843 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; |
| 3844 | |
| 3845 | s = bc_lex_next(&p->l); |
| 3846 | if (s) return s; |
| 3847 | |
| 3848 | s = bc_parse_expr(p, flags, bc_parse_next_rel); |
| 3849 | if (s) return s; |
| 3850 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3851 | 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] | 3852 | |
| 3853 | *prev = (type == BC_LEX_KEY_LENGTH) ? BC_INST_LENGTH : BC_INST_SQRT; |
| 3854 | bc_parse_push(p, *prev); |
| 3855 | |
| 3856 | return bc_lex_next(&p->l); |
| 3857 | } |
| 3858 | |
| 3859 | static BcStatus bc_parse_scale(BcParse *p, BcInst *type, uint8_t flags) |
| 3860 | { |
| 3861 | BcStatus s; |
| 3862 | |
| 3863 | s = bc_lex_next(&p->l); |
| 3864 | if (s) return s; |
| 3865 | |
| 3866 | if (p->l.t.t != BC_LEX_LPAREN) { |
| 3867 | *type = BC_INST_SCALE; |
| 3868 | bc_parse_push(p, BC_INST_SCALE); |
| 3869 | return BC_STATUS_SUCCESS; |
| 3870 | } |
| 3871 | |
| 3872 | *type = BC_INST_SCALE_FUNC; |
| 3873 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); |
| 3874 | |
| 3875 | s = bc_lex_next(&p->l); |
| 3876 | if (s) return s; |
| 3877 | |
| 3878 | s = bc_parse_expr(p, flags, bc_parse_next_rel); |
| 3879 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3880 | if (p->l.t.t != BC_LEX_RPAREN) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3881 | bc_parse_push(p, BC_INST_SCALE_FUNC); |
| 3882 | |
| 3883 | return bc_lex_next(&p->l); |
| 3884 | } |
| 3885 | |
| 3886 | static BcStatus bc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr, |
| 3887 | size_t *nexprs, uint8_t flags) |
| 3888 | { |
| 3889 | BcStatus s; |
| 3890 | BcLexType type; |
| 3891 | char inst; |
| 3892 | BcInst etype = *prev; |
| 3893 | |
| 3894 | if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM || |
| 3895 | etype == BC_INST_SCALE || etype == BC_INST_LAST || |
| 3896 | etype == BC_INST_IBASE || etype == BC_INST_OBASE) |
| 3897 | { |
| 3898 | *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC); |
| 3899 | bc_parse_push(p, inst); |
| 3900 | s = bc_lex_next(&p->l); |
| 3901 | } |
| 3902 | else { |
| 3903 | |
| 3904 | *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC); |
| 3905 | *paren_expr = true; |
| 3906 | |
| 3907 | s = bc_lex_next(&p->l); |
| 3908 | if (s) return s; |
| 3909 | type = p->l.t.t; |
| 3910 | |
| 3911 | // Because we parse the next part of the expression |
| 3912 | // right here, we need to increment this. |
| 3913 | *nexprs = *nexprs + 1; |
| 3914 | |
| 3915 | switch (type) { |
| 3916 | |
| 3917 | case BC_LEX_NAME: |
| 3918 | { |
| 3919 | s = bc_parse_name(p, prev, flags | BC_PARSE_NOCALL); |
| 3920 | break; |
| 3921 | } |
| 3922 | |
| 3923 | case BC_LEX_KEY_IBASE: |
| 3924 | case BC_LEX_KEY_LAST: |
| 3925 | case BC_LEX_KEY_OBASE: |
| 3926 | { |
| 3927 | bc_parse_push(p, type - BC_LEX_KEY_IBASE + BC_INST_IBASE); |
| 3928 | s = bc_lex_next(&p->l); |
| 3929 | break; |
| 3930 | } |
| 3931 | |
| 3932 | case BC_LEX_KEY_SCALE: |
| 3933 | { |
| 3934 | s = bc_lex_next(&p->l); |
| 3935 | if (s) return s; |
| 3936 | if (p->l.t.t == BC_LEX_LPAREN) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3937 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3938 | else |
| 3939 | bc_parse_push(p, BC_INST_SCALE); |
| 3940 | break; |
| 3941 | } |
| 3942 | |
| 3943 | default: |
| 3944 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3945 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3946 | break; |
| 3947 | } |
| 3948 | } |
| 3949 | |
| 3950 | if (!s) bc_parse_push(p, inst); |
| 3951 | } |
| 3952 | |
| 3953 | return s; |
| 3954 | } |
| 3955 | |
| 3956 | static BcStatus bc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn, |
| 3957 | bool rparen, size_t *nexprs) |
| 3958 | { |
| 3959 | BcStatus s; |
| 3960 | BcLexType type; |
| 3961 | BcInst etype = *prev; |
| 3962 | |
| 3963 | s = bc_lex_next(&p->l); |
| 3964 | if (s) return s; |
| 3965 | |
| 3966 | type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST || |
| 3967 | (etype >= BC_INST_NUM && etype <= BC_INST_SQRT) ? |
| 3968 | BC_LEX_OP_MINUS : |
| 3969 | BC_LEX_NEG; |
| 3970 | *prev = BC_PARSE_TOKEN_INST(type); |
| 3971 | |
| 3972 | // We can just push onto the op stack because this is the largest |
| 3973 | // precedence operator that gets pushed. Inc/dec does not. |
| 3974 | if (type != BC_LEX_OP_MINUS) |
| 3975 | bc_vec_push(&p->ops, &type); |
| 3976 | else |
| 3977 | s = bc_parse_operator(p, type, ops_bgn, nexprs, false); |
| 3978 | |
| 3979 | return s; |
| 3980 | } |
| 3981 | |
| 3982 | static BcStatus bc_parse_string(BcParse *p, char inst) |
| 3983 | { |
| 3984 | char *str = xstrdup(p->l.t.v.v); |
| 3985 | |
| 3986 | bc_parse_push(p, BC_INST_STR); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3987 | bc_parse_pushIndex(p, G.prog.strs.len); |
| 3988 | bc_vec_push(&G.prog.strs, &str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3989 | bc_parse_push(p, inst); |
| 3990 | |
| 3991 | return bc_lex_next(&p->l); |
| 3992 | } |
| 3993 | |
| 3994 | static BcStatus bc_parse_print(BcParse *p) |
| 3995 | { |
| 3996 | BcStatus s; |
| 3997 | BcLexType type; |
| 3998 | bool comma = false; |
| 3999 | |
| 4000 | s = bc_lex_next(&p->l); |
| 4001 | if (s) return s; |
| 4002 | |
| 4003 | type = p->l.t.t; |
| 4004 | |
| 4005 | if (type == BC_LEX_SCOLON || type == BC_LEX_NLINE) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4006 | return bc_error("bad print statement"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4007 | |
| 4008 | while (!s && type != BC_LEX_SCOLON && type != BC_LEX_NLINE) { |
| 4009 | |
| 4010 | if (type == BC_LEX_STR) |
| 4011 | s = bc_parse_string(p, BC_INST_PRINT_POP); |
| 4012 | else { |
| 4013 | s = bc_parse_expr(p, 0, bc_parse_next_print); |
| 4014 | if (s) return s; |
| 4015 | bc_parse_push(p, BC_INST_PRINT_POP); |
| 4016 | } |
| 4017 | |
| 4018 | if (s) return s; |
| 4019 | |
| 4020 | comma = p->l.t.t == BC_LEX_COMMA; |
| 4021 | if (comma) s = bc_lex_next(&p->l); |
| 4022 | type = p->l.t.t; |
| 4023 | } |
| 4024 | |
| 4025 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4026 | if (comma) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4027 | |
| 4028 | return bc_lex_next(&p->l); |
| 4029 | } |
| 4030 | |
| 4031 | static BcStatus bc_parse_return(BcParse *p) |
| 4032 | { |
| 4033 | BcStatus s; |
| 4034 | BcLexType t; |
| 4035 | bool paren; |
| 4036 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4037 | if (!BC_PARSE_FUNC(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4038 | |
| 4039 | s = bc_lex_next(&p->l); |
| 4040 | if (s) return s; |
| 4041 | |
| 4042 | t = p->l.t.t; |
| 4043 | paren = t == BC_LEX_LPAREN; |
| 4044 | |
| 4045 | if (t == BC_LEX_NLINE || t == BC_LEX_SCOLON) |
| 4046 | bc_parse_push(p, BC_INST_RET0); |
| 4047 | else { |
| 4048 | |
| 4049 | s = bc_parse_expr(p, 0, bc_parse_next_expr); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4050 | if (s == BC_STATUS_PARSE_EMPTY_EXP) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4051 | bc_parse_push(p, BC_INST_RET0); |
| 4052 | s = bc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4053 | } |
Denys Vlasenko | 452df92 | 2018-12-05 20:28:26 +0100 | [diff] [blame^] | 4054 | if (s) return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4055 | |
| 4056 | if (!paren || p->l.t.last != BC_LEX_RPAREN) { |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 4057 | s = bc_posix_error("POSIX requires parentheses around return expressions"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4058 | if (s) return s; |
| 4059 | } |
| 4060 | |
| 4061 | bc_parse_push(p, BC_INST_RET); |
| 4062 | } |
| 4063 | |
| 4064 | return s; |
| 4065 | } |
| 4066 | |
| 4067 | static BcStatus bc_parse_endBody(BcParse *p, bool brace) |
| 4068 | { |
| 4069 | BcStatus s = BC_STATUS_SUCCESS; |
| 4070 | |
| 4071 | if (p->flags.len <= 1 || (brace && p->nbraces == 0)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4072 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4073 | |
| 4074 | if (brace) { |
| 4075 | |
| 4076 | if (p->l.t.t == BC_LEX_RBRACE) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4077 | if (!p->nbraces) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4078 | --p->nbraces; |
| 4079 | s = bc_lex_next(&p->l); |
| 4080 | if (s) return s; |
| 4081 | } |
| 4082 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4083 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4084 | } |
| 4085 | |
| 4086 | if (BC_PARSE_IF(p)) { |
| 4087 | |
| 4088 | uint8_t *flag_ptr; |
| 4089 | |
| 4090 | while (p->l.t.t == BC_LEX_NLINE) { |
| 4091 | s = bc_lex_next(&p->l); |
| 4092 | if (s) return s; |
| 4093 | } |
| 4094 | |
| 4095 | bc_vec_pop(&p->flags); |
| 4096 | |
| 4097 | flag_ptr = BC_PARSE_TOP_FLAG_PTR(p); |
| 4098 | *flag_ptr = (*flag_ptr | BC_PARSE_FLAG_IF_END); |
| 4099 | |
| 4100 | if (p->l.t.t == BC_LEX_KEY_ELSE) s = bc_parse_else(p); |
| 4101 | } |
| 4102 | else if (BC_PARSE_ELSE(p)) { |
| 4103 | |
| 4104 | BcInstPtr *ip; |
| 4105 | size_t *label; |
| 4106 | |
| 4107 | bc_vec_pop(&p->flags); |
| 4108 | |
| 4109 | ip = bc_vec_top(&p->exits); |
| 4110 | label = bc_vec_item(&p->func->labels, ip->idx); |
| 4111 | *label = p->func->code.len; |
| 4112 | |
| 4113 | bc_vec_pop(&p->exits); |
| 4114 | } |
| 4115 | else if (BC_PARSE_FUNC_INNER(p)) { |
| 4116 | bc_parse_push(p, BC_INST_RET0); |
| 4117 | bc_parse_updateFunc(p, BC_PROG_MAIN); |
| 4118 | bc_vec_pop(&p->flags); |
| 4119 | } |
| 4120 | else { |
| 4121 | |
| 4122 | BcInstPtr *ip = bc_vec_top(&p->exits); |
| 4123 | size_t *label = bc_vec_top(&p->conds); |
| 4124 | |
| 4125 | bc_parse_push(p, BC_INST_JUMP); |
| 4126 | bc_parse_pushIndex(p, *label); |
| 4127 | |
| 4128 | label = bc_vec_item(&p->func->labels, ip->idx); |
| 4129 | *label = p->func->code.len; |
| 4130 | |
| 4131 | bc_vec_pop(&p->flags); |
| 4132 | bc_vec_pop(&p->exits); |
| 4133 | bc_vec_pop(&p->conds); |
| 4134 | } |
| 4135 | |
| 4136 | return s; |
| 4137 | } |
| 4138 | |
| 4139 | static void bc_parse_startBody(BcParse *p, uint8_t flags) |
| 4140 | { |
| 4141 | uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p); |
| 4142 | flags |= (*flag_ptr & (BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_LOOP)); |
| 4143 | flags |= BC_PARSE_FLAG_BODY; |
| 4144 | bc_vec_push(&p->flags, &flags); |
| 4145 | } |
| 4146 | |
| 4147 | static void bc_parse_noElse(BcParse *p) |
| 4148 | { |
| 4149 | BcInstPtr *ip; |
| 4150 | size_t *label; |
| 4151 | uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p); |
| 4152 | |
| 4153 | *flag_ptr = (*flag_ptr & ~(BC_PARSE_FLAG_IF_END)); |
| 4154 | |
| 4155 | ip = bc_vec_top(&p->exits); |
| 4156 | label = bc_vec_item(&p->func->labels, ip->idx); |
| 4157 | *label = p->func->code.len; |
| 4158 | |
| 4159 | bc_vec_pop(&p->exits); |
| 4160 | } |
| 4161 | |
| 4162 | static BcStatus bc_parse_if(BcParse *p) |
| 4163 | { |
| 4164 | BcStatus s; |
| 4165 | BcInstPtr ip; |
| 4166 | |
| 4167 | s = bc_lex_next(&p->l); |
| 4168 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4169 | 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] | 4170 | |
| 4171 | s = bc_lex_next(&p->l); |
| 4172 | if (s) return s; |
| 4173 | s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel); |
| 4174 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4175 | 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] | 4176 | |
| 4177 | s = bc_lex_next(&p->l); |
| 4178 | if (s) return s; |
| 4179 | bc_parse_push(p, BC_INST_JUMP_ZERO); |
| 4180 | |
| 4181 | ip.idx = p->func->labels.len; |
| 4182 | ip.func = ip.len = 0; |
| 4183 | |
| 4184 | bc_parse_pushIndex(p, ip.idx); |
| 4185 | bc_vec_push(&p->exits, &ip); |
| 4186 | bc_vec_push(&p->func->labels, &ip.idx); |
| 4187 | bc_parse_startBody(p, BC_PARSE_FLAG_IF); |
| 4188 | |
| 4189 | return BC_STATUS_SUCCESS; |
| 4190 | } |
| 4191 | |
| 4192 | static BcStatus bc_parse_else(BcParse *p) |
| 4193 | { |
| 4194 | BcInstPtr ip; |
| 4195 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4196 | if (!BC_PARSE_IF_END(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4197 | |
| 4198 | ip.idx = p->func->labels.len; |
| 4199 | ip.func = ip.len = 0; |
| 4200 | |
| 4201 | bc_parse_push(p, BC_INST_JUMP); |
| 4202 | bc_parse_pushIndex(p, ip.idx); |
| 4203 | |
| 4204 | bc_parse_noElse(p); |
| 4205 | |
| 4206 | bc_vec_push(&p->exits, &ip); |
| 4207 | bc_vec_push(&p->func->labels, &ip.idx); |
| 4208 | bc_parse_startBody(p, BC_PARSE_FLAG_ELSE); |
| 4209 | |
| 4210 | return bc_lex_next(&p->l); |
| 4211 | } |
| 4212 | |
| 4213 | static BcStatus bc_parse_while(BcParse *p) |
| 4214 | { |
| 4215 | BcStatus s; |
| 4216 | BcInstPtr ip; |
| 4217 | |
| 4218 | s = bc_lex_next(&p->l); |
| 4219 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4220 | 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] | 4221 | s = bc_lex_next(&p->l); |
| 4222 | if (s) return s; |
| 4223 | |
| 4224 | ip.idx = p->func->labels.len; |
| 4225 | |
| 4226 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4227 | bc_vec_push(&p->conds, &ip.idx); |
| 4228 | |
| 4229 | ip.idx = p->func->labels.len; |
| 4230 | ip.func = 1; |
| 4231 | ip.len = 0; |
| 4232 | |
| 4233 | bc_vec_push(&p->exits, &ip); |
| 4234 | bc_vec_push(&p->func->labels, &ip.idx); |
| 4235 | |
| 4236 | s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel); |
| 4237 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4238 | 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] | 4239 | s = bc_lex_next(&p->l); |
| 4240 | if (s) return s; |
| 4241 | |
| 4242 | bc_parse_push(p, BC_INST_JUMP_ZERO); |
| 4243 | bc_parse_pushIndex(p, ip.idx); |
| 4244 | bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER); |
| 4245 | |
| 4246 | return BC_STATUS_SUCCESS; |
| 4247 | } |
| 4248 | |
| 4249 | static BcStatus bc_parse_for(BcParse *p) |
| 4250 | { |
| 4251 | BcStatus s; |
| 4252 | BcInstPtr ip; |
| 4253 | size_t cond_idx, exit_idx, body_idx, update_idx; |
| 4254 | |
| 4255 | s = bc_lex_next(&p->l); |
| 4256 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4257 | 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] | 4258 | s = bc_lex_next(&p->l); |
| 4259 | if (s) return s; |
| 4260 | |
| 4261 | if (p->l.t.t != BC_LEX_SCOLON) |
| 4262 | s = bc_parse_expr(p, 0, bc_parse_next_for); |
| 4263 | else |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 4264 | s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4265 | |
| 4266 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4267 | 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] | 4268 | s = bc_lex_next(&p->l); |
| 4269 | if (s) return s; |
| 4270 | |
| 4271 | cond_idx = p->func->labels.len; |
| 4272 | update_idx = cond_idx + 1; |
| 4273 | body_idx = update_idx + 1; |
| 4274 | exit_idx = body_idx + 1; |
| 4275 | |
| 4276 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4277 | |
| 4278 | if (p->l.t.t != BC_LEX_SCOLON) |
| 4279 | s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_for); |
| 4280 | else |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 4281 | s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4282 | |
| 4283 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4284 | 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] | 4285 | |
| 4286 | s = bc_lex_next(&p->l); |
| 4287 | if (s) return s; |
| 4288 | |
| 4289 | bc_parse_push(p, BC_INST_JUMP_ZERO); |
| 4290 | bc_parse_pushIndex(p, exit_idx); |
| 4291 | bc_parse_push(p, BC_INST_JUMP); |
| 4292 | bc_parse_pushIndex(p, body_idx); |
| 4293 | |
| 4294 | ip.idx = p->func->labels.len; |
| 4295 | |
| 4296 | bc_vec_push(&p->conds, &update_idx); |
| 4297 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4298 | |
| 4299 | if (p->l.t.t != BC_LEX_RPAREN) |
| 4300 | s = bc_parse_expr(p, 0, bc_parse_next_rel); |
| 4301 | else |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 4302 | s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4303 | |
| 4304 | if (s) return s; |
| 4305 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4306 | 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] | 4307 | bc_parse_push(p, BC_INST_JUMP); |
| 4308 | bc_parse_pushIndex(p, cond_idx); |
| 4309 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4310 | |
| 4311 | ip.idx = exit_idx; |
| 4312 | ip.func = 1; |
| 4313 | ip.len = 0; |
| 4314 | |
| 4315 | bc_vec_push(&p->exits, &ip); |
| 4316 | bc_vec_push(&p->func->labels, &ip.idx); |
| 4317 | bc_lex_next(&p->l); |
| 4318 | bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER); |
| 4319 | |
| 4320 | return BC_STATUS_SUCCESS; |
| 4321 | } |
| 4322 | |
| 4323 | static BcStatus bc_parse_loopExit(BcParse *p, BcLexType type) |
| 4324 | { |
| 4325 | BcStatus s; |
| 4326 | size_t i; |
| 4327 | BcInstPtr *ip; |
| 4328 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4329 | if (!BC_PARSE_LOOP(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4330 | |
| 4331 | if (type == BC_LEX_KEY_BREAK) { |
| 4332 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4333 | if (p->exits.len == 0) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4334 | |
| 4335 | i = p->exits.len - 1; |
| 4336 | ip = bc_vec_item(&p->exits, i); |
| 4337 | |
| 4338 | 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] | 4339 | if (i >= p->exits.len && !ip->func) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4340 | |
| 4341 | i = ip->idx; |
| 4342 | } |
| 4343 | else |
| 4344 | i = *((size_t *) bc_vec_top(&p->conds)); |
| 4345 | |
| 4346 | bc_parse_push(p, BC_INST_JUMP); |
| 4347 | bc_parse_pushIndex(p, i); |
| 4348 | |
| 4349 | s = bc_lex_next(&p->l); |
| 4350 | if (s) return s; |
| 4351 | |
| 4352 | 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] | 4353 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4354 | |
| 4355 | return bc_lex_next(&p->l); |
| 4356 | } |
| 4357 | |
| 4358 | static BcStatus bc_parse_func(BcParse *p) |
| 4359 | { |
| 4360 | BcStatus s; |
| 4361 | bool var, comma = false; |
| 4362 | uint8_t flags; |
| 4363 | char *name; |
| 4364 | |
| 4365 | s = bc_lex_next(&p->l); |
| 4366 | if (s) return s; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4367 | if (p->l.t.t != BC_LEX_NAME) |
| 4368 | return bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4369 | |
| 4370 | name = xstrdup(p->l.t.v.v); |
| 4371 | bc_parse_addFunc(p, name, &p->fidx); |
| 4372 | |
| 4373 | s = bc_lex_next(&p->l); |
| 4374 | if (s) return s; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4375 | if (p->l.t.t != BC_LEX_LPAREN) |
| 4376 | return bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4377 | s = bc_lex_next(&p->l); |
| 4378 | if (s) return s; |
| 4379 | |
| 4380 | while (p->l.t.t != BC_LEX_RPAREN) { |
| 4381 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4382 | if (p->l.t.t != BC_LEX_NAME) |
| 4383 | return bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4384 | |
| 4385 | ++p->func->nparams; |
| 4386 | |
| 4387 | name = xstrdup(p->l.t.v.v); |
| 4388 | s = bc_lex_next(&p->l); |
| 4389 | if (s) goto err; |
| 4390 | |
| 4391 | var = p->l.t.t != BC_LEX_LBRACKET; |
| 4392 | |
| 4393 | if (!var) { |
| 4394 | |
| 4395 | s = bc_lex_next(&p->l); |
| 4396 | if (s) goto err; |
| 4397 | |
| 4398 | if (p->l.t.t != BC_LEX_RBRACKET) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4399 | s = bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4400 | goto err; |
| 4401 | } |
| 4402 | |
| 4403 | s = bc_lex_next(&p->l); |
| 4404 | if (s) goto err; |
| 4405 | } |
| 4406 | |
| 4407 | comma = p->l.t.t == BC_LEX_COMMA; |
| 4408 | if (comma) { |
| 4409 | s = bc_lex_next(&p->l); |
| 4410 | if (s) goto err; |
| 4411 | } |
| 4412 | |
| 4413 | s = bc_func_insert(p->func, name, var); |
| 4414 | if (s) goto err; |
| 4415 | } |
| 4416 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4417 | if (comma) return bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4418 | |
| 4419 | flags = BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_BODY; |
| 4420 | bc_parse_startBody(p, flags); |
| 4421 | |
| 4422 | s = bc_lex_next(&p->l); |
| 4423 | if (s) return s; |
| 4424 | |
| 4425 | if (p->l.t.t != BC_LEX_LBRACE) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 4426 | s = bc_posix_error("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] | 4427 | |
| 4428 | return s; |
| 4429 | |
| 4430 | err: |
| 4431 | free(name); |
| 4432 | return s; |
| 4433 | } |
| 4434 | |
| 4435 | static BcStatus bc_parse_auto(BcParse *p) |
| 4436 | { |
| 4437 | BcStatus s; |
| 4438 | bool comma, var, one; |
| 4439 | char *name; |
| 4440 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4441 | if (!p->auto_part) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4442 | s = bc_lex_next(&p->l); |
| 4443 | if (s) return s; |
| 4444 | |
| 4445 | p->auto_part = comma = false; |
| 4446 | one = p->l.t.t == BC_LEX_NAME; |
| 4447 | |
| 4448 | while (p->l.t.t == BC_LEX_NAME) { |
| 4449 | |
| 4450 | name = xstrdup(p->l.t.v.v); |
| 4451 | s = bc_lex_next(&p->l); |
| 4452 | if (s) goto err; |
| 4453 | |
| 4454 | var = p->l.t.t != BC_LEX_LBRACKET; |
| 4455 | if (!var) { |
| 4456 | |
| 4457 | s = bc_lex_next(&p->l); |
| 4458 | if (s) goto err; |
| 4459 | |
| 4460 | if (p->l.t.t != BC_LEX_RBRACKET) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4461 | s = bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4462 | goto err; |
| 4463 | } |
| 4464 | |
| 4465 | s = bc_lex_next(&p->l); |
| 4466 | if (s) goto err; |
| 4467 | } |
| 4468 | |
| 4469 | comma = p->l.t.t == BC_LEX_COMMA; |
| 4470 | if (comma) { |
| 4471 | s = bc_lex_next(&p->l); |
| 4472 | if (s) goto err; |
| 4473 | } |
| 4474 | |
| 4475 | s = bc_func_insert(p->func, name, var); |
| 4476 | if (s) goto err; |
| 4477 | } |
| 4478 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4479 | if (comma) return bc_error("bad function definition"); |
Denys Vlasenko | abbc433 | 2018-12-03 21:46:41 +0100 | [diff] [blame] | 4480 | if (!one) return bc_error("no auto variable found"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4481 | |
| 4482 | 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] | 4483 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4484 | |
| 4485 | return bc_lex_next(&p->l); |
| 4486 | |
| 4487 | err: |
| 4488 | free(name); |
| 4489 | return s; |
| 4490 | } |
| 4491 | |
| 4492 | static BcStatus bc_parse_body(BcParse *p, bool brace) |
| 4493 | { |
| 4494 | BcStatus s = BC_STATUS_SUCCESS; |
| 4495 | uint8_t *flag_ptr = bc_vec_top(&p->flags); |
| 4496 | |
| 4497 | *flag_ptr &= ~(BC_PARSE_FLAG_BODY); |
| 4498 | |
| 4499 | if (*flag_ptr & BC_PARSE_FLAG_FUNC_INNER) { |
| 4500 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4501 | if (!brace) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4502 | p->auto_part = p->l.t.t != BC_LEX_KEY_AUTO; |
| 4503 | |
| 4504 | if (!p->auto_part) { |
| 4505 | s = bc_parse_auto(p); |
| 4506 | if (s) return s; |
| 4507 | } |
| 4508 | |
| 4509 | if (p->l.t.t == BC_LEX_NLINE) s = bc_lex_next(&p->l); |
| 4510 | } |
| 4511 | else { |
| 4512 | s = bc_parse_stmt(p); |
| 4513 | if (!s && !brace) s = bc_parse_endBody(p, false); |
| 4514 | } |
| 4515 | |
| 4516 | return s; |
| 4517 | } |
| 4518 | |
| 4519 | static BcStatus bc_parse_stmt(BcParse *p) |
| 4520 | { |
| 4521 | BcStatus s = BC_STATUS_SUCCESS; |
| 4522 | |
| 4523 | switch (p->l.t.t) { |
| 4524 | |
| 4525 | case BC_LEX_NLINE: |
| 4526 | { |
| 4527 | return bc_lex_next(&p->l); |
| 4528 | } |
| 4529 | |
| 4530 | case BC_LEX_KEY_ELSE: |
| 4531 | { |
| 4532 | p->auto_part = false; |
| 4533 | break; |
| 4534 | } |
| 4535 | |
| 4536 | case BC_LEX_LBRACE: |
| 4537 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4538 | if (!BC_PARSE_BODY(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4539 | |
| 4540 | ++p->nbraces; |
| 4541 | s = bc_lex_next(&p->l); |
| 4542 | if (s) return s; |
| 4543 | |
| 4544 | return bc_parse_body(p, true); |
| 4545 | } |
| 4546 | |
| 4547 | case BC_LEX_KEY_AUTO: |
| 4548 | { |
| 4549 | return bc_parse_auto(p); |
| 4550 | } |
| 4551 | |
| 4552 | default: |
| 4553 | { |
| 4554 | p->auto_part = false; |
| 4555 | |
| 4556 | if (BC_PARSE_IF_END(p)) { |
| 4557 | bc_parse_noElse(p); |
| 4558 | return BC_STATUS_SUCCESS; |
| 4559 | } |
| 4560 | else if (BC_PARSE_BODY(p)) |
| 4561 | return bc_parse_body(p, false); |
| 4562 | |
| 4563 | break; |
| 4564 | } |
| 4565 | } |
| 4566 | |
| 4567 | switch (p->l.t.t) { |
| 4568 | |
| 4569 | case BC_LEX_OP_INC: |
| 4570 | case BC_LEX_OP_DEC: |
| 4571 | case BC_LEX_OP_MINUS: |
| 4572 | case BC_LEX_OP_BOOL_NOT: |
| 4573 | case BC_LEX_LPAREN: |
| 4574 | case BC_LEX_NAME: |
| 4575 | case BC_LEX_NUMBER: |
| 4576 | case BC_LEX_KEY_IBASE: |
| 4577 | case BC_LEX_KEY_LAST: |
| 4578 | case BC_LEX_KEY_LENGTH: |
| 4579 | case BC_LEX_KEY_OBASE: |
| 4580 | case BC_LEX_KEY_READ: |
| 4581 | case BC_LEX_KEY_SCALE: |
| 4582 | case BC_LEX_KEY_SQRT: |
| 4583 | { |
| 4584 | s = bc_parse_expr(p, BC_PARSE_PRINT, bc_parse_next_expr); |
| 4585 | break; |
| 4586 | } |
| 4587 | |
| 4588 | case BC_LEX_KEY_ELSE: |
| 4589 | { |
| 4590 | s = bc_parse_else(p); |
| 4591 | break; |
| 4592 | } |
| 4593 | |
| 4594 | case BC_LEX_SCOLON: |
| 4595 | { |
| 4596 | while (!s && p->l.t.t == BC_LEX_SCOLON) s = bc_lex_next(&p->l); |
| 4597 | break; |
| 4598 | } |
| 4599 | |
| 4600 | case BC_LEX_RBRACE: |
| 4601 | { |
| 4602 | s = bc_parse_endBody(p, true); |
| 4603 | break; |
| 4604 | } |
| 4605 | |
| 4606 | case BC_LEX_STR: |
| 4607 | { |
| 4608 | s = bc_parse_string(p, BC_INST_PRINT_STR); |
| 4609 | break; |
| 4610 | } |
| 4611 | |
| 4612 | case BC_LEX_KEY_BREAK: |
| 4613 | case BC_LEX_KEY_CONTINUE: |
| 4614 | { |
| 4615 | s = bc_parse_loopExit(p, p->l.t.t); |
| 4616 | break; |
| 4617 | } |
| 4618 | |
| 4619 | case BC_LEX_KEY_FOR: |
| 4620 | { |
| 4621 | s = bc_parse_for(p); |
| 4622 | break; |
| 4623 | } |
| 4624 | |
| 4625 | case BC_LEX_KEY_HALT: |
| 4626 | { |
| 4627 | bc_parse_push(p, BC_INST_HALT); |
| 4628 | s = bc_lex_next(&p->l); |
| 4629 | break; |
| 4630 | } |
| 4631 | |
| 4632 | case BC_LEX_KEY_IF: |
| 4633 | { |
| 4634 | s = bc_parse_if(p); |
| 4635 | break; |
| 4636 | } |
| 4637 | |
| 4638 | case BC_LEX_KEY_LIMITS: |
| 4639 | { |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 4640 | // "limits" is a compile-time command, |
| 4641 | // the output is produced at _parse time_. |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4642 | s = bc_lex_next(&p->l); |
| 4643 | if (s) return s; |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 4644 | printf("BC_BASE_MAX = %u\n", BC_MAX_OBASE); |
| 4645 | printf("BC_DIM_MAX = %u\n", BC_MAX_DIM); |
| 4646 | printf("BC_SCALE_MAX = %u\n", BC_MAX_SCALE); |
| 4647 | printf("BC_STRING_MAX = %u\n", BC_MAX_STRING); |
| 4648 | printf("BC_NAME_MAX = %u\n", BC_MAX_NAME); |
| 4649 | printf("BC_NUM_MAX = %u\n", BC_MAX_NUM); |
| 4650 | printf("MAX Exponent = %lu\n", BC_MAX_EXP); |
| 4651 | printf("Number of vars = %lu\n", BC_MAX_VARS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4652 | break; |
| 4653 | } |
| 4654 | |
| 4655 | case BC_LEX_KEY_PRINT: |
| 4656 | { |
| 4657 | s = bc_parse_print(p); |
| 4658 | break; |
| 4659 | } |
| 4660 | |
| 4661 | case BC_LEX_KEY_QUIT: |
| 4662 | { |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 4663 | // "quit" is a compile-time command. For example, |
| 4664 | // "if (0 == 1) quit" terminates when parsing the statement, |
| 4665 | // not when it is executed |
| 4666 | quit(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4667 | } |
| 4668 | |
| 4669 | case BC_LEX_KEY_RETURN: |
| 4670 | { |
| 4671 | s = bc_parse_return(p); |
| 4672 | break; |
| 4673 | } |
| 4674 | |
| 4675 | case BC_LEX_KEY_WHILE: |
| 4676 | { |
| 4677 | s = bc_parse_while(p); |
| 4678 | break; |
| 4679 | } |
| 4680 | |
| 4681 | default: |
| 4682 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4683 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4684 | break; |
| 4685 | } |
| 4686 | } |
| 4687 | |
| 4688 | return s; |
| 4689 | } |
| 4690 | |
| 4691 | static BcStatus bc_parse_parse(BcParse *p) |
| 4692 | { |
| 4693 | BcStatus s; |
| 4694 | |
| 4695 | if (p->l.t.t == BC_LEX_EOF) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4696 | 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] | 4697 | else if (p->l.t.t == BC_LEX_KEY_DEFINE) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4698 | if (!BC_PARSE_CAN_EXEC(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4699 | s = bc_parse_func(p); |
| 4700 | } |
| 4701 | else |
| 4702 | s = bc_parse_stmt(p); |
| 4703 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 4704 | if (s || G_interrupt) { |
| 4705 | bc_parse_reset(p); |
| 4706 | s = BC_STATUS_FAILURE; |
| 4707 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4708 | |
| 4709 | return s; |
| 4710 | } |
| 4711 | |
| 4712 | static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next) |
| 4713 | { |
| 4714 | BcStatus s = BC_STATUS_SUCCESS; |
| 4715 | BcInst prev = BC_INST_PRINT; |
| 4716 | BcLexType top, t = p->l.t.t; |
| 4717 | size_t nexprs = 0, ops_bgn = p->ops.len; |
| 4718 | uint32_t i, nparens, nrelops; |
| 4719 | bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last; |
| 4720 | |
| 4721 | paren_first = p->l.t.t == BC_LEX_LPAREN; |
| 4722 | nparens = nrelops = 0; |
| 4723 | paren_expr = rprn = done = get_token = assign = false; |
| 4724 | bin_last = true; |
| 4725 | |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 4726 | 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] | 4727 | switch (t) { |
| 4728 | |
| 4729 | case BC_LEX_OP_INC: |
| 4730 | case BC_LEX_OP_DEC: |
| 4731 | { |
| 4732 | s = bc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags); |
| 4733 | rprn = get_token = bin_last = false; |
| 4734 | break; |
| 4735 | } |
| 4736 | |
| 4737 | case BC_LEX_OP_MINUS: |
| 4738 | { |
| 4739 | s = bc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs); |
| 4740 | rprn = get_token = false; |
| 4741 | bin_last = prev == BC_INST_MINUS; |
| 4742 | break; |
| 4743 | } |
| 4744 | |
| 4745 | case BC_LEX_OP_ASSIGN_POWER: |
| 4746 | case BC_LEX_OP_ASSIGN_MULTIPLY: |
| 4747 | case BC_LEX_OP_ASSIGN_DIVIDE: |
| 4748 | case BC_LEX_OP_ASSIGN_MODULUS: |
| 4749 | case BC_LEX_OP_ASSIGN_PLUS: |
| 4750 | case BC_LEX_OP_ASSIGN_MINUS: |
| 4751 | case BC_LEX_OP_ASSIGN: |
| 4752 | { |
| 4753 | if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM && |
| 4754 | prev != BC_INST_SCALE && prev != BC_INST_IBASE && |
| 4755 | prev != BC_INST_OBASE && prev != BC_INST_LAST) |
| 4756 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4757 | s = bc_error("bad assignment:" |
| 4758 | " left side must be scale," |
| 4759 | " ibase, obase, last, var," |
| 4760 | " or array element" |
| 4761 | ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4762 | break; |
| 4763 | } |
| 4764 | } |
| 4765 | // Fallthrough. |
| 4766 | case BC_LEX_OP_POWER: |
| 4767 | case BC_LEX_OP_MULTIPLY: |
| 4768 | case BC_LEX_OP_DIVIDE: |
| 4769 | case BC_LEX_OP_MODULUS: |
| 4770 | case BC_LEX_OP_PLUS: |
| 4771 | case BC_LEX_OP_REL_EQ: |
| 4772 | case BC_LEX_OP_REL_LE: |
| 4773 | case BC_LEX_OP_REL_GE: |
| 4774 | case BC_LEX_OP_REL_NE: |
| 4775 | case BC_LEX_OP_REL_LT: |
| 4776 | case BC_LEX_OP_REL_GT: |
| 4777 | case BC_LEX_OP_BOOL_NOT: |
| 4778 | case BC_LEX_OP_BOOL_OR: |
| 4779 | case BC_LEX_OP_BOOL_AND: |
| 4780 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4781 | if (((t == BC_LEX_OP_BOOL_NOT) != bin_last) |
| 4782 | || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT) |
| 4783 | ) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4784 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4785 | } |
| 4786 | |
| 4787 | nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT; |
| 4788 | prev = BC_PARSE_TOKEN_INST(t); |
| 4789 | s = bc_parse_operator(p, t, ops_bgn, &nexprs, true); |
| 4790 | rprn = get_token = false; |
| 4791 | bin_last = t != BC_LEX_OP_BOOL_NOT; |
| 4792 | |
| 4793 | break; |
| 4794 | } |
| 4795 | |
| 4796 | case BC_LEX_LPAREN: |
| 4797 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4798 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4799 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4800 | ++nparens; |
| 4801 | paren_expr = rprn = bin_last = false; |
| 4802 | get_token = true; |
| 4803 | bc_vec_push(&p->ops, &t); |
| 4804 | |
| 4805 | break; |
| 4806 | } |
| 4807 | |
| 4808 | case BC_LEX_RPAREN: |
| 4809 | { |
| 4810 | if (bin_last || prev == BC_INST_BOOL_NOT) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4811 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4812 | |
| 4813 | if (nparens == 0) { |
| 4814 | s = BC_STATUS_SUCCESS; |
| 4815 | done = true; |
| 4816 | get_token = false; |
| 4817 | break; |
| 4818 | } |
| 4819 | else if (!paren_expr) |
| 4820 | return BC_STATUS_PARSE_EMPTY_EXP; |
| 4821 | |
| 4822 | --nparens; |
| 4823 | paren_expr = rprn = true; |
| 4824 | get_token = bin_last = false; |
| 4825 | |
| 4826 | s = bc_parse_rightParen(p, ops_bgn, &nexprs); |
| 4827 | |
| 4828 | break; |
| 4829 | } |
| 4830 | |
| 4831 | case BC_LEX_NAME: |
| 4832 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4833 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4834 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4835 | paren_expr = true; |
| 4836 | rprn = get_token = bin_last = false; |
| 4837 | s = bc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL); |
| 4838 | ++nexprs; |
| 4839 | |
| 4840 | break; |
| 4841 | } |
| 4842 | |
| 4843 | case BC_LEX_NUMBER: |
| 4844 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4845 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4846 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4847 | bc_parse_number(p, &prev, &nexprs); |
| 4848 | paren_expr = get_token = true; |
| 4849 | rprn = bin_last = false; |
| 4850 | |
| 4851 | break; |
| 4852 | } |
| 4853 | |
| 4854 | case BC_LEX_KEY_IBASE: |
| 4855 | case BC_LEX_KEY_LAST: |
| 4856 | case BC_LEX_KEY_OBASE: |
| 4857 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4858 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4859 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4860 | prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE); |
| 4861 | bc_parse_push(p, (char) prev); |
| 4862 | |
| 4863 | paren_expr = get_token = true; |
| 4864 | rprn = bin_last = false; |
| 4865 | ++nexprs; |
| 4866 | |
| 4867 | break; |
| 4868 | } |
| 4869 | |
| 4870 | case BC_LEX_KEY_LENGTH: |
| 4871 | case BC_LEX_KEY_SQRT: |
| 4872 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4873 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4874 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4875 | s = bc_parse_builtin(p, t, flags, &prev); |
| 4876 | paren_expr = true; |
| 4877 | rprn = get_token = bin_last = false; |
| 4878 | ++nexprs; |
| 4879 | |
| 4880 | break; |
| 4881 | } |
| 4882 | |
| 4883 | case BC_LEX_KEY_READ: |
| 4884 | { |
| 4885 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4886 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4887 | else if (flags & BC_PARSE_NOREAD) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4888 | s = bc_error_nested_read_call(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4889 | else |
| 4890 | s = bc_parse_read(p); |
| 4891 | |
| 4892 | paren_expr = true; |
| 4893 | rprn = get_token = bin_last = false; |
| 4894 | ++nexprs; |
| 4895 | prev = BC_INST_READ; |
| 4896 | |
| 4897 | break; |
| 4898 | } |
| 4899 | |
| 4900 | case BC_LEX_KEY_SCALE: |
| 4901 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4902 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4903 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4904 | s = bc_parse_scale(p, &prev, flags); |
| 4905 | paren_expr = true; |
| 4906 | rprn = get_token = bin_last = false; |
| 4907 | ++nexprs; |
| 4908 | prev = BC_INST_SCALE; |
| 4909 | |
| 4910 | break; |
| 4911 | } |
| 4912 | |
| 4913 | default: |
| 4914 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4915 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4916 | break; |
| 4917 | } |
| 4918 | } |
| 4919 | |
| 4920 | if (!s && get_token) s = bc_lex_next(&p->l); |
| 4921 | } |
| 4922 | |
| 4923 | if (s) return s; |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 4924 | if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4925 | |
| 4926 | while (p->ops.len > ops_bgn) { |
| 4927 | |
| 4928 | top = BC_PARSE_TOP_OP(p); |
| 4929 | assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN; |
| 4930 | |
| 4931 | if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4932 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4933 | |
| 4934 | bc_parse_push(p, BC_PARSE_TOKEN_INST(top)); |
| 4935 | |
| 4936 | nexprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG; |
| 4937 | bc_vec_pop(&p->ops); |
| 4938 | } |
| 4939 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4940 | if (prev == BC_INST_BOOL_NOT || nexprs != 1) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4941 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4942 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4943 | for (i = 0; i < next.len; ++i) |
| 4944 | if (t == next.tokens[i]) |
| 4945 | goto ok; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4946 | return bc_error_bad_expression(); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4947 | ok: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4948 | |
| 4949 | if (!(flags & BC_PARSE_REL) && nrelops) { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 4950 | s = bc_POSIX_does_not_allow("comparison operators outside if or loops"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4951 | if (s) return s; |
| 4952 | } |
| 4953 | else if ((flags & BC_PARSE_REL) && nrelops > 1) { |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 4954 | s = bc_posix_error("POSIX requires exactly one comparison operator per condition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4955 | if (s) return s; |
| 4956 | } |
| 4957 | |
| 4958 | if (flags & BC_PARSE_PRINT) { |
| 4959 | if (paren_first || !assign) bc_parse_push(p, BC_INST_PRINT); |
| 4960 | bc_parse_push(p, BC_INST_POP); |
| 4961 | } |
| 4962 | |
| 4963 | return s; |
| 4964 | } |
| 4965 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 4966 | static void bc_parse_init(BcParse *p, size_t func) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4967 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 4968 | bc_parse_create(p, func, bc_parse_parse, bc_lex_token); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4969 | } |
| 4970 | |
| 4971 | static BcStatus bc_parse_expression(BcParse *p, uint8_t flags) |
| 4972 | { |
| 4973 | return bc_parse_expr(p, flags, bc_parse_next_read); |
| 4974 | } |
| 4975 | #endif // ENABLE_BC |
| 4976 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 4977 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4978 | static BcStatus dc_parse_register(BcParse *p) |
| 4979 | { |
| 4980 | BcStatus s; |
| 4981 | char *name; |
| 4982 | |
| 4983 | s = bc_lex_next(&p->l); |
| 4984 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4985 | 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] | 4986 | |
| 4987 | name = xstrdup(p->l.t.v.v); |
| 4988 | bc_parse_pushName(p, name); |
| 4989 | |
| 4990 | return s; |
| 4991 | } |
| 4992 | |
| 4993 | static BcStatus dc_parse_string(BcParse *p) |
| 4994 | { |
| 4995 | char *str, *name, b[DC_PARSE_BUF_LEN + 1]; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 4996 | size_t idx, len = G.prog.strs.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4997 | |
| 4998 | sprintf(b, "%0*zu", DC_PARSE_BUF_LEN, len); |
| 4999 | name = xstrdup(b); |
| 5000 | |
| 5001 | str = xstrdup(p->l.t.v.v); |
| 5002 | bc_parse_push(p, BC_INST_STR); |
| 5003 | bc_parse_pushIndex(p, len); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5004 | bc_vec_push(&G.prog.strs, &str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5005 | bc_parse_addFunc(p, name, &idx); |
| 5006 | |
| 5007 | return bc_lex_next(&p->l); |
| 5008 | } |
| 5009 | |
| 5010 | static BcStatus dc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store) |
| 5011 | { |
| 5012 | BcStatus s; |
| 5013 | |
| 5014 | bc_parse_push(p, inst); |
| 5015 | if (name) { |
| 5016 | s = dc_parse_register(p); |
| 5017 | if (s) return s; |
| 5018 | } |
| 5019 | |
| 5020 | if (store) { |
| 5021 | bc_parse_push(p, BC_INST_SWAP); |
| 5022 | bc_parse_push(p, BC_INST_ASSIGN); |
| 5023 | bc_parse_push(p, BC_INST_POP); |
| 5024 | } |
| 5025 | |
| 5026 | return bc_lex_next(&p->l); |
| 5027 | } |
| 5028 | |
| 5029 | static BcStatus dc_parse_cond(BcParse *p, uint8_t inst) |
| 5030 | { |
| 5031 | BcStatus s; |
| 5032 | |
| 5033 | bc_parse_push(p, inst); |
| 5034 | bc_parse_push(p, BC_INST_EXEC_COND); |
| 5035 | |
| 5036 | s = dc_parse_register(p); |
| 5037 | if (s) return s; |
| 5038 | |
| 5039 | s = bc_lex_next(&p->l); |
| 5040 | if (s) return s; |
| 5041 | |
| 5042 | if (p->l.t.t == BC_LEX_ELSE) { |
| 5043 | s = dc_parse_register(p); |
| 5044 | if (s) return s; |
| 5045 | s = bc_lex_next(&p->l); |
| 5046 | } |
| 5047 | else |
| 5048 | bc_parse_push(p, BC_PARSE_STREND); |
| 5049 | |
| 5050 | return s; |
| 5051 | } |
| 5052 | |
| 5053 | static BcStatus dc_parse_token(BcParse *p, BcLexType t, uint8_t flags) |
| 5054 | { |
| 5055 | BcStatus s = BC_STATUS_SUCCESS; |
| 5056 | BcInst prev; |
| 5057 | uint8_t inst; |
| 5058 | bool assign, get_token = false; |
| 5059 | |
| 5060 | switch (t) { |
| 5061 | |
| 5062 | case BC_LEX_OP_REL_EQ: |
| 5063 | case BC_LEX_OP_REL_LE: |
| 5064 | case BC_LEX_OP_REL_GE: |
| 5065 | case BC_LEX_OP_REL_NE: |
| 5066 | case BC_LEX_OP_REL_LT: |
| 5067 | case BC_LEX_OP_REL_GT: |
| 5068 | { |
| 5069 | s = dc_parse_cond(p, t - BC_LEX_OP_REL_EQ + BC_INST_REL_EQ); |
| 5070 | break; |
| 5071 | } |
| 5072 | |
| 5073 | case BC_LEX_SCOLON: |
| 5074 | case BC_LEX_COLON: |
| 5075 | { |
| 5076 | s = dc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON); |
| 5077 | break; |
| 5078 | } |
| 5079 | |
| 5080 | case BC_LEX_STR: |
| 5081 | { |
| 5082 | s = dc_parse_string(p); |
| 5083 | break; |
| 5084 | } |
| 5085 | |
| 5086 | case BC_LEX_NEG: |
| 5087 | case BC_LEX_NUMBER: |
| 5088 | { |
| 5089 | if (t == BC_LEX_NEG) { |
| 5090 | s = bc_lex_next(&p->l); |
| 5091 | if (s) return s; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5092 | if (p->l.t.t != BC_LEX_NUMBER) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5093 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5094 | } |
| 5095 | |
| 5096 | bc_parse_number(p, &prev, &p->nbraces); |
| 5097 | |
| 5098 | if (t == BC_LEX_NEG) bc_parse_push(p, BC_INST_NEG); |
| 5099 | get_token = true; |
| 5100 | |
| 5101 | break; |
| 5102 | } |
| 5103 | |
| 5104 | case BC_LEX_KEY_READ: |
| 5105 | { |
| 5106 | if (flags & BC_PARSE_NOREAD) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5107 | s = bc_error_nested_read_call(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5108 | else |
| 5109 | bc_parse_push(p, BC_INST_READ); |
| 5110 | get_token = true; |
| 5111 | break; |
| 5112 | } |
| 5113 | |
| 5114 | case BC_LEX_OP_ASSIGN: |
| 5115 | case BC_LEX_STORE_PUSH: |
| 5116 | { |
| 5117 | assign = t == BC_LEX_OP_ASSIGN; |
| 5118 | inst = assign ? BC_INST_VAR : BC_INST_PUSH_TO_VAR; |
| 5119 | s = dc_parse_mem(p, inst, true, assign); |
| 5120 | break; |
| 5121 | } |
| 5122 | |
| 5123 | case BC_LEX_LOAD: |
| 5124 | case BC_LEX_LOAD_POP: |
| 5125 | { |
| 5126 | inst = t == BC_LEX_LOAD_POP ? BC_INST_PUSH_VAR : BC_INST_LOAD; |
| 5127 | s = dc_parse_mem(p, inst, true, false); |
| 5128 | break; |
| 5129 | } |
| 5130 | |
| 5131 | case BC_LEX_STORE_IBASE: |
| 5132 | case BC_LEX_STORE_SCALE: |
| 5133 | case BC_LEX_STORE_OBASE: |
| 5134 | { |
| 5135 | inst = t - BC_LEX_STORE_IBASE + BC_INST_IBASE; |
| 5136 | s = dc_parse_mem(p, inst, false, true); |
| 5137 | break; |
| 5138 | } |
| 5139 | |
| 5140 | default: |
| 5141 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5142 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5143 | get_token = true; |
| 5144 | break; |
| 5145 | } |
| 5146 | } |
| 5147 | |
| 5148 | if (!s && get_token) s = bc_lex_next(&p->l); |
| 5149 | |
| 5150 | return s; |
| 5151 | } |
| 5152 | |
| 5153 | static BcStatus dc_parse_expr(BcParse *p, uint8_t flags) |
| 5154 | { |
| 5155 | BcStatus s = BC_STATUS_SUCCESS; |
| 5156 | BcInst inst; |
| 5157 | BcLexType t; |
| 5158 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5159 | if (flags & BC_PARSE_NOCALL) p->nbraces = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5160 | |
| 5161 | for (t = p->l.t.t; !s && t != BC_LEX_EOF; t = p->l.t.t) { |
| 5162 | |
| 5163 | inst = dc_parse_insts[t]; |
| 5164 | |
| 5165 | if (inst != BC_INST_INVALID) { |
| 5166 | bc_parse_push(p, inst); |
| 5167 | s = bc_lex_next(&p->l); |
| 5168 | } |
| 5169 | else |
| 5170 | s = dc_parse_token(p, t, flags); |
| 5171 | } |
| 5172 | |
| 5173 | if (!s && p->l.t.t == BC_LEX_EOF && (flags & BC_PARSE_NOCALL)) |
| 5174 | bc_parse_push(p, BC_INST_POP_EXEC); |
| 5175 | |
| 5176 | return s; |
| 5177 | } |
| 5178 | |
| 5179 | static BcStatus dc_parse_parse(BcParse *p) |
| 5180 | { |
| 5181 | BcStatus s; |
| 5182 | |
| 5183 | if (p->l.t.t == BC_LEX_EOF) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5184 | s = bc_error("end of file"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5185 | else |
| 5186 | s = dc_parse_expr(p, 0); |
| 5187 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 5188 | if (s || G_interrupt) { |
| 5189 | bc_parse_reset(p); |
| 5190 | s = BC_STATUS_FAILURE; |
| 5191 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5192 | |
| 5193 | return s; |
| 5194 | } |
| 5195 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5196 | static void dc_parse_init(BcParse *p, size_t func) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5197 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5198 | bc_parse_create(p, func, dc_parse_parse, dc_lex_token); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5199 | } |
| 5200 | #endif // ENABLE_DC |
| 5201 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5202 | static void common_parse_init(BcParse *p, size_t func) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5203 | { |
| 5204 | if (IS_BC) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5205 | bc_parse_init(p, func); |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5206 | } else { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5207 | dc_parse_init(p, func); |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5208 | } |
| 5209 | } |
| 5210 | |
| 5211 | static BcStatus common_parse_expr(BcParse *p, uint8_t flags) |
| 5212 | { |
| 5213 | if (IS_BC) { |
| 5214 | return bc_parse_expression(p, flags); |
| 5215 | } else { |
| 5216 | return dc_parse_expr(p, flags); |
| 5217 | } |
| 5218 | } |
| 5219 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5220 | static BcVec* bc_program_search(char *id, bool var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5221 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5222 | BcId e, *ptr; |
| 5223 | BcVec *v, *map; |
| 5224 | size_t i; |
| 5225 | BcResultData data; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 5226 | int new; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5227 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5228 | v = var ? &G.prog.vars : &G.prog.arrs; |
| 5229 | map = var ? &G.prog.var_map : &G.prog.arr_map; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5230 | |
| 5231 | e.name = id; |
| 5232 | e.idx = v->len; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 5233 | new = bc_map_insert(map, &e, &i); // 1 if insertion was successful |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5234 | |
| 5235 | if (new) { |
| 5236 | bc_array_init(&data.v, var); |
| 5237 | bc_vec_push(v, &data.v); |
| 5238 | } |
| 5239 | |
| 5240 | ptr = bc_vec_item(map, i); |
| 5241 | if (new) ptr->name = xstrdup(e.name); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5242 | return bc_vec_item(v, ptr->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5243 | } |
| 5244 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5245 | static BcStatus bc_program_num(BcResult *r, BcNum **num, bool hex) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5246 | { |
| 5247 | BcStatus s = BC_STATUS_SUCCESS; |
| 5248 | |
| 5249 | switch (r->t) { |
| 5250 | |
| 5251 | case BC_RESULT_STR: |
| 5252 | case BC_RESULT_TEMP: |
| 5253 | case BC_RESULT_IBASE: |
| 5254 | case BC_RESULT_SCALE: |
| 5255 | case BC_RESULT_OBASE: |
| 5256 | { |
| 5257 | *num = &r->d.n; |
| 5258 | break; |
| 5259 | } |
| 5260 | |
| 5261 | case BC_RESULT_CONSTANT: |
| 5262 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5263 | char **str = bc_vec_item(&G.prog.consts, r->d.id.idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5264 | size_t base_t, len = strlen(*str); |
| 5265 | BcNum *base; |
| 5266 | |
| 5267 | bc_num_init(&r->d.n, len); |
| 5268 | |
| 5269 | hex = hex && len == 1; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5270 | base = hex ? &G.prog.hexb : &G.prog.ib; |
| 5271 | base_t = hex ? BC_NUM_MAX_IBASE : G.prog.ib_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5272 | s = bc_num_parse(&r->d.n, *str, base, base_t); |
| 5273 | |
| 5274 | if (s) { |
| 5275 | bc_num_free(&r->d.n); |
| 5276 | return s; |
| 5277 | } |
| 5278 | |
| 5279 | *num = &r->d.n; |
| 5280 | r->t = BC_RESULT_TEMP; |
| 5281 | |
| 5282 | break; |
| 5283 | } |
| 5284 | |
| 5285 | case BC_RESULT_VAR: |
| 5286 | case BC_RESULT_ARRAY: |
| 5287 | case BC_RESULT_ARRAY_ELEM: |
| 5288 | { |
| 5289 | BcVec *v; |
| 5290 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5291 | 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] | 5292 | |
| 5293 | if (r->t == BC_RESULT_ARRAY_ELEM) { |
| 5294 | v = bc_vec_top(v); |
| 5295 | if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1); |
| 5296 | *num = bc_vec_item(v, r->d.id.idx); |
| 5297 | } |
| 5298 | else |
| 5299 | *num = bc_vec_top(v); |
| 5300 | |
| 5301 | break; |
| 5302 | } |
| 5303 | |
| 5304 | case BC_RESULT_LAST: |
| 5305 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5306 | *num = &G.prog.last; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5307 | break; |
| 5308 | } |
| 5309 | |
| 5310 | case BC_RESULT_ONE: |
| 5311 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5312 | *num = &G.prog.one; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5313 | break; |
| 5314 | } |
| 5315 | } |
| 5316 | |
| 5317 | return s; |
| 5318 | } |
| 5319 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5320 | static BcStatus bc_program_binOpPrep(BcResult **l, BcNum **ln, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5321 | BcResult **r, BcNum **rn, bool assign) |
| 5322 | { |
| 5323 | BcStatus s; |
| 5324 | bool hex; |
| 5325 | BcResultType lt, rt; |
| 5326 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5327 | if (!BC_PROG_STACK(&G.prog.results, 2)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5328 | return bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5329 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5330 | *r = bc_vec_item_rev(&G.prog.results, 0); |
| 5331 | *l = bc_vec_item_rev(&G.prog.results, 1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5332 | |
| 5333 | lt = (*l)->t; |
| 5334 | rt = (*r)->t; |
| 5335 | hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE); |
| 5336 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5337 | s = bc_program_num(*l, ln, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5338 | if (s) return s; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5339 | s = bc_program_num(*r, rn, hex); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5340 | if (s) return s; |
| 5341 | |
| 5342 | // We run this again under these conditions in case any vector has been |
| 5343 | // reallocated out from under the BcNums or arrays we had. |
| 5344 | if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5345 | s = bc_program_num(*l, ln, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5346 | if (s) return s; |
| 5347 | } |
| 5348 | |
| 5349 | if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5350 | return bc_error_variable_is_wrong_type(); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5351 | if (!assign && !BC_PROG_NUM((*r), (*ln))) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5352 | return bc_error_variable_is_wrong_type(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5353 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5354 | return s; |
| 5355 | } |
| 5356 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5357 | static void bc_program_binOpRetire(BcResult *r) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5358 | { |
| 5359 | r->t = BC_RESULT_TEMP; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5360 | bc_vec_pop(&G.prog.results); |
| 5361 | bc_vec_pop(&G.prog.results); |
| 5362 | bc_vec_push(&G.prog.results, r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5363 | } |
| 5364 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5365 | static BcStatus bc_program_prep(BcResult **r, BcNum **n) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5366 | { |
| 5367 | BcStatus s; |
| 5368 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5369 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5370 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5371 | *r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5372 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5373 | s = bc_program_num(*r, n, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5374 | if (s) return s; |
| 5375 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5376 | if (!BC_PROG_NUM((*r), (*n))) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5377 | return bc_error_variable_is_wrong_type(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5378 | |
| 5379 | return s; |
| 5380 | } |
| 5381 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5382 | static void bc_program_retire(BcResult *r, BcResultType t) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5383 | { |
| 5384 | r->t = t; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5385 | bc_vec_pop(&G.prog.results); |
| 5386 | bc_vec_push(&G.prog.results, r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5387 | } |
| 5388 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5389 | static BcStatus bc_program_op(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5390 | { |
| 5391 | BcStatus s; |
| 5392 | BcResult *opd1, *opd2, res; |
| 5393 | BcNum *n1, *n2 = NULL; |
| 5394 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5395 | s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5396 | if (s) return s; |
| 5397 | bc_num_init(&res.d.n, BC_NUM_DEF_SIZE); |
| 5398 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5399 | 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] | 5400 | if (s) goto err; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5401 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5402 | |
| 5403 | return s; |
| 5404 | |
| 5405 | err: |
| 5406 | bc_num_free(&res.d.n); |
| 5407 | return s; |
| 5408 | } |
| 5409 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 5410 | static BcStatus bc_program_read(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5411 | { |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5412 | const char *sv_file; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5413 | BcStatus s; |
| 5414 | BcParse parse; |
| 5415 | BcVec buf; |
| 5416 | BcInstPtr ip; |
| 5417 | size_t i; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5418 | BcFunc *f = bc_vec_item(&G.prog.fns, BC_PROG_READ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5419 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5420 | for (i = 0; i < G.prog.stack.len; ++i) { |
| 5421 | BcInstPtr *ip_ptr = bc_vec_item(&G.prog.stack, i); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5422 | if (ip_ptr->func == BC_PROG_READ) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5423 | return bc_error_nested_read_call(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5424 | } |
| 5425 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 5426 | bc_vec_pop_all(&f->code); |
| 5427 | bc_char_vec_init(&buf); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5428 | |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5429 | sv_file = G.prog.file; |
| 5430 | G.prog.file = NULL; |
| 5431 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5432 | s = bc_read_line(&buf, "read> "); |
| 5433 | if (s) goto io_err; |
| 5434 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5435 | common_parse_init(&parse, BC_PROG_READ); |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5436 | bc_lex_file(&parse.l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5437 | |
| 5438 | s = bc_parse_text(&parse, buf.v); |
| 5439 | if (s) goto exec_err; |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5440 | s = common_parse_expr(&parse, BC_PARSE_NOREAD); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5441 | if (s) goto exec_err; |
| 5442 | |
| 5443 | 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] | 5444 | s = bc_error("bad read() expression"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5445 | goto exec_err; |
| 5446 | } |
| 5447 | |
| 5448 | ip.func = BC_PROG_READ; |
| 5449 | ip.idx = 0; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5450 | ip.len = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5451 | |
| 5452 | // Update this pointer, just in case. |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5453 | f = bc_vec_item(&G.prog.fns, BC_PROG_READ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5454 | |
| 5455 | bc_vec_pushByte(&f->code, BC_INST_POP_EXEC); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5456 | bc_vec_push(&G.prog.stack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5457 | |
| 5458 | exec_err: |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5459 | G.prog.file = sv_file; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5460 | bc_parse_free(&parse); |
| 5461 | io_err: |
| 5462 | bc_vec_free(&buf); |
| 5463 | return s; |
| 5464 | } |
| 5465 | |
| 5466 | static size_t bc_program_index(char *code, size_t *bgn) |
| 5467 | { |
| 5468 | char amt = code[(*bgn)++], i = 0; |
| 5469 | size_t res = 0; |
| 5470 | |
| 5471 | for (; i < amt; ++i, ++(*bgn)) |
| 5472 | res |= (((size_t)((int) code[*bgn]) & UCHAR_MAX) << (i * CHAR_BIT)); |
| 5473 | |
| 5474 | return res; |
| 5475 | } |
| 5476 | |
| 5477 | static char *bc_program_name(char *code, size_t *bgn) |
| 5478 | { |
| 5479 | size_t i; |
| 5480 | char c, *s, *str = code + *bgn, *ptr = strchr(str, BC_PARSE_STREND); |
| 5481 | |
| 5482 | s = xmalloc(ptr - str + 1); |
| 5483 | c = code[(*bgn)++]; |
| 5484 | |
| 5485 | for (i = 0; c != 0 && c != BC_PARSE_STREND; c = code[(*bgn)++], ++i) |
| 5486 | s[i] = c; |
| 5487 | |
| 5488 | s[i] = '\0'; |
| 5489 | |
| 5490 | return s; |
| 5491 | } |
| 5492 | |
| 5493 | static void bc_program_printString(const char *str, size_t *nchars) |
| 5494 | { |
| 5495 | size_t i, len = strlen(str); |
| 5496 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5497 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5498 | if (len == 0) { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5499 | bb_putchar('\0'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5500 | return; |
| 5501 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5502 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5503 | |
| 5504 | for (i = 0; i < len; ++i, ++(*nchars)) { |
| 5505 | |
| 5506 | int c = str[i]; |
| 5507 | |
| 5508 | if (c != '\\' || i == len - 1) |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5509 | bb_putchar(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5510 | else { |
| 5511 | |
| 5512 | c = str[++i]; |
| 5513 | |
| 5514 | switch (c) { |
| 5515 | |
| 5516 | case 'a': |
| 5517 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5518 | bb_putchar('\a'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5519 | break; |
| 5520 | } |
| 5521 | |
| 5522 | case 'b': |
| 5523 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5524 | bb_putchar('\b'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5525 | break; |
| 5526 | } |
| 5527 | |
| 5528 | case '\\': |
| 5529 | case 'e': |
| 5530 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5531 | bb_putchar('\\'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5532 | break; |
| 5533 | } |
| 5534 | |
| 5535 | case 'f': |
| 5536 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5537 | bb_putchar('\f'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5538 | break; |
| 5539 | } |
| 5540 | |
| 5541 | case 'n': |
| 5542 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5543 | bb_putchar('\n'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5544 | *nchars = SIZE_MAX; |
| 5545 | break; |
| 5546 | } |
| 5547 | |
| 5548 | case 'r': |
| 5549 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5550 | bb_putchar('\r'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5551 | break; |
| 5552 | } |
| 5553 | |
| 5554 | case 'q': |
| 5555 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5556 | bb_putchar('"'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5557 | break; |
| 5558 | } |
| 5559 | |
| 5560 | case 't': |
| 5561 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5562 | bb_putchar('\t'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5563 | break; |
| 5564 | } |
| 5565 | |
| 5566 | default: |
| 5567 | { |
| 5568 | // Just print the backslash and following character. |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5569 | bb_putchar('\\'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5570 | ++(*nchars); |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5571 | bb_putchar(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5572 | break; |
| 5573 | } |
| 5574 | } |
| 5575 | } |
| 5576 | } |
| 5577 | } |
| 5578 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5579 | static BcStatus bc_program_print(char inst, size_t idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5580 | { |
| 5581 | BcStatus s = BC_STATUS_SUCCESS; |
| 5582 | BcResult *r; |
| 5583 | size_t len, i; |
| 5584 | char *str; |
| 5585 | BcNum *num = NULL; |
| 5586 | bool pop = inst != BC_INST_PRINT; |
| 5587 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5588 | if (!BC_PROG_STACK(&G.prog.results, idx + 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5589 | return bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5590 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5591 | r = bc_vec_item_rev(&G.prog.results, idx); |
| 5592 | s = bc_program_num(r, &num, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5593 | if (s) return s; |
| 5594 | |
| 5595 | if (BC_PROG_NUM(r, num)) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5596 | s = bc_num_print(num, &G.prog.ob, G.prog.ob_t, !pop, &G.prog.nchars, G.prog.len); |
| 5597 | if (!s) bc_num_copy(&G.prog.last, num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5598 | } |
| 5599 | else { |
| 5600 | |
| 5601 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5602 | str = *((char **) bc_vec_item(&G.prog.strs, idx)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5603 | |
| 5604 | if (inst == BC_INST_PRINT_STR) { |
| 5605 | for (i = 0, len = strlen(str); i < len; ++i) { |
| 5606 | char c = str[i]; |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5607 | bb_putchar(c); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5608 | if (c == '\n') G.prog.nchars = SIZE_MAX; |
| 5609 | ++G.prog.nchars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5610 | } |
| 5611 | } |
| 5612 | else { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5613 | bc_program_printString(str, &G.prog.nchars); |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5614 | if (inst == BC_INST_PRINT) bb_putchar('\n'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5615 | } |
| 5616 | } |
| 5617 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5618 | if (!s && pop) bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5619 | |
| 5620 | return s; |
| 5621 | } |
| 5622 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5623 | static BcStatus bc_program_negate(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5624 | { |
| 5625 | BcStatus s; |
| 5626 | BcResult res, *ptr; |
| 5627 | BcNum *num = NULL; |
| 5628 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5629 | s = bc_program_prep(&ptr, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5630 | if (s) return s; |
| 5631 | |
| 5632 | bc_num_init(&res.d.n, num->len); |
| 5633 | bc_num_copy(&res.d.n, num); |
| 5634 | if (res.d.n.len) res.d.n.neg = !res.d.n.neg; |
| 5635 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5636 | bc_program_retire(&res, BC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5637 | |
| 5638 | return s; |
| 5639 | } |
| 5640 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5641 | static BcStatus bc_program_logical(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5642 | { |
| 5643 | BcStatus s; |
| 5644 | BcResult *opd1, *opd2, res; |
| 5645 | BcNum *n1, *n2; |
| 5646 | bool cond = 0; |
| 5647 | ssize_t cmp; |
| 5648 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5649 | s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5650 | if (s) return s; |
| 5651 | bc_num_init(&res.d.n, BC_NUM_DEF_SIZE); |
| 5652 | |
| 5653 | if (inst == BC_INST_BOOL_AND) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5654 | 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] | 5655 | else if (inst == BC_INST_BOOL_OR) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5656 | 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] | 5657 | else { |
| 5658 | |
| 5659 | cmp = bc_num_cmp(n1, n2); |
| 5660 | |
| 5661 | switch (inst) { |
| 5662 | |
| 5663 | case BC_INST_REL_EQ: |
| 5664 | { |
| 5665 | cond = cmp == 0; |
| 5666 | break; |
| 5667 | } |
| 5668 | |
| 5669 | case BC_INST_REL_LE: |
| 5670 | { |
| 5671 | cond = cmp <= 0; |
| 5672 | break; |
| 5673 | } |
| 5674 | |
| 5675 | case BC_INST_REL_GE: |
| 5676 | { |
| 5677 | cond = cmp >= 0; |
| 5678 | break; |
| 5679 | } |
| 5680 | |
| 5681 | case BC_INST_REL_NE: |
| 5682 | { |
| 5683 | cond = cmp != 0; |
| 5684 | break; |
| 5685 | } |
| 5686 | |
| 5687 | case BC_INST_REL_LT: |
| 5688 | { |
| 5689 | cond = cmp < 0; |
| 5690 | break; |
| 5691 | } |
| 5692 | |
| 5693 | case BC_INST_REL_GT: |
| 5694 | { |
| 5695 | cond = cmp > 0; |
| 5696 | break; |
| 5697 | } |
| 5698 | } |
| 5699 | } |
| 5700 | |
| 5701 | (cond ? bc_num_one : bc_num_zero)(&res.d.n); |
| 5702 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5703 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5704 | |
| 5705 | return s; |
| 5706 | } |
| 5707 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5708 | #if ENABLE_DC |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5709 | static BcStatus bc_program_assignStr(BcResult *r, BcVec *v, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5710 | bool push) |
| 5711 | { |
| 5712 | BcNum n2; |
| 5713 | BcResult res; |
| 5714 | |
| 5715 | memset(&n2, 0, sizeof(BcNum)); |
| 5716 | n2.rdx = res.d.id.idx = r->d.id.idx; |
| 5717 | res.t = BC_RESULT_STR; |
| 5718 | |
| 5719 | if (!push) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5720 | if (!BC_PROG_STACK(&G.prog.results, 2)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5721 | return bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5722 | bc_vec_pop(v); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5723 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5724 | } |
| 5725 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5726 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5727 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5728 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5729 | bc_vec_push(v, &n2); |
| 5730 | |
| 5731 | return BC_STATUS_SUCCESS; |
| 5732 | } |
| 5733 | #endif // ENABLE_DC |
| 5734 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5735 | static BcStatus bc_program_copyToVar(char *name, bool var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5736 | { |
| 5737 | BcStatus s; |
| 5738 | BcResult *ptr, r; |
| 5739 | BcVec *v; |
| 5740 | BcNum *n; |
| 5741 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5742 | if (!BC_PROG_STACK(&G.prog.results, 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 | ptr = bc_vec_top(&G.prog.results); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5746 | if ((ptr->t == BC_RESULT_ARRAY) != !var) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5747 | return bc_error_variable_is_wrong_type(); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5748 | v = bc_program_search(name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5749 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5750 | #if ENABLE_DC |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5751 | if (ptr->t == BC_RESULT_STR && !var) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5752 | return bc_error_variable_is_wrong_type(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5753 | 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] | 5754 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5755 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5756 | s = bc_program_num(ptr, &n, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5757 | if (s) return s; |
| 5758 | |
| 5759 | // Do this once more to make sure that pointers were not invalidated. |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5760 | v = bc_program_search(name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5761 | |
| 5762 | if (var) { |
| 5763 | bc_num_init(&r.d.n, BC_NUM_DEF_SIZE); |
| 5764 | bc_num_copy(&r.d.n, n); |
| 5765 | } |
| 5766 | else { |
| 5767 | bc_array_init(&r.d.v, true); |
| 5768 | bc_array_copy(&r.d.v, (BcVec *) n); |
| 5769 | } |
| 5770 | |
| 5771 | bc_vec_push(v, &r.d); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5772 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5773 | |
| 5774 | return s; |
| 5775 | } |
| 5776 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5777 | static BcStatus bc_program_assign(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5778 | { |
| 5779 | BcStatus s; |
| 5780 | BcResult *left, *right, res; |
| 5781 | BcNum *l = NULL, *r = NULL; |
| 5782 | unsigned long val, max; |
| 5783 | bool assign = inst == BC_INST_ASSIGN, ib, sc; |
| 5784 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5785 | s = bc_program_binOpPrep(&left, &l, &right, &r, assign); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5786 | if (s) return s; |
| 5787 | |
| 5788 | ib = left->t == BC_RESULT_IBASE; |
| 5789 | sc = left->t == BC_RESULT_SCALE; |
| 5790 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5791 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5792 | |
| 5793 | if (right->t == BC_RESULT_STR) { |
| 5794 | |
| 5795 | BcVec *v; |
| 5796 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5797 | if (left->t != BC_RESULT_VAR) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5798 | return bc_error_variable_is_wrong_type(); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5799 | v = bc_program_search(left->d.id.name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5800 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5801 | return bc_program_assignStr(right, v, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5802 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5803 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5804 | |
| 5805 | if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5806 | return bc_error("bad assignment:" |
| 5807 | " left side must be scale," |
| 5808 | " ibase, obase, last, var," |
| 5809 | " or array element" |
| 5810 | ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5811 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5812 | #if ENABLE_BC |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5813 | 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] | 5814 | return bc_error("divide by zero"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5815 | |
| 5816 | if (assign) |
| 5817 | bc_num_copy(l, r); |
| 5818 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5819 | 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] | 5820 | |
| 5821 | if (s) return s; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5822 | #else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5823 | bc_num_copy(l, r); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5824 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5825 | |
| 5826 | if (ib || sc || left->t == BC_RESULT_OBASE) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5827 | static const char *const msg[] = { |
| 5828 | "bad ibase; must be [2, 16]", //BC_RESULT_IBASE |
| 5829 | "bad scale; must be [0, BC_SCALE_MAX]", //BC_RESULT_SCALE |
| 5830 | "?1", //BC_RESULT_LAST |
| 5831 | "?2", //BC_RESULT_CONSTANT |
| 5832 | "?3", //BC_RESULT_ONE |
| 5833 | "bad obase; must be [2, BC_BASE_MAX]", //BC_RESULT_OBASE |
| 5834 | }; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5835 | size_t *ptr; |
| 5836 | |
| 5837 | s = bc_num_ulong(l, &val); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5838 | if (s) |
| 5839 | return s; |
| 5840 | s = left->t - BC_RESULT_IBASE; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5841 | if (sc) { |
| 5842 | max = BC_MAX_SCALE; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5843 | ptr = &G.prog.scale; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5844 | } |
| 5845 | else { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5846 | if (val < BC_NUM_MIN_BASE) |
| 5847 | return bc_error(msg[s]); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5848 | max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5849 | ptr = ib ? &G.prog.ib_t : &G.prog.ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5850 | } |
| 5851 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5852 | if (val > max) |
| 5853 | return bc_error(msg[s]); |
| 5854 | if (!sc) |
| 5855 | bc_num_copy(ib ? &G.prog.ib : &G.prog.ob, l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5856 | |
| 5857 | *ptr = (size_t) val; |
| 5858 | s = BC_STATUS_SUCCESS; |
| 5859 | } |
| 5860 | |
| 5861 | bc_num_init(&res.d.n, l->len); |
| 5862 | bc_num_copy(&res.d.n, l); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5863 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5864 | |
| 5865 | return s; |
| 5866 | } |
| 5867 | |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 5868 | #if !ENABLE_DC |
| 5869 | #define bc_program_pushVar(code, bgn, pop, copy) \ |
| 5870 | bc_program_pushVar(code, bgn) |
| 5871 | // for bc, 'pop' and 'copy' are always false |
| 5872 | #endif |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5873 | static BcStatus bc_program_pushVar(char *code, size_t *bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5874 | bool pop, bool copy) |
| 5875 | { |
| 5876 | BcStatus s = BC_STATUS_SUCCESS; |
| 5877 | BcResult r; |
| 5878 | char *name = bc_program_name(code, bgn); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5879 | |
| 5880 | r.t = BC_RESULT_VAR; |
| 5881 | r.d.id.name = name; |
| 5882 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5883 | #if ENABLE_DC |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 5884 | { |
| 5885 | BcVec *v = bc_program_search(name, true); |
| 5886 | BcNum *num = bc_vec_top(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5887 | |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 5888 | if (pop || copy) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5889 | |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 5890 | if (!BC_PROG_STACK(v, 2 - copy)) { |
| 5891 | free(name); |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5892 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 5893 | } |
| 5894 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5895 | free(name); |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 5896 | name = NULL; |
| 5897 | |
| 5898 | if (!BC_PROG_STR(num)) { |
| 5899 | |
| 5900 | r.t = BC_RESULT_TEMP; |
| 5901 | |
| 5902 | bc_num_init(&r.d.n, BC_NUM_DEF_SIZE); |
| 5903 | bc_num_copy(&r.d.n, num); |
| 5904 | } |
| 5905 | else { |
| 5906 | r.t = BC_RESULT_STR; |
| 5907 | r.d.id.idx = num->rdx; |
| 5908 | } |
| 5909 | |
| 5910 | if (!copy) bc_vec_pop(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5911 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5912 | } |
| 5913 | #endif // ENABLE_DC |
| 5914 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5915 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5916 | |
| 5917 | return s; |
| 5918 | } |
| 5919 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5920 | static BcStatus bc_program_pushArray(char *code, size_t *bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5921 | char inst) |
| 5922 | { |
| 5923 | BcStatus s = BC_STATUS_SUCCESS; |
| 5924 | BcResult r; |
| 5925 | BcNum *num; |
| 5926 | |
| 5927 | r.d.id.name = bc_program_name(code, bgn); |
| 5928 | |
| 5929 | if (inst == BC_INST_ARRAY) { |
| 5930 | r.t = BC_RESULT_ARRAY; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5931 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5932 | } |
| 5933 | else { |
| 5934 | |
| 5935 | BcResult *operand; |
| 5936 | unsigned long temp; |
| 5937 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5938 | s = bc_program_prep(&operand, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5939 | if (s) goto err; |
| 5940 | s = bc_num_ulong(num, &temp); |
| 5941 | if (s) goto err; |
| 5942 | |
| 5943 | if (temp > BC_MAX_DIM) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5944 | s = bc_error("array too long; must be [1, BC_DIM_MAX]"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5945 | goto err; |
| 5946 | } |
| 5947 | |
| 5948 | r.d.id.idx = (size_t) temp; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5949 | bc_program_retire(&r, BC_RESULT_ARRAY_ELEM); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5950 | } |
| 5951 | |
| 5952 | err: |
| 5953 | if (s) free(r.d.id.name); |
| 5954 | return s; |
| 5955 | } |
| 5956 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5957 | #if ENABLE_BC |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5958 | static BcStatus bc_program_incdec(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5959 | { |
| 5960 | BcStatus s; |
| 5961 | BcResult *ptr, res, copy; |
| 5962 | BcNum *num = NULL; |
| 5963 | char inst2 = inst; |
| 5964 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5965 | s = bc_program_prep(&ptr, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5966 | if (s) return s; |
| 5967 | |
| 5968 | if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) { |
| 5969 | copy.t = BC_RESULT_TEMP; |
| 5970 | bc_num_init(©.d.n, num->len); |
| 5971 | bc_num_copy(©.d.n, num); |
| 5972 | } |
| 5973 | |
| 5974 | res.t = BC_RESULT_ONE; |
| 5975 | inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ? |
| 5976 | BC_INST_ASSIGN_PLUS : |
| 5977 | BC_INST_ASSIGN_MINUS; |
| 5978 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5979 | bc_vec_push(&G.prog.results, &res); |
| 5980 | bc_program_assign(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5981 | |
| 5982 | if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5983 | bc_vec_pop(&G.prog.results); |
| 5984 | bc_vec_push(&G.prog.results, ©); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5985 | } |
| 5986 | |
| 5987 | return s; |
| 5988 | } |
| 5989 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5990 | static BcStatus bc_program_call(char *code, size_t *idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5991 | { |
| 5992 | BcStatus s = BC_STATUS_SUCCESS; |
| 5993 | BcInstPtr ip; |
| 5994 | size_t i, nparams = bc_program_index(code, idx); |
| 5995 | BcFunc *func; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5996 | BcId *a; |
| 5997 | BcResultData param; |
| 5998 | BcResult *arg; |
| 5999 | |
| 6000 | ip.idx = 0; |
| 6001 | ip.func = bc_program_index(code, idx); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6002 | func = bc_vec_item(&G.prog.fns, ip.func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6003 | |
Denys Vlasenko | 04a1c76 | 2018-12-03 21:10:57 +0100 | [diff] [blame] | 6004 | if (func->code.len == 0) { |
| 6005 | return bc_error("undefined function"); |
| 6006 | } |
| 6007 | if (nparams != func->nparams) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6008 | 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] | 6009 | } |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6010 | ip.len = G.prog.results.len - nparams; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6011 | |
| 6012 | for (i = 0; i < nparams; ++i) { |
| 6013 | |
| 6014 | a = bc_vec_item(&func->autos, nparams - 1 - i); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6015 | arg = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6016 | |
| 6017 | 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] | 6018 | return bc_error_variable_is_wrong_type(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6019 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6020 | s = bc_program_copyToVar(a->name, a->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6021 | if (s) return s; |
| 6022 | } |
| 6023 | |
| 6024 | for (; i < func->autos.len; ++i) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6025 | BcVec *v; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6026 | |
| 6027 | a = bc_vec_item(&func->autos, i); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6028 | v = bc_program_search(a->name, a->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6029 | |
| 6030 | if (a->idx) { |
| 6031 | bc_num_init(¶m.n, BC_NUM_DEF_SIZE); |
| 6032 | bc_vec_push(v, ¶m.n); |
| 6033 | } |
| 6034 | else { |
| 6035 | bc_array_init(¶m.v, true); |
| 6036 | bc_vec_push(v, ¶m.v); |
| 6037 | } |
| 6038 | } |
| 6039 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6040 | bc_vec_push(&G.prog.stack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6041 | |
| 6042 | return BC_STATUS_SUCCESS; |
| 6043 | } |
| 6044 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6045 | static BcStatus bc_program_return(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6046 | { |
| 6047 | BcStatus s; |
| 6048 | BcResult res; |
| 6049 | BcFunc *f; |
| 6050 | size_t i; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6051 | BcInstPtr *ip = bc_vec_top(&G.prog.stack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6052 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6053 | 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] | 6054 | return bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6055 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6056 | f = bc_vec_item(&G.prog.fns, ip->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6057 | res.t = BC_RESULT_TEMP; |
| 6058 | |
| 6059 | if (inst == BC_INST_RET) { |
| 6060 | |
| 6061 | BcNum *num; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6062 | BcResult *operand = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6063 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6064 | s = bc_program_num(operand, &num, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6065 | if (s) return s; |
| 6066 | bc_num_init(&res.d.n, num->len); |
| 6067 | bc_num_copy(&res.d.n, num); |
| 6068 | } |
| 6069 | else { |
| 6070 | bc_num_init(&res.d.n, BC_NUM_DEF_SIZE); |
| 6071 | bc_num_zero(&res.d.n); |
| 6072 | } |
| 6073 | |
| 6074 | // We need to pop arguments as well, so this takes that into account. |
| 6075 | for (i = 0; i < f->autos.len; ++i) { |
| 6076 | |
| 6077 | BcVec *v; |
| 6078 | BcId *a = bc_vec_item(&f->autos, i); |
| 6079 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6080 | v = bc_program_search(a->name, a->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6081 | bc_vec_pop(v); |
| 6082 | } |
| 6083 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6084 | bc_vec_npop(&G.prog.results, G.prog.results.len - ip->len); |
| 6085 | bc_vec_push(&G.prog.results, &res); |
| 6086 | bc_vec_pop(&G.prog.stack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6087 | |
| 6088 | return BC_STATUS_SUCCESS; |
| 6089 | } |
| 6090 | #endif // ENABLE_BC |
| 6091 | |
| 6092 | static unsigned long bc_program_scale(BcNum *n) |
| 6093 | { |
| 6094 | return (unsigned long) n->rdx; |
| 6095 | } |
| 6096 | |
| 6097 | static unsigned long bc_program_len(BcNum *n) |
| 6098 | { |
| 6099 | unsigned long len = n->len; |
| 6100 | size_t i; |
| 6101 | |
| 6102 | if (n->rdx != n->len) return len; |
| 6103 | for (i = n->len - 1; i < n->len && n->num[i] == 0; --len, --i); |
| 6104 | |
| 6105 | return len; |
| 6106 | } |
| 6107 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6108 | static BcStatus bc_program_builtin(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6109 | { |
| 6110 | BcStatus s; |
| 6111 | BcResult *opnd; |
| 6112 | BcNum *num = NULL; |
| 6113 | BcResult res; |
| 6114 | bool len = inst == BC_INST_LENGTH; |
| 6115 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6116 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6117 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6118 | opnd = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6119 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6120 | s = bc_program_num(opnd, &num, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6121 | if (s) return s; |
| 6122 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6123 | #if ENABLE_DC |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6124 | if (!BC_PROG_NUM(opnd, num) && !len) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6125 | return bc_error_variable_is_wrong_type(); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6126 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6127 | |
| 6128 | bc_num_init(&res.d.n, BC_NUM_DEF_SIZE); |
| 6129 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6130 | 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] | 6131 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6132 | else if (len != 0 && opnd->t == BC_RESULT_ARRAY) { |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6133 | bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6134 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6135 | #endif |
| 6136 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6137 | else if (len != 0 && !BC_PROG_NUM(opnd, num)) { |
| 6138 | |
| 6139 | char **str; |
| 6140 | size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx; |
| 6141 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6142 | str = bc_vec_item(&G.prog.strs, idx); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6143 | bc_num_ulong2num(&res.d.n, strlen(*str)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6144 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6145 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6146 | else { |
| 6147 | BcProgramBuiltIn f = len ? bc_program_len : bc_program_scale; |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6148 | bc_num_ulong2num(&res.d.n, f(num)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6149 | } |
| 6150 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6151 | bc_program_retire(&res, BC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6152 | |
| 6153 | return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6154 | } |
| 6155 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6156 | #if ENABLE_DC |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6157 | static BcStatus bc_program_divmod(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6158 | { |
| 6159 | BcStatus s; |
| 6160 | BcResult *opd1, *opd2, res, res2; |
| 6161 | BcNum *n1, *n2 = NULL; |
| 6162 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6163 | s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6164 | if (s) return s; |
| 6165 | |
| 6166 | bc_num_init(&res.d.n, BC_NUM_DEF_SIZE); |
| 6167 | bc_num_init(&res2.d.n, n2->len); |
| 6168 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6169 | 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] | 6170 | if (s) goto err; |
| 6171 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6172 | bc_program_binOpRetire(&res2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6173 | res.t = BC_RESULT_TEMP; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6174 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6175 | |
| 6176 | return s; |
| 6177 | |
| 6178 | err: |
| 6179 | bc_num_free(&res2.d.n); |
| 6180 | bc_num_free(&res.d.n); |
| 6181 | return s; |
| 6182 | } |
| 6183 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6184 | static BcStatus bc_program_modexp(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6185 | { |
| 6186 | BcStatus s; |
| 6187 | BcResult *r1, *r2, *r3, res; |
| 6188 | BcNum *n1, *n2, *n3; |
| 6189 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6190 | if (!BC_PROG_STACK(&G.prog.results, 3)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6191 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6192 | s = bc_program_binOpPrep(&r2, &n2, &r3, &n3, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6193 | if (s) return s; |
| 6194 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6195 | r1 = bc_vec_item_rev(&G.prog.results, 2); |
| 6196 | s = bc_program_num(r1, &n1, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6197 | if (s) return s; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6198 | if (!BC_PROG_NUM(r1, n1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6199 | return bc_error_variable_is_wrong_type(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6200 | |
| 6201 | // Make sure that the values have their pointers updated, if necessary. |
| 6202 | if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) { |
| 6203 | |
| 6204 | if (r1->t == r2->t) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6205 | s = bc_program_num(r2, &n2, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6206 | if (s) return s; |
| 6207 | } |
| 6208 | |
| 6209 | if (r1->t == r3->t) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6210 | s = bc_program_num(r3, &n3, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6211 | if (s) return s; |
| 6212 | } |
| 6213 | } |
| 6214 | |
| 6215 | bc_num_init(&res.d.n, n3->len); |
| 6216 | s = bc_num_modexp(n1, n2, n3, &res.d.n); |
| 6217 | if (s) goto err; |
| 6218 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6219 | bc_vec_pop(&G.prog.results); |
| 6220 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6221 | |
| 6222 | return s; |
| 6223 | |
| 6224 | err: |
| 6225 | bc_num_free(&res.d.n); |
| 6226 | return s; |
| 6227 | } |
| 6228 | |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6229 | static void bc_program_stackLen(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6230 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6231 | BcResult res; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6232 | size_t len = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6233 | |
| 6234 | res.t = BC_RESULT_TEMP; |
| 6235 | |
| 6236 | bc_num_init(&res.d.n, BC_NUM_DEF_SIZE); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6237 | bc_num_ulong2num(&res.d.n, len); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6238 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6239 | } |
| 6240 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6241 | static BcStatus bc_program_asciify(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6242 | { |
| 6243 | BcStatus s; |
| 6244 | BcResult *r, res; |
| 6245 | BcNum *num = NULL, n; |
| 6246 | char *str, *str2, c; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6247 | size_t len = G.prog.strs.len, idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6248 | unsigned long val; |
| 6249 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6250 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6251 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6252 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6253 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6254 | s = bc_program_num(r, &num, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6255 | if (s) return s; |
| 6256 | |
| 6257 | if (BC_PROG_NUM(r, num)) { |
| 6258 | |
| 6259 | bc_num_init(&n, BC_NUM_DEF_SIZE); |
| 6260 | bc_num_copy(&n, num); |
| 6261 | bc_num_truncate(&n, n.rdx); |
| 6262 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6263 | s = bc_num_mod(&n, &G.prog.strmb, &n, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6264 | if (s) goto num_err; |
| 6265 | s = bc_num_ulong(&n, &val); |
| 6266 | if (s) goto num_err; |
| 6267 | |
| 6268 | c = (char) val; |
| 6269 | |
| 6270 | bc_num_free(&n); |
| 6271 | } |
| 6272 | else { |
| 6273 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6274 | str2 = *((char **) bc_vec_item(&G.prog.strs, idx)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6275 | c = str2[0]; |
| 6276 | } |
| 6277 | |
| 6278 | str = xmalloc(2); |
| 6279 | str[0] = c; |
| 6280 | str[1] = '\0'; |
| 6281 | |
| 6282 | str2 = xstrdup(str); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6283 | bc_program_addFunc(str2, &idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6284 | |
| 6285 | if (idx != len + BC_PROG_REQ_FUNCS) { |
| 6286 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6287 | for (idx = 0; idx < G.prog.strs.len; ++idx) { |
| 6288 | if (!strcmp(*((char **) bc_vec_item(&G.prog.strs, idx)), str)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6289 | len = idx; |
| 6290 | break; |
| 6291 | } |
| 6292 | } |
| 6293 | |
| 6294 | free(str); |
| 6295 | } |
| 6296 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6297 | bc_vec_push(&G.prog.strs, &str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6298 | |
| 6299 | res.t = BC_RESULT_STR; |
| 6300 | res.d.id.idx = len; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6301 | bc_vec_pop(&G.prog.results); |
| 6302 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6303 | |
| 6304 | return BC_STATUS_SUCCESS; |
| 6305 | |
| 6306 | num_err: |
| 6307 | bc_num_free(&n); |
| 6308 | return s; |
| 6309 | } |
| 6310 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6311 | static BcStatus bc_program_printStream(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6312 | { |
| 6313 | BcStatus s; |
| 6314 | BcResult *r; |
| 6315 | BcNum *n = NULL; |
| 6316 | size_t idx; |
| 6317 | char *str; |
| 6318 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6319 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6320 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6321 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6322 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6323 | s = bc_program_num(r, &n, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6324 | if (s) return s; |
| 6325 | |
| 6326 | if (BC_PROG_NUM(r, n)) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6327 | s = bc_num_stream(n, &G.prog.strmb, &G.prog.nchars, G.prog.len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6328 | else { |
| 6329 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6330 | str = *((char **) bc_vec_item(&G.prog.strs, idx)); |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 6331 | printf("%s", str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6332 | } |
| 6333 | |
| 6334 | return s; |
| 6335 | } |
| 6336 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6337 | static BcStatus bc_program_nquit(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6338 | { |
| 6339 | BcStatus s; |
| 6340 | BcResult *opnd; |
| 6341 | BcNum *num = NULL; |
| 6342 | unsigned long val; |
| 6343 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6344 | s = bc_program_prep(&opnd, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6345 | if (s) return s; |
| 6346 | s = bc_num_ulong(num, &val); |
| 6347 | if (s) return s; |
| 6348 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6349 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6350 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6351 | if (G.prog.stack.len < val) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6352 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 6353 | if (G.prog.stack.len == val) |
| 6354 | quit(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6355 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6356 | bc_vec_npop(&G.prog.stack, val); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6357 | |
| 6358 | return s; |
| 6359 | } |
| 6360 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6361 | static BcStatus bc_program_execStr(char *code, size_t *bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6362 | bool cond) |
| 6363 | { |
| 6364 | BcStatus s = BC_STATUS_SUCCESS; |
| 6365 | BcResult *r; |
| 6366 | char **str; |
| 6367 | BcFunc *f; |
| 6368 | BcParse prs; |
| 6369 | BcInstPtr ip; |
| 6370 | size_t fidx, sidx; |
| 6371 | BcNum *n; |
| 6372 | bool exec; |
| 6373 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6374 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6375 | return bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6376 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6377 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6378 | |
| 6379 | if (cond) { |
| 6380 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6381 | char *name, *then_name = bc_program_name(code, bgn), *else_name = NULL; |
| 6382 | |
| 6383 | if (code[*bgn] == BC_PARSE_STREND) |
| 6384 | (*bgn) += 1; |
| 6385 | else |
| 6386 | else_name = bc_program_name(code, bgn); |
| 6387 | |
| 6388 | exec = r->d.n.len != 0; |
| 6389 | |
| 6390 | if (exec) |
| 6391 | name = then_name; |
| 6392 | else if (else_name != NULL) { |
| 6393 | exec = true; |
| 6394 | name = else_name; |
| 6395 | } |
| 6396 | |
| 6397 | if (exec) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6398 | BcVec *v; |
| 6399 | v = bc_program_search(name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6400 | n = bc_vec_top(v); |
| 6401 | } |
| 6402 | |
| 6403 | free(then_name); |
| 6404 | free(else_name); |
| 6405 | |
| 6406 | if (!exec) goto exit; |
| 6407 | if (!BC_PROG_STR(n)) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6408 | s = bc_error_variable_is_wrong_type(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6409 | goto exit; |
| 6410 | } |
| 6411 | |
| 6412 | sidx = n->rdx; |
| 6413 | } |
| 6414 | else { |
| 6415 | |
| 6416 | if (r->t == BC_RESULT_STR) |
| 6417 | sidx = r->d.id.idx; |
| 6418 | else if (r->t == BC_RESULT_VAR) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6419 | s = bc_program_num(r, &n, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6420 | if (s || !BC_PROG_STR(n)) goto exit; |
| 6421 | sidx = n->rdx; |
| 6422 | } |
| 6423 | else |
| 6424 | goto exit; |
| 6425 | } |
| 6426 | |
| 6427 | fidx = sidx + BC_PROG_REQ_FUNCS; |
| 6428 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6429 | str = bc_vec_item(&G.prog.strs, sidx); |
| 6430 | f = bc_vec_item(&G.prog.fns, fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6431 | |
| 6432 | if (f->code.len == 0) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6433 | common_parse_init(&prs, fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6434 | s = bc_parse_text(&prs, *str); |
| 6435 | if (s) goto err; |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 6436 | s = common_parse_expr(&prs, BC_PARSE_NOCALL); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6437 | if (s) goto err; |
| 6438 | |
| 6439 | if (prs.l.t.t != BC_LEX_EOF) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6440 | s = bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6441 | goto err; |
| 6442 | } |
| 6443 | |
| 6444 | bc_parse_free(&prs); |
| 6445 | } |
| 6446 | |
| 6447 | ip.idx = 0; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6448 | ip.len = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6449 | ip.func = fidx; |
| 6450 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6451 | bc_vec_pop(&G.prog.results); |
| 6452 | bc_vec_push(&G.prog.stack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6453 | |
| 6454 | return BC_STATUS_SUCCESS; |
| 6455 | |
| 6456 | err: |
| 6457 | bc_parse_free(&prs); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6458 | f = bc_vec_item(&G.prog.fns, fidx); |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 6459 | bc_vec_pop_all(&f->code); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6460 | exit: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6461 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6462 | return s; |
| 6463 | } |
| 6464 | #endif // ENABLE_DC |
| 6465 | |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6466 | static void bc_program_pushGlobal(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6467 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6468 | BcResult res; |
| 6469 | unsigned long val; |
| 6470 | |
| 6471 | res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE; |
| 6472 | if (inst == BC_INST_IBASE) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6473 | val = (unsigned long) G.prog.ib_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6474 | else if (inst == BC_INST_SCALE) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6475 | val = (unsigned long) G.prog.scale; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6476 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6477 | val = (unsigned long) G.prog.ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6478 | |
| 6479 | bc_num_init(&res.d.n, BC_NUM_DEF_SIZE); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6480 | bc_num_ulong2num(&res.d.n, val); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6481 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6482 | } |
| 6483 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6484 | static void bc_program_addFunc(char *name, size_t *idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6485 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6486 | BcId entry, *entry_ptr; |
| 6487 | BcFunc f; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 6488 | int inserted; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6489 | |
| 6490 | entry.name = name; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6491 | entry.idx = G.prog.fns.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6492 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 6493 | inserted = bc_map_insert(&G.prog.fn_map, &entry, idx); |
| 6494 | if (!inserted) free(name); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6495 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6496 | entry_ptr = bc_vec_item(&G.prog.fn_map, *idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6497 | *idx = entry_ptr->idx; |
| 6498 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 6499 | if (!inserted) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6500 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6501 | BcFunc *func = bc_vec_item(&G.prog.fns, entry_ptr->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6502 | |
| 6503 | // We need to reset these, so the function can be repopulated. |
| 6504 | func->nparams = 0; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 6505 | bc_vec_pop_all(&func->autos); |
| 6506 | bc_vec_pop_all(&func->code); |
| 6507 | bc_vec_pop_all(&func->labels); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6508 | } |
| 6509 | else { |
| 6510 | bc_func_init(&f); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6511 | bc_vec_push(&G.prog.fns, &f); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6512 | } |
| 6513 | } |
| 6514 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6515 | // Called when parsing or execution detects a failure, |
| 6516 | // resets execution structures. |
| 6517 | static void bc_program_reset(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6518 | { |
| 6519 | BcFunc *f; |
| 6520 | BcInstPtr *ip; |
| 6521 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6522 | bc_vec_npop(&G.prog.stack, G.prog.stack.len - 1); |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 6523 | bc_vec_pop_all(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6524 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6525 | f = bc_vec_item(&G.prog.fns, 0); |
| 6526 | ip = bc_vec_top(&G.prog.stack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6527 | ip->idx = f->code.len; |
| 6528 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6529 | // If !tty, no need to check for ^C: we don't have ^C handler, |
| 6530 | // we would be killed by a signal and won't reach this place |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6531 | } |
| 6532 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6533 | static BcStatus bc_program_exec(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6534 | { |
| 6535 | BcStatus s = BC_STATUS_SUCCESS; |
| 6536 | size_t idx; |
| 6537 | BcResult r, *ptr; |
| 6538 | BcNum *num; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6539 | BcInstPtr *ip = bc_vec_top(&G.prog.stack); |
| 6540 | BcFunc *func = bc_vec_item(&G.prog.fns, ip->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6541 | char *code = func->code.v; |
| 6542 | bool cond = false; |
| 6543 | |
| 6544 | while (!s && ip->idx < func->code.len) { |
| 6545 | |
| 6546 | char inst = code[(ip->idx)++]; |
| 6547 | |
| 6548 | switch (inst) { |
| 6549 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6550 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6551 | case BC_INST_JUMP_ZERO: |
| 6552 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6553 | s = bc_program_prep(&ptr, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6554 | if (s) return s; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6555 | cond = !bc_num_cmp(num, &G.prog.zero); |
| 6556 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6557 | } |
| 6558 | // Fallthrough. |
| 6559 | case BC_INST_JUMP: |
| 6560 | { |
| 6561 | size_t *addr; |
| 6562 | idx = bc_program_index(code, &ip->idx); |
| 6563 | addr = bc_vec_item(&func->labels, idx); |
| 6564 | if (inst == BC_INST_JUMP || cond) ip->idx = *addr; |
| 6565 | break; |
| 6566 | } |
| 6567 | |
| 6568 | case BC_INST_CALL: |
| 6569 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6570 | s = bc_program_call(code, &ip->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6571 | break; |
| 6572 | } |
| 6573 | |
| 6574 | case BC_INST_INC_PRE: |
| 6575 | case BC_INST_DEC_PRE: |
| 6576 | case BC_INST_INC_POST: |
| 6577 | case BC_INST_DEC_POST: |
| 6578 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6579 | s = bc_program_incdec(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6580 | break; |
| 6581 | } |
| 6582 | |
| 6583 | case BC_INST_HALT: |
| 6584 | { |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 6585 | quit(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6586 | break; |
| 6587 | } |
| 6588 | |
| 6589 | case BC_INST_RET: |
| 6590 | case BC_INST_RET0: |
| 6591 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6592 | s = bc_program_return(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6593 | break; |
| 6594 | } |
| 6595 | |
| 6596 | case BC_INST_BOOL_OR: |
| 6597 | case BC_INST_BOOL_AND: |
| 6598 | #endif // ENABLE_BC |
| 6599 | case BC_INST_REL_EQ: |
| 6600 | case BC_INST_REL_LE: |
| 6601 | case BC_INST_REL_GE: |
| 6602 | case BC_INST_REL_NE: |
| 6603 | case BC_INST_REL_LT: |
| 6604 | case BC_INST_REL_GT: |
| 6605 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6606 | s = bc_program_logical(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6607 | break; |
| 6608 | } |
| 6609 | |
| 6610 | case BC_INST_READ: |
| 6611 | { |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6612 | s = bc_program_read(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6613 | break; |
| 6614 | } |
| 6615 | |
| 6616 | case BC_INST_VAR: |
| 6617 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6618 | s = bc_program_pushVar(code, &ip->idx, false, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6619 | break; |
| 6620 | } |
| 6621 | |
| 6622 | case BC_INST_ARRAY_ELEM: |
| 6623 | case BC_INST_ARRAY: |
| 6624 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6625 | s = bc_program_pushArray(code, &ip->idx, inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6626 | break; |
| 6627 | } |
| 6628 | |
| 6629 | case BC_INST_LAST: |
| 6630 | { |
| 6631 | r.t = BC_RESULT_LAST; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6632 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6633 | break; |
| 6634 | } |
| 6635 | |
| 6636 | case BC_INST_IBASE: |
| 6637 | case BC_INST_SCALE: |
| 6638 | case BC_INST_OBASE: |
| 6639 | { |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6640 | bc_program_pushGlobal(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6641 | break; |
| 6642 | } |
| 6643 | |
| 6644 | case BC_INST_SCALE_FUNC: |
| 6645 | case BC_INST_LENGTH: |
| 6646 | case BC_INST_SQRT: |
| 6647 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6648 | s = bc_program_builtin(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6649 | break; |
| 6650 | } |
| 6651 | |
| 6652 | case BC_INST_NUM: |
| 6653 | { |
| 6654 | r.t = BC_RESULT_CONSTANT; |
| 6655 | r.d.id.idx = bc_program_index(code, &ip->idx); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6656 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6657 | break; |
| 6658 | } |
| 6659 | |
| 6660 | case BC_INST_POP: |
| 6661 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6662 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6663 | s = bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6664 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6665 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6666 | break; |
| 6667 | } |
| 6668 | |
| 6669 | case BC_INST_POP_EXEC: |
| 6670 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6671 | bc_vec_pop(&G.prog.stack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6672 | break; |
| 6673 | } |
| 6674 | |
| 6675 | case BC_INST_PRINT: |
| 6676 | case BC_INST_PRINT_POP: |
| 6677 | case BC_INST_PRINT_STR: |
| 6678 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6679 | s = bc_program_print(inst, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6680 | break; |
| 6681 | } |
| 6682 | |
| 6683 | case BC_INST_STR: |
| 6684 | { |
| 6685 | r.t = BC_RESULT_STR; |
| 6686 | r.d.id.idx = bc_program_index(code, &ip->idx); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6687 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6688 | break; |
| 6689 | } |
| 6690 | |
| 6691 | case BC_INST_POWER: |
| 6692 | case BC_INST_MULTIPLY: |
| 6693 | case BC_INST_DIVIDE: |
| 6694 | case BC_INST_MODULUS: |
| 6695 | case BC_INST_PLUS: |
| 6696 | case BC_INST_MINUS: |
| 6697 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6698 | s = bc_program_op(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6699 | break; |
| 6700 | } |
| 6701 | |
| 6702 | case BC_INST_BOOL_NOT: |
| 6703 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6704 | s = bc_program_prep(&ptr, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6705 | if (s) return s; |
| 6706 | |
| 6707 | bc_num_init(&r.d.n, BC_NUM_DEF_SIZE); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6708 | (!bc_num_cmp(num, &G.prog.zero) ? bc_num_one : bc_num_zero)(&r.d.n); |
| 6709 | bc_program_retire(&r, BC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6710 | |
| 6711 | break; |
| 6712 | } |
| 6713 | |
| 6714 | case BC_INST_NEG: |
| 6715 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6716 | s = bc_program_negate(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6717 | break; |
| 6718 | } |
| 6719 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6720 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6721 | case BC_INST_ASSIGN_POWER: |
| 6722 | case BC_INST_ASSIGN_MULTIPLY: |
| 6723 | case BC_INST_ASSIGN_DIVIDE: |
| 6724 | case BC_INST_ASSIGN_MODULUS: |
| 6725 | case BC_INST_ASSIGN_PLUS: |
| 6726 | case BC_INST_ASSIGN_MINUS: |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6727 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6728 | case BC_INST_ASSIGN: |
| 6729 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6730 | s = bc_program_assign(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6731 | break; |
| 6732 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6733 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6734 | case BC_INST_MODEXP: |
| 6735 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6736 | s = bc_program_modexp(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6737 | break; |
| 6738 | } |
| 6739 | |
| 6740 | case BC_INST_DIVMOD: |
| 6741 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6742 | s = bc_program_divmod(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6743 | break; |
| 6744 | } |
| 6745 | |
| 6746 | case BC_INST_EXECUTE: |
| 6747 | case BC_INST_EXEC_COND: |
| 6748 | { |
| 6749 | cond = inst == BC_INST_EXEC_COND; |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6750 | s = bc_program_execStr(code, &ip->idx, cond); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6751 | break; |
| 6752 | } |
| 6753 | |
| 6754 | case BC_INST_PRINT_STACK: |
| 6755 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6756 | for (idx = 0; !s && idx < G.prog.results.len; ++idx) |
| 6757 | s = bc_program_print(BC_INST_PRINT, idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6758 | break; |
| 6759 | } |
| 6760 | |
| 6761 | case BC_INST_CLEAR_STACK: |
| 6762 | { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 6763 | bc_vec_pop_all(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6764 | break; |
| 6765 | } |
| 6766 | |
| 6767 | case BC_INST_STACK_LEN: |
| 6768 | { |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6769 | bc_program_stackLen(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6770 | break; |
| 6771 | } |
| 6772 | |
| 6773 | case BC_INST_DUPLICATE: |
| 6774 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6775 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6776 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6777 | ptr = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6778 | bc_result_copy(&r, ptr); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6779 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6780 | break; |
| 6781 | } |
| 6782 | |
| 6783 | case BC_INST_SWAP: |
| 6784 | { |
| 6785 | BcResult *ptr2; |
| 6786 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6787 | if (!BC_PROG_STACK(&G.prog.results, 2)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6788 | return bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6789 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6790 | ptr = bc_vec_item_rev(&G.prog.results, 0); |
| 6791 | ptr2 = bc_vec_item_rev(&G.prog.results, 1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6792 | memcpy(&r, ptr, sizeof(BcResult)); |
| 6793 | memcpy(ptr, ptr2, sizeof(BcResult)); |
| 6794 | memcpy(ptr2, &r, sizeof(BcResult)); |
| 6795 | |
| 6796 | break; |
| 6797 | } |
| 6798 | |
| 6799 | case BC_INST_ASCIIFY: |
| 6800 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6801 | s = bc_program_asciify(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6802 | break; |
| 6803 | } |
| 6804 | |
| 6805 | case BC_INST_PRINT_STREAM: |
| 6806 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6807 | s = bc_program_printStream(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6808 | break; |
| 6809 | } |
| 6810 | |
| 6811 | case BC_INST_LOAD: |
| 6812 | case BC_INST_PUSH_VAR: |
| 6813 | { |
| 6814 | bool copy = inst == BC_INST_LOAD; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6815 | s = bc_program_pushVar(code, &ip->idx, true, copy); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6816 | break; |
| 6817 | } |
| 6818 | |
| 6819 | case BC_INST_PUSH_TO_VAR: |
| 6820 | { |
| 6821 | char *name = bc_program_name(code, &ip->idx); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6822 | s = bc_program_copyToVar(name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6823 | free(name); |
| 6824 | break; |
| 6825 | } |
| 6826 | |
| 6827 | case BC_INST_QUIT: |
| 6828 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6829 | if (G.prog.stack.len <= 2) |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 6830 | quit(); |
| 6831 | bc_vec_npop(&G.prog.stack, 2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6832 | break; |
| 6833 | } |
| 6834 | |
| 6835 | case BC_INST_NQUIT: |
| 6836 | { |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6837 | s = bc_program_nquit(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6838 | break; |
| 6839 | } |
| 6840 | #endif // ENABLE_DC |
| 6841 | } |
| 6842 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6843 | if (s || G_interrupt) { |
| 6844 | bc_program_reset(); |
| 6845 | break; |
| 6846 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6847 | |
| 6848 | // If the stack has changed, pointers may be invalid. |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6849 | ip = bc_vec_top(&G.prog.stack); |
| 6850 | func = bc_vec_item(&G.prog.fns, ip->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6851 | code = func->code.v; |
| 6852 | } |
| 6853 | |
| 6854 | return s; |
| 6855 | } |
| 6856 | |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 6857 | static void bc_vm_info(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6858 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 6859 | printf("%s "BB_VER"\n" |
| 6860 | "Copyright (c) 2018 Gavin D. Howard and contributors\n" |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 6861 | "Report bugs at: https://github.com/gavinhoward/bc\n" |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 6862 | "This is free software with ABSOLUTELY NO WARRANTY\n" |
| 6863 | , applet_name); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6864 | } |
| 6865 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6866 | #if ENABLE_BC |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6867 | static void bc_vm_envArgs(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6868 | { |
Denys Vlasenko | 1f67e93 | 2018-12-03 00:08:59 +0100 | [diff] [blame] | 6869 | static const char* const bc_args_env_name = "BC_ENV_ARGS"; |
| 6870 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6871 | BcVec v; |
| 6872 | char *env_args = getenv(bc_args_env_name), *buf; |
| 6873 | |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 6874 | if (!env_args) return; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6875 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6876 | G.env_args = xstrdup(env_args); |
| 6877 | buf = G.env_args; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6878 | |
| 6879 | bc_vec_init(&v, sizeof(char *), NULL); |
| 6880 | bc_vec_push(&v, &bc_args_env_name); |
| 6881 | |
| 6882 | while (*buf != 0) { |
| 6883 | if (!isspace(*buf)) { |
| 6884 | bc_vec_push(&v, &buf); |
| 6885 | while (*buf != 0 && !isspace(*buf)) ++buf; |
| 6886 | if (*buf != 0) (*(buf++)) = '\0'; |
| 6887 | } |
| 6888 | else |
| 6889 | ++buf; |
| 6890 | } |
| 6891 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6892 | bc_args((int) v.len, (char **) v.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6893 | |
| 6894 | bc_vec_free(&v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6895 | } |
| 6896 | #endif // ENABLE_BC |
| 6897 | |
| 6898 | static size_t bc_vm_envLen(const char *var) |
| 6899 | { |
| 6900 | char *lenv = getenv(var); |
| 6901 | size_t i, len = BC_NUM_PRINT_WIDTH; |
| 6902 | int num; |
| 6903 | |
| 6904 | if (!lenv) return len; |
| 6905 | |
| 6906 | len = strlen(lenv); |
| 6907 | |
| 6908 | for (num = 1, i = 0; num && i < len; ++i) num = isdigit(lenv[i]); |
| 6909 | if (num) { |
| 6910 | len = (size_t) atoi(lenv) - 1; |
| 6911 | if (len < 2 || len >= INT32_MAX) len = BC_NUM_PRINT_WIDTH; |
| 6912 | } |
| 6913 | else |
| 6914 | len = BC_NUM_PRINT_WIDTH; |
| 6915 | |
| 6916 | return len; |
| 6917 | } |
| 6918 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6919 | static BcStatus bc_vm_process(const char *text) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6920 | { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6921 | BcStatus s = bc_parse_text(&G.prs, text); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6922 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6923 | if (s) return s; |
| 6924 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6925 | while (G.prs.l.t.t != BC_LEX_EOF) { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6926 | s = G.prs.parse(&G.prs); |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 6927 | if (s) return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6928 | } |
| 6929 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6930 | if (BC_PARSE_CAN_EXEC(&G.prs)) { |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6931 | s = bc_program_exec(); |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 6932 | fflush_and_check(); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 6933 | if (s) |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6934 | bc_program_reset(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6935 | } |
| 6936 | |
| 6937 | return s; |
| 6938 | } |
| 6939 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6940 | static BcStatus bc_vm_file(const char *file) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6941 | { |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 6942 | const char *sv_file; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6943 | char *data; |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 6944 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6945 | BcFunc *main_func; |
| 6946 | BcInstPtr *ip; |
| 6947 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6948 | data = bc_read_file(file); |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6949 | if (!data) return bc_error_fmt("file '%s' is not text", file); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6950 | |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 6951 | sv_file = G.prog.file; |
| 6952 | G.prog.file = file; |
| 6953 | bc_lex_file(&G.prs.l); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6954 | s = bc_vm_process(data); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6955 | if (s) goto err; |
| 6956 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6957 | main_func = bc_vec_item(&G.prog.fns, BC_PROG_MAIN); |
| 6958 | ip = bc_vec_item(&G.prog.stack, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6959 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6960 | if (main_func->code.len < ip->idx) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6961 | s = bc_error_fmt("file '%s' is not executable", file); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6962 | |
| 6963 | err: |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 6964 | G.prog.file = sv_file; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6965 | free(data); |
| 6966 | return s; |
| 6967 | } |
| 6968 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6969 | static BcStatus bc_vm_stdin(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6970 | { |
Denys Vlasenko | a0c421c | 2018-12-02 20:16:52 +0100 | [diff] [blame] | 6971 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6972 | BcVec buf, buffer; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6973 | size_t len, i, str = 0; |
Denys Vlasenko | a0c421c | 2018-12-02 20:16:52 +0100 | [diff] [blame] | 6974 | bool comment = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6975 | |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 6976 | G.prog.file = NULL; |
| 6977 | bc_lex_file(&G.prs.l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6978 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 6979 | bc_char_vec_init(&buffer); |
| 6980 | bc_char_vec_init(&buf); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 6981 | bc_vec_pushZeroByte(&buffer); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6982 | |
| 6983 | // This loop is complex because the vm tries not to send any lines that end |
| 6984 | // with a backslash to the parser. The reason for that is because the parser |
| 6985 | // treats a backslash+newline combo as whitespace, per the bc spec. In that |
| 6986 | // case, and for strings and comments, the parser will expect more stuff. |
Denys Vlasenko | 657d6bb | 2018-12-05 20:25:03 +0100 | [diff] [blame] | 6987 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 6988 | while (!G.eof && (s = bc_read_line(&buf, ">>> ")) == BC_STATUS_SUCCESS) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6989 | |
| 6990 | char *string = buf.v; |
| 6991 | |
| 6992 | len = buf.len - 1; |
| 6993 | |
| 6994 | if (len == 1) { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6995 | if (str && buf.v[0] == G.send) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6996 | str -= 1; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6997 | else if (buf.v[0] == G.sbgn) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6998 | str += 1; |
| 6999 | } |
| 7000 | else if (len > 1 || comment) { |
| 7001 | |
| 7002 | for (i = 0; i < len; ++i) { |
| 7003 | |
Denys Vlasenko | a0c421c | 2018-12-02 20:16:52 +0100 | [diff] [blame] | 7004 | bool notend = len > i + 1; |
| 7005 | char c = string[i]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7006 | |
| 7007 | if (i - 1 > len || string[i - 1] != '\\') { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7008 | if (G.sbgn == G.send) |
| 7009 | str ^= c == G.sbgn; |
| 7010 | else if (c == G.send) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7011 | str -= 1; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7012 | else if (c == G.sbgn) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7013 | str += 1; |
| 7014 | } |
| 7015 | |
| 7016 | if (c == '/' && notend && !comment && string[i + 1] == '*') { |
| 7017 | comment = true; |
| 7018 | break; |
| 7019 | } |
| 7020 | else if (c == '*' && notend && comment && string[i + 1] == '/') |
| 7021 | comment = false; |
| 7022 | } |
| 7023 | |
| 7024 | if (str || comment || string[len - 2] == '\\') { |
| 7025 | bc_vec_concat(&buffer, buf.v); |
| 7026 | continue; |
| 7027 | } |
| 7028 | } |
| 7029 | |
| 7030 | bc_vec_concat(&buffer, buf.v); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7031 | s = bc_vm_process(buffer.v); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7032 | if (s) { |
| 7033 | fflush_and_check(); |
| 7034 | fputs("ready for more input\n", stderr); |
| 7035 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7036 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 7037 | bc_vec_pop_all(&buffer); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7038 | } |
| 7039 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 7040 | if (str) { |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7041 | s = bc_error("string end could not be found"); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 7042 | } |
| 7043 | else if (comment) { |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7044 | s = bc_error("comment end could not be found"); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 7045 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7046 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7047 | bc_vec_free(&buf); |
| 7048 | bc_vec_free(&buffer); |
| 7049 | return s; |
| 7050 | } |
| 7051 | |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7052 | #if ENABLE_BC |
| 7053 | static const char bc_lib[] = { |
| 7054 | "scale=20" |
| 7055 | "\n" "define e(x){" |
| 7056 | "\n" "auto b,s,n,r,d,i,p,f,v" |
| 7057 | "\n" "b=ibase" |
| 7058 | "\n" "ibase=A" |
| 7059 | "\n" "if(x<0){" |
| 7060 | "\n" "n=1" |
| 7061 | "\n" "x=-x" |
| 7062 | "\n" "}" |
| 7063 | "\n" "s=scale" |
| 7064 | "\n" "r=6+s+0.44*x" |
| 7065 | "\n" "scale=scale(x)+1" |
| 7066 | "\n" "while(x>1){" |
| 7067 | "\n" "d+=1" |
| 7068 | "\n" "x/=2" |
| 7069 | "\n" "scale+=1" |
| 7070 | "\n" "}" |
| 7071 | "\n" "scale=r" |
| 7072 | "\n" "r=x+1" |
| 7073 | "\n" "p=x" |
| 7074 | "\n" "f=v=1" |
| 7075 | "\n" "for(i=2;v!=0;++i){" |
| 7076 | "\n" "p*=x" |
| 7077 | "\n" "f*=i" |
| 7078 | "\n" "v=p/f" |
| 7079 | "\n" "r+=v" |
| 7080 | "\n" "}" |
| 7081 | "\n" "while((d--)!=0)r*=r" |
| 7082 | "\n" "scale=s" |
| 7083 | "\n" "ibase=b" |
| 7084 | "\n" "if(n!=0)return(1/r)" |
| 7085 | "\n" "return(r/1)" |
| 7086 | "\n" "}" |
| 7087 | "\n" "define l(x){" |
| 7088 | "\n" "auto b,s,r,p,a,q,i,v" |
| 7089 | "\n" "b=ibase" |
| 7090 | "\n" "ibase=A" |
| 7091 | "\n" "if(x<=0){" |
| 7092 | "\n" "r=(1-10^scale)/1" |
| 7093 | "\n" "ibase=b" |
| 7094 | "\n" "return(r)" |
| 7095 | "\n" "}" |
| 7096 | "\n" "s=scale" |
| 7097 | "\n" "scale+=6" |
| 7098 | "\n" "p=2" |
| 7099 | "\n" "while(x>=2){" |
| 7100 | "\n" "p*=2" |
| 7101 | "\n" "x=sqrt(x)" |
| 7102 | "\n" "}" |
| 7103 | "\n" "while(x<=0.5){" |
| 7104 | "\n" "p*=2" |
| 7105 | "\n" "x=sqrt(x)" |
| 7106 | "\n" "}" |
| 7107 | "\n" "r=a=(x-1)/(x+1)" |
| 7108 | "\n" "q=a*a" |
| 7109 | "\n" "v=1" |
| 7110 | "\n" "for(i=3;v!=0;i+=2){" |
| 7111 | "\n" "a*=q" |
| 7112 | "\n" "v=a/i" |
| 7113 | "\n" "r+=v" |
| 7114 | "\n" "}" |
| 7115 | "\n" "r*=p" |
| 7116 | "\n" "scale=s" |
| 7117 | "\n" "ibase=b" |
| 7118 | "\n" "return(r/1)" |
| 7119 | "\n" "}" |
| 7120 | "\n" "define s(x){" |
| 7121 | "\n" "auto b,s,r,n,a,q,i" |
| 7122 | "\n" "b=ibase" |
| 7123 | "\n" "ibase=A" |
| 7124 | "\n" "s=scale" |
| 7125 | "\n" "scale=1.1*s+2" |
| 7126 | "\n" "a=a(1)" |
| 7127 | "\n" "if(x<0){" |
| 7128 | "\n" "n=1" |
| 7129 | "\n" "x=-x" |
| 7130 | "\n" "}" |
| 7131 | "\n" "scale=0" |
| 7132 | "\n" "q=(x/a+2)/4" |
| 7133 | "\n" "x=x-4*q*a" |
| 7134 | "\n" "if(q%2!=0)x=-x" |
| 7135 | "\n" "scale=s+2" |
| 7136 | "\n" "r=a=x" |
| 7137 | "\n" "q=-x*x" |
| 7138 | "\n" "for(i=3;a!=0;i+=2){" |
| 7139 | "\n" "a*=q/(i*(i-1))" |
| 7140 | "\n" "r+=a" |
| 7141 | "\n" "}" |
| 7142 | "\n" "scale=s" |
| 7143 | "\n" "ibase=b" |
| 7144 | "\n" "if(n!=0)return(-r/1)" |
| 7145 | "\n" "return(r/1)" |
| 7146 | "\n" "}" |
| 7147 | "\n" "define c(x){" |
| 7148 | "\n" "auto b,s" |
| 7149 | "\n" "b=ibase" |
| 7150 | "\n" "ibase=A" |
| 7151 | "\n" "s=scale" |
| 7152 | "\n" "scale*=1.2" |
| 7153 | "\n" "x=s(2*a(1)+x)" |
| 7154 | "\n" "scale=s" |
| 7155 | "\n" "ibase=b" |
| 7156 | "\n" "return(x/1)" |
| 7157 | "\n" "}" |
| 7158 | "\n" "define a(x){" |
| 7159 | "\n" "auto b,s,r,n,a,m,t,f,i,u" |
| 7160 | "\n" "b=ibase" |
| 7161 | "\n" "ibase=A" |
| 7162 | "\n" "n=1" |
| 7163 | "\n" "if(x<0){" |
| 7164 | "\n" "n=-1" |
| 7165 | "\n" "x=-x" |
| 7166 | "\n" "}" |
| 7167 | "\n" "if(x==1){" |
| 7168 | "\n" "if(scale<65){" |
| 7169 | "\n" "return(.7853981633974483096156608458198757210492923498437764552437361480/n)" |
| 7170 | "\n" "}" |
| 7171 | "\n" "}" |
| 7172 | "\n" "if(x==.2){" |
| 7173 | "\n" "if(scale<65){" |
| 7174 | "\n" "return(.1973955598498807583700497651947902934475851037878521015176889402/n)" |
| 7175 | "\n" "}" |
| 7176 | "\n" "}" |
| 7177 | "\n" "s=scale" |
| 7178 | "\n" "if(x>.2){" |
| 7179 | "\n" "scale+=5" |
| 7180 | "\n" "a=a(.2)" |
| 7181 | "\n" "}" |
| 7182 | "\n" "scale=s+3" |
| 7183 | "\n" "while(x>.2){" |
| 7184 | "\n" "m+=1" |
| 7185 | "\n" "x=(x-.2)/(1+.2*x)" |
| 7186 | "\n" "}" |
| 7187 | "\n" "r=u=x" |
| 7188 | "\n" "f=-x*x" |
| 7189 | "\n" "t=1" |
| 7190 | "\n" "for(i=3;t!=0;i+=2){" |
| 7191 | "\n" "u*=f" |
| 7192 | "\n" "t=u/i" |
| 7193 | "\n" "r+=t" |
| 7194 | "\n" "}" |
| 7195 | "\n" "scale=s" |
| 7196 | "\n" "ibase=b" |
| 7197 | "\n" "return((m*a+r)/n)" |
| 7198 | "\n" "}" |
| 7199 | "\n" "define j(n,x){" |
| 7200 | "\n" "auto b,s,o,a,i,v,f" |
| 7201 | "\n" "b=ibase" |
| 7202 | "\n" "ibase=A" |
| 7203 | "\n" "s=scale" |
| 7204 | "\n" "scale=0" |
| 7205 | "\n" "n/=1" |
| 7206 | "\n" "if(n<0){" |
| 7207 | "\n" "n=-n" |
| 7208 | "\n" "if(n%2==1)o=1" |
| 7209 | "\n" "}" |
| 7210 | "\n" "a=1" |
| 7211 | "\n" "for(i=2;i<=n;++i)a*=i" |
| 7212 | "\n" "scale=1.5*s" |
| 7213 | "\n" "a=(x^n)/2^n/a" |
| 7214 | "\n" "r=v=1" |
| 7215 | "\n" "f=-x*x/4" |
| 7216 | "\n" "scale=scale+length(a)-scale(a)" |
| 7217 | "\n" "for(i=1;v!=0;++i){" |
| 7218 | "\n" "v=v*f/i/(n+i)" |
| 7219 | "\n" "r+=v" |
| 7220 | "\n" "}" |
| 7221 | "\n" "scale=s" |
| 7222 | "\n" "ibase=b" |
| 7223 | "\n" "if(o!=0)a=-a" |
| 7224 | "\n" "return(a*r/1)" |
| 7225 | "\n" "}" |
| 7226 | }; |
| 7227 | #endif // ENABLE_BC |
| 7228 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7229 | static BcStatus bc_vm_exec(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7230 | { |
| 7231 | BcStatus s = BC_STATUS_SUCCESS; |
| 7232 | size_t i; |
| 7233 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7234 | #if ENABLE_BC |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 7235 | if (option_mask32 & BC_FLAG_L) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7236 | |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7237 | // We know that internal library is not buggy, |
| 7238 | // thus error checking is normally disabled. |
| 7239 | # define DEBUG_LIB 0 |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7240 | bc_lex_file(&G.prs.l); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7241 | s = bc_parse_text(&G.prs, bc_lib); |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7242 | if (DEBUG_LIB && s) return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7243 | |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7244 | while (G.prs.l.t.t != BC_LEX_EOF) { |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7245 | s = G.prs.parse(&G.prs); |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7246 | if (DEBUG_LIB && s) return s; |
| 7247 | } |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7248 | s = bc_program_exec(); |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7249 | if (DEBUG_LIB && s) return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7250 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7251 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7252 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7253 | for (i = 0; !s && i < G.files.len; ++i) |
| 7254 | s = bc_vm_file(*((char **) bc_vec_item(&G.files, i))); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7255 | if (s) { |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7256 | fflush_and_check(); |
| 7257 | fputs("ready for more input\n", stderr); |
| 7258 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7259 | |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7260 | if (IS_BC || !G.files.len) |
| 7261 | s = bc_vm_stdin(); |
| 7262 | if (!s && !BC_PARSE_CAN_EXEC(&G.prs)) |
| 7263 | s = bc_vm_process(""); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7264 | |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 7265 | return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7266 | } |
| 7267 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7268 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7269 | static void bc_program_free() |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7270 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7271 | bc_num_free(&G.prog.ib); |
| 7272 | bc_num_free(&G.prog.ob); |
| 7273 | bc_num_free(&G.prog.hexb); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7274 | # if ENABLE_DC |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7275 | bc_num_free(&G.prog.strmb); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7276 | # endif |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7277 | bc_vec_free(&G.prog.fns); |
| 7278 | bc_vec_free(&G.prog.fn_map); |
| 7279 | bc_vec_free(&G.prog.vars); |
| 7280 | bc_vec_free(&G.prog.var_map); |
| 7281 | bc_vec_free(&G.prog.arrs); |
| 7282 | bc_vec_free(&G.prog.arr_map); |
| 7283 | bc_vec_free(&G.prog.strs); |
| 7284 | bc_vec_free(&G.prog.consts); |
| 7285 | bc_vec_free(&G.prog.results); |
| 7286 | bc_vec_free(&G.prog.stack); |
| 7287 | bc_num_free(&G.prog.last); |
| 7288 | bc_num_free(&G.prog.zero); |
| 7289 | bc_num_free(&G.prog.one); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7290 | } |
| 7291 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7292 | static void bc_vm_free(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7293 | { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7294 | bc_vec_free(&G.files); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7295 | bc_program_free(); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7296 | bc_parse_free(&G.prs); |
| 7297 | free(G.env_args); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7298 | } |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7299 | #endif |
| 7300 | |
| 7301 | static void bc_program_init(size_t line_len) |
| 7302 | { |
| 7303 | size_t idx; |
| 7304 | BcInstPtr ip; |
| 7305 | |
| 7306 | /* memset(&G.prog, 0, sizeof(G.prog)); - already is */ |
| 7307 | memset(&ip, 0, sizeof(BcInstPtr)); |
| 7308 | |
| 7309 | /* G.prog.nchars = G.prog.scale = 0; - already is */ |
| 7310 | G.prog.len = line_len; |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7311 | |
| 7312 | bc_num_init(&G.prog.ib, BC_NUM_DEF_SIZE); |
| 7313 | bc_num_ten(&G.prog.ib); |
| 7314 | G.prog.ib_t = 10; |
| 7315 | |
| 7316 | bc_num_init(&G.prog.ob, BC_NUM_DEF_SIZE); |
| 7317 | bc_num_ten(&G.prog.ob); |
| 7318 | G.prog.ob_t = 10; |
| 7319 | |
| 7320 | bc_num_init(&G.prog.hexb, BC_NUM_DEF_SIZE); |
| 7321 | bc_num_ten(&G.prog.hexb); |
| 7322 | G.prog.hexb.num[0] = 6; |
| 7323 | |
| 7324 | #if ENABLE_DC |
| 7325 | bc_num_init(&G.prog.strmb, BC_NUM_DEF_SIZE); |
| 7326 | bc_num_ulong2num(&G.prog.strmb, UCHAR_MAX + 1); |
| 7327 | #endif |
| 7328 | |
| 7329 | bc_num_init(&G.prog.last, BC_NUM_DEF_SIZE); |
| 7330 | bc_num_zero(&G.prog.last); |
| 7331 | |
| 7332 | bc_num_init(&G.prog.zero, BC_NUM_DEF_SIZE); |
| 7333 | bc_num_zero(&G.prog.zero); |
| 7334 | |
| 7335 | bc_num_init(&G.prog.one, BC_NUM_DEF_SIZE); |
| 7336 | bc_num_one(&G.prog.one); |
| 7337 | |
| 7338 | bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7339 | bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7340 | |
Denys Vlasenko | 1f67e93 | 2018-12-03 00:08:59 +0100 | [diff] [blame] | 7341 | bc_program_addFunc(xstrdup("(main)"), &idx); |
| 7342 | bc_program_addFunc(xstrdup("(read)"), &idx); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7343 | |
| 7344 | bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7345 | bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7346 | |
| 7347 | bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7348 | bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7349 | |
| 7350 | bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free); |
| 7351 | bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free); |
| 7352 | bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free); |
| 7353 | bc_vec_init(&G.prog.stack, sizeof(BcInstPtr), NULL); |
| 7354 | bc_vec_push(&G.prog.stack, &ip); |
| 7355 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7356 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7357 | static void bc_vm_init(const char *env_len) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7358 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7359 | size_t len = bc_vm_envLen(env_len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7360 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7361 | bc_vec_init(&G.files, sizeof(char *), NULL); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7362 | |
Denys Vlasenko | ab3c568 | 2018-12-02 16:32:36 +0100 | [diff] [blame] | 7363 | if (IS_BC) { |
Denys Vlasenko | ab3c568 | 2018-12-02 16:32:36 +0100 | [diff] [blame] | 7364 | bc_vm_envArgs(); |
| 7365 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7366 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7367 | bc_program_init(len); |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 7368 | if (IS_BC) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7369 | bc_parse_init(&G.prs, BC_PROG_MAIN); |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 7370 | } else { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7371 | dc_parse_init(&G.prs, BC_PROG_MAIN); |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 7372 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7373 | } |
| 7374 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7375 | static BcStatus bc_vm_run(int argc, char *argv[], |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7376 | const char *env_len) |
| 7377 | { |
| 7378 | BcStatus st; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7379 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7380 | bc_vm_init(env_len); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7381 | bc_args(argc, argv); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7382 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7383 | G.ttyin = isatty(0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7384 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7385 | if (G.ttyin) { |
| 7386 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7387 | // With SA_RESTART, most system calls will restart |
| 7388 | // (IOW: they won't fail with EINTR). |
| 7389 | // In particular, this means ^C won't cause |
| 7390 | // stdout to get into "error state" if SIGINT hits |
| 7391 | // within write() syscall. |
| 7392 | // The downside is that ^C while line input is taken |
| 7393 | // will only be handled after [Enter] since read() |
| 7394 | // from stdin is not interrupted by ^C either, |
| 7395 | // it restarts, thus fgetc() does not return on ^C. |
| 7396 | signal_SA_RESTART_empty_mask(SIGINT, record_signo); |
| 7397 | |
| 7398 | // Without SA_RESTART, this exhibits a bug: |
| 7399 | // "while (1) print 1" and try ^C-ing it. |
| 7400 | // Intermittently, instead of returning to input line, |
| 7401 | // you'll get "output error: Interrupted system call" |
| 7402 | // and exit. |
| 7403 | //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo); |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7404 | #endif |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 7405 | if (!(option_mask32 & BC_FLAG_Q)) |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7406 | bc_vm_info(); |
| 7407 | } |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7408 | st = bc_vm_exec(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7409 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7410 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7411 | bc_vm_free(); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7412 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7413 | return st; |
| 7414 | } |
| 7415 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7416 | #if ENABLE_BC |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7417 | int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 7418 | int bc_main(int argc, char **argv) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7419 | { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7420 | INIT_G(); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7421 | G.sbgn = G.send = '"'; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7422 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7423 | return bc_vm_run(argc, argv, "BC_LINE_LENGTH"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7424 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7425 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7426 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7427 | #if ENABLE_DC |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7428 | int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 7429 | int dc_main(int argc, char **argv) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7430 | { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7431 | INIT_G(); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7432 | G.sbgn = '['; |
| 7433 | G.send = ']'; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7434 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7435 | return bc_vm_run(argc, argv, "DC_LINE_LENGTH"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7436 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7437 | #endif |