Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
| 4 | * Copyright (c) 2018 Gavin D. Howard and contributors. |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5 | */ |
| 6 | //config:config BC |
| 7 | //config: bool "bc (45 kb; 49 kb when combined with dc)" |
| 8 | //config: default y |
| 9 | //config: help |
| 10 | //config: bc is a command-line, arbitrary-precision calculator with a |
| 11 | //config: Turing-complete language. See the GNU bc manual |
| 12 | //config: (https://www.gnu.org/software/bc/manual/bc.html) and bc spec |
| 13 | //config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html) |
| 14 | //config: for details. |
| 15 | //config: |
| 16 | //config: This bc has four differences to the GNU bc: |
| 17 | //config: |
| 18 | //config: 1) The period (.) can also be used as a shortcut for "last", as in |
| 19 | //config: the BSD bc. |
| 20 | //config: 2) Arrays are copied before being passed as arguments to |
| 21 | //config: functions. This behavior is required by the bc spec. |
| 22 | //config: 3) Arrays can be passed to the builtin "length" function to get |
| 23 | //config: the number of elements currently in the array. The following |
| 24 | //config: example prints "1": |
| 25 | //config: |
| 26 | //config: a[0] = 0 |
| 27 | //config: length(a[]) |
| 28 | //config: |
| 29 | //config: 4) The precedence of the boolean "not" operator (!) is equal to |
| 30 | //config: that of the unary minus (-), or negation, operator. This still |
| 31 | //config: allows POSIX-compliant scripts to work while somewhat |
| 32 | //config: preserving expected behavior (versus C) and making parsing |
| 33 | //config: easier. |
| 34 | //config: |
| 35 | //config: Options: |
| 36 | //config: |
| 37 | //config: -i --interactive force interactive mode |
| 38 | //config: -l --mathlib use predefined math routines: |
| 39 | //config: |
| 40 | //config: s(expr) = sine of expr in radians |
| 41 | //config: c(expr) = cosine of expr in radians |
| 42 | //config: a(expr) = arctangent of expr, returning |
| 43 | //config: radians |
| 44 | //config: l(expr) = natural log of expr |
| 45 | //config: e(expr) = raises e to the power of expr |
| 46 | //config: j(n, x) = Bessel function of integer order |
| 47 | //config: n of x |
| 48 | //config: |
| 49 | //config: -q --quiet don't print version and copyright. |
| 50 | //config: -s --standard error if any non-POSIX extensions are used. |
| 51 | //config: -w --warn warn if any non-POSIX extensions are used. |
| 52 | //config: -v --version print version and copyright and exit. |
| 53 | //config: |
| 54 | //config: Long options are only available if FEATURE_BC_LONG_OPTIONS is |
| 55 | //config: enabled. |
| 56 | //config: |
| 57 | //config:config DC |
| 58 | //config: bool "dc (38 kb; 49 kb when combined with bc)" |
| 59 | //config: default y |
| 60 | //config: help |
| 61 | //config: dc is a reverse-polish notation command-line calculator which |
| 62 | //config: supports unlimited precision arithmetic. See the FreeBSD man page |
| 63 | //config: (https://www.unix.com/man-page/FreeBSD/1/dc/) and GNU dc manual |
| 64 | //config: (https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html) |
| 65 | //config: for details. |
| 66 | //config: |
| 67 | //config: This dc has a few differences from the two above: |
| 68 | //config: |
| 69 | //config: 1) When printing a byte stream (command "P"), this bc follows what |
| 70 | //config: the FreeBSD dc does. |
| 71 | //config: 2) This dc implements the GNU extensions for divmod ("~") and |
| 72 | //config: modular exponentiation ("|"). |
| 73 | //config: 3) This dc implements all FreeBSD extensions, except for "J" and |
| 74 | //config: "M". |
| 75 | //config: 4) Like the FreeBSD dc, this dc supports extended registers. |
| 76 | //config: However, they are implemented differently. When it encounters |
| 77 | //config: whitespace where a register should be, it skips the whitespace. |
| 78 | //config: If the character following is not a lowercase letter, an error |
| 79 | //config: is issued. Otherwise, the register name is parsed by the |
| 80 | //config: following regex: |
| 81 | //config: |
| 82 | //config: [a-z][a-z0-9_]* |
| 83 | //config: |
| 84 | //config: This generally means that register names will be surrounded by |
| 85 | //config: whitespace. |
| 86 | //config: |
| 87 | //config: Examples: |
| 88 | //config: |
| 89 | //config: l idx s temp L index S temp2 < do_thing |
| 90 | //config: |
| 91 | //config: Also note that, like the FreeBSD dc, extended registers are not |
| 92 | //config: allowed unless the "-x" option is given. |
| 93 | //config: |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 94 | //config:config FEATURE_DC_SMALL |
| 95 | //config: bool "Minimal dc implementation (4.2 kb), not using bc code base" |
| 96 | //config: depends on DC && !BC |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 97 | //config: default n |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 98 | //config: |
| 99 | //config:config FEATURE_DC_LIBM |
| 100 | //config: bool "Enable power and exp functions (requires libm)" |
| 101 | //config: default y |
| 102 | //config: depends on FEATURE_DC_SMALL |
| 103 | //config: help |
| 104 | //config: Enable power and exp functions. |
| 105 | //config: NOTE: This will require libm to be present for linking. |
| 106 | //config: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 107 | //config:config FEATURE_BC_SIGNALS |
| 108 | //config: bool "Enable bc/dc signal handling" |
| 109 | //config: default y |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 110 | //config: depends on (BC || DC) && !FEATURE_DC_SMALL |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 111 | //config: help |
| 112 | //config: Enable signal handling for bc and dc. |
| 113 | //config: |
| 114 | //config:config FEATURE_BC_LONG_OPTIONS |
| 115 | //config: bool "Enable bc/dc long options" |
| 116 | //config: default y |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 117 | //config: depends on (BC || DC) && !FEATURE_DC_SMALL |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 118 | //config: help |
| 119 | //config: Enable long options for bc and dc. |
| 120 | |
| 121 | //applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 122 | //applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 123 | |
| 124 | //kbuild:lib-$(CONFIG_BC) += bc.o |
| 125 | //kbuild:lib-$(CONFIG_DC) += bc.o |
| 126 | |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 127 | //See www.gnu.org/software/bc/manual/bc.html |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 128 | //usage:#define bc_trivial_usage |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 129 | //usage: "[-sqliw] FILE..." |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 130 | //usage: |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 131 | //usage:#define bc_full_usage "\n" |
| 132 | //usage: "\nArbitrary precision calculator" |
| 133 | //usage: "\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 134 | ///////: "\n -i Interactive" - has no effect for now |
| 135 | //usage: "\n -q Quiet" |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 136 | //usage: "\n -l Load standard math library" |
| 137 | //usage: "\n -s Be POSIX compatible" |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 138 | //usage: "\n -w Warn if extensions are used" |
| 139 | ///////: "\n -v Version" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 140 | //usage: "\n" |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 141 | //usage: "\n$BC_LINE_LENGTH changes output width" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 142 | //usage: |
| 143 | //usage:#define bc_example_usage |
| 144 | //usage: "3 + 4.129\n" |
| 145 | //usage: "1903 - 2893\n" |
| 146 | //usage: "-129 * 213.28935\n" |
| 147 | //usage: "12 / -1932\n" |
| 148 | //usage: "12 % 12\n" |
| 149 | //usage: "34 ^ 189\n" |
| 150 | //usage: "scale = 13\n" |
| 151 | //usage: "ibase = 2\n" |
| 152 | //usage: "obase = A\n" |
| 153 | //usage: |
| 154 | //usage:#define dc_trivial_usage |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 155 | //usage: IF_NOT_FEATURE_DC_SMALL("[-x] ")"[-eSCRIPT]... [-fFILE]... [FILE]..." |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 156 | //usage: |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 157 | //usage:#define dc_full_usage "\n" |
| 158 | //usage: "\nTiny RPN calculator. Operations:" |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 159 | //usage: "\n+, -, *, /, %, ~, ^," IF_NOT_FEATURE_DC_SMALL(" |,") |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 160 | //usage: "\np - print top of the stack (without popping)" |
| 161 | //usage: "\nf - print entire stack" |
| 162 | //usage: "\nk - pop the value and set the precision" |
| 163 | //usage: "\ni - pop the value and set input radix" |
| 164 | //usage: "\no - pop the value and set output radix" |
| 165 | //usage: "\nExamples: dc -e'2 2 + p' -> 4, dc -e'8 8 * 2 2 + / p' -> 16" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 166 | //usage: |
| 167 | //usage:#define dc_example_usage |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 168 | //usage: "$ dc -e'2 2 + p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 169 | //usage: "4\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 170 | //usage: "$ dc -e'8 8 \\* 2 2 + / p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 171 | //usage: "16\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 172 | //usage: "$ dc -e'0 1 & p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 173 | //usage: "0\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 174 | //usage: "$ dc -e'0 1 | p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 175 | //usage: "1\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 176 | //usage: "$ echo '72 9 / 8 * p' | dc\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 177 | //usage: "64\n" |
| 178 | |
| 179 | #include "libbb.h" |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 180 | #include "common_bufsiz.h" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 181 | |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 182 | #if ENABLE_FEATURE_DC_SMALL |
| 183 | # include "dc.c" |
| 184 | #else |
| 185 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 186 | typedef enum BcStatus { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 187 | BC_STATUS_SUCCESS = 0, |
| 188 | BC_STATUS_FAILURE = 1, |
| 189 | BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr() uses this |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 190 | BC_STATUS_EOF = 3, // bc_vm_stdin() uses this |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 191 | } BcStatus; |
| 192 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 193 | #define BC_VEC_INVALID_IDX ((size_t) -1) |
| 194 | #define BC_VEC_START_CAP (1 << 5) |
| 195 | |
| 196 | typedef void (*BcVecFree)(void *); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 197 | |
| 198 | typedef struct BcVec { |
| 199 | char *v; |
| 200 | size_t len; |
| 201 | size_t cap; |
| 202 | size_t size; |
| 203 | BcVecFree dtor; |
| 204 | } BcVec; |
| 205 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 206 | typedef signed char BcDig; |
| 207 | |
| 208 | typedef struct BcNum { |
| 209 | BcDig *restrict num; |
| 210 | size_t rdx; |
| 211 | size_t len; |
| 212 | size_t cap; |
| 213 | bool neg; |
| 214 | } BcNum; |
| 215 | |
Denys Vlasenko | 2fa11b6 | 2018-12-06 12:34:39 +0100 | [diff] [blame] | 216 | #define BC_NUM_MIN_BASE ((unsigned long) 2) |
| 217 | #define BC_NUM_MAX_IBASE ((unsigned long) 16) |
| 218 | // larger value might speed up BIGNUM calculations a bit: |
| 219 | #define BC_NUM_DEF_SIZE (16) |
| 220 | #define BC_NUM_PRINT_WIDTH (69) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 221 | |
Denys Vlasenko | 2fa11b6 | 2018-12-06 12:34:39 +0100 | [diff] [blame] | 222 | #define BC_NUM_KARATSUBA_LEN (32) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 223 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 224 | typedef void (*BcNumDigitOp)(size_t, size_t, bool); |
Denys Vlasenko | 2a8ad48 | 2018-12-08 21:56:37 +0100 | [diff] [blame] | 225 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 226 | typedef BcStatus (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 227 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 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 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 390 | // BC_LEX_NEG is not used in lexing; it is only for parsing. |
| 391 | typedef enum BcLexType { |
| 392 | |
| 393 | BC_LEX_EOF, |
| 394 | BC_LEX_INVALID, |
| 395 | |
| 396 | BC_LEX_OP_INC, |
| 397 | BC_LEX_OP_DEC, |
| 398 | |
| 399 | BC_LEX_NEG, |
| 400 | |
| 401 | BC_LEX_OP_POWER, |
| 402 | BC_LEX_OP_MULTIPLY, |
| 403 | BC_LEX_OP_DIVIDE, |
| 404 | BC_LEX_OP_MODULUS, |
| 405 | BC_LEX_OP_PLUS, |
| 406 | BC_LEX_OP_MINUS, |
| 407 | |
| 408 | BC_LEX_OP_REL_EQ, |
| 409 | BC_LEX_OP_REL_LE, |
| 410 | BC_LEX_OP_REL_GE, |
| 411 | BC_LEX_OP_REL_NE, |
| 412 | BC_LEX_OP_REL_LT, |
| 413 | BC_LEX_OP_REL_GT, |
| 414 | |
| 415 | BC_LEX_OP_BOOL_NOT, |
| 416 | BC_LEX_OP_BOOL_OR, |
| 417 | BC_LEX_OP_BOOL_AND, |
| 418 | |
| 419 | BC_LEX_OP_ASSIGN_POWER, |
| 420 | BC_LEX_OP_ASSIGN_MULTIPLY, |
| 421 | BC_LEX_OP_ASSIGN_DIVIDE, |
| 422 | BC_LEX_OP_ASSIGN_MODULUS, |
| 423 | BC_LEX_OP_ASSIGN_PLUS, |
| 424 | BC_LEX_OP_ASSIGN_MINUS, |
| 425 | BC_LEX_OP_ASSIGN, |
| 426 | |
| 427 | BC_LEX_NLINE, |
| 428 | BC_LEX_WHITESPACE, |
| 429 | |
| 430 | BC_LEX_LPAREN, |
| 431 | BC_LEX_RPAREN, |
| 432 | |
| 433 | BC_LEX_LBRACKET, |
| 434 | BC_LEX_COMMA, |
| 435 | BC_LEX_RBRACKET, |
| 436 | |
| 437 | BC_LEX_LBRACE, |
| 438 | BC_LEX_SCOLON, |
| 439 | BC_LEX_RBRACE, |
| 440 | |
| 441 | BC_LEX_STR, |
| 442 | BC_LEX_NAME, |
| 443 | BC_LEX_NUMBER, |
| 444 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 445 | BC_LEX_KEY_1st_keyword, |
| 446 | BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 447 | BC_LEX_KEY_BREAK, |
| 448 | BC_LEX_KEY_CONTINUE, |
| 449 | BC_LEX_KEY_DEFINE, |
| 450 | BC_LEX_KEY_ELSE, |
| 451 | BC_LEX_KEY_FOR, |
| 452 | BC_LEX_KEY_HALT, |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 453 | // code uses "type - BC_LEX_KEY_IBASE + BC_INST_IBASE" construct, |
| 454 | BC_LEX_KEY_IBASE, // relative order should match for: BC_INST_IBASE |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 455 | BC_LEX_KEY_IF, |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 456 | BC_LEX_KEY_LAST, // relative order should match for: BC_INST_LAST |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 457 | BC_LEX_KEY_LENGTH, |
| 458 | BC_LEX_KEY_LIMITS, |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 459 | BC_LEX_KEY_OBASE, // relative order should match for: BC_INST_OBASE |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 460 | BC_LEX_KEY_PRINT, |
| 461 | BC_LEX_KEY_QUIT, |
| 462 | BC_LEX_KEY_READ, |
| 463 | BC_LEX_KEY_RETURN, |
| 464 | BC_LEX_KEY_SCALE, |
| 465 | BC_LEX_KEY_SQRT, |
| 466 | BC_LEX_KEY_WHILE, |
| 467 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 468 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 469 | BC_LEX_EQ_NO_REG, |
| 470 | BC_LEX_OP_MODEXP, |
| 471 | BC_LEX_OP_DIVMOD, |
| 472 | |
| 473 | BC_LEX_COLON, |
| 474 | BC_LEX_ELSE, |
| 475 | BC_LEX_EXECUTE, |
| 476 | BC_LEX_PRINT_STACK, |
| 477 | BC_LEX_CLEAR_STACK, |
| 478 | BC_LEX_STACK_LEVEL, |
| 479 | BC_LEX_DUPLICATE, |
| 480 | BC_LEX_SWAP, |
| 481 | BC_LEX_POP, |
| 482 | |
| 483 | BC_LEX_ASCIIFY, |
| 484 | BC_LEX_PRINT_STREAM, |
| 485 | |
| 486 | BC_LEX_STORE_IBASE, |
| 487 | BC_LEX_STORE_SCALE, |
| 488 | BC_LEX_LOAD, |
| 489 | BC_LEX_LOAD_POP, |
| 490 | BC_LEX_STORE_PUSH, |
| 491 | BC_LEX_STORE_OBASE, |
| 492 | BC_LEX_PRINT_POP, |
| 493 | BC_LEX_NQUIT, |
| 494 | BC_LEX_SCALE_FACTOR, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 495 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 496 | } BcLexType; |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 497 | // must match order of BC_LEX_KEY_foo etc above |
| 498 | #if ENABLE_BC |
| 499 | struct BcLexKeyword { |
| 500 | char name8[8]; |
| 501 | }; |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 502 | #define BC_LEX_KW_ENTRY(a, b) \ |
| 503 | { .name8 = a /*, .posix = b */ } |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 504 | static const struct BcLexKeyword bc_lex_kws[20] = { |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 505 | BC_LEX_KW_ENTRY("auto" , 1), // 0 |
| 506 | BC_LEX_KW_ENTRY("break" , 1), // 1 |
| 507 | BC_LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL |
| 508 | BC_LEX_KW_ENTRY("define" , 1), // 3 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 509 | |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 510 | BC_LEX_KW_ENTRY("else" , 0), // 4 |
| 511 | BC_LEX_KW_ENTRY("for" , 1), // 5 |
| 512 | BC_LEX_KW_ENTRY("halt" , 0), // 6 |
| 513 | BC_LEX_KW_ENTRY("ibase" , 1), // 7 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 514 | |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 515 | BC_LEX_KW_ENTRY("if" , 1), // 8 |
| 516 | BC_LEX_KW_ENTRY("last" , 0), // 9 |
| 517 | BC_LEX_KW_ENTRY("length" , 1), // 10 |
| 518 | BC_LEX_KW_ENTRY("limits" , 0), // 11 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 519 | |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 520 | BC_LEX_KW_ENTRY("obase" , 1), // 12 |
| 521 | BC_LEX_KW_ENTRY("print" , 0), // 13 |
| 522 | BC_LEX_KW_ENTRY("quit" , 1), // 14 |
| 523 | BC_LEX_KW_ENTRY("read" , 0), // 15 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 524 | |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 525 | BC_LEX_KW_ENTRY("return" , 1), // 16 |
| 526 | BC_LEX_KW_ENTRY("scale" , 1), // 17 |
| 527 | BC_LEX_KW_ENTRY("sqrt" , 1), // 18 |
| 528 | BC_LEX_KW_ENTRY("while" , 1), // 19 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 529 | }; |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 530 | #undef BC_LEX_KW_ENTRY |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 531 | enum { |
| 532 | POSIX_KWORD_MASK = 0 |
| 533 | | (1 << 0) |
| 534 | | (1 << 1) |
| 535 | | (0 << 2) |
| 536 | | (1 << 3) |
| 537 | \ |
| 538 | | (0 << 4) |
| 539 | | (1 << 5) |
| 540 | | (0 << 6) |
| 541 | | (1 << 7) |
| 542 | \ |
| 543 | | (1 << 8) |
| 544 | | (0 << 9) |
| 545 | | (1 << 10) |
| 546 | | (0 << 11) |
| 547 | \ |
| 548 | | (1 << 12) |
| 549 | | (0 << 13) |
| 550 | | (1 << 14) |
| 551 | | (0 << 15) |
| 552 | \ |
| 553 | | (1 << 16) |
| 554 | | (1 << 17) |
| 555 | | (1 << 18) |
| 556 | | (1 << 19) |
| 557 | }; |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 558 | #define bc_lex_kws_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK) |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 559 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 560 | |
| 561 | struct BcLex; |
| 562 | typedef BcStatus (*BcLexNext)(struct BcLex *); |
| 563 | |
| 564 | typedef struct BcLex { |
| 565 | |
| 566 | const char *buf; |
| 567 | size_t i; |
| 568 | size_t line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 569 | size_t len; |
| 570 | bool newline; |
| 571 | |
| 572 | struct { |
| 573 | BcLexType t; |
| 574 | BcLexType last; |
| 575 | BcVec v; |
| 576 | } t; |
| 577 | |
| 578 | BcLexNext next; |
| 579 | |
| 580 | } BcLex; |
| 581 | |
| 582 | #define BC_PARSE_STREND ((char) UCHAR_MAX) |
| 583 | |
Denys Vlasenko | e55a572 | 2018-12-06 12:47:17 +0100 | [diff] [blame] | 584 | #define BC_PARSE_REL (1 << 0) |
| 585 | #define BC_PARSE_PRINT (1 << 1) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 586 | #define BC_PARSE_NOCALL (1 << 2) |
| 587 | #define BC_PARSE_NOREAD (1 << 3) |
Denys Vlasenko | e55a572 | 2018-12-06 12:47:17 +0100 | [diff] [blame] | 588 | #define BC_PARSE_ARRAY (1 << 4) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 589 | |
| 590 | #define BC_PARSE_TOP_FLAG_PTR(parse) ((uint8_t *) bc_vec_top(&(parse)->flags)) |
| 591 | #define BC_PARSE_TOP_FLAG(parse) (*(BC_PARSE_TOP_FLAG_PTR(parse))) |
| 592 | |
| 593 | #define BC_PARSE_FLAG_FUNC_INNER (1 << 0) |
| 594 | #define BC_PARSE_FUNC_INNER(parse) \ |
| 595 | (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC_INNER) |
| 596 | |
| 597 | #define BC_PARSE_FLAG_FUNC (1 << 1) |
| 598 | #define BC_PARSE_FUNC(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC) |
| 599 | |
| 600 | #define BC_PARSE_FLAG_BODY (1 << 2) |
| 601 | #define BC_PARSE_BODY(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_BODY) |
| 602 | |
| 603 | #define BC_PARSE_FLAG_LOOP (1 << 3) |
| 604 | #define BC_PARSE_LOOP(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP) |
| 605 | |
| 606 | #define BC_PARSE_FLAG_LOOP_INNER (1 << 4) |
| 607 | #define BC_PARSE_LOOP_INNER(parse) \ |
| 608 | (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP_INNER) |
| 609 | |
| 610 | #define BC_PARSE_FLAG_IF (1 << 5) |
| 611 | #define BC_PARSE_IF(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF) |
| 612 | |
| 613 | #define BC_PARSE_FLAG_ELSE (1 << 6) |
| 614 | #define BC_PARSE_ELSE(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_ELSE) |
| 615 | |
| 616 | #define BC_PARSE_FLAG_IF_END (1 << 7) |
| 617 | #define BC_PARSE_IF_END(parse) (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF_END) |
| 618 | |
| 619 | #define BC_PARSE_CAN_EXEC(parse) \ |
| 620 | (!(BC_PARSE_TOP_FLAG(parse) & \ |
| 621 | (BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_BODY | \ |
| 622 | BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER | BC_PARSE_FLAG_IF | \ |
| 623 | BC_PARSE_FLAG_ELSE | BC_PARSE_FLAG_IF_END))) |
| 624 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 625 | struct BcParse; |
| 626 | |
| 627 | struct BcProgram; |
| 628 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 629 | typedef BcStatus (*BcParseParse)(struct BcParse *); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 630 | |
| 631 | typedef struct BcParse { |
| 632 | |
| 633 | BcParseParse parse; |
| 634 | |
| 635 | BcLex l; |
| 636 | |
| 637 | BcVec flags; |
| 638 | |
| 639 | BcVec exits; |
| 640 | BcVec conds; |
| 641 | |
| 642 | BcVec ops; |
| 643 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 644 | BcFunc *func; |
| 645 | size_t fidx; |
| 646 | |
| 647 | size_t nbraces; |
| 648 | bool auto_part; |
| 649 | |
| 650 | } BcParse; |
| 651 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 652 | typedef struct BcProgram { |
| 653 | |
| 654 | size_t len; |
| 655 | size_t scale; |
| 656 | |
| 657 | BcNum ib; |
| 658 | size_t ib_t; |
| 659 | BcNum ob; |
| 660 | size_t ob_t; |
| 661 | |
| 662 | BcNum hexb; |
| 663 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 664 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 665 | BcNum strmb; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 666 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 667 | |
| 668 | BcVec results; |
| 669 | BcVec stack; |
| 670 | |
| 671 | BcVec fns; |
| 672 | BcVec fn_map; |
| 673 | |
| 674 | BcVec vars; |
| 675 | BcVec var_map; |
| 676 | |
| 677 | BcVec arrs; |
| 678 | BcVec arr_map; |
| 679 | |
| 680 | BcVec strs; |
| 681 | BcVec consts; |
| 682 | |
| 683 | const char *file; |
| 684 | |
| 685 | BcNum last; |
| 686 | BcNum zero; |
| 687 | BcNum one; |
| 688 | |
| 689 | size_t nchars; |
| 690 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 691 | } BcProgram; |
| 692 | |
| 693 | #define BC_PROG_STACK(s, n) ((s)->len >= ((size_t) n)) |
| 694 | |
| 695 | #define BC_PROG_MAIN (0) |
| 696 | #define BC_PROG_READ (1) |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 697 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 698 | #define BC_PROG_REQ_FUNCS (2) |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 699 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 700 | |
| 701 | #define BC_PROG_STR(n) (!(n)->num && !(n)->cap) |
| 702 | #define BC_PROG_NUM(r, n) \ |
| 703 | ((r)->t != BC_RESULT_ARRAY && (r)->t != BC_RESULT_STR && !BC_PROG_STR(n)) |
| 704 | |
| 705 | typedef unsigned long (*BcProgramBuiltIn)(BcNum *); |
| 706 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 707 | #define BC_FLAG_W (1 << 0) |
| 708 | #define BC_FLAG_V (1 << 1) |
| 709 | #define BC_FLAG_S (1 << 2) |
| 710 | #define BC_FLAG_Q (1 << 3) |
| 711 | #define BC_FLAG_L (1 << 4) |
| 712 | #define BC_FLAG_I (1 << 5) |
| 713 | #define DC_FLAG_X (1 << 6) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 714 | |
| 715 | #define BC_MAX(a, b) ((a) > (b) ? (a) : (b)) |
| 716 | #define BC_MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 717 | |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 718 | #define BC_MAX_OBASE ((unsigned) 999) |
| 719 | #define BC_MAX_DIM ((unsigned) INT_MAX) |
| 720 | #define BC_MAX_SCALE ((unsigned) UINT_MAX) |
| 721 | #define BC_MAX_STRING ((unsigned) UINT_MAX - 1) |
| 722 | #define BC_MAX_NUM BC_MAX_STRING |
| 723 | // Unused apart from "limits" message. Just show a "biggish number" there. |
| 724 | //#define BC_MAX_NAME BC_MAX_STRING |
| 725 | //#define BC_MAX_EXP ((unsigned long) LONG_MAX) |
| 726 | //#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1) |
| 727 | #define BC_MAX_NAME_STR "999999999" |
| 728 | #define BC_MAX_EXP_STR "999999999" |
| 729 | #define BC_MAX_VARS_STR "999999999" |
| 730 | |
| 731 | #define BC_MAX_OBASE_STR "999" |
| 732 | |
| 733 | #if INT_MAX == 2147483647 |
| 734 | # define BC_MAX_DIM_STR "2147483647" |
| 735 | #elif INT_MAX == 9223372036854775807 |
| 736 | # define BC_MAX_DIM_STR "9223372036854775807" |
| 737 | #else |
| 738 | # error Strange INT_MAX |
| 739 | #endif |
| 740 | |
| 741 | #if UINT_MAX == 4294967295 |
| 742 | # define BC_MAX_SCALE_STR "4294967295" |
| 743 | # define BC_MAX_STRING_STR "4294967294" |
| 744 | #elif UINT_MAX == 18446744073709551615 |
| 745 | # define BC_MAX_SCALE_STR "18446744073709551615" |
| 746 | # define BC_MAX_STRING_STR "18446744073709551614" |
| 747 | #else |
| 748 | # error Strange UINT_MAX |
| 749 | #endif |
| 750 | #define BC_MAX_NUM_STR BC_MAX_STRING_STR |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 751 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 752 | struct globals { |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 753 | IF_FEATURE_BC_SIGNALS(smallint ttyin;) |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 754 | IF_FEATURE_CLEAN_UP(smallint exiting;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 755 | char sbgn; |
| 756 | char send; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 757 | |
| 758 | BcParse prs; |
| 759 | BcProgram prog; |
| 760 | |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 761 | // For error messages. Can be set to current parsed line, |
| 762 | // or [TODO] to current executing line (can be before last parsed one) |
| 763 | unsigned err_line; |
| 764 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 765 | BcVec files; |
| 766 | |
| 767 | char *env_args; |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 768 | |
| 769 | #if ENABLE_FEATURE_EDITING |
| 770 | line_input_t *line_input_state; |
| 771 | #endif |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 772 | } FIX_ALIASING; |
| 773 | #define G (*ptr_to_globals) |
| 774 | #define INIT_G() do { \ |
| 775 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 776 | } while (0) |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 777 | #define FREE_G() do { \ |
| 778 | FREE_PTR_TO_GLOBALS(); \ |
| 779 | } while (0) |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 780 | #define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S)) |
| 781 | #define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W)) |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 782 | #define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X)) |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 783 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | b9c321d | 2018-12-07 12:41:42 +0100 | [diff] [blame] | 784 | # define G_interrupt bb_got_signal |
| 785 | # define G_ttyin G.ttyin |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 786 | #else |
Denys Vlasenko | b9c321d | 2018-12-07 12:41:42 +0100 | [diff] [blame] | 787 | # define G_interrupt 0 |
| 788 | # define G_ttyin 0 |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 789 | #endif |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 790 | #if ENABLE_FEATURE_CLEAN_UP |
| 791 | # define G_exiting G.exiting |
| 792 | #else |
| 793 | # define G_exiting 0 |
| 794 | #endif |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 795 | #define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b')) |
| 796 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 797 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 798 | |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 799 | // 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] | 800 | // true if the token is valid in an expression, false otherwise. |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 801 | enum { |
| 802 | BC_PARSE_EXPRS_BITS = 0 |
| 803 | + ((uint64_t)((0 << 0)+(0 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (0*8)) |
| 804 | + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (1*8)) |
| 805 | + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(1 << 5)+(1 << 6)+(1 << 7)) << (2*8)) |
| 806 | + ((uint64_t)((1 << 0)+(1 << 1)+(1 << 2)+(0 << 3)+(0 << 4)+(1 << 5)+(1 << 6)+(0 << 7)) << (3*8)) |
| 807 | + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(1 << 6)+(1 << 7)) << (4*8)) |
| 808 | + ((uint64_t)((0 << 0)+(0 << 1)+(0 << 2)+(0 << 3)+(0 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (5*8)) |
| 809 | + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(1 << 3)+(1 << 4)+(0 << 5)+(0 << 6)+(1 << 7)) << (6*8)) |
| 810 | + ((uint64_t)((0 << 0)+(1 << 1)+(1 << 2)+(0 << 3) ) << (7*8)) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 811 | }; |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 812 | static ALWAYS_INLINE long bc_parse_exprs(unsigned i) |
| 813 | { |
| 814 | #if ULONG_MAX > 0xffffffff |
| 815 | // 64-bit version (will not work correctly for 32-bit longs!) |
| 816 | return BC_PARSE_EXPRS_BITS & (1UL << i); |
| 817 | #else |
| 818 | // 32-bit version |
| 819 | unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS; |
| 820 | if (i >= 32) { |
| 821 | m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32); |
| 822 | i &= 31; |
| 823 | } |
| 824 | return m & (1UL << i); |
| 825 | #endif |
| 826 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 827 | |
| 828 | // 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] | 829 | static const uint8_t bc_parse_ops[] = { |
| 830 | #define OP(p,l) ((int)(l) * 0x10 + (p)) |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 831 | OP(0, false), OP( 0, false ), // inc dec |
| 832 | OP(1, false), // neg |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 833 | OP(2, false), |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 834 | OP(3, true ), OP( 3, true ), OP( 3, true ), // pow mul div |
| 835 | OP(4, true ), OP( 4, true ), // mod + - |
| 836 | OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < > |
| 837 | OP(1, false), // not |
| 838 | OP(7, true ), OP( 7, true ), // or and |
| 839 | OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= += |
| 840 | OP(5, false), OP( 5, false ), // -= = |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 841 | #undef OP |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 842 | }; |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 843 | #define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f) |
| 844 | #define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 845 | |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 846 | // Byte array of up to 4 BC_LEX's, packed into 32-bit word |
| 847 | typedef uint32_t BcParseNext; |
| 848 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 849 | // These identify what tokens can come after expressions in certain cases. |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 850 | enum { |
| 851 | #define BC_PARSE_NEXT4(a,b,c,d) ( (a) | ((b)<<8) | ((c)<<16) | ((((d)|0x80)<<24)) ) |
| 852 | #define BC_PARSE_NEXT2(a,b) BC_PARSE_NEXT4(a,b,0xff,0xff) |
| 853 | #define BC_PARSE_NEXT1(a) BC_PARSE_NEXT4(a,0xff,0xff,0xff) |
| 854 | bc_parse_next_expr = BC_PARSE_NEXT4(BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_RBRACE, BC_LEX_EOF), |
| 855 | bc_parse_next_param = BC_PARSE_NEXT2(BC_LEX_RPAREN, BC_LEX_COMMA), |
| 856 | bc_parse_next_print = BC_PARSE_NEXT4(BC_LEX_COMMA, BC_LEX_NLINE, BC_LEX_SCOLON, BC_LEX_EOF), |
| 857 | bc_parse_next_rel = BC_PARSE_NEXT1(BC_LEX_RPAREN), |
| 858 | bc_parse_next_elem = BC_PARSE_NEXT1(BC_LEX_RBRACKET), |
| 859 | bc_parse_next_for = BC_PARSE_NEXT1(BC_LEX_SCOLON), |
| 860 | bc_parse_next_read = BC_PARSE_NEXT2(BC_LEX_NLINE, BC_LEX_EOF), |
| 861 | #undef BC_PARSE_NEXT4 |
| 862 | #undef BC_PARSE_NEXT2 |
| 863 | #undef BC_PARSE_NEXT1 |
| 864 | }; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 865 | #endif // ENABLE_BC |
| 866 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 867 | #if ENABLE_DC |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 868 | static const //BcLexType - should be this type, but narrower type saves size: |
| 869 | uint8_t |
| 870 | dc_lex_regs[] = { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 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 | |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 877 | static const //BcLexType - should be this type |
| 878 | uint8_t |
| 879 | dc_lex_tokens[] = { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 880 | BC_LEX_OP_MODULUS, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_LPAREN, |
| 881 | BC_LEX_INVALID, BC_LEX_OP_MULTIPLY, BC_LEX_OP_PLUS, BC_LEX_INVALID, |
| 882 | BC_LEX_OP_MINUS, BC_LEX_INVALID, BC_LEX_OP_DIVIDE, |
| 883 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, |
| 884 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, |
| 885 | BC_LEX_INVALID, BC_LEX_INVALID, |
| 886 | BC_LEX_COLON, BC_LEX_SCOLON, BC_LEX_OP_REL_GT, BC_LEX_OP_REL_EQ, |
| 887 | BC_LEX_OP_REL_LT, BC_LEX_KEY_READ, BC_LEX_INVALID, |
| 888 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, |
| 889 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_EQ_NO_REG, BC_LEX_INVALID, |
| 890 | BC_LEX_KEY_IBASE, BC_LEX_INVALID, BC_LEX_KEY_SCALE, BC_LEX_LOAD_POP, |
| 891 | BC_LEX_INVALID, BC_LEX_OP_BOOL_NOT, BC_LEX_KEY_OBASE, BC_LEX_PRINT_STREAM, |
| 892 | BC_LEX_NQUIT, BC_LEX_POP, BC_LEX_STORE_PUSH, BC_LEX_INVALID, BC_LEX_INVALID, |
| 893 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_SCALE_FACTOR, BC_LEX_INVALID, |
| 894 | BC_LEX_KEY_LENGTH, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, |
| 895 | BC_LEX_OP_POWER, BC_LEX_NEG, BC_LEX_INVALID, |
| 896 | BC_LEX_ASCIIFY, BC_LEX_INVALID, BC_LEX_CLEAR_STACK, BC_LEX_DUPLICATE, |
| 897 | BC_LEX_ELSE, BC_LEX_PRINT_STACK, BC_LEX_INVALID, BC_LEX_INVALID, |
| 898 | BC_LEX_STORE_IBASE, BC_LEX_INVALID, BC_LEX_STORE_SCALE, BC_LEX_LOAD, |
| 899 | BC_LEX_INVALID, BC_LEX_PRINT_POP, BC_LEX_STORE_OBASE, BC_LEX_KEY_PRINT, |
| 900 | BC_LEX_KEY_QUIT, BC_LEX_SWAP, BC_LEX_OP_ASSIGN, BC_LEX_INVALID, |
| 901 | BC_LEX_INVALID, BC_LEX_KEY_SQRT, BC_LEX_INVALID, BC_LEX_EXECUTE, |
| 902 | BC_LEX_INVALID, BC_LEX_STACK_LEVEL, |
| 903 | BC_LEX_LBRACE, BC_LEX_OP_MODEXP, BC_LEX_INVALID, BC_LEX_OP_DIVMOD, |
| 904 | BC_LEX_INVALID |
| 905 | }; |
| 906 | |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 907 | static const //BcInst - should be this type. Using signed narrow type since BC_INST_INVALID is -1 |
| 908 | int8_t |
| 909 | dc_parse_insts[] = { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 910 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE, |
| 911 | BC_INST_INVALID, BC_INST_POWER, BC_INST_MULTIPLY, BC_INST_DIVIDE, |
| 912 | BC_INST_MODULUS, BC_INST_PLUS, BC_INST_MINUS, |
| 913 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 914 | BC_INST_INVALID, BC_INST_INVALID, |
| 915 | BC_INST_BOOL_NOT, BC_INST_INVALID, BC_INST_INVALID, |
| 916 | BC_INST_INVALID, BC_INST_INVALID, 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_REL_GT, BC_INST_INVALID, |
| 919 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE, |
| 920 | BC_INST_INVALID, BC_INST_INVALID, |
| 921 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 922 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 923 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_IBASE, |
| 924 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_LENGTH, BC_INST_INVALID, |
| 925 | BC_INST_OBASE, BC_INST_PRINT, BC_INST_QUIT, BC_INST_INVALID, |
| 926 | BC_INST_INVALID, BC_INST_SCALE, BC_INST_SQRT, BC_INST_INVALID, |
| 927 | BC_INST_REL_EQ, BC_INST_MODEXP, BC_INST_DIVMOD, BC_INST_INVALID, |
| 928 | BC_INST_INVALID, BC_INST_EXECUTE, BC_INST_PRINT_STACK, BC_INST_CLEAR_STACK, |
| 929 | BC_INST_STACK_LEN, BC_INST_DUPLICATE, BC_INST_SWAP, BC_INST_POP, |
| 930 | BC_INST_ASCIIFY, BC_INST_PRINT_STREAM, BC_INST_INVALID, BC_INST_INVALID, |
| 931 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 932 | BC_INST_PRINT, BC_INST_NQUIT, BC_INST_SCALE_FUNC, |
| 933 | }; |
| 934 | #endif // ENABLE_DC |
| 935 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 936 | static const BcNumBinaryOp bc_program_ops[] = { |
| 937 | bc_num_pow, bc_num_mul, bc_num_div, bc_num_mod, bc_num_add, bc_num_sub, |
| 938 | }; |
| 939 | |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 940 | static void fflush_and_check(void) |
| 941 | { |
| 942 | fflush_all(); |
| 943 | if (ferror(stdout) || ferror(stderr)) |
| 944 | bb_perror_msg_and_die("output error"); |
| 945 | } |
| 946 | |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 947 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 948 | #define QUIT_OR_RETURN_TO_MAIN \ |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 949 | do { \ |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 950 | IF_FEATURE_BC_SIGNALS(G_ttyin = 0;) /* do not loop in main loop anymore */ \ |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 951 | G_exiting = 1; \ |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 952 | return BC_STATUS_FAILURE; \ |
| 953 | } while (0) |
| 954 | #else |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 955 | #define QUIT_OR_RETURN_TO_MAIN quit() |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 956 | #endif |
| 957 | |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 958 | static void quit(void) NORETURN; |
| 959 | static void quit(void) |
| 960 | { |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 961 | if (ferror(stdin)) |
| 962 | bb_perror_msg_and_die("input error"); |
| 963 | fflush_and_check(); |
| 964 | exit(0); |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 965 | } |
| 966 | |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 967 | static void bc_verror_msg(const char *fmt, va_list p) |
| 968 | { |
| 969 | const char *sv = sv; /* for compiler */ |
| 970 | if (G.prog.file) { |
| 971 | sv = applet_name; |
| 972 | applet_name = xasprintf("%s: %s:%u", applet_name, G.prog.file, G.err_line); |
| 973 | } |
| 974 | bb_verror_msg(fmt, p, NULL); |
| 975 | if (G.prog.file) { |
| 976 | free((char*)applet_name); |
| 977 | applet_name = sv; |
| 978 | } |
| 979 | } |
| 980 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 981 | static NOINLINE int bc_error_fmt(const char *fmt, ...) |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 982 | { |
| 983 | va_list p; |
| 984 | |
| 985 | va_start(p, fmt); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 986 | bc_verror_msg(fmt, p); |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 987 | va_end(p); |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 988 | |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 989 | if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin) |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 990 | exit(1); |
| 991 | return BC_STATUS_FAILURE; |
| 992 | } |
| 993 | |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 994 | #if ENABLE_BC |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 995 | static NOINLINE int bc_posix_error_fmt(const char *fmt, ...) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 996 | { |
| 997 | va_list p; |
| 998 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 999 | // Are non-POSIX constructs totally ok? |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 1000 | if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W))) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1001 | return BC_STATUS_SUCCESS; // yes |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1002 | |
| 1003 | va_start(p, fmt); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 1004 | bc_verror_msg(fmt, p); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1005 | va_end(p); |
| 1006 | |
| 1007 | // Do we treat non-POSIX constructs as errors? |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 1008 | if (!(option_mask32 & BC_FLAG_S)) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1009 | return BC_STATUS_SUCCESS; // no, it's a warning |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 1010 | if (!ENABLE_FEATURE_CLEAN_UP && !G_ttyin) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1011 | exit(1); |
| 1012 | return BC_STATUS_FAILURE; |
| 1013 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1014 | #endif |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 1015 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1016 | // We use error functions with "return bc_error(FMT[, PARAMS])" idiom. |
| 1017 | // This idiom begs for tail-call optimization, but for it to work, |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1018 | // function must not have caller-cleaned parameters on stack. |
| 1019 | // Unfortunately, vararg function API does exactly that on most arches. |
| 1020 | // Thus, use these shims for the cases when we have no vararg PARAMS: |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1021 | static int bc_error(const char *msg) |
| 1022 | { |
| 1023 | return bc_error_fmt("%s", msg); |
| 1024 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1025 | #if ENABLE_BC |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 1026 | static int bc_POSIX_requires(const char *msg) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1027 | { |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 1028 | return bc_posix_error_fmt("POSIX requires %s", msg); |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1029 | } |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 1030 | static int bc_POSIX_does_not_allow(const char *msg) |
| 1031 | { |
| 1032 | return bc_posix_error_fmt("%s%s", "POSIX does not allow ", msg); |
| 1033 | } |
| 1034 | static int bc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg) |
| 1035 | { |
| 1036 | return bc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; the following is bad:", msg); |
| 1037 | } |
| 1038 | static int bc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg) |
| 1039 | { |
| 1040 | return bc_posix_error_fmt("%san empty %s expression in a for loop", "POSIX does not allow ", msg); |
| 1041 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1042 | #endif |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1043 | static int bc_error_bad_character(char c) |
| 1044 | { |
| 1045 | return bc_error_fmt("bad character '%c'", c); |
| 1046 | } |
| 1047 | static int bc_error_bad_expression(void) |
| 1048 | { |
| 1049 | return bc_error("bad expression"); |
| 1050 | } |
| 1051 | static int bc_error_bad_token(void) |
| 1052 | { |
| 1053 | return bc_error("bad token"); |
| 1054 | } |
| 1055 | static int bc_error_stack_has_too_few_elements(void) |
| 1056 | { |
| 1057 | return bc_error("stack has too few elements"); |
| 1058 | } |
| 1059 | static int bc_error_variable_is_wrong_type(void) |
| 1060 | { |
| 1061 | return bc_error("variable is wrong type"); |
| 1062 | } |
| 1063 | static int bc_error_nested_read_call(void) |
| 1064 | { |
| 1065 | return bc_error("read() call inside of a read() call"); |
| 1066 | } |
| 1067 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1068 | static void bc_vec_grow(BcVec *v, size_t n) |
| 1069 | { |
| 1070 | size_t cap = v->cap * 2; |
| 1071 | while (cap < v->len + n) cap *= 2; |
| 1072 | v->v = xrealloc(v->v, v->size * cap); |
| 1073 | v->cap = cap; |
| 1074 | } |
| 1075 | |
| 1076 | static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor) |
| 1077 | { |
| 1078 | v->size = esize; |
| 1079 | v->cap = BC_VEC_START_CAP; |
| 1080 | v->len = 0; |
| 1081 | v->dtor = dtor; |
| 1082 | v->v = xmalloc(esize * BC_VEC_START_CAP); |
| 1083 | } |
| 1084 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1085 | static void bc_char_vec_init(BcVec *v) |
| 1086 | { |
| 1087 | bc_vec_init(v, sizeof(char), NULL); |
| 1088 | } |
| 1089 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1090 | static void bc_vec_expand(BcVec *v, size_t req) |
| 1091 | { |
| 1092 | if (v->cap < req) { |
| 1093 | v->v = xrealloc(v->v, v->size * req); |
| 1094 | v->cap = req; |
| 1095 | } |
| 1096 | } |
| 1097 | |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 1098 | static void bc_vec_pop(BcVec *v) |
| 1099 | { |
| 1100 | v->len--; |
| 1101 | if (v->dtor) |
| 1102 | v->dtor(v->v + (v->size * v->len)); |
| 1103 | } |
Denys Vlasenko | 2fa11b6 | 2018-12-06 12:34:39 +0100 | [diff] [blame] | 1104 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1105 | static void bc_vec_npop(BcVec *v, size_t n) |
| 1106 | { |
| 1107 | if (!v->dtor) |
| 1108 | v->len -= n; |
| 1109 | else { |
| 1110 | size_t len = v->len - n; |
| 1111 | while (v->len > len) v->dtor(v->v + (v->size * --v->len)); |
| 1112 | } |
| 1113 | } |
| 1114 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1115 | static void bc_vec_pop_all(BcVec *v) |
| 1116 | { |
| 1117 | bc_vec_npop(v, v->len); |
| 1118 | } |
| 1119 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1120 | static void bc_vec_push(BcVec *v, const void *data) |
| 1121 | { |
| 1122 | if (v->len + 1 > v->cap) bc_vec_grow(v, 1); |
| 1123 | memmove(v->v + (v->size * v->len), data, v->size); |
| 1124 | v->len += 1; |
| 1125 | } |
| 1126 | |
| 1127 | static void bc_vec_pushByte(BcVec *v, char data) |
| 1128 | { |
| 1129 | bc_vec_push(v, &data); |
| 1130 | } |
| 1131 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1132 | static void bc_vec_pushZeroByte(BcVec *v) |
| 1133 | { |
| 1134 | //bc_vec_pushByte(v, '\0'); |
| 1135 | // better: |
| 1136 | bc_vec_push(v, &const_int_0); |
| 1137 | } |
| 1138 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1139 | static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx) |
| 1140 | { |
| 1141 | if (idx == v->len) |
| 1142 | bc_vec_push(v, data); |
| 1143 | else { |
| 1144 | |
| 1145 | char *ptr; |
| 1146 | |
| 1147 | if (v->len == v->cap) bc_vec_grow(v, 1); |
| 1148 | |
| 1149 | ptr = v->v + v->size * idx; |
| 1150 | |
| 1151 | memmove(ptr + v->size, ptr, v->size * (v->len++ - idx)); |
| 1152 | memmove(ptr, data, v->size); |
| 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | static void bc_vec_string(BcVec *v, size_t len, const char *str) |
| 1157 | { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1158 | bc_vec_pop_all(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1159 | bc_vec_expand(v, len + 1); |
| 1160 | memcpy(v->v, str, len); |
| 1161 | v->len = len; |
| 1162 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1163 | bc_vec_pushZeroByte(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | static void bc_vec_concat(BcVec *v, const char *str) |
| 1167 | { |
| 1168 | size_t len; |
| 1169 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1170 | if (v->len == 0) bc_vec_pushZeroByte(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1171 | |
| 1172 | len = v->len + strlen(str); |
| 1173 | |
| 1174 | if (v->cap < len) bc_vec_grow(v, len - v->len); |
Denys Vlasenko | 1ff8862 | 2018-12-06 12:06:16 +0100 | [diff] [blame] | 1175 | strcpy(v->v + v->len - 1, str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1176 | |
| 1177 | v->len = len; |
| 1178 | } |
| 1179 | |
| 1180 | static void *bc_vec_item(const BcVec *v, size_t idx) |
| 1181 | { |
| 1182 | return v->v + v->size * idx; |
| 1183 | } |
| 1184 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 1185 | static char** bc_program_str(size_t idx) |
| 1186 | { |
| 1187 | return bc_vec_item(&G.prog.strs, idx); |
| 1188 | } |
| 1189 | |
| 1190 | static BcFunc* bc_program_func(size_t idx) |
| 1191 | { |
| 1192 | return bc_vec_item(&G.prog.fns, idx); |
| 1193 | } |
| 1194 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1195 | static void *bc_vec_item_rev(const BcVec *v, size_t idx) |
| 1196 | { |
| 1197 | return v->v + v->size * (v->len - idx - 1); |
| 1198 | } |
| 1199 | |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 1200 | static void *bc_vec_top(const BcVec *v) |
| 1201 | { |
| 1202 | return v->v + v->size * (v->len - 1); |
| 1203 | } |
| 1204 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1205 | static void bc_vec_free(void *vec) |
| 1206 | { |
| 1207 | BcVec *v = (BcVec *) vec; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1208 | bc_vec_pop_all(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1209 | free(v->v); |
| 1210 | } |
| 1211 | |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 1212 | static int bc_id_cmp(const void *e1, const void *e2) |
| 1213 | { |
| 1214 | return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name); |
| 1215 | } |
| 1216 | |
| 1217 | static void bc_id_free(void *id) |
| 1218 | { |
| 1219 | free(((BcId *) id)->name); |
| 1220 | } |
| 1221 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1222 | static size_t bc_map_find(const BcVec *v, const void *ptr) |
| 1223 | { |
| 1224 | size_t low = 0, high = v->len; |
| 1225 | |
| 1226 | while (low < high) { |
| 1227 | |
| 1228 | size_t mid = (low + high) / 2; |
| 1229 | BcId *id = bc_vec_item(v, mid); |
| 1230 | int result = bc_id_cmp(ptr, id); |
| 1231 | |
| 1232 | if (result == 0) |
| 1233 | return mid; |
| 1234 | else if (result < 0) |
| 1235 | high = mid; |
| 1236 | else |
| 1237 | low = mid + 1; |
| 1238 | } |
| 1239 | |
| 1240 | return low; |
| 1241 | } |
| 1242 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1243 | 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] | 1244 | { |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1245 | size_t n = *i = bc_map_find(v, ptr); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1246 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1247 | if (n == v->len) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1248 | bc_vec_push(v, ptr); |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1249 | else if (!bc_id_cmp(ptr, bc_vec_item(v, n))) |
| 1250 | return 0; // "was not inserted" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1251 | else |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1252 | bc_vec_pushAt(v, ptr, n); |
| 1253 | return 1; // "was inserted" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1254 | } |
| 1255 | |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1256 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1257 | static size_t bc_map_index(const BcVec *v, const void *ptr) |
| 1258 | { |
| 1259 | size_t i = bc_map_find(v, ptr); |
| 1260 | if (i >= v->len) return BC_VEC_INVALID_IDX; |
| 1261 | return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i; |
| 1262 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1263 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1264 | |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1265 | static int push_input_byte(BcVec *vec, char c) |
| 1266 | { |
| 1267 | if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'? |
| 1268 | || c > 0x7e |
| 1269 | ) { |
| 1270 | // Bad chars on this line, ignore entire line |
| 1271 | bc_error_fmt("illegal character 0x%02x", c); |
| 1272 | return 1; |
| 1273 | } |
| 1274 | bc_vec_pushByte(vec, (char)c); |
| 1275 | return 0; |
| 1276 | } |
| 1277 | |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 1278 | static BcStatus bc_read_line(BcVec *vec) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1279 | { |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 1280 | BcStatus s; |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1281 | bool bad_chars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1282 | |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 1283 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 1284 | do { |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1285 | int c; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1286 | |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1287 | bad_chars = 0; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1288 | bc_vec_pop_all(vec); |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1289 | |
| 1290 | fflush_and_check(); |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1291 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1292 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | b9c321d | 2018-12-07 12:41:42 +0100 | [diff] [blame] | 1293 | if (G_interrupt) { // ^C was pressed |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1294 | intr: |
Denys Vlasenko | b9c321d | 2018-12-07 12:41:42 +0100 | [diff] [blame] | 1295 | G_interrupt = 0; |
Denys Vlasenko | ac6ed11 | 2018-12-08 21:39:10 +0100 | [diff] [blame] | 1296 | // GNU bc says "interrupted execution." |
| 1297 | // GNU dc says "Interrupt!" |
| 1298 | fputs("\ninterrupted execution\n", stderr); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1299 | } |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1300 | # if ENABLE_FEATURE_EDITING |
| 1301 | if (G_ttyin) { |
| 1302 | int n, i; |
| 1303 | # define line_buf bb_common_bufsiz1 |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 1304 | n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE); |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1305 | if (n <= 0) { // read errors or EOF, or ^D, or ^C |
| 1306 | if (n == 0) // ^C |
| 1307 | goto intr; |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 1308 | s = BC_STATUS_EOF; |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1309 | break; |
| 1310 | } |
| 1311 | i = 0; |
| 1312 | for (;;) { |
| 1313 | c = line_buf[i++]; |
| 1314 | if (!c) break; |
| 1315 | bad_chars |= push_input_byte(vec, c); |
| 1316 | } |
| 1317 | # undef line_buf |
| 1318 | } else |
| 1319 | # endif |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1320 | #endif |
Denys Vlasenko | ed84935 | 2018-12-06 10:26:13 +0100 | [diff] [blame] | 1321 | { |
Denys Vlasenko | ed84935 | 2018-12-06 10:26:13 +0100 | [diff] [blame] | 1322 | IF_FEATURE_BC_SIGNALS(errno = 0;) |
| 1323 | do { |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1324 | c = fgetc(stdin); |
| 1325 | #if ENABLE_FEATURE_BC_SIGNALS && !ENABLE_FEATURE_EDITING |
| 1326 | // Both conditions appear simultaneously, check both just in case |
Denys Vlasenko | b9c321d | 2018-12-07 12:41:42 +0100 | [diff] [blame] | 1327 | if (errno == EINTR || G_interrupt) { |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1328 | // ^C was pressed |
| 1329 | clearerr(stdin); |
| 1330 | goto intr; |
| 1331 | } |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1332 | #endif |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1333 | if (c == EOF) { |
Denys Vlasenko | ed84935 | 2018-12-06 10:26:13 +0100 | [diff] [blame] | 1334 | if (ferror(stdin)) |
| 1335 | quit(); // this emits error message |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 1336 | s = BC_STATUS_EOF; |
Denys Vlasenko | ed84935 | 2018-12-06 10:26:13 +0100 | [diff] [blame] | 1337 | // Note: EOF does not append '\n', therefore: |
| 1338 | // printf 'print 123\n' | bc - works |
| 1339 | // printf 'print 123' | bc - fails (syntax error) |
| 1340 | break; |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1341 | } |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1342 | bad_chars |= push_input_byte(vec, c); |
| 1343 | } while (c != '\n'); |
Denys Vlasenko | ed84935 | 2018-12-06 10:26:13 +0100 | [diff] [blame] | 1344 | } |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1345 | } while (bad_chars); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1346 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1347 | bc_vec_pushZeroByte(vec); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1348 | |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 1349 | return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1350 | } |
| 1351 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 1352 | static char* bc_read_file(const char *path) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1353 | { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 1354 | char *buf; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 1355 | size_t size = ((size_t) -1); |
| 1356 | size_t i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1357 | |
Denys Vlasenko | 4c9455f | 2018-12-06 15:21:39 +0100 | [diff] [blame] | 1358 | // Never returns NULL (dies on errors) |
| 1359 | buf = xmalloc_xopen_read_close(path, &size); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1360 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 1361 | for (i = 0; i < size; ++i) { |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1362 | char c = buf[i]; |
| 1363 | if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'? |
| 1364 | || c > 0x7e |
| 1365 | ) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 1366 | free(buf); |
| 1367 | buf = NULL; |
| 1368 | break; |
| 1369 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1370 | } |
| 1371 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 1372 | return buf; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1373 | } |
| 1374 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1375 | static void bc_num_setToZero(BcNum *n, size_t scale) |
| 1376 | { |
| 1377 | n->len = 0; |
| 1378 | n->neg = false; |
| 1379 | n->rdx = scale; |
| 1380 | } |
| 1381 | |
| 1382 | static void bc_num_zero(BcNum *n) |
| 1383 | { |
| 1384 | bc_num_setToZero(n, 0); |
| 1385 | } |
| 1386 | |
| 1387 | static void bc_num_one(BcNum *n) |
| 1388 | { |
| 1389 | bc_num_setToZero(n, 0); |
| 1390 | n->len = 1; |
| 1391 | n->num[0] = 1; |
| 1392 | } |
| 1393 | |
| 1394 | static void bc_num_ten(BcNum *n) |
| 1395 | { |
| 1396 | bc_num_setToZero(n, 0); |
| 1397 | n->len = 2; |
| 1398 | n->num[0] = 0; |
| 1399 | n->num[1] = 1; |
| 1400 | } |
| 1401 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1402 | static void bc_num_init(BcNum *n, size_t req) |
| 1403 | { |
| 1404 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
| 1405 | memset(n, 0, sizeof(BcNum)); |
| 1406 | n->num = xmalloc(req); |
| 1407 | n->cap = req; |
| 1408 | } |
| 1409 | |
| 1410 | static void bc_num_expand(BcNum *n, size_t req) |
| 1411 | { |
| 1412 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
| 1413 | if (req > n->cap) { |
| 1414 | n->num = xrealloc(n->num, req); |
| 1415 | n->cap = req; |
| 1416 | } |
| 1417 | } |
| 1418 | |
| 1419 | static void bc_num_free(void *num) |
| 1420 | { |
| 1421 | free(((BcNum *) num)->num); |
| 1422 | } |
| 1423 | |
| 1424 | static void bc_num_copy(BcNum *d, BcNum *s) |
| 1425 | { |
| 1426 | if (d != s) { |
| 1427 | bc_num_expand(d, s->cap); |
| 1428 | d->len = s->len; |
| 1429 | d->neg = s->neg; |
| 1430 | d->rdx = s->rdx; |
| 1431 | memcpy(d->num, s->num, sizeof(BcDig) * d->len); |
| 1432 | } |
| 1433 | } |
| 1434 | |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1435 | static BcStatus bc_num_ulong(BcNum *n, unsigned long *result_p) |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1436 | { |
| 1437 | size_t i; |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1438 | unsigned long pow, result; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1439 | |
| 1440 | if (n->neg) return bc_error("negative number"); |
| 1441 | |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1442 | for (result = 0, pow = 1, i = n->rdx; i < n->len; ++i) { |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1443 | |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1444 | unsigned long prev = result, powprev = pow; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1445 | |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1446 | result += ((unsigned long) n->num[i]) * pow; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1447 | pow *= 10; |
| 1448 | |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1449 | if (result < prev || pow < powprev) |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1450 | return bc_error("overflow"); |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1451 | prev = result; |
| 1452 | powprev = pow; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1453 | } |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1454 | *result_p = result; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1455 | |
| 1456 | return BC_STATUS_SUCCESS; |
| 1457 | } |
| 1458 | |
| 1459 | static void bc_num_ulong2num(BcNum *n, unsigned long val) |
| 1460 | { |
| 1461 | size_t len; |
| 1462 | BcDig *ptr; |
| 1463 | unsigned long i; |
| 1464 | |
| 1465 | bc_num_zero(n); |
| 1466 | |
| 1467 | if (val == 0) return; |
| 1468 | |
| 1469 | for (len = 1, i = ULONG_MAX; i != 0; i /= 10, ++len) bc_num_expand(n, len); |
| 1470 | for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= 10) ptr[i] = val % 10; |
| 1471 | } |
| 1472 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1473 | static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1474 | size_t len) |
| 1475 | { |
| 1476 | size_t i, j; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1477 | for (i = 0; i < len; ++i) { |
| 1478 | for (a[i] -= b[i], j = 0; a[i + j] < 0;) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1479 | a[i + j++] += 10; |
| 1480 | a[i + j] -= 1; |
| 1481 | } |
| 1482 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1483 | } |
| 1484 | |
Denys Vlasenko | 2d615fe | 2018-12-07 16:22:45 +0100 | [diff] [blame] | 1485 | #define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg)) |
| 1486 | #define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1) |
| 1487 | #define BC_NUM_INT(n) ((n)->len - (n)->rdx) |
| 1488 | //#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1) |
| 1489 | static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b) |
| 1490 | { |
| 1491 | return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1; |
| 1492 | } |
| 1493 | //#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1) |
| 1494 | static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale) |
| 1495 | { |
| 1496 | return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1; |
| 1497 | } |
| 1498 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1499 | static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len) |
| 1500 | { |
| 1501 | size_t i; |
| 1502 | int c = 0; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1503 | for (i = len - 1; i < len && !(c = a[i] - b[i]); --i); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1504 | return BC_NUM_NEG(i + 1, c < 0); |
| 1505 | } |
| 1506 | |
| 1507 | static ssize_t bc_num_cmp(BcNum *a, BcNum *b) |
| 1508 | { |
| 1509 | size_t i, min, a_int, b_int, diff; |
| 1510 | BcDig *max_num, *min_num; |
| 1511 | bool a_max, neg = false; |
| 1512 | ssize_t cmp; |
| 1513 | |
| 1514 | if (a == b) return 0; |
| 1515 | if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg); |
| 1516 | if (b->len == 0) return BC_NUM_NEG(1, a->neg); |
| 1517 | if (a->neg) { |
| 1518 | if (b->neg) |
| 1519 | neg = true; |
| 1520 | else |
| 1521 | return -1; |
| 1522 | } |
| 1523 | else if (b->neg) |
| 1524 | return 1; |
| 1525 | |
| 1526 | a_int = BC_NUM_INT(a); |
| 1527 | b_int = BC_NUM_INT(b); |
| 1528 | a_int -= b_int; |
| 1529 | a_max = (a->rdx > b->rdx); |
| 1530 | |
| 1531 | if (a_int != 0) return (ssize_t) a_int; |
| 1532 | |
| 1533 | if (a_max) { |
| 1534 | min = b->rdx; |
| 1535 | diff = a->rdx - b->rdx; |
| 1536 | max_num = a->num + diff; |
| 1537 | min_num = b->num; |
| 1538 | } |
| 1539 | else { |
| 1540 | min = a->rdx; |
| 1541 | diff = b->rdx - a->rdx; |
| 1542 | max_num = b->num + diff; |
| 1543 | min_num = a->num; |
| 1544 | } |
| 1545 | |
| 1546 | cmp = bc_num_compare(max_num, min_num, b_int + min); |
| 1547 | if (cmp != 0) return BC_NUM_NEG(cmp, (!a_max) != neg); |
| 1548 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1549 | for (max_num -= diff, i = diff - 1; i < diff; --i) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1550 | if (max_num[i]) return BC_NUM_NEG(1, (!a_max) != neg); |
| 1551 | } |
| 1552 | |
| 1553 | return 0; |
| 1554 | } |
| 1555 | |
| 1556 | static void bc_num_truncate(BcNum *n, size_t places) |
| 1557 | { |
| 1558 | if (places == 0) return; |
| 1559 | |
| 1560 | n->rdx -= places; |
| 1561 | |
| 1562 | if (n->len != 0) { |
| 1563 | n->len -= places; |
| 1564 | memmove(n->num, n->num + places, n->len * sizeof(BcDig)); |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | static void bc_num_extend(BcNum *n, size_t places) |
| 1569 | { |
| 1570 | size_t len = n->len + places; |
| 1571 | |
| 1572 | if (places != 0) { |
| 1573 | |
| 1574 | if (n->cap < len) bc_num_expand(n, len); |
| 1575 | |
| 1576 | memmove(n->num + places, n->num, sizeof(BcDig) * n->len); |
| 1577 | memset(n->num, 0, sizeof(BcDig) * places); |
| 1578 | |
| 1579 | n->len += places; |
| 1580 | n->rdx += places; |
| 1581 | } |
| 1582 | } |
| 1583 | |
| 1584 | static void bc_num_clean(BcNum *n) |
| 1585 | { |
| 1586 | while (n->len > 0 && n->num[n->len - 1] == 0) --n->len; |
| 1587 | if (n->len == 0) |
| 1588 | n->neg = false; |
| 1589 | else if (n->len < n->rdx) |
| 1590 | n->len = n->rdx; |
| 1591 | } |
| 1592 | |
| 1593 | static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2) |
| 1594 | { |
| 1595 | if (n->rdx < scale) |
| 1596 | bc_num_extend(n, scale - n->rdx); |
| 1597 | else |
| 1598 | bc_num_truncate(n, n->rdx - scale); |
| 1599 | |
| 1600 | bc_num_clean(n); |
| 1601 | if (n->len != 0) n->neg = !neg1 != !neg2; |
| 1602 | } |
| 1603 | |
| 1604 | static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a, |
| 1605 | BcNum *restrict b) |
| 1606 | { |
| 1607 | if (idx < n->len) { |
| 1608 | |
| 1609 | b->len = n->len - idx; |
| 1610 | a->len = idx; |
| 1611 | a->rdx = b->rdx = 0; |
| 1612 | |
| 1613 | memcpy(b->num, n->num + idx, b->len * sizeof(BcDig)); |
| 1614 | memcpy(a->num, n->num, idx * sizeof(BcDig)); |
| 1615 | } |
| 1616 | else { |
| 1617 | bc_num_zero(b); |
| 1618 | bc_num_copy(a, n); |
| 1619 | } |
| 1620 | |
| 1621 | bc_num_clean(a); |
| 1622 | bc_num_clean(b); |
| 1623 | } |
| 1624 | |
| 1625 | static BcStatus bc_num_shift(BcNum *n, size_t places) |
| 1626 | { |
| 1627 | if (places == 0 || n->len == 0) return BC_STATUS_SUCCESS; |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 1628 | |
| 1629 | // This check makes sense only if size_t is (much) larger than BC_MAX_NUM. |
| 1630 | if (SIZE_MAX > (BC_MAX_NUM | 0xff)) { |
| 1631 | if (places + n->len > BC_MAX_NUM) |
| 1632 | return bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"); |
| 1633 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1634 | |
| 1635 | if (n->rdx >= places) |
| 1636 | n->rdx -= places; |
| 1637 | else { |
| 1638 | bc_num_extend(n, places - n->rdx); |
| 1639 | n->rdx = 0; |
| 1640 | } |
| 1641 | |
| 1642 | bc_num_clean(n); |
| 1643 | |
| 1644 | return BC_STATUS_SUCCESS; |
| 1645 | } |
| 1646 | |
| 1647 | static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) |
| 1648 | { |
| 1649 | BcNum one; |
| 1650 | BcDig num[2]; |
| 1651 | |
| 1652 | one.cap = 2; |
| 1653 | one.num = num; |
| 1654 | bc_num_one(&one); |
| 1655 | |
| 1656 | return bc_num_div(&one, a, b, scale); |
| 1657 | } |
| 1658 | |
| 1659 | static BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) |
| 1660 | { |
| 1661 | BcDig *ptr, *ptr_a, *ptr_b, *ptr_c; |
| 1662 | size_t i, max, min_rdx, min_int, diff, a_int, b_int; |
| 1663 | int carry, in; |
| 1664 | |
| 1665 | // Because this function doesn't need to use scale (per the bc spec), |
| 1666 | // I am hijacking it to say whether it's doing an add or a subtract. |
| 1667 | |
| 1668 | if (a->len == 0) { |
| 1669 | bc_num_copy(c, b); |
| 1670 | if (sub && c->len) c->neg = !c->neg; |
| 1671 | return BC_STATUS_SUCCESS; |
| 1672 | } |
| 1673 | else if (b->len == 0) { |
| 1674 | bc_num_copy(c, a); |
| 1675 | return BC_STATUS_SUCCESS; |
| 1676 | } |
| 1677 | |
| 1678 | c->neg = a->neg; |
| 1679 | c->rdx = BC_MAX(a->rdx, b->rdx); |
| 1680 | min_rdx = BC_MIN(a->rdx, b->rdx); |
| 1681 | c->len = 0; |
| 1682 | |
| 1683 | if (a->rdx > b->rdx) { |
| 1684 | diff = a->rdx - b->rdx; |
| 1685 | ptr = a->num; |
| 1686 | ptr_a = a->num + diff; |
| 1687 | ptr_b = b->num; |
| 1688 | } |
| 1689 | else { |
| 1690 | diff = b->rdx - a->rdx; |
| 1691 | ptr = b->num; |
| 1692 | ptr_a = a->num; |
| 1693 | ptr_b = b->num + diff; |
| 1694 | } |
| 1695 | |
| 1696 | for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i]; |
| 1697 | |
| 1698 | ptr_c += diff; |
| 1699 | a_int = BC_NUM_INT(a); |
| 1700 | b_int = BC_NUM_INT(b); |
| 1701 | |
| 1702 | if (a_int > b_int) { |
| 1703 | min_int = b_int; |
| 1704 | max = a_int; |
| 1705 | ptr = ptr_a; |
| 1706 | } |
| 1707 | else { |
| 1708 | min_int = a_int; |
| 1709 | max = b_int; |
| 1710 | ptr = ptr_b; |
| 1711 | } |
| 1712 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1713 | 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] | 1714 | in = ((int) ptr_a[i]) + ((int) ptr_b[i]) + carry; |
| 1715 | carry = in / 10; |
| 1716 | ptr_c[i] = (BcDig)(in % 10); |
| 1717 | } |
| 1718 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1719 | for (; i < max + min_rdx; ++i, ++c->len) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1720 | in = ((int) ptr[i]) + carry; |
| 1721 | carry = in / 10; |
| 1722 | ptr_c[i] = (BcDig)(in % 10); |
| 1723 | } |
| 1724 | |
| 1725 | if (carry != 0) c->num[c->len++] = (BcDig) carry; |
| 1726 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1727 | return BC_STATUS_SUCCESS; // can't make void, see bc_num_binary() |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1728 | } |
| 1729 | |
| 1730 | static BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) |
| 1731 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1732 | ssize_t cmp; |
| 1733 | BcNum *minuend, *subtrahend; |
| 1734 | size_t start; |
| 1735 | bool aneg, bneg, neg; |
| 1736 | |
| 1737 | // Because this function doesn't need to use scale (per the bc spec), |
| 1738 | // I am hijacking it to say whether it's doing an add or a subtract. |
| 1739 | |
| 1740 | if (a->len == 0) { |
| 1741 | bc_num_copy(c, b); |
| 1742 | if (sub && c->len) c->neg = !c->neg; |
| 1743 | return BC_STATUS_SUCCESS; |
| 1744 | } |
| 1745 | else if (b->len == 0) { |
| 1746 | bc_num_copy(c, a); |
| 1747 | return BC_STATUS_SUCCESS; |
| 1748 | } |
| 1749 | |
| 1750 | aneg = a->neg; |
| 1751 | bneg = b->neg; |
| 1752 | a->neg = b->neg = false; |
| 1753 | |
| 1754 | cmp = bc_num_cmp(a, b); |
| 1755 | |
| 1756 | a->neg = aneg; |
| 1757 | b->neg = bneg; |
| 1758 | |
| 1759 | if (cmp == 0) { |
| 1760 | bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx)); |
| 1761 | return BC_STATUS_SUCCESS; |
| 1762 | } |
| 1763 | else if (cmp > 0) { |
| 1764 | neg = a->neg; |
| 1765 | minuend = a; |
| 1766 | subtrahend = b; |
| 1767 | } |
| 1768 | else { |
| 1769 | neg = b->neg; |
| 1770 | if (sub) neg = !neg; |
| 1771 | minuend = b; |
| 1772 | subtrahend = a; |
| 1773 | } |
| 1774 | |
| 1775 | bc_num_copy(c, minuend); |
| 1776 | c->neg = neg; |
| 1777 | |
| 1778 | if (c->rdx < subtrahend->rdx) { |
| 1779 | bc_num_extend(c, subtrahend->rdx - c->rdx); |
| 1780 | start = 0; |
| 1781 | } |
| 1782 | else |
| 1783 | start = c->rdx - subtrahend->rdx; |
| 1784 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1785 | bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1786 | |
| 1787 | bc_num_clean(c); |
| 1788 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1789 | return BC_STATUS_SUCCESS; // can't make void, see bc_num_binary() |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1790 | } |
| 1791 | |
| 1792 | static BcStatus bc_num_k(BcNum *restrict a, BcNum *restrict b, |
| 1793 | BcNum *restrict c) |
| 1794 | { |
| 1795 | BcStatus s; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1796 | 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] | 1797 | BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1798 | bool aone; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1799 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1800 | if (a->len == 0 || b->len == 0) { |
| 1801 | bc_num_zero(c); |
| 1802 | return BC_STATUS_SUCCESS; |
| 1803 | } |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1804 | aone = BC_NUM_ONE(a); |
| 1805 | if (aone || BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1806 | bc_num_copy(c, aone ? b : a); |
| 1807 | return BC_STATUS_SUCCESS; |
| 1808 | } |
| 1809 | |
| 1810 | if (a->len + b->len < BC_NUM_KARATSUBA_LEN || |
| 1811 | a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN) |
| 1812 | { |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1813 | size_t i, j, len; |
Denys Vlasenko | b3cb901 | 2018-12-05 19:05:32 +0100 | [diff] [blame] | 1814 | unsigned carry; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1815 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1816 | bc_num_expand(c, a->len + b->len + 1); |
| 1817 | |
| 1818 | memset(c->num, 0, sizeof(BcDig) * c->cap); |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1819 | c->len = len = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1820 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1821 | for (i = 0; i < b->len; ++i) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1822 | |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1823 | carry = 0; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1824 | for (j = 0; j < a->len; ++j) { |
Denys Vlasenko | b3cb901 | 2018-12-05 19:05:32 +0100 | [diff] [blame] | 1825 | unsigned in = c->num[i + j]; |
| 1826 | in += ((unsigned) a->num[j]) * ((unsigned) b->num[i]) + carry; |
| 1827 | // note: compilers prefer _unsigned_ div/const |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1828 | carry = in / 10; |
| 1829 | c->num[i + j] = (BcDig)(in % 10); |
| 1830 | } |
| 1831 | |
| 1832 | c->num[i + j] += (BcDig) carry; |
| 1833 | len = BC_MAX(len, i + j + !!carry); |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 1834 | |
| 1835 | // a=2^1000000 |
| 1836 | // a*a <- without check below, this will not be interruptible |
| 1837 | if (G_interrupt) return BC_STATUS_FAILURE; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | c->len = len; |
| 1841 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1842 | return BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | bc_num_init(&l1, max); |
| 1846 | bc_num_init(&h1, max); |
| 1847 | bc_num_init(&l2, max); |
| 1848 | bc_num_init(&h2, max); |
| 1849 | bc_num_init(&m1, max); |
| 1850 | bc_num_init(&m2, max); |
| 1851 | bc_num_init(&z0, max); |
| 1852 | bc_num_init(&z1, max); |
| 1853 | bc_num_init(&z2, max); |
| 1854 | bc_num_init(&temp, max + max); |
| 1855 | |
| 1856 | bc_num_split(a, max2, &l1, &h1); |
| 1857 | bc_num_split(b, max2, &l2, &h2); |
| 1858 | |
| 1859 | s = bc_num_add(&h1, &l1, &m1, 0); |
| 1860 | if (s) goto err; |
| 1861 | s = bc_num_add(&h2, &l2, &m2, 0); |
| 1862 | if (s) goto err; |
| 1863 | |
| 1864 | s = bc_num_k(&h1, &h2, &z0); |
| 1865 | if (s) goto err; |
| 1866 | s = bc_num_k(&m1, &m2, &z1); |
| 1867 | if (s) goto err; |
| 1868 | s = bc_num_k(&l1, &l2, &z2); |
| 1869 | if (s) goto err; |
| 1870 | |
| 1871 | s = bc_num_sub(&z1, &z0, &temp, 0); |
| 1872 | if (s) goto err; |
| 1873 | s = bc_num_sub(&temp, &z2, &z1, 0); |
| 1874 | if (s) goto err; |
| 1875 | |
| 1876 | s = bc_num_shift(&z0, max2 * 2); |
| 1877 | if (s) goto err; |
| 1878 | s = bc_num_shift(&z1, max2); |
| 1879 | if (s) goto err; |
| 1880 | s = bc_num_add(&z0, &z1, &temp, 0); |
| 1881 | if (s) goto err; |
| 1882 | s = bc_num_add(&temp, &z2, c, 0); |
| 1883 | |
| 1884 | err: |
| 1885 | bc_num_free(&temp); |
| 1886 | bc_num_free(&z2); |
| 1887 | bc_num_free(&z1); |
| 1888 | bc_num_free(&z0); |
| 1889 | bc_num_free(&m2); |
| 1890 | bc_num_free(&m1); |
| 1891 | bc_num_free(&h2); |
| 1892 | bc_num_free(&l2); |
| 1893 | bc_num_free(&h1); |
| 1894 | bc_num_free(&l1); |
| 1895 | return s; |
| 1896 | } |
| 1897 | |
| 1898 | static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
| 1899 | { |
| 1900 | BcStatus s; |
| 1901 | BcNum cpa, cpb; |
| 1902 | size_t maxrdx = BC_MAX(a->rdx, b->rdx); |
| 1903 | |
| 1904 | scale = BC_MAX(scale, a->rdx); |
| 1905 | scale = BC_MAX(scale, b->rdx); |
| 1906 | scale = BC_MIN(a->rdx + b->rdx, scale); |
| 1907 | maxrdx = BC_MAX(maxrdx, scale); |
| 1908 | |
| 1909 | bc_num_init(&cpa, a->len); |
| 1910 | bc_num_init(&cpb, b->len); |
| 1911 | |
| 1912 | bc_num_copy(&cpa, a); |
| 1913 | bc_num_copy(&cpb, b); |
| 1914 | cpa.neg = cpb.neg = false; |
| 1915 | |
| 1916 | s = bc_num_shift(&cpa, maxrdx); |
| 1917 | if (s) goto err; |
| 1918 | s = bc_num_shift(&cpb, maxrdx); |
| 1919 | if (s) goto err; |
| 1920 | s = bc_num_k(&cpa, &cpb, c); |
| 1921 | if (s) goto err; |
| 1922 | |
| 1923 | maxrdx += scale; |
| 1924 | bc_num_expand(c, c->len + maxrdx); |
| 1925 | |
| 1926 | if (c->len < maxrdx) { |
| 1927 | memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig)); |
| 1928 | c->len += maxrdx; |
| 1929 | } |
| 1930 | |
| 1931 | c->rdx = maxrdx; |
| 1932 | bc_num_retireMul(c, scale, a->neg, b->neg); |
| 1933 | |
| 1934 | err: |
| 1935 | bc_num_free(&cpb); |
| 1936 | bc_num_free(&cpa); |
| 1937 | return s; |
| 1938 | } |
| 1939 | |
| 1940 | static BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
| 1941 | { |
| 1942 | BcStatus s = BC_STATUS_SUCCESS; |
| 1943 | BcDig *n, *p, q; |
| 1944 | size_t len, end, i; |
| 1945 | BcNum cp; |
| 1946 | bool zero = true; |
| 1947 | |
| 1948 | if (b->len == 0) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 1949 | return bc_error("divide by zero"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1950 | else if (a->len == 0) { |
| 1951 | bc_num_setToZero(c, scale); |
| 1952 | return BC_STATUS_SUCCESS; |
| 1953 | } |
| 1954 | else if (BC_NUM_ONE(b)) { |
| 1955 | bc_num_copy(c, a); |
| 1956 | bc_num_retireMul(c, scale, a->neg, b->neg); |
| 1957 | return BC_STATUS_SUCCESS; |
| 1958 | } |
| 1959 | |
| 1960 | bc_num_init(&cp, BC_NUM_MREQ(a, b, scale)); |
| 1961 | bc_num_copy(&cp, a); |
| 1962 | len = b->len; |
| 1963 | |
| 1964 | if (len > cp.len) { |
| 1965 | bc_num_expand(&cp, len + 2); |
| 1966 | bc_num_extend(&cp, len - cp.len); |
| 1967 | } |
| 1968 | |
| 1969 | if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx); |
| 1970 | cp.rdx -= b->rdx; |
| 1971 | if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx); |
| 1972 | |
| 1973 | if (b->rdx == b->len) { |
| 1974 | for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1]; |
| 1975 | len -= i - 1; |
| 1976 | } |
| 1977 | |
| 1978 | if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1); |
| 1979 | |
| 1980 | // We want an extra zero in front to make things simpler. |
| 1981 | cp.num[cp.len++] = 0; |
| 1982 | end = cp.len - len; |
| 1983 | |
| 1984 | bc_num_expand(c, cp.len); |
| 1985 | |
| 1986 | bc_num_zero(c); |
| 1987 | memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig)); |
| 1988 | c->rdx = cp.rdx; |
| 1989 | c->len = cp.len; |
| 1990 | p = b->num; |
| 1991 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1992 | for (i = end - 1; !s && i < end; --i) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1993 | n = cp.num + i; |
| 1994 | 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] | 1995 | bc_num_subArrays(n, p, len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1996 | c->num[i] = q; |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 1997 | // a=2^100000 |
| 1998 | // scale=40000 |
| 1999 | // 1/a <- without check below, this will not be interruptible |
| 2000 | if (G_interrupt) { |
| 2001 | s = BC_STATUS_FAILURE; |
| 2002 | break; |
| 2003 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2004 | } |
| 2005 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2006 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2007 | bc_num_free(&cp); |
| 2008 | |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 2009 | return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2010 | } |
| 2011 | |
| 2012 | static BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c, |
| 2013 | BcNum *restrict d, size_t scale, size_t ts) |
| 2014 | { |
| 2015 | BcStatus s; |
| 2016 | BcNum temp; |
| 2017 | bool neg; |
| 2018 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2019 | if (b->len == 0) |
| 2020 | return bc_error("divide by zero"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2021 | |
| 2022 | if (a->len == 0) { |
| 2023 | bc_num_setToZero(d, ts); |
| 2024 | return BC_STATUS_SUCCESS; |
| 2025 | } |
| 2026 | |
| 2027 | bc_num_init(&temp, d->cap); |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 2028 | s = bc_num_d(a, b, c, scale); |
| 2029 | if (s) goto err; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2030 | |
| 2031 | if (scale != 0) scale = ts; |
| 2032 | |
| 2033 | s = bc_num_m(c, b, &temp, scale); |
| 2034 | if (s) goto err; |
| 2035 | s = bc_num_sub(a, &temp, d, scale); |
| 2036 | if (s) goto err; |
| 2037 | |
| 2038 | if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx); |
| 2039 | |
| 2040 | neg = d->neg; |
| 2041 | bc_num_retireMul(d, ts, a->neg, b->neg); |
| 2042 | d->neg = neg; |
| 2043 | |
| 2044 | err: |
| 2045 | bc_num_free(&temp); |
| 2046 | return s; |
| 2047 | } |
| 2048 | |
| 2049 | static BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
| 2050 | { |
| 2051 | BcStatus s; |
| 2052 | BcNum c1; |
| 2053 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 2054 | |
| 2055 | bc_num_init(&c1, len); |
| 2056 | s = bc_num_r(a, b, &c1, c, scale, ts); |
| 2057 | bc_num_free(&c1); |
| 2058 | |
| 2059 | return s; |
| 2060 | } |
| 2061 | |
| 2062 | static BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
| 2063 | { |
| 2064 | BcStatus s = BC_STATUS_SUCCESS; |
| 2065 | BcNum copy; |
| 2066 | unsigned long pow; |
| 2067 | size_t i, powrdx, resrdx; |
| 2068 | bool neg, zero; |
| 2069 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2070 | if (b->rdx) return bc_error("non integer number"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2071 | |
| 2072 | if (b->len == 0) { |
| 2073 | bc_num_one(c); |
| 2074 | return BC_STATUS_SUCCESS; |
| 2075 | } |
| 2076 | else if (a->len == 0) { |
| 2077 | bc_num_setToZero(c, scale); |
| 2078 | return BC_STATUS_SUCCESS; |
| 2079 | } |
| 2080 | else if (BC_NUM_ONE(b)) { |
| 2081 | if (!b->neg) |
| 2082 | bc_num_copy(c, a); |
| 2083 | else |
| 2084 | s = bc_num_inv(a, c, scale); |
| 2085 | return s; |
| 2086 | } |
| 2087 | |
| 2088 | neg = b->neg; |
| 2089 | b->neg = false; |
| 2090 | |
| 2091 | s = bc_num_ulong(b, &pow); |
| 2092 | if (s) return s; |
| 2093 | |
| 2094 | bc_num_init(©, a->len); |
| 2095 | bc_num_copy(©, a); |
| 2096 | |
Denys Vlasenko | 2d615fe | 2018-12-07 16:22:45 +0100 | [diff] [blame] | 2097 | if (!neg) { |
| 2098 | if (a->rdx > scale) |
| 2099 | scale = a->rdx; |
| 2100 | if (a->rdx * pow < scale) |
| 2101 | scale = a->rdx * pow; |
| 2102 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2103 | |
| 2104 | b->neg = neg; |
| 2105 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2106 | for (powrdx = a->rdx; !(pow & 1); pow >>= 1) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2107 | powrdx <<= 1; |
| 2108 | s = bc_num_mul(©, ©, ©, powrdx); |
| 2109 | if (s) goto err; |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 2110 | // Not needed: bc_num_mul() has a check for ^C: |
| 2111 | //if (G_interrupt) { |
| 2112 | // s = BC_STATUS_FAILURE; |
| 2113 | // goto err; |
| 2114 | //} |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2115 | } |
| 2116 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2117 | bc_num_copy(c, ©); |
| 2118 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2119 | for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2120 | |
| 2121 | powrdx <<= 1; |
| 2122 | s = bc_num_mul(©, ©, ©, powrdx); |
| 2123 | if (s) goto err; |
| 2124 | |
| 2125 | if (pow & 1) { |
| 2126 | resrdx += powrdx; |
| 2127 | s = bc_num_mul(c, ©, c, resrdx); |
| 2128 | if (s) goto err; |
| 2129 | } |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 2130 | // Not needed: bc_num_mul() has a check for ^C: |
| 2131 | //if (G_interrupt) { |
| 2132 | // s = BC_STATUS_FAILURE; |
| 2133 | // goto err; |
| 2134 | //} |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2135 | } |
| 2136 | |
| 2137 | if (neg) { |
| 2138 | s = bc_num_inv(c, c, scale); |
| 2139 | if (s) goto err; |
| 2140 | } |
| 2141 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2142 | if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale); |
| 2143 | |
| 2144 | // We can't use bc_num_clean() here. |
| 2145 | for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i]; |
| 2146 | if (zero) bc_num_setToZero(c, scale); |
| 2147 | |
| 2148 | err: |
| 2149 | bc_num_free(©); |
| 2150 | return s; |
| 2151 | } |
| 2152 | |
| 2153 | static BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale, |
| 2154 | BcNumBinaryOp op, size_t req) |
| 2155 | { |
| 2156 | BcStatus s; |
| 2157 | BcNum num2, *ptr_a, *ptr_b; |
| 2158 | bool init = false; |
| 2159 | |
| 2160 | if (c == a) { |
| 2161 | ptr_a = &num2; |
| 2162 | memcpy(ptr_a, c, sizeof(BcNum)); |
| 2163 | init = true; |
| 2164 | } |
| 2165 | else |
| 2166 | ptr_a = a; |
| 2167 | |
| 2168 | if (c == b) { |
| 2169 | ptr_b = &num2; |
| 2170 | if (c != a) { |
| 2171 | memcpy(ptr_b, c, sizeof(BcNum)); |
| 2172 | init = true; |
| 2173 | } |
| 2174 | } |
| 2175 | else |
| 2176 | ptr_b = b; |
| 2177 | |
| 2178 | if (init) |
| 2179 | bc_num_init(c, req); |
| 2180 | else |
| 2181 | bc_num_expand(c, req); |
| 2182 | |
| 2183 | s = op(ptr_a, ptr_b, c, scale); |
| 2184 | |
| 2185 | if (init) bc_num_free(&num2); |
| 2186 | |
| 2187 | return s; |
| 2188 | } |
| 2189 | |
| 2190 | static bool bc_num_strValid(const char *val, size_t base) |
| 2191 | { |
| 2192 | BcDig b; |
| 2193 | bool small, radix = false; |
| 2194 | size_t i, len = strlen(val); |
| 2195 | |
| 2196 | if (!len) return true; |
| 2197 | |
| 2198 | small = base <= 10; |
| 2199 | b = (BcDig)(small ? base + '0' : base - 10 + 'A'); |
| 2200 | |
| 2201 | for (i = 0; i < len; ++i) { |
| 2202 | |
| 2203 | BcDig c = val[i]; |
| 2204 | |
| 2205 | if (c == '.') { |
| 2206 | |
| 2207 | if (radix) return false; |
| 2208 | |
| 2209 | radix = true; |
| 2210 | continue; |
| 2211 | } |
| 2212 | |
| 2213 | if (c < '0' || (small && c >= b) || (c > '9' && (c < 'A' || c >= b))) |
| 2214 | return false; |
| 2215 | } |
| 2216 | |
| 2217 | return true; |
| 2218 | } |
| 2219 | |
| 2220 | static void bc_num_parseDecimal(BcNum *n, const char *val) |
| 2221 | { |
| 2222 | size_t len, i; |
| 2223 | const char *ptr; |
| 2224 | bool zero = true; |
| 2225 | |
| 2226 | for (i = 0; val[i] == '0'; ++i); |
| 2227 | |
| 2228 | val += i; |
| 2229 | len = strlen(val); |
| 2230 | bc_num_zero(n); |
| 2231 | |
| 2232 | if (len != 0) { |
| 2233 | for (i = 0; zero && i < len; ++i) zero = val[i] == '0' || val[i] == '.'; |
| 2234 | bc_num_expand(n, len); |
| 2235 | } |
| 2236 | |
| 2237 | ptr = strchr(val, '.'); |
| 2238 | |
Denys Vlasenko | d5f7703 | 2018-12-04 21:37:56 +0100 | [diff] [blame] | 2239 | n->rdx = 0; |
| 2240 | if (ptr != NULL) |
| 2241 | n->rdx = (size_t)((val + len) - (ptr + 1)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2242 | |
| 2243 | if (!zero) { |
| 2244 | for (i = len - 1; i < len; ++n->len, i -= 1 + (i && val[i - 1] == '.')) |
| 2245 | n->num[n->len] = val[i] - '0'; |
| 2246 | } |
| 2247 | } |
| 2248 | |
| 2249 | static void bc_num_parseBase(BcNum *n, const char *val, BcNum *base) |
| 2250 | { |
| 2251 | BcStatus s; |
| 2252 | BcNum temp, mult, result; |
| 2253 | BcDig c = '\0'; |
| 2254 | bool zero = true; |
| 2255 | unsigned long v; |
| 2256 | size_t i, digits, len = strlen(val); |
| 2257 | |
| 2258 | bc_num_zero(n); |
| 2259 | |
| 2260 | for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0'); |
| 2261 | if (zero) return; |
| 2262 | |
| 2263 | bc_num_init(&temp, BC_NUM_DEF_SIZE); |
| 2264 | bc_num_init(&mult, BC_NUM_DEF_SIZE); |
| 2265 | |
| 2266 | for (i = 0; i < len; ++i) { |
| 2267 | |
| 2268 | c = val[i]; |
| 2269 | if (c == '.') break; |
| 2270 | |
| 2271 | v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10); |
| 2272 | |
| 2273 | s = bc_num_mul(n, base, &mult, 0); |
| 2274 | if (s) goto int_err; |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 2275 | bc_num_ulong2num(&temp, v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2276 | s = bc_num_add(&mult, &temp, n, 0); |
| 2277 | if (s) goto int_err; |
| 2278 | } |
| 2279 | |
| 2280 | if (i == len) { |
| 2281 | c = val[i]; |
| 2282 | if (c == 0) goto int_err; |
| 2283 | } |
| 2284 | |
| 2285 | bc_num_init(&result, base->len); |
| 2286 | bc_num_zero(&result); |
| 2287 | bc_num_one(&mult); |
| 2288 | |
| 2289 | for (i += 1, digits = 0; i < len; ++i, ++digits) { |
| 2290 | |
| 2291 | c = val[i]; |
| 2292 | if (c == 0) break; |
| 2293 | |
| 2294 | v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10); |
| 2295 | |
| 2296 | s = bc_num_mul(&result, base, &result, 0); |
| 2297 | if (s) goto err; |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 2298 | bc_num_ulong2num(&temp, v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2299 | s = bc_num_add(&result, &temp, &result, 0); |
| 2300 | if (s) goto err; |
| 2301 | s = bc_num_mul(&mult, base, &mult, 0); |
| 2302 | if (s) goto err; |
| 2303 | } |
| 2304 | |
| 2305 | s = bc_num_div(&result, &mult, &result, digits); |
| 2306 | if (s) goto err; |
| 2307 | s = bc_num_add(n, &result, n, digits); |
| 2308 | if (s) goto err; |
| 2309 | |
| 2310 | if (n->len != 0) { |
| 2311 | if (n->rdx < digits) bc_num_extend(n, digits - n->rdx); |
| 2312 | } |
| 2313 | else |
| 2314 | bc_num_zero(n); |
| 2315 | |
| 2316 | err: |
| 2317 | bc_num_free(&result); |
| 2318 | int_err: |
| 2319 | bc_num_free(&mult); |
| 2320 | bc_num_free(&temp); |
| 2321 | } |
| 2322 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2323 | static void bc_num_printNewline(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2324 | { |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2325 | if (G.prog.nchars == G.prog.len - 1) { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2326 | bb_putchar('\\'); |
| 2327 | bb_putchar('\n'); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2328 | G.prog.nchars = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2329 | } |
| 2330 | } |
| 2331 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2332 | #if ENABLE_DC |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2333 | static void bc_num_printChar(size_t num, size_t width, bool radix) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2334 | { |
Denys Vlasenko | 2a8ad48 | 2018-12-08 21:56:37 +0100 | [diff] [blame] | 2335 | (void) radix; |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2336 | bb_putchar((char) num); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2337 | G.prog.nchars += width; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2338 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2339 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2340 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2341 | static void bc_num_printDigits(size_t num, size_t width, bool radix) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2342 | { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2343 | size_t exp, pow; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2344 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2345 | bc_num_printNewline(); |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2346 | bb_putchar(radix ? '.' : ' '); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2347 | ++G.prog.nchars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2348 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2349 | bc_num_printNewline(); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2350 | for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10) |
| 2351 | continue; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2352 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2353 | for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2354 | size_t dig; |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2355 | bc_num_printNewline(); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2356 | dig = num / pow; |
| 2357 | num -= dig * pow; |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2358 | bb_putchar(((char) dig) + '0'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2359 | } |
| 2360 | } |
| 2361 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2362 | static void bc_num_printHex(size_t num, size_t width, bool radix) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2363 | { |
| 2364 | if (radix) { |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2365 | bc_num_printNewline(); |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2366 | bb_putchar('.'); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2367 | G.prog.nchars += 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2368 | } |
| 2369 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2370 | bc_num_printNewline(); |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2371 | bb_putchar(bb_hexdigits_upcase[num]); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2372 | G.prog.nchars += width; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2373 | } |
| 2374 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2375 | static void bc_num_printDecimal(BcNum *n) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2376 | { |
| 2377 | size_t i, rdx = n->rdx - 1; |
| 2378 | |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2379 | if (n->neg) bb_putchar('-'); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2380 | G.prog.nchars += n->neg; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2381 | |
| 2382 | for (i = n->len - 1; i < n->len; --i) |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2383 | bc_num_printHex((size_t) n->num[i], 1, i == rdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2384 | } |
| 2385 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2386 | static BcStatus bc_num_printNum(BcNum *n, BcNum *base, size_t width, BcNumDigitOp print) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2387 | { |
| 2388 | BcStatus s; |
| 2389 | BcVec stack; |
| 2390 | BcNum intp, fracp, digit, frac_len; |
| 2391 | unsigned long dig, *ptr; |
| 2392 | size_t i; |
| 2393 | bool radix; |
| 2394 | |
| 2395 | if (n->len == 0) { |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2396 | print(0, width, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2397 | return BC_STATUS_SUCCESS; |
| 2398 | } |
| 2399 | |
| 2400 | bc_vec_init(&stack, sizeof(long), NULL); |
| 2401 | bc_num_init(&intp, n->len); |
| 2402 | bc_num_init(&fracp, n->rdx); |
| 2403 | bc_num_init(&digit, width); |
| 2404 | bc_num_init(&frac_len, BC_NUM_INT(n)); |
| 2405 | bc_num_copy(&intp, n); |
| 2406 | bc_num_one(&frac_len); |
| 2407 | |
| 2408 | bc_num_truncate(&intp, intp.rdx); |
| 2409 | s = bc_num_sub(n, &intp, &fracp, 0); |
| 2410 | if (s) goto err; |
| 2411 | |
| 2412 | while (intp.len != 0) { |
| 2413 | s = bc_num_divmod(&intp, base, &intp, &digit, 0); |
| 2414 | if (s) goto err; |
| 2415 | s = bc_num_ulong(&digit, &dig); |
| 2416 | if (s) goto err; |
| 2417 | bc_vec_push(&stack, &dig); |
| 2418 | } |
| 2419 | |
| 2420 | for (i = 0; i < stack.len; ++i) { |
| 2421 | ptr = bc_vec_item_rev(&stack, i); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2422 | print(*ptr, width, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2423 | } |
| 2424 | |
| 2425 | if (!n->rdx) goto err; |
| 2426 | |
| 2427 | for (radix = true; frac_len.len <= n->rdx; radix = false) { |
| 2428 | s = bc_num_mul(&fracp, base, &fracp, n->rdx); |
| 2429 | if (s) goto err; |
| 2430 | s = bc_num_ulong(&fracp, &dig); |
| 2431 | if (s) goto err; |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 2432 | bc_num_ulong2num(&intp, dig); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2433 | s = bc_num_sub(&fracp, &intp, &fracp, 0); |
| 2434 | if (s) goto err; |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2435 | print(dig, width, radix); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2436 | s = bc_num_mul(&frac_len, base, &frac_len, 0); |
| 2437 | if (s) goto err; |
| 2438 | } |
| 2439 | |
| 2440 | err: |
| 2441 | bc_num_free(&frac_len); |
| 2442 | bc_num_free(&digit); |
| 2443 | bc_num_free(&fracp); |
| 2444 | bc_num_free(&intp); |
| 2445 | bc_vec_free(&stack); |
| 2446 | return s; |
| 2447 | } |
| 2448 | |
Denys Vlasenko | 0f37b32 | 2018-12-08 23:48:53 +0100 | [diff] [blame] | 2449 | static BcStatus bc_num_printBase(BcNum *n) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2450 | { |
| 2451 | BcStatus s; |
| 2452 | size_t width, i; |
| 2453 | BcNumDigitOp print; |
| 2454 | bool neg = n->neg; |
| 2455 | |
Denys Vlasenko | 0f37b32 | 2018-12-08 23:48:53 +0100 | [diff] [blame] | 2456 | if (neg) { |
| 2457 | bb_putchar('-'); |
| 2458 | G.prog.nchars++; |
| 2459 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2460 | |
| 2461 | n->neg = false; |
| 2462 | |
Denys Vlasenko | 0f37b32 | 2018-12-08 23:48:53 +0100 | [diff] [blame] | 2463 | if (G.prog.ob_t <= BC_NUM_MAX_IBASE) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2464 | width = 1; |
| 2465 | print = bc_num_printHex; |
| 2466 | } |
| 2467 | else { |
Denys Vlasenko | 0f37b32 | 2018-12-08 23:48:53 +0100 | [diff] [blame] | 2468 | for (i = G.prog.ob_t - 1, width = 0; i != 0; i /= 10, ++width) |
| 2469 | continue; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2470 | print = bc_num_printDigits; |
| 2471 | } |
| 2472 | |
Denys Vlasenko | 0f37b32 | 2018-12-08 23:48:53 +0100 | [diff] [blame] | 2473 | s = bc_num_printNum(n, &G.prog.ob, width, print); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2474 | n->neg = neg; |
| 2475 | |
| 2476 | return s; |
| 2477 | } |
| 2478 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2479 | #if ENABLE_DC |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2480 | static BcStatus bc_num_stream(BcNum *n, BcNum *base) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2481 | { |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2482 | return bc_num_printNum(n, base, 1, bc_num_printChar); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2483 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2484 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2485 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2486 | static BcStatus bc_num_parse(BcNum *n, const char *val, BcNum *base, |
| 2487 | size_t base_t) |
| 2488 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2489 | if (!bc_num_strValid(val, base_t)) |
| 2490 | return bc_error("bad number string"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2491 | |
| 2492 | if (base_t == 10) |
| 2493 | bc_num_parseDecimal(n, val); |
| 2494 | else |
| 2495 | bc_num_parseBase(n, val, base); |
| 2496 | |
| 2497 | return BC_STATUS_SUCCESS; |
| 2498 | } |
| 2499 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2500 | static BcStatus bc_num_print(BcNum *n, bool newline) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2501 | { |
| 2502 | BcStatus s = BC_STATUS_SUCCESS; |
| 2503 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2504 | bc_num_printNewline(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2505 | |
| 2506 | if (n->len == 0) { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2507 | bb_putchar('0'); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2508 | ++G.prog.nchars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2509 | } |
Denys Vlasenko | 0f37b32 | 2018-12-08 23:48:53 +0100 | [diff] [blame] | 2510 | else if (G.prog.ob_t == 10) |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2511 | bc_num_printDecimal(n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2512 | else |
Denys Vlasenko | 0f37b32 | 2018-12-08 23:48:53 +0100 | [diff] [blame] | 2513 | s = bc_num_printBase(n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2514 | |
| 2515 | if (newline) { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 2516 | bb_putchar('\n'); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 2517 | G.prog.nchars = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2518 | } |
| 2519 | |
| 2520 | return s; |
| 2521 | } |
| 2522 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2523 | static BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 2524 | { |
| 2525 | BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s; |
| 2526 | (void) scale; |
| 2527 | return bc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)); |
| 2528 | } |
| 2529 | |
| 2530 | static BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 2531 | { |
| 2532 | BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a; |
| 2533 | (void) scale; |
| 2534 | return bc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)); |
| 2535 | } |
| 2536 | |
| 2537 | static BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 2538 | { |
| 2539 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 2540 | return bc_num_binary(a, b, c, scale, bc_num_m, req); |
| 2541 | } |
| 2542 | |
| 2543 | static BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 2544 | { |
| 2545 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 2546 | return bc_num_binary(a, b, c, scale, bc_num_d, req); |
| 2547 | } |
| 2548 | |
| 2549 | static BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 2550 | { |
| 2551 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 2552 | return bc_num_binary(a, b, c, scale, bc_num_rem, req); |
| 2553 | } |
| 2554 | |
| 2555 | static BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 2556 | { |
| 2557 | return bc_num_binary(a, b, c, scale, bc_num_p, a->len * b->len + 1); |
| 2558 | } |
| 2559 | |
| 2560 | static BcStatus bc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale) |
| 2561 | { |
| 2562 | BcStatus s; |
| 2563 | BcNum num1, num2, half, f, fprime, *x0, *x1, *temp; |
| 2564 | size_t pow, len, digs, digs1, resrdx, req, times = 0; |
| 2565 | ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX; |
| 2566 | |
| 2567 | req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1; |
| 2568 | bc_num_expand(b, req); |
| 2569 | |
| 2570 | if (a->len == 0) { |
| 2571 | bc_num_setToZero(b, scale); |
| 2572 | return BC_STATUS_SUCCESS; |
| 2573 | } |
| 2574 | else if (a->neg) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2575 | return bc_error("negative number"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2576 | else if (BC_NUM_ONE(a)) { |
| 2577 | bc_num_one(b); |
| 2578 | bc_num_extend(b, scale); |
| 2579 | return BC_STATUS_SUCCESS; |
| 2580 | } |
| 2581 | |
| 2582 | scale = BC_MAX(scale, a->rdx) + 1; |
| 2583 | len = a->len + scale; |
| 2584 | |
| 2585 | bc_num_init(&num1, len); |
| 2586 | bc_num_init(&num2, len); |
| 2587 | bc_num_init(&half, BC_NUM_DEF_SIZE); |
| 2588 | |
| 2589 | bc_num_one(&half); |
| 2590 | half.num[0] = 5; |
| 2591 | half.rdx = 1; |
| 2592 | |
| 2593 | bc_num_init(&f, len); |
| 2594 | bc_num_init(&fprime, len); |
| 2595 | |
| 2596 | x0 = &num1; |
| 2597 | x1 = &num2; |
| 2598 | |
| 2599 | bc_num_one(x0); |
| 2600 | pow = BC_NUM_INT(a); |
| 2601 | |
| 2602 | if (pow) { |
| 2603 | |
| 2604 | if (pow & 1) |
| 2605 | x0->num[0] = 2; |
| 2606 | else |
| 2607 | x0->num[0] = 6; |
| 2608 | |
| 2609 | pow -= 2 - (pow & 1); |
| 2610 | |
| 2611 | bc_num_extend(x0, pow); |
| 2612 | |
| 2613 | // Make sure to move the radix back. |
| 2614 | x0->rdx -= pow; |
| 2615 | } |
| 2616 | |
| 2617 | x0->rdx = digs = digs1 = 0; |
| 2618 | resrdx = scale + 2; |
| 2619 | len = BC_NUM_INT(x0) + resrdx - 1; |
| 2620 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2621 | while (cmp != 0 || digs < len) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2622 | |
| 2623 | s = bc_num_div(a, x0, &f, resrdx); |
| 2624 | if (s) goto err; |
| 2625 | s = bc_num_add(x0, &f, &fprime, resrdx); |
| 2626 | if (s) goto err; |
| 2627 | s = bc_num_mul(&fprime, &half, x1, resrdx); |
| 2628 | if (s) goto err; |
| 2629 | |
| 2630 | cmp = bc_num_cmp(x1, x0); |
| 2631 | digs = x1->len - (unsigned long long) llabs(cmp); |
| 2632 | |
| 2633 | if (cmp == cmp2 && digs == digs1) |
| 2634 | times += 1; |
| 2635 | else |
| 2636 | times = 0; |
| 2637 | |
| 2638 | resrdx += times > 4; |
| 2639 | |
| 2640 | cmp2 = cmp1; |
| 2641 | cmp1 = cmp; |
| 2642 | digs1 = digs; |
| 2643 | |
| 2644 | temp = x0; |
| 2645 | x0 = x1; |
| 2646 | x1 = temp; |
| 2647 | } |
| 2648 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2649 | bc_num_copy(b, x0); |
| 2650 | scale -= 1; |
| 2651 | if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale); |
| 2652 | |
| 2653 | err: |
| 2654 | bc_num_free(&fprime); |
| 2655 | bc_num_free(&f); |
| 2656 | bc_num_free(&half); |
| 2657 | bc_num_free(&num2); |
| 2658 | bc_num_free(&num1); |
| 2659 | return s; |
| 2660 | } |
| 2661 | |
| 2662 | static BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, |
| 2663 | size_t scale) |
| 2664 | { |
| 2665 | BcStatus s; |
| 2666 | BcNum num2, *ptr_a; |
| 2667 | bool init = false; |
| 2668 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 2669 | |
| 2670 | if (c == a) { |
| 2671 | memcpy(&num2, c, sizeof(BcNum)); |
| 2672 | ptr_a = &num2; |
| 2673 | bc_num_init(c, len); |
| 2674 | init = true; |
| 2675 | } |
| 2676 | else { |
| 2677 | ptr_a = a; |
| 2678 | bc_num_expand(c, len); |
| 2679 | } |
| 2680 | |
| 2681 | s = bc_num_r(ptr_a, b, c, d, scale, ts); |
| 2682 | |
| 2683 | if (init) bc_num_free(&num2); |
| 2684 | |
| 2685 | return s; |
| 2686 | } |
| 2687 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2688 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2689 | static BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) |
| 2690 | { |
| 2691 | BcStatus s; |
| 2692 | BcNum base, exp, two, temp; |
| 2693 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2694 | if (c->len == 0) |
| 2695 | return bc_error("divide by zero"); |
| 2696 | if (a->rdx || b->rdx || c->rdx) |
| 2697 | return bc_error("non integer number"); |
| 2698 | if (b->neg) |
| 2699 | return bc_error("negative number"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2700 | |
| 2701 | bc_num_expand(d, c->len); |
| 2702 | bc_num_init(&base, c->len); |
| 2703 | bc_num_init(&exp, b->len); |
| 2704 | bc_num_init(&two, BC_NUM_DEF_SIZE); |
| 2705 | bc_num_init(&temp, b->len); |
| 2706 | |
| 2707 | bc_num_one(&two); |
| 2708 | two.num[0] = 2; |
| 2709 | bc_num_one(d); |
| 2710 | |
| 2711 | s = bc_num_rem(a, c, &base, 0); |
| 2712 | if (s) goto err; |
| 2713 | bc_num_copy(&exp, b); |
| 2714 | |
| 2715 | while (exp.len != 0) { |
| 2716 | |
| 2717 | s = bc_num_divmod(&exp, &two, &exp, &temp, 0); |
| 2718 | if (s) goto err; |
| 2719 | |
| 2720 | if (BC_NUM_ONE(&temp)) { |
| 2721 | s = bc_num_mul(d, &base, &temp, 0); |
| 2722 | if (s) goto err; |
| 2723 | s = bc_num_rem(&temp, c, d, 0); |
| 2724 | if (s) goto err; |
| 2725 | } |
| 2726 | |
| 2727 | s = bc_num_mul(&base, &base, &temp, 0); |
| 2728 | if (s) goto err; |
| 2729 | s = bc_num_rem(&temp, c, &base, 0); |
| 2730 | if (s) goto err; |
| 2731 | } |
| 2732 | |
| 2733 | err: |
| 2734 | bc_num_free(&temp); |
| 2735 | bc_num_free(&two); |
| 2736 | bc_num_free(&exp); |
| 2737 | bc_num_free(&base); |
| 2738 | return s; |
| 2739 | } |
| 2740 | #endif // ENABLE_DC |
| 2741 | |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 2742 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2743 | static BcStatus bc_func_insert(BcFunc *f, char *name, bool var) |
| 2744 | { |
| 2745 | BcId a; |
| 2746 | size_t i; |
| 2747 | |
| 2748 | for (i = 0; i < f->autos.len; ++i) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2749 | if (strcmp(name, ((BcId *) bc_vec_item(&f->autos, i))->name) == 0) |
| 2750 | 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] | 2751 | } |
| 2752 | |
| 2753 | a.idx = var; |
| 2754 | a.name = name; |
| 2755 | |
| 2756 | bc_vec_push(&f->autos, &a); |
| 2757 | |
| 2758 | return BC_STATUS_SUCCESS; |
| 2759 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 2760 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2761 | |
| 2762 | static void bc_func_init(BcFunc *f) |
| 2763 | { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 2764 | bc_char_vec_init(&f->code); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2765 | bc_vec_init(&f->autos, sizeof(BcId), bc_id_free); |
| 2766 | bc_vec_init(&f->labels, sizeof(size_t), NULL); |
| 2767 | f->nparams = 0; |
| 2768 | } |
| 2769 | |
| 2770 | static void bc_func_free(void *func) |
| 2771 | { |
| 2772 | BcFunc *f = (BcFunc *) func; |
| 2773 | bc_vec_free(&f->code); |
| 2774 | bc_vec_free(&f->autos); |
| 2775 | bc_vec_free(&f->labels); |
| 2776 | } |
| 2777 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2778 | static void bc_array_expand(BcVec *a, size_t len); |
| 2779 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2780 | static void bc_array_init(BcVec *a, bool nums) |
| 2781 | { |
| 2782 | if (nums) |
| 2783 | bc_vec_init(a, sizeof(BcNum), bc_num_free); |
| 2784 | else |
| 2785 | bc_vec_init(a, sizeof(BcVec), bc_vec_free); |
| 2786 | bc_array_expand(a, 1); |
| 2787 | } |
| 2788 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2789 | static void bc_array_expand(BcVec *a, size_t len) |
| 2790 | { |
| 2791 | BcResultData data; |
| 2792 | |
| 2793 | if (a->size == sizeof(BcNum) && a->dtor == bc_num_free) { |
| 2794 | while (len > a->len) { |
| 2795 | bc_num_init(&data.n, BC_NUM_DEF_SIZE); |
| 2796 | bc_vec_push(a, &data.n); |
| 2797 | } |
| 2798 | } |
| 2799 | else { |
| 2800 | while (len > a->len) { |
| 2801 | bc_array_init(&data.v, true); |
| 2802 | bc_vec_push(a, &data.v); |
| 2803 | } |
| 2804 | } |
| 2805 | } |
| 2806 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2807 | static void bc_array_copy(BcVec *d, const BcVec *s) |
| 2808 | { |
| 2809 | size_t i; |
| 2810 | |
| 2811 | bc_vec_pop_all(d); |
| 2812 | bc_vec_expand(d, s->cap); |
| 2813 | d->len = s->len; |
| 2814 | |
| 2815 | for (i = 0; i < s->len; ++i) { |
| 2816 | BcNum *dnum = bc_vec_item(d, i), *snum = bc_vec_item(s, i); |
| 2817 | bc_num_init(dnum, snum->len); |
| 2818 | bc_num_copy(dnum, snum); |
| 2819 | } |
| 2820 | } |
| 2821 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2822 | static void bc_string_free(void *string) |
| 2823 | { |
| 2824 | free(*((char **) string)); |
| 2825 | } |
| 2826 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2827 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2828 | static void bc_result_copy(BcResult *d, BcResult *src) |
| 2829 | { |
| 2830 | d->t = src->t; |
| 2831 | |
| 2832 | switch (d->t) { |
| 2833 | |
| 2834 | case BC_RESULT_TEMP: |
| 2835 | case BC_RESULT_IBASE: |
| 2836 | case BC_RESULT_SCALE: |
| 2837 | case BC_RESULT_OBASE: |
| 2838 | { |
| 2839 | bc_num_init(&d->d.n, src->d.n.len); |
| 2840 | bc_num_copy(&d->d.n, &src->d.n); |
| 2841 | break; |
| 2842 | } |
| 2843 | |
| 2844 | case BC_RESULT_VAR: |
| 2845 | case BC_RESULT_ARRAY: |
| 2846 | case BC_RESULT_ARRAY_ELEM: |
| 2847 | { |
| 2848 | d->d.id.name = xstrdup(src->d.id.name); |
| 2849 | break; |
| 2850 | } |
| 2851 | |
| 2852 | case BC_RESULT_CONSTANT: |
| 2853 | case BC_RESULT_LAST: |
| 2854 | case BC_RESULT_ONE: |
| 2855 | case BC_RESULT_STR: |
| 2856 | { |
| 2857 | memcpy(&d->d.n, &src->d.n, sizeof(BcNum)); |
| 2858 | break; |
| 2859 | } |
| 2860 | } |
| 2861 | } |
| 2862 | #endif // ENABLE_DC |
| 2863 | |
| 2864 | static void bc_result_free(void *result) |
| 2865 | { |
| 2866 | BcResult *r = (BcResult *) result; |
| 2867 | |
| 2868 | switch (r->t) { |
| 2869 | |
| 2870 | case BC_RESULT_TEMP: |
| 2871 | case BC_RESULT_IBASE: |
| 2872 | case BC_RESULT_SCALE: |
| 2873 | case BC_RESULT_OBASE: |
| 2874 | { |
| 2875 | bc_num_free(&r->d.n); |
| 2876 | break; |
| 2877 | } |
| 2878 | |
| 2879 | case BC_RESULT_VAR: |
| 2880 | case BC_RESULT_ARRAY: |
| 2881 | case BC_RESULT_ARRAY_ELEM: |
| 2882 | { |
| 2883 | free(r->d.id.name); |
| 2884 | break; |
| 2885 | } |
| 2886 | |
| 2887 | default: |
| 2888 | { |
| 2889 | // Do nothing. |
| 2890 | break; |
| 2891 | } |
| 2892 | } |
| 2893 | } |
| 2894 | |
| 2895 | static void bc_lex_lineComment(BcLex *l) |
| 2896 | { |
| 2897 | l->t.t = BC_LEX_WHITESPACE; |
| 2898 | while (l->i < l->len && l->buf[l->i++] != '\n'); |
| 2899 | --l->i; |
| 2900 | } |
| 2901 | |
| 2902 | static void bc_lex_whitespace(BcLex *l) |
| 2903 | { |
| 2904 | char c; |
| 2905 | l->t.t = BC_LEX_WHITESPACE; |
| 2906 | for (c = l->buf[l->i]; c != '\n' && isspace(c); c = l->buf[++l->i]); |
| 2907 | } |
| 2908 | |
| 2909 | static BcStatus bc_lex_number(BcLex *l, char start) |
| 2910 | { |
| 2911 | const char *buf = l->buf + l->i; |
| 2912 | size_t len, hits = 0, bslashes = 0, i = 0, j; |
| 2913 | char c = buf[i]; |
| 2914 | bool last_pt, pt = start == '.'; |
| 2915 | |
| 2916 | last_pt = pt; |
| 2917 | l->t.t = BC_LEX_NUMBER; |
| 2918 | |
| 2919 | while (c != 0 && (isdigit(c) || (c >= 'A' && c <= 'F') || |
| 2920 | (c == '.' && !pt) || (c == '\\' && buf[i + 1] == '\n'))) |
| 2921 | { |
| 2922 | if (c != '\\') { |
| 2923 | last_pt = c == '.'; |
| 2924 | pt = pt || last_pt; |
| 2925 | } |
| 2926 | else { |
| 2927 | ++i; |
| 2928 | bslashes += 1; |
| 2929 | } |
| 2930 | |
| 2931 | c = buf[++i]; |
| 2932 | } |
| 2933 | |
Denys Vlasenko | d5f7703 | 2018-12-04 21:37:56 +0100 | [diff] [blame] | 2934 | len = i + !last_pt - bslashes * 2; |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 2935 | // This check makes sense only if size_t is (much) larger than BC_MAX_NUM. |
| 2936 | if (SIZE_MAX > (BC_MAX_NUM | 0xff)) { |
| 2937 | if (len > BC_MAX_NUM) |
| 2938 | return bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]"); |
| 2939 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2940 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 2941 | bc_vec_pop_all(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2942 | bc_vec_expand(&l->t.v, len + 1); |
| 2943 | bc_vec_push(&l->t.v, &start); |
| 2944 | |
| 2945 | for (buf -= 1, j = 1; j < len + hits * 2; ++j) { |
| 2946 | |
| 2947 | c = buf[j]; |
| 2948 | |
| 2949 | // If we have hit a backslash, skip it. We don't have |
| 2950 | // to check for a newline because it's guaranteed. |
| 2951 | if (hits < bslashes && c == '\\') { |
| 2952 | ++hits; |
| 2953 | ++j; |
| 2954 | continue; |
| 2955 | } |
| 2956 | |
| 2957 | bc_vec_push(&l->t.v, &c); |
| 2958 | } |
| 2959 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 2960 | bc_vec_pushZeroByte(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2961 | l->i += i; |
| 2962 | |
| 2963 | return BC_STATUS_SUCCESS; |
| 2964 | } |
| 2965 | |
| 2966 | static BcStatus bc_lex_name(BcLex *l) |
| 2967 | { |
| 2968 | size_t i = 0; |
| 2969 | const char *buf = l->buf + l->i - 1; |
| 2970 | char c = buf[i]; |
| 2971 | |
| 2972 | l->t.t = BC_LEX_NAME; |
| 2973 | |
| 2974 | while ((c >= 'a' && c <= 'z') || isdigit(c) || c == '_') c = buf[++i]; |
| 2975 | |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 2976 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
| 2977 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
| 2978 | if (i > BC_MAX_STRING) |
| 2979 | return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]"); |
| 2980 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2981 | bc_vec_string(&l->t.v, i, buf); |
| 2982 | |
| 2983 | // Increment the index. We minus 1 because it has already been incremented. |
| 2984 | l->i += i - 1; |
| 2985 | |
| 2986 | return BC_STATUS_SUCCESS; |
| 2987 | } |
| 2988 | |
| 2989 | static void bc_lex_init(BcLex *l, BcLexNext next) |
| 2990 | { |
| 2991 | l->next = next; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 2992 | bc_char_vec_init(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2993 | } |
| 2994 | |
| 2995 | static void bc_lex_free(BcLex *l) |
| 2996 | { |
| 2997 | bc_vec_free(&l->t.v); |
| 2998 | } |
| 2999 | |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 3000 | static void bc_lex_file(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3001 | { |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3002 | G.err_line = l->line = 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3003 | l->newline = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3004 | } |
| 3005 | |
| 3006 | static BcStatus bc_lex_next(BcLex *l) |
| 3007 | { |
| 3008 | BcStatus s; |
| 3009 | |
| 3010 | l->t.last = l->t.t; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3011 | 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] | 3012 | |
| 3013 | l->line += l->newline; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3014 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3015 | l->t.t = BC_LEX_EOF; |
| 3016 | |
| 3017 | l->newline = (l->i == l->len); |
| 3018 | if (l->newline) return BC_STATUS_SUCCESS; |
| 3019 | |
| 3020 | // Loop until failure or we don't have whitespace. This |
| 3021 | // is so the parser doesn't get inundated with whitespace. |
| 3022 | do { |
| 3023 | s = l->next(l); |
| 3024 | } while (!s && l->t.t == BC_LEX_WHITESPACE); |
| 3025 | |
| 3026 | return s; |
| 3027 | } |
| 3028 | |
| 3029 | static BcStatus bc_lex_text(BcLex *l, const char *text) |
| 3030 | { |
| 3031 | l->buf = text; |
| 3032 | l->i = 0; |
| 3033 | l->len = strlen(text); |
| 3034 | l->t.t = l->t.last = BC_LEX_INVALID; |
| 3035 | return bc_lex_next(l); |
| 3036 | } |
| 3037 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 3038 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3039 | static BcStatus bc_lex_identifier(BcLex *l) |
| 3040 | { |
| 3041 | BcStatus s; |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3042 | unsigned i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3043 | const char *buf = l->buf + l->i - 1; |
| 3044 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3045 | for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) { |
| 3046 | const char *keyword8 = bc_lex_kws[i].name8; |
| 3047 | unsigned j = 0; |
| 3048 | while (buf[j] != '\0' && buf[j] == keyword8[j]) { |
| 3049 | j++; |
| 3050 | if (j == 8) goto match; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3051 | } |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3052 | if (keyword8[j] != '\0') |
| 3053 | continue; |
| 3054 | match: |
| 3055 | // buf starts with keyword bc_lex_kws[i] |
| 3056 | l->t.t = BC_LEX_KEY_1st_keyword + i; |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 3057 | if (!bc_lex_kws_POSIX(i)) { |
Denys Vlasenko | 0d7e46b | 2018-12-05 18:31:19 +0100 | [diff] [blame] | 3058 | 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] | 3059 | if (s) return s; |
| 3060 | } |
| 3061 | |
| 3062 | // We minus 1 because the index has already been incremented. |
| 3063 | l->i += j - 1; |
| 3064 | return BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3065 | } |
| 3066 | |
| 3067 | s = bc_lex_name(l); |
| 3068 | if (s) return s; |
| 3069 | |
Denys Vlasenko | 0d7e46b | 2018-12-05 18:31:19 +0100 | [diff] [blame] | 3070 | if (l->t.v.len > 2) { |
| 3071 | // Prevent this: |
| 3072 | // >>> qwe=1 |
| 3073 | // bc: POSIX only allows one character names; the following is bad: 'qwe=1 |
| 3074 | // ' |
| 3075 | unsigned len = strchrnul(buf, '\n') - buf; |
| 3076 | s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf); |
| 3077 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3078 | |
| 3079 | return s; |
| 3080 | } |
| 3081 | |
| 3082 | static BcStatus bc_lex_string(BcLex *l) |
| 3083 | { |
| 3084 | size_t len, nls = 0, i = l->i; |
| 3085 | char c; |
| 3086 | |
| 3087 | l->t.t = BC_LEX_STR; |
| 3088 | |
| 3089 | for (c = l->buf[i]; c != 0 && c != '"'; c = l->buf[++i]) nls += (c == '\n'); |
| 3090 | |
| 3091 | if (c == '\0') { |
| 3092 | l->i = i; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3093 | return bc_error("string end could not be found"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3094 | } |
| 3095 | |
| 3096 | len = i - l->i; |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 3097 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
| 3098 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
| 3099 | if (len > BC_MAX_STRING) |
| 3100 | return bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"); |
| 3101 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3102 | bc_vec_string(&l->t.v, len, l->buf + l->i); |
| 3103 | |
| 3104 | l->i = i + 1; |
| 3105 | l->line += nls; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3106 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3107 | |
| 3108 | return BC_STATUS_SUCCESS; |
| 3109 | } |
| 3110 | |
| 3111 | static void bc_lex_assign(BcLex *l, BcLexType with, BcLexType without) |
| 3112 | { |
| 3113 | if (l->buf[l->i] == '=') { |
| 3114 | ++l->i; |
| 3115 | l->t.t = with; |
| 3116 | } |
| 3117 | else |
| 3118 | l->t.t = without; |
| 3119 | } |
| 3120 | |
| 3121 | static BcStatus bc_lex_comment(BcLex *l) |
| 3122 | { |
| 3123 | size_t i, nls = 0; |
| 3124 | const char *buf = l->buf; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3125 | |
| 3126 | l->t.t = BC_LEX_WHITESPACE; |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3127 | i = ++l->i; |
| 3128 | for (;;) { |
| 3129 | char c = buf[i]; |
| 3130 | check_star: |
| 3131 | if (c == '*') { |
| 3132 | c = buf[++i]; |
| 3133 | if (c == '/') |
| 3134 | break; |
| 3135 | goto check_star; |
| 3136 | } |
| 3137 | if (c == '\0') { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3138 | l->i = i; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3139 | return bc_error("comment end could not be found"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3140 | } |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3141 | nls += (c == '\n'); |
| 3142 | i++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3143 | } |
| 3144 | |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3145 | l->i = i + 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3146 | l->line += nls; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3147 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3148 | |
| 3149 | return BC_STATUS_SUCCESS; |
| 3150 | } |
| 3151 | |
| 3152 | static BcStatus bc_lex_token(BcLex *l) |
| 3153 | { |
| 3154 | BcStatus s = BC_STATUS_SUCCESS; |
| 3155 | char c = l->buf[l->i++], c2; |
| 3156 | |
| 3157 | // This is the workhorse of the lexer. |
| 3158 | switch (c) { |
| 3159 | |
| 3160 | case '\0': |
| 3161 | case '\n': |
| 3162 | { |
| 3163 | l->newline = true; |
| 3164 | l->t.t = !c ? BC_LEX_EOF : BC_LEX_NLINE; |
| 3165 | break; |
| 3166 | } |
| 3167 | |
| 3168 | case '\t': |
| 3169 | case '\v': |
| 3170 | case '\f': |
| 3171 | case '\r': |
| 3172 | case ' ': |
| 3173 | { |
| 3174 | bc_lex_whitespace(l); |
| 3175 | break; |
| 3176 | } |
| 3177 | |
| 3178 | case '!': |
| 3179 | { |
| 3180 | bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT); |
| 3181 | |
| 3182 | if (l->t.t == BC_LEX_OP_BOOL_NOT) { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3183 | s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3184 | if (s) return s; |
| 3185 | } |
| 3186 | |
| 3187 | break; |
| 3188 | } |
| 3189 | |
| 3190 | case '"': |
| 3191 | { |
| 3192 | s = bc_lex_string(l); |
| 3193 | break; |
| 3194 | } |
| 3195 | |
| 3196 | case '#': |
| 3197 | { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3198 | s = bc_POSIX_does_not_allow("'#' script comments"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3199 | if (s) return s; |
| 3200 | |
| 3201 | bc_lex_lineComment(l); |
| 3202 | |
| 3203 | break; |
| 3204 | } |
| 3205 | |
| 3206 | case '%': |
| 3207 | { |
| 3208 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS); |
| 3209 | break; |
| 3210 | } |
| 3211 | |
| 3212 | case '&': |
| 3213 | { |
| 3214 | c2 = l->buf[l->i]; |
| 3215 | if (c2 == '&') { |
| 3216 | |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3217 | s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3218 | if (s) return s; |
| 3219 | |
| 3220 | ++l->i; |
| 3221 | l->t.t = BC_LEX_OP_BOOL_AND; |
| 3222 | } |
| 3223 | else { |
| 3224 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3225 | s = bc_error_bad_character('&'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3226 | } |
| 3227 | |
| 3228 | break; |
| 3229 | } |
| 3230 | |
| 3231 | case '(': |
| 3232 | case ')': |
| 3233 | { |
| 3234 | l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN); |
| 3235 | break; |
| 3236 | } |
| 3237 | |
| 3238 | case '*': |
| 3239 | { |
| 3240 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY); |
| 3241 | break; |
| 3242 | } |
| 3243 | |
| 3244 | case '+': |
| 3245 | { |
| 3246 | c2 = l->buf[l->i]; |
| 3247 | if (c2 == '+') { |
| 3248 | ++l->i; |
| 3249 | l->t.t = BC_LEX_OP_INC; |
| 3250 | } |
| 3251 | else |
| 3252 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS); |
| 3253 | break; |
| 3254 | } |
| 3255 | |
| 3256 | case ',': |
| 3257 | { |
| 3258 | l->t.t = BC_LEX_COMMA; |
| 3259 | break; |
| 3260 | } |
| 3261 | |
| 3262 | case '-': |
| 3263 | { |
| 3264 | c2 = l->buf[l->i]; |
| 3265 | if (c2 == '-') { |
| 3266 | ++l->i; |
| 3267 | l->t.t = BC_LEX_OP_DEC; |
| 3268 | } |
| 3269 | else |
| 3270 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS); |
| 3271 | break; |
| 3272 | } |
| 3273 | |
| 3274 | case '.': |
| 3275 | { |
| 3276 | if (isdigit(l->buf[l->i])) |
| 3277 | s = bc_lex_number(l, c); |
| 3278 | else { |
| 3279 | l->t.t = BC_LEX_KEY_LAST; |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3280 | 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] | 3281 | } |
| 3282 | break; |
| 3283 | } |
| 3284 | |
| 3285 | case '/': |
| 3286 | { |
| 3287 | c2 = l->buf[l->i]; |
| 3288 | if (c2 == '*') |
| 3289 | s = bc_lex_comment(l); |
| 3290 | else |
| 3291 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE); |
| 3292 | break; |
| 3293 | } |
| 3294 | |
| 3295 | case '0': |
| 3296 | case '1': |
| 3297 | case '2': |
| 3298 | case '3': |
| 3299 | case '4': |
| 3300 | case '5': |
| 3301 | case '6': |
| 3302 | case '7': |
| 3303 | case '8': |
| 3304 | case '9': |
| 3305 | case 'A': |
| 3306 | case 'B': |
| 3307 | case 'C': |
| 3308 | case 'D': |
| 3309 | case 'E': |
| 3310 | case 'F': |
| 3311 | { |
| 3312 | s = bc_lex_number(l, c); |
| 3313 | break; |
| 3314 | } |
| 3315 | |
| 3316 | case ';': |
| 3317 | { |
| 3318 | l->t.t = BC_LEX_SCOLON; |
| 3319 | break; |
| 3320 | } |
| 3321 | |
| 3322 | case '<': |
| 3323 | { |
| 3324 | bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT); |
| 3325 | break; |
| 3326 | } |
| 3327 | |
| 3328 | case '=': |
| 3329 | { |
| 3330 | bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN); |
| 3331 | break; |
| 3332 | } |
| 3333 | |
| 3334 | case '>': |
| 3335 | { |
| 3336 | bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT); |
| 3337 | break; |
| 3338 | } |
| 3339 | |
| 3340 | case '[': |
| 3341 | case ']': |
| 3342 | { |
| 3343 | l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET); |
| 3344 | break; |
| 3345 | } |
| 3346 | |
| 3347 | case '\\': |
| 3348 | { |
| 3349 | if (l->buf[l->i] == '\n') { |
| 3350 | l->t.t = BC_LEX_WHITESPACE; |
| 3351 | ++l->i; |
| 3352 | } |
| 3353 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3354 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3355 | break; |
| 3356 | } |
| 3357 | |
| 3358 | case '^': |
| 3359 | { |
| 3360 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER); |
| 3361 | break; |
| 3362 | } |
| 3363 | |
| 3364 | case 'a': |
| 3365 | case 'b': |
| 3366 | case 'c': |
| 3367 | case 'd': |
| 3368 | case 'e': |
| 3369 | case 'f': |
| 3370 | case 'g': |
| 3371 | case 'h': |
| 3372 | case 'i': |
| 3373 | case 'j': |
| 3374 | case 'k': |
| 3375 | case 'l': |
| 3376 | case 'm': |
| 3377 | case 'n': |
| 3378 | case 'o': |
| 3379 | case 'p': |
| 3380 | case 'q': |
| 3381 | case 'r': |
| 3382 | case 's': |
| 3383 | case 't': |
| 3384 | case 'u': |
| 3385 | case 'v': |
| 3386 | case 'w': |
| 3387 | case 'x': |
| 3388 | case 'y': |
| 3389 | case 'z': |
| 3390 | { |
| 3391 | s = bc_lex_identifier(l); |
| 3392 | break; |
| 3393 | } |
| 3394 | |
| 3395 | case '{': |
| 3396 | case '}': |
| 3397 | { |
| 3398 | l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE); |
| 3399 | break; |
| 3400 | } |
| 3401 | |
| 3402 | case '|': |
| 3403 | { |
| 3404 | c2 = l->buf[l->i]; |
| 3405 | |
| 3406 | if (c2 == '|') { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3407 | s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3408 | if (s) return s; |
| 3409 | |
| 3410 | ++l->i; |
| 3411 | l->t.t = BC_LEX_OP_BOOL_OR; |
| 3412 | } |
| 3413 | else { |
| 3414 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3415 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3416 | } |
| 3417 | |
| 3418 | break; |
| 3419 | } |
| 3420 | |
| 3421 | default: |
| 3422 | { |
| 3423 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3424 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3425 | break; |
| 3426 | } |
| 3427 | } |
| 3428 | |
| 3429 | return s; |
| 3430 | } |
| 3431 | #endif // ENABLE_BC |
| 3432 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 3433 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3434 | static BcStatus dc_lex_register(BcLex *l) |
| 3435 | { |
| 3436 | BcStatus s = BC_STATUS_SUCCESS; |
| 3437 | |
| 3438 | if (isspace(l->buf[l->i - 1])) { |
| 3439 | bc_lex_whitespace(l); |
| 3440 | ++l->i; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 3441 | if (!G_exreg) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3442 | s = bc_error("extended register"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3443 | else |
| 3444 | s = bc_lex_name(l); |
| 3445 | } |
| 3446 | else { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3447 | bc_vec_pop_all(&l->t.v); |
Denys Vlasenko | e55a572 | 2018-12-06 12:47:17 +0100 | [diff] [blame] | 3448 | bc_vec_push(&l->t.v, &l->buf[l->i - 1]); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 3449 | bc_vec_pushZeroByte(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3450 | l->t.t = BC_LEX_NAME; |
| 3451 | } |
| 3452 | |
| 3453 | return s; |
| 3454 | } |
| 3455 | |
| 3456 | static BcStatus dc_lex_string(BcLex *l) |
| 3457 | { |
| 3458 | size_t depth = 1, nls = 0, i = l->i; |
| 3459 | char c; |
| 3460 | |
| 3461 | l->t.t = BC_LEX_STR; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3462 | bc_vec_pop_all(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3463 | |
| 3464 | for (c = l->buf[i]; c != 0 && depth; c = l->buf[++i]) { |
| 3465 | |
| 3466 | depth += (c == '[' && (i == l->i || l->buf[i - 1] != '\\')); |
| 3467 | depth -= (c == ']' && (i == l->i || l->buf[i - 1] != '\\')); |
| 3468 | nls += (c == '\n'); |
| 3469 | |
| 3470 | if (depth) bc_vec_push(&l->t.v, &c); |
| 3471 | } |
| 3472 | |
| 3473 | if (c == '\0') { |
| 3474 | l->i = i; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3475 | return bc_error("string end could not be found"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3476 | } |
| 3477 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 3478 | bc_vec_pushZeroByte(&l->t.v); |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 3479 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
| 3480 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
| 3481 | if (i - l->i > BC_MAX_STRING) |
| 3482 | return bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]"); |
| 3483 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3484 | |
| 3485 | l->i = i; |
| 3486 | l->line += nls; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3487 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3488 | |
| 3489 | return BC_STATUS_SUCCESS; |
| 3490 | } |
| 3491 | |
| 3492 | static BcStatus dc_lex_token(BcLex *l) |
| 3493 | { |
| 3494 | BcStatus s = BC_STATUS_SUCCESS; |
| 3495 | char c = l->buf[l->i++], c2; |
| 3496 | size_t i; |
| 3497 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3498 | for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) { |
| 3499 | if (l->t.last == dc_lex_regs[i]) |
| 3500 | return dc_lex_register(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3501 | } |
| 3502 | |
| 3503 | if (c >= '%' && c <= '~' && |
| 3504 | (l->t.t = dc_lex_tokens[(c - '%')]) != BC_LEX_INVALID) |
| 3505 | { |
| 3506 | return s; |
| 3507 | } |
| 3508 | |
| 3509 | // This is the workhorse of the lexer. |
| 3510 | switch (c) { |
| 3511 | |
| 3512 | case '\0': |
| 3513 | { |
| 3514 | l->t.t = BC_LEX_EOF; |
| 3515 | break; |
| 3516 | } |
| 3517 | |
| 3518 | case '\n': |
| 3519 | case '\t': |
| 3520 | case '\v': |
| 3521 | case '\f': |
| 3522 | case '\r': |
| 3523 | case ' ': |
| 3524 | { |
| 3525 | l->newline = (c == '\n'); |
| 3526 | bc_lex_whitespace(l); |
| 3527 | break; |
| 3528 | } |
| 3529 | |
| 3530 | case '!': |
| 3531 | { |
| 3532 | c2 = l->buf[l->i]; |
| 3533 | |
| 3534 | if (c2 == '=') |
| 3535 | l->t.t = BC_LEX_OP_REL_NE; |
| 3536 | else if (c2 == '<') |
| 3537 | l->t.t = BC_LEX_OP_REL_LE; |
| 3538 | else if (c2 == '>') |
| 3539 | l->t.t = BC_LEX_OP_REL_GE; |
| 3540 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3541 | return bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3542 | |
| 3543 | ++l->i; |
| 3544 | break; |
| 3545 | } |
| 3546 | |
| 3547 | case '#': |
| 3548 | { |
| 3549 | bc_lex_lineComment(l); |
| 3550 | break; |
| 3551 | } |
| 3552 | |
| 3553 | case '.': |
| 3554 | { |
| 3555 | if (isdigit(l->buf[l->i])) |
| 3556 | s = bc_lex_number(l, c); |
| 3557 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3558 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3559 | break; |
| 3560 | } |
| 3561 | |
| 3562 | case '0': |
| 3563 | case '1': |
| 3564 | case '2': |
| 3565 | case '3': |
| 3566 | case '4': |
| 3567 | case '5': |
| 3568 | case '6': |
| 3569 | case '7': |
| 3570 | case '8': |
| 3571 | case '9': |
| 3572 | case 'A': |
| 3573 | case 'B': |
| 3574 | case 'C': |
| 3575 | case 'D': |
| 3576 | case 'E': |
| 3577 | case 'F': |
| 3578 | { |
| 3579 | s = bc_lex_number(l, c); |
| 3580 | break; |
| 3581 | } |
| 3582 | |
| 3583 | case '[': |
| 3584 | { |
| 3585 | s = dc_lex_string(l); |
| 3586 | break; |
| 3587 | } |
| 3588 | |
| 3589 | default: |
| 3590 | { |
| 3591 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3592 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3593 | break; |
| 3594 | } |
| 3595 | } |
| 3596 | |
| 3597 | return s; |
| 3598 | } |
| 3599 | #endif // ENABLE_DC |
| 3600 | |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3601 | static void bc_program_addFunc(char *name, size_t *idx); |
| 3602 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3603 | static void bc_parse_addFunc(BcParse *p, char *name, size_t *idx) |
| 3604 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3605 | bc_program_addFunc(name, idx); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 3606 | p->func = bc_program_func(p->fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3607 | } |
| 3608 | |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 3609 | #define bc_parse_push(p, i) bc_vec_pushByte(&(p)->func->code, (char) (i)) |
| 3610 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3611 | static void bc_parse_pushName(BcParse *p, char *name) |
| 3612 | { |
| 3613 | size_t i = 0, len = strlen(name); |
| 3614 | |
| 3615 | for (; i < len; ++i) bc_parse_push(p, name[i]); |
| 3616 | bc_parse_push(p, BC_PARSE_STREND); |
| 3617 | |
| 3618 | free(name); |
| 3619 | } |
| 3620 | |
| 3621 | static void bc_parse_pushIndex(BcParse *p, size_t idx) |
| 3622 | { |
| 3623 | unsigned char amt, i, nums[sizeof(size_t)]; |
| 3624 | |
| 3625 | for (amt = 0; idx; ++amt) { |
| 3626 | nums[amt] = (char) idx; |
| 3627 | idx = (idx & ((unsigned long) ~(UCHAR_MAX))) >> sizeof(char) * CHAR_BIT; |
| 3628 | } |
| 3629 | |
| 3630 | bc_parse_push(p, amt); |
| 3631 | for (i = 0; i < amt; ++i) bc_parse_push(p, nums[i]); |
| 3632 | } |
| 3633 | |
| 3634 | static void bc_parse_number(BcParse *p, BcInst *prev, size_t *nexs) |
| 3635 | { |
| 3636 | char *num = xstrdup(p->l.t.v.v); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3637 | size_t idx = G.prog.consts.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3638 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3639 | bc_vec_push(&G.prog.consts, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3640 | |
| 3641 | bc_parse_push(p, BC_INST_NUM); |
| 3642 | bc_parse_pushIndex(p, idx); |
| 3643 | |
| 3644 | ++(*nexs); |
| 3645 | (*prev) = BC_INST_NUM; |
| 3646 | } |
| 3647 | |
| 3648 | static BcStatus bc_parse_text(BcParse *p, const char *text) |
| 3649 | { |
| 3650 | BcStatus s; |
| 3651 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 3652 | p->func = bc_program_func(p->fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3653 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3654 | if (!text[0] && !BC_PARSE_CAN_EXEC(p)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3655 | p->l.t.t = BC_LEX_INVALID; |
| 3656 | s = p->parse(p); |
| 3657 | if (s) return s; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3658 | if (!BC_PARSE_CAN_EXEC(p)) |
| 3659 | return bc_error("file is not executable"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3660 | } |
| 3661 | |
| 3662 | return bc_lex_text(&p->l, text); |
| 3663 | } |
| 3664 | |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3665 | // Called when parsing or execution detects a failure, |
| 3666 | // resets execution structures. |
| 3667 | static void bc_program_reset(void) |
| 3668 | { |
| 3669 | BcFunc *f; |
| 3670 | BcInstPtr *ip; |
| 3671 | |
| 3672 | bc_vec_npop(&G.prog.stack, G.prog.stack.len - 1); |
| 3673 | bc_vec_pop_all(&G.prog.results); |
| 3674 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 3675 | f = bc_program_func(0); |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3676 | ip = bc_vec_top(&G.prog.stack); |
| 3677 | ip->idx = f->code.len; |
| 3678 | } |
| 3679 | |
Denys Vlasenko | e55a572 | 2018-12-06 12:47:17 +0100 | [diff] [blame] | 3680 | #define bc_parse_updateFunc(p, f) \ |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 3681 | ((p)->func = bc_program_func((p)->fidx = (f))) |
Denys Vlasenko | e55a572 | 2018-12-06 12:47:17 +0100 | [diff] [blame] | 3682 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 3683 | // Called when bc/dc_parse_parse() detects a failure, |
| 3684 | // resets parsing structures. |
| 3685 | static void bc_parse_reset(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3686 | { |
| 3687 | if (p->fidx != BC_PROG_MAIN) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3688 | p->func->nparams = 0; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3689 | bc_vec_pop_all(&p->func->code); |
| 3690 | bc_vec_pop_all(&p->func->autos); |
| 3691 | bc_vec_pop_all(&p->func->labels); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3692 | |
| 3693 | bc_parse_updateFunc(p, BC_PROG_MAIN); |
| 3694 | } |
| 3695 | |
| 3696 | p->l.i = p->l.len; |
| 3697 | p->l.t.t = BC_LEX_EOF; |
| 3698 | p->auto_part = (p->nbraces = 0); |
| 3699 | |
| 3700 | bc_vec_npop(&p->flags, p->flags.len - 1); |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3701 | bc_vec_pop_all(&p->exits); |
| 3702 | bc_vec_pop_all(&p->conds); |
| 3703 | bc_vec_pop_all(&p->ops); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3704 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 3705 | bc_program_reset(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3706 | } |
| 3707 | |
| 3708 | static void bc_parse_free(BcParse *p) |
| 3709 | { |
| 3710 | bc_vec_free(&p->flags); |
| 3711 | bc_vec_free(&p->exits); |
| 3712 | bc_vec_free(&p->conds); |
| 3713 | bc_vec_free(&p->ops); |
| 3714 | bc_lex_free(&p->l); |
| 3715 | } |
| 3716 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3717 | static void bc_parse_create(BcParse *p, size_t func, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3718 | BcParseParse parse, BcLexNext next) |
| 3719 | { |
| 3720 | memset(p, 0, sizeof(BcParse)); |
| 3721 | |
| 3722 | bc_lex_init(&p->l, next); |
| 3723 | bc_vec_init(&p->flags, sizeof(uint8_t), NULL); |
| 3724 | bc_vec_init(&p->exits, sizeof(BcInstPtr), NULL); |
| 3725 | bc_vec_init(&p->conds, sizeof(size_t), NULL); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 3726 | bc_vec_pushZeroByte(&p->flags); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3727 | bc_vec_init(&p->ops, sizeof(BcLexType), NULL); |
| 3728 | |
| 3729 | p->parse = parse; |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 3730 | // p->auto_part = p->nbraces = 0; - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3731 | bc_parse_updateFunc(p, func); |
| 3732 | } |
| 3733 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 3734 | #if ENABLE_BC |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 3735 | |
| 3736 | #define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops))) |
| 3737 | #define BC_PARSE_LEAF(p, rparen) \ |
| 3738 | (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \ |
| 3739 | (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST) |
| 3740 | |
| 3741 | // We can calculate the conversion between tokens and exprs by subtracting the |
| 3742 | // position of the first operator in the lex enum and adding the position of the |
| 3743 | // first in the expr enum. Note: This only works for binary operators. |
| 3744 | #define BC_PARSE_TOKEN_INST(t) ((char) ((t) -BC_LEX_NEG + BC_INST_NEG)) |
| 3745 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3746 | static BcStatus bc_parse_else(BcParse *p); |
| 3747 | static BcStatus bc_parse_stmt(BcParse *p); |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 3748 | static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next); |
Denys Vlasenko | 050b0fe | 2018-12-05 22:40:44 +0100 | [diff] [blame] | 3749 | static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3750 | |
| 3751 | static BcStatus bc_parse_operator(BcParse *p, BcLexType type, size_t start, |
| 3752 | size_t *nexprs, bool next) |
| 3753 | { |
| 3754 | BcStatus s = BC_STATUS_SUCCESS; |
| 3755 | BcLexType t; |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 3756 | char l, r = bc_parse_op_PREC(type - BC_LEX_OP_INC); |
| 3757 | bool left = bc_parse_op_LEFT(type - BC_LEX_OP_INC); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3758 | |
| 3759 | while (p->ops.len > start) { |
| 3760 | |
| 3761 | t = BC_PARSE_TOP_OP(p); |
| 3762 | if (t == BC_LEX_LPAREN) break; |
| 3763 | |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 3764 | l = bc_parse_op_PREC(t - BC_LEX_OP_INC); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3765 | if (l >= r && (l != r || !left)) break; |
| 3766 | |
| 3767 | bc_parse_push(p, BC_PARSE_TOKEN_INST(t)); |
| 3768 | bc_vec_pop(&p->ops); |
| 3769 | *nexprs -= t != BC_LEX_OP_BOOL_NOT && t != BC_LEX_NEG; |
| 3770 | } |
| 3771 | |
| 3772 | bc_vec_push(&p->ops, &type); |
| 3773 | if (next) s = bc_lex_next(&p->l); |
| 3774 | |
| 3775 | return s; |
| 3776 | } |
| 3777 | |
| 3778 | static BcStatus bc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs) |
| 3779 | { |
| 3780 | BcLexType top; |
| 3781 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3782 | if (p->ops.len <= ops_bgn) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3783 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3784 | top = BC_PARSE_TOP_OP(p); |
| 3785 | |
| 3786 | while (top != BC_LEX_LPAREN) { |
| 3787 | |
| 3788 | bc_parse_push(p, BC_PARSE_TOKEN_INST(top)); |
| 3789 | |
| 3790 | bc_vec_pop(&p->ops); |
| 3791 | *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG; |
| 3792 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3793 | if (p->ops.len <= ops_bgn) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3794 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3795 | top = BC_PARSE_TOP_OP(p); |
| 3796 | } |
| 3797 | |
| 3798 | bc_vec_pop(&p->ops); |
| 3799 | |
| 3800 | return bc_lex_next(&p->l); |
| 3801 | } |
| 3802 | |
| 3803 | static BcStatus bc_parse_params(BcParse *p, uint8_t flags) |
| 3804 | { |
| 3805 | BcStatus s; |
| 3806 | bool comma = false; |
| 3807 | size_t nparams; |
| 3808 | |
| 3809 | s = bc_lex_next(&p->l); |
| 3810 | if (s) return s; |
| 3811 | |
| 3812 | for (nparams = 0; p->l.t.t != BC_LEX_RPAREN; ++nparams) { |
| 3813 | |
| 3814 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; |
| 3815 | s = bc_parse_expr(p, flags, bc_parse_next_param); |
| 3816 | if (s) return s; |
| 3817 | |
| 3818 | comma = p->l.t.t == BC_LEX_COMMA; |
| 3819 | if (comma) { |
| 3820 | s = bc_lex_next(&p->l); |
| 3821 | if (s) return s; |
| 3822 | } |
| 3823 | } |
| 3824 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3825 | if (comma) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3826 | bc_parse_push(p, BC_INST_CALL); |
| 3827 | bc_parse_pushIndex(p, nparams); |
| 3828 | |
| 3829 | return BC_STATUS_SUCCESS; |
| 3830 | } |
| 3831 | |
| 3832 | static BcStatus bc_parse_call(BcParse *p, char *name, uint8_t flags) |
| 3833 | { |
| 3834 | BcStatus s; |
| 3835 | BcId entry, *entry_ptr; |
| 3836 | size_t idx; |
| 3837 | |
| 3838 | entry.name = name; |
| 3839 | |
| 3840 | s = bc_parse_params(p, flags); |
| 3841 | if (s) goto err; |
| 3842 | |
| 3843 | if (p->l.t.t != BC_LEX_RPAREN) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3844 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3845 | goto err; |
| 3846 | } |
| 3847 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3848 | idx = bc_map_index(&G.prog.fn_map, &entry); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3849 | |
| 3850 | if (idx == BC_VEC_INVALID_IDX) { |
| 3851 | name = xstrdup(entry.name); |
| 3852 | bc_parse_addFunc(p, name, &idx); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3853 | idx = bc_map_index(&G.prog.fn_map, &entry); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3854 | free(entry.name); |
| 3855 | } |
| 3856 | else |
| 3857 | free(name); |
| 3858 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3859 | entry_ptr = bc_vec_item(&G.prog.fn_map, idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3860 | bc_parse_pushIndex(p, entry_ptr->idx); |
| 3861 | |
| 3862 | return bc_lex_next(&p->l); |
| 3863 | |
| 3864 | err: |
| 3865 | free(name); |
| 3866 | return s; |
| 3867 | } |
| 3868 | |
| 3869 | static BcStatus bc_parse_name(BcParse *p, BcInst *type, uint8_t flags) |
| 3870 | { |
| 3871 | BcStatus s; |
| 3872 | char *name; |
| 3873 | |
| 3874 | name = xstrdup(p->l.t.v.v); |
| 3875 | s = bc_lex_next(&p->l); |
| 3876 | if (s) goto err; |
| 3877 | |
| 3878 | if (p->l.t.t == BC_LEX_LBRACKET) { |
| 3879 | |
| 3880 | s = bc_lex_next(&p->l); |
| 3881 | if (s) goto err; |
| 3882 | |
| 3883 | if (p->l.t.t == BC_LEX_RBRACKET) { |
| 3884 | |
| 3885 | if (!(flags & BC_PARSE_ARRAY)) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3886 | s = bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3887 | goto err; |
| 3888 | } |
| 3889 | |
| 3890 | *type = BC_INST_ARRAY; |
| 3891 | } |
| 3892 | else { |
| 3893 | |
| 3894 | *type = BC_INST_ARRAY_ELEM; |
| 3895 | |
| 3896 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); |
| 3897 | s = bc_parse_expr(p, flags, bc_parse_next_elem); |
| 3898 | if (s) goto err; |
| 3899 | } |
| 3900 | |
| 3901 | s = bc_lex_next(&p->l); |
| 3902 | if (s) goto err; |
| 3903 | bc_parse_push(p, *type); |
| 3904 | bc_parse_pushName(p, name); |
| 3905 | } |
| 3906 | else if (p->l.t.t == BC_LEX_LPAREN) { |
| 3907 | |
| 3908 | if (flags & BC_PARSE_NOCALL) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3909 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3910 | goto err; |
| 3911 | } |
| 3912 | |
| 3913 | *type = BC_INST_CALL; |
| 3914 | s = bc_parse_call(p, name, flags); |
| 3915 | } |
| 3916 | else { |
| 3917 | *type = BC_INST_VAR; |
| 3918 | bc_parse_push(p, BC_INST_VAR); |
| 3919 | bc_parse_pushName(p, name); |
| 3920 | } |
| 3921 | |
| 3922 | return s; |
| 3923 | |
| 3924 | err: |
| 3925 | free(name); |
| 3926 | return s; |
| 3927 | } |
| 3928 | |
| 3929 | static BcStatus bc_parse_read(BcParse *p) |
| 3930 | { |
| 3931 | BcStatus s; |
| 3932 | |
| 3933 | s = bc_lex_next(&p->l); |
| 3934 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3935 | if (p->l.t.t != BC_LEX_LPAREN) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3936 | |
| 3937 | s = bc_lex_next(&p->l); |
| 3938 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3939 | 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] | 3940 | |
| 3941 | bc_parse_push(p, BC_INST_READ); |
| 3942 | |
| 3943 | return bc_lex_next(&p->l); |
| 3944 | } |
| 3945 | |
| 3946 | static BcStatus bc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags, |
| 3947 | BcInst *prev) |
| 3948 | { |
| 3949 | BcStatus s; |
| 3950 | |
| 3951 | s = bc_lex_next(&p->l); |
| 3952 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3953 | 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] | 3954 | |
| 3955 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; |
| 3956 | |
| 3957 | s = bc_lex_next(&p->l); |
| 3958 | if (s) return s; |
| 3959 | |
| 3960 | s = bc_parse_expr(p, flags, bc_parse_next_rel); |
| 3961 | if (s) return s; |
| 3962 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3963 | 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] | 3964 | |
| 3965 | *prev = (type == BC_LEX_KEY_LENGTH) ? BC_INST_LENGTH : BC_INST_SQRT; |
| 3966 | bc_parse_push(p, *prev); |
| 3967 | |
| 3968 | return bc_lex_next(&p->l); |
| 3969 | } |
| 3970 | |
| 3971 | static BcStatus bc_parse_scale(BcParse *p, BcInst *type, uint8_t flags) |
| 3972 | { |
| 3973 | BcStatus s; |
| 3974 | |
| 3975 | s = bc_lex_next(&p->l); |
| 3976 | if (s) return s; |
| 3977 | |
| 3978 | if (p->l.t.t != BC_LEX_LPAREN) { |
| 3979 | *type = BC_INST_SCALE; |
| 3980 | bc_parse_push(p, BC_INST_SCALE); |
| 3981 | return BC_STATUS_SUCCESS; |
| 3982 | } |
| 3983 | |
| 3984 | *type = BC_INST_SCALE_FUNC; |
| 3985 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); |
| 3986 | |
| 3987 | s = bc_lex_next(&p->l); |
| 3988 | if (s) return s; |
| 3989 | |
| 3990 | s = bc_parse_expr(p, flags, bc_parse_next_rel); |
| 3991 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3992 | 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] | 3993 | bc_parse_push(p, BC_INST_SCALE_FUNC); |
| 3994 | |
| 3995 | return bc_lex_next(&p->l); |
| 3996 | } |
| 3997 | |
| 3998 | static BcStatus bc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr, |
| 3999 | size_t *nexprs, uint8_t flags) |
| 4000 | { |
| 4001 | BcStatus s; |
| 4002 | BcLexType type; |
| 4003 | char inst; |
| 4004 | BcInst etype = *prev; |
| 4005 | |
| 4006 | if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM || |
| 4007 | etype == BC_INST_SCALE || etype == BC_INST_LAST || |
| 4008 | etype == BC_INST_IBASE || etype == BC_INST_OBASE) |
| 4009 | { |
| 4010 | *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC); |
| 4011 | bc_parse_push(p, inst); |
| 4012 | s = bc_lex_next(&p->l); |
| 4013 | } |
| 4014 | else { |
| 4015 | |
| 4016 | *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC); |
| 4017 | *paren_expr = true; |
| 4018 | |
| 4019 | s = bc_lex_next(&p->l); |
| 4020 | if (s) return s; |
| 4021 | type = p->l.t.t; |
| 4022 | |
| 4023 | // Because we parse the next part of the expression |
| 4024 | // right here, we need to increment this. |
| 4025 | *nexprs = *nexprs + 1; |
| 4026 | |
| 4027 | switch (type) { |
| 4028 | |
| 4029 | case BC_LEX_NAME: |
| 4030 | { |
| 4031 | s = bc_parse_name(p, prev, flags | BC_PARSE_NOCALL); |
| 4032 | break; |
| 4033 | } |
| 4034 | |
| 4035 | case BC_LEX_KEY_IBASE: |
| 4036 | case BC_LEX_KEY_LAST: |
| 4037 | case BC_LEX_KEY_OBASE: |
| 4038 | { |
| 4039 | bc_parse_push(p, type - BC_LEX_KEY_IBASE + BC_INST_IBASE); |
| 4040 | s = bc_lex_next(&p->l); |
| 4041 | break; |
| 4042 | } |
| 4043 | |
| 4044 | case BC_LEX_KEY_SCALE: |
| 4045 | { |
| 4046 | s = bc_lex_next(&p->l); |
| 4047 | if (s) return s; |
| 4048 | if (p->l.t.t == BC_LEX_LPAREN) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4049 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4050 | else |
| 4051 | bc_parse_push(p, BC_INST_SCALE); |
| 4052 | break; |
| 4053 | } |
| 4054 | |
| 4055 | default: |
| 4056 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4057 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4058 | break; |
| 4059 | } |
| 4060 | } |
| 4061 | |
| 4062 | if (!s) bc_parse_push(p, inst); |
| 4063 | } |
| 4064 | |
| 4065 | return s; |
| 4066 | } |
| 4067 | |
| 4068 | static BcStatus bc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn, |
| 4069 | bool rparen, size_t *nexprs) |
| 4070 | { |
| 4071 | BcStatus s; |
| 4072 | BcLexType type; |
| 4073 | BcInst etype = *prev; |
| 4074 | |
| 4075 | s = bc_lex_next(&p->l); |
| 4076 | if (s) return s; |
| 4077 | |
| 4078 | type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST || |
| 4079 | (etype >= BC_INST_NUM && etype <= BC_INST_SQRT) ? |
| 4080 | BC_LEX_OP_MINUS : |
| 4081 | BC_LEX_NEG; |
| 4082 | *prev = BC_PARSE_TOKEN_INST(type); |
| 4083 | |
| 4084 | // We can just push onto the op stack because this is the largest |
| 4085 | // precedence operator that gets pushed. Inc/dec does not. |
| 4086 | if (type != BC_LEX_OP_MINUS) |
| 4087 | bc_vec_push(&p->ops, &type); |
| 4088 | else |
| 4089 | s = bc_parse_operator(p, type, ops_bgn, nexprs, false); |
| 4090 | |
| 4091 | return s; |
| 4092 | } |
| 4093 | |
| 4094 | static BcStatus bc_parse_string(BcParse *p, char inst) |
| 4095 | { |
| 4096 | char *str = xstrdup(p->l.t.v.v); |
| 4097 | |
| 4098 | bc_parse_push(p, BC_INST_STR); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 4099 | bc_parse_pushIndex(p, G.prog.strs.len); |
| 4100 | bc_vec_push(&G.prog.strs, &str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4101 | bc_parse_push(p, inst); |
| 4102 | |
| 4103 | return bc_lex_next(&p->l); |
| 4104 | } |
| 4105 | |
| 4106 | static BcStatus bc_parse_print(BcParse *p) |
| 4107 | { |
| 4108 | BcStatus s; |
| 4109 | BcLexType type; |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4110 | bool comma; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4111 | |
| 4112 | s = bc_lex_next(&p->l); |
| 4113 | if (s) return s; |
| 4114 | |
| 4115 | type = p->l.t.t; |
| 4116 | |
| 4117 | if (type == BC_LEX_SCOLON || type == BC_LEX_NLINE) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4118 | return bc_error("bad print statement"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4119 | |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4120 | comma = false; |
| 4121 | while (type != BC_LEX_SCOLON && type != BC_LEX_NLINE) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4122 | |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4123 | if (type == BC_LEX_STR) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4124 | s = bc_parse_string(p, BC_INST_PRINT_POP); |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4125 | if (s) return s; |
| 4126 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4127 | s = bc_parse_expr(p, 0, bc_parse_next_print); |
| 4128 | if (s) return s; |
| 4129 | bc_parse_push(p, BC_INST_PRINT_POP); |
| 4130 | } |
| 4131 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4132 | comma = p->l.t.t == BC_LEX_COMMA; |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4133 | if (comma) { |
| 4134 | s = bc_lex_next(&p->l); |
| 4135 | if (s) return s; |
| 4136 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4137 | type = p->l.t.t; |
| 4138 | } |
| 4139 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4140 | if (comma) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4141 | |
| 4142 | return bc_lex_next(&p->l); |
| 4143 | } |
| 4144 | |
| 4145 | static BcStatus bc_parse_return(BcParse *p) |
| 4146 | { |
| 4147 | BcStatus s; |
| 4148 | BcLexType t; |
| 4149 | bool paren; |
| 4150 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4151 | if (!BC_PARSE_FUNC(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4152 | |
| 4153 | s = bc_lex_next(&p->l); |
| 4154 | if (s) return s; |
| 4155 | |
| 4156 | t = p->l.t.t; |
| 4157 | paren = t == BC_LEX_LPAREN; |
| 4158 | |
| 4159 | if (t == BC_LEX_NLINE || t == BC_LEX_SCOLON) |
| 4160 | bc_parse_push(p, BC_INST_RET0); |
| 4161 | else { |
| 4162 | |
Denys Vlasenko | 050b0fe | 2018-12-05 22:40:44 +0100 | [diff] [blame] | 4163 | s = bc_parse_expr_empty_ok(p, 0, bc_parse_next_expr); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4164 | if (s == BC_STATUS_PARSE_EMPTY_EXP) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4165 | bc_parse_push(p, BC_INST_RET0); |
| 4166 | s = bc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4167 | } |
Denys Vlasenko | 452df92 | 2018-12-05 20:28:26 +0100 | [diff] [blame] | 4168 | if (s) return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4169 | |
| 4170 | if (!paren || p->l.t.last != BC_LEX_RPAREN) { |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 4171 | s = bc_POSIX_requires("parentheses around return expressions"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4172 | if (s) return s; |
| 4173 | } |
| 4174 | |
| 4175 | bc_parse_push(p, BC_INST_RET); |
| 4176 | } |
| 4177 | |
| 4178 | return s; |
| 4179 | } |
| 4180 | |
| 4181 | static BcStatus bc_parse_endBody(BcParse *p, bool brace) |
| 4182 | { |
| 4183 | BcStatus s = BC_STATUS_SUCCESS; |
| 4184 | |
| 4185 | if (p->flags.len <= 1 || (brace && p->nbraces == 0)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4186 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4187 | |
| 4188 | if (brace) { |
| 4189 | |
| 4190 | if (p->l.t.t == BC_LEX_RBRACE) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4191 | if (!p->nbraces) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4192 | --p->nbraces; |
| 4193 | s = bc_lex_next(&p->l); |
| 4194 | if (s) return s; |
| 4195 | } |
| 4196 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4197 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4198 | } |
| 4199 | |
| 4200 | if (BC_PARSE_IF(p)) { |
| 4201 | |
| 4202 | uint8_t *flag_ptr; |
| 4203 | |
| 4204 | while (p->l.t.t == BC_LEX_NLINE) { |
| 4205 | s = bc_lex_next(&p->l); |
| 4206 | if (s) return s; |
| 4207 | } |
| 4208 | |
| 4209 | bc_vec_pop(&p->flags); |
| 4210 | |
| 4211 | flag_ptr = BC_PARSE_TOP_FLAG_PTR(p); |
| 4212 | *flag_ptr = (*flag_ptr | BC_PARSE_FLAG_IF_END); |
| 4213 | |
| 4214 | if (p->l.t.t == BC_LEX_KEY_ELSE) s = bc_parse_else(p); |
| 4215 | } |
| 4216 | else if (BC_PARSE_ELSE(p)) { |
| 4217 | |
| 4218 | BcInstPtr *ip; |
| 4219 | size_t *label; |
| 4220 | |
| 4221 | bc_vec_pop(&p->flags); |
| 4222 | |
| 4223 | ip = bc_vec_top(&p->exits); |
| 4224 | label = bc_vec_item(&p->func->labels, ip->idx); |
| 4225 | *label = p->func->code.len; |
| 4226 | |
| 4227 | bc_vec_pop(&p->exits); |
| 4228 | } |
| 4229 | else if (BC_PARSE_FUNC_INNER(p)) { |
| 4230 | bc_parse_push(p, BC_INST_RET0); |
| 4231 | bc_parse_updateFunc(p, BC_PROG_MAIN); |
| 4232 | bc_vec_pop(&p->flags); |
| 4233 | } |
| 4234 | else { |
| 4235 | |
| 4236 | BcInstPtr *ip = bc_vec_top(&p->exits); |
| 4237 | size_t *label = bc_vec_top(&p->conds); |
| 4238 | |
| 4239 | bc_parse_push(p, BC_INST_JUMP); |
| 4240 | bc_parse_pushIndex(p, *label); |
| 4241 | |
| 4242 | label = bc_vec_item(&p->func->labels, ip->idx); |
| 4243 | *label = p->func->code.len; |
| 4244 | |
| 4245 | bc_vec_pop(&p->flags); |
| 4246 | bc_vec_pop(&p->exits); |
| 4247 | bc_vec_pop(&p->conds); |
| 4248 | } |
| 4249 | |
| 4250 | return s; |
| 4251 | } |
| 4252 | |
| 4253 | static void bc_parse_startBody(BcParse *p, uint8_t flags) |
| 4254 | { |
| 4255 | uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p); |
| 4256 | flags |= (*flag_ptr & (BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_LOOP)); |
| 4257 | flags |= BC_PARSE_FLAG_BODY; |
| 4258 | bc_vec_push(&p->flags, &flags); |
| 4259 | } |
| 4260 | |
| 4261 | static void bc_parse_noElse(BcParse *p) |
| 4262 | { |
| 4263 | BcInstPtr *ip; |
| 4264 | size_t *label; |
| 4265 | uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p); |
| 4266 | |
| 4267 | *flag_ptr = (*flag_ptr & ~(BC_PARSE_FLAG_IF_END)); |
| 4268 | |
| 4269 | ip = bc_vec_top(&p->exits); |
| 4270 | label = bc_vec_item(&p->func->labels, ip->idx); |
| 4271 | *label = p->func->code.len; |
| 4272 | |
| 4273 | bc_vec_pop(&p->exits); |
| 4274 | } |
| 4275 | |
| 4276 | static BcStatus bc_parse_if(BcParse *p) |
| 4277 | { |
| 4278 | BcStatus s; |
| 4279 | BcInstPtr ip; |
| 4280 | |
| 4281 | s = bc_lex_next(&p->l); |
| 4282 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4283 | 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] | 4284 | |
| 4285 | s = bc_lex_next(&p->l); |
| 4286 | if (s) return s; |
| 4287 | s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel); |
| 4288 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4289 | 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] | 4290 | |
| 4291 | s = bc_lex_next(&p->l); |
| 4292 | if (s) return s; |
| 4293 | bc_parse_push(p, BC_INST_JUMP_ZERO); |
| 4294 | |
| 4295 | ip.idx = p->func->labels.len; |
| 4296 | ip.func = ip.len = 0; |
| 4297 | |
| 4298 | bc_parse_pushIndex(p, ip.idx); |
| 4299 | bc_vec_push(&p->exits, &ip); |
| 4300 | bc_vec_push(&p->func->labels, &ip.idx); |
| 4301 | bc_parse_startBody(p, BC_PARSE_FLAG_IF); |
| 4302 | |
| 4303 | return BC_STATUS_SUCCESS; |
| 4304 | } |
| 4305 | |
| 4306 | static BcStatus bc_parse_else(BcParse *p) |
| 4307 | { |
| 4308 | BcInstPtr ip; |
| 4309 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4310 | if (!BC_PARSE_IF_END(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4311 | |
| 4312 | ip.idx = p->func->labels.len; |
| 4313 | ip.func = ip.len = 0; |
| 4314 | |
| 4315 | bc_parse_push(p, BC_INST_JUMP); |
| 4316 | bc_parse_pushIndex(p, ip.idx); |
| 4317 | |
| 4318 | bc_parse_noElse(p); |
| 4319 | |
| 4320 | bc_vec_push(&p->exits, &ip); |
| 4321 | bc_vec_push(&p->func->labels, &ip.idx); |
| 4322 | bc_parse_startBody(p, BC_PARSE_FLAG_ELSE); |
| 4323 | |
| 4324 | return bc_lex_next(&p->l); |
| 4325 | } |
| 4326 | |
| 4327 | static BcStatus bc_parse_while(BcParse *p) |
| 4328 | { |
| 4329 | BcStatus s; |
| 4330 | BcInstPtr ip; |
| 4331 | |
| 4332 | s = bc_lex_next(&p->l); |
| 4333 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4334 | 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] | 4335 | s = bc_lex_next(&p->l); |
| 4336 | if (s) return s; |
| 4337 | |
| 4338 | ip.idx = p->func->labels.len; |
| 4339 | |
| 4340 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4341 | bc_vec_push(&p->conds, &ip.idx); |
| 4342 | |
| 4343 | ip.idx = p->func->labels.len; |
| 4344 | ip.func = 1; |
| 4345 | ip.len = 0; |
| 4346 | |
| 4347 | bc_vec_push(&p->exits, &ip); |
| 4348 | bc_vec_push(&p->func->labels, &ip.idx); |
| 4349 | |
| 4350 | s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_rel); |
| 4351 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4352 | 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] | 4353 | s = bc_lex_next(&p->l); |
| 4354 | if (s) return s; |
| 4355 | |
| 4356 | bc_parse_push(p, BC_INST_JUMP_ZERO); |
| 4357 | bc_parse_pushIndex(p, ip.idx); |
| 4358 | bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER); |
| 4359 | |
| 4360 | return BC_STATUS_SUCCESS; |
| 4361 | } |
| 4362 | |
| 4363 | static BcStatus bc_parse_for(BcParse *p) |
| 4364 | { |
| 4365 | BcStatus s; |
| 4366 | BcInstPtr ip; |
| 4367 | size_t cond_idx, exit_idx, body_idx, update_idx; |
| 4368 | |
| 4369 | s = bc_lex_next(&p->l); |
| 4370 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4371 | 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] | 4372 | s = bc_lex_next(&p->l); |
| 4373 | if (s) return s; |
| 4374 | |
| 4375 | if (p->l.t.t != BC_LEX_SCOLON) |
| 4376 | s = bc_parse_expr(p, 0, bc_parse_next_for); |
| 4377 | else |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 4378 | s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4379 | |
| 4380 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4381 | 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] | 4382 | s = bc_lex_next(&p->l); |
| 4383 | if (s) return s; |
| 4384 | |
| 4385 | cond_idx = p->func->labels.len; |
| 4386 | update_idx = cond_idx + 1; |
| 4387 | body_idx = update_idx + 1; |
| 4388 | exit_idx = body_idx + 1; |
| 4389 | |
| 4390 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4391 | |
| 4392 | if (p->l.t.t != BC_LEX_SCOLON) |
| 4393 | s = bc_parse_expr(p, BC_PARSE_REL, bc_parse_next_for); |
| 4394 | else |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 4395 | s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4396 | |
| 4397 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4398 | 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] | 4399 | |
| 4400 | s = bc_lex_next(&p->l); |
| 4401 | if (s) return s; |
| 4402 | |
| 4403 | bc_parse_push(p, BC_INST_JUMP_ZERO); |
| 4404 | bc_parse_pushIndex(p, exit_idx); |
| 4405 | bc_parse_push(p, BC_INST_JUMP); |
| 4406 | bc_parse_pushIndex(p, body_idx); |
| 4407 | |
| 4408 | ip.idx = p->func->labels.len; |
| 4409 | |
| 4410 | bc_vec_push(&p->conds, &update_idx); |
| 4411 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4412 | |
| 4413 | if (p->l.t.t != BC_LEX_RPAREN) |
| 4414 | s = bc_parse_expr(p, 0, bc_parse_next_rel); |
| 4415 | else |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 4416 | s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4417 | |
| 4418 | if (s) return s; |
| 4419 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4420 | 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] | 4421 | bc_parse_push(p, BC_INST_JUMP); |
| 4422 | bc_parse_pushIndex(p, cond_idx); |
| 4423 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4424 | |
| 4425 | ip.idx = exit_idx; |
| 4426 | ip.func = 1; |
| 4427 | ip.len = 0; |
| 4428 | |
| 4429 | bc_vec_push(&p->exits, &ip); |
| 4430 | bc_vec_push(&p->func->labels, &ip.idx); |
| 4431 | bc_lex_next(&p->l); |
| 4432 | bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER); |
| 4433 | |
| 4434 | return BC_STATUS_SUCCESS; |
| 4435 | } |
| 4436 | |
| 4437 | static BcStatus bc_parse_loopExit(BcParse *p, BcLexType type) |
| 4438 | { |
| 4439 | BcStatus s; |
| 4440 | size_t i; |
| 4441 | BcInstPtr *ip; |
| 4442 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4443 | if (!BC_PARSE_LOOP(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4444 | |
| 4445 | if (type == BC_LEX_KEY_BREAK) { |
| 4446 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4447 | if (p->exits.len == 0) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4448 | |
| 4449 | i = p->exits.len - 1; |
| 4450 | ip = bc_vec_item(&p->exits, i); |
| 4451 | |
| 4452 | 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] | 4453 | if (i >= p->exits.len && !ip->func) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4454 | |
| 4455 | i = ip->idx; |
| 4456 | } |
| 4457 | else |
| 4458 | i = *((size_t *) bc_vec_top(&p->conds)); |
| 4459 | |
| 4460 | bc_parse_push(p, BC_INST_JUMP); |
| 4461 | bc_parse_pushIndex(p, i); |
| 4462 | |
| 4463 | s = bc_lex_next(&p->l); |
| 4464 | if (s) return s; |
| 4465 | |
| 4466 | 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] | 4467 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4468 | |
| 4469 | return bc_lex_next(&p->l); |
| 4470 | } |
| 4471 | |
| 4472 | static BcStatus bc_parse_func(BcParse *p) |
| 4473 | { |
| 4474 | BcStatus s; |
| 4475 | bool var, comma = false; |
| 4476 | uint8_t flags; |
| 4477 | char *name; |
| 4478 | |
| 4479 | s = bc_lex_next(&p->l); |
| 4480 | if (s) return s; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4481 | if (p->l.t.t != BC_LEX_NAME) |
| 4482 | return bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4483 | |
| 4484 | name = xstrdup(p->l.t.v.v); |
| 4485 | bc_parse_addFunc(p, name, &p->fidx); |
| 4486 | |
| 4487 | s = bc_lex_next(&p->l); |
| 4488 | if (s) return s; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4489 | if (p->l.t.t != BC_LEX_LPAREN) |
| 4490 | return bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4491 | s = bc_lex_next(&p->l); |
| 4492 | if (s) return s; |
| 4493 | |
| 4494 | while (p->l.t.t != BC_LEX_RPAREN) { |
| 4495 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4496 | if (p->l.t.t != BC_LEX_NAME) |
| 4497 | return bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4498 | |
| 4499 | ++p->func->nparams; |
| 4500 | |
| 4501 | name = xstrdup(p->l.t.v.v); |
| 4502 | s = bc_lex_next(&p->l); |
| 4503 | if (s) goto err; |
| 4504 | |
| 4505 | var = p->l.t.t != BC_LEX_LBRACKET; |
| 4506 | |
| 4507 | if (!var) { |
| 4508 | |
| 4509 | s = bc_lex_next(&p->l); |
| 4510 | if (s) goto err; |
| 4511 | |
| 4512 | if (p->l.t.t != BC_LEX_RBRACKET) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4513 | s = bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4514 | goto err; |
| 4515 | } |
| 4516 | |
| 4517 | s = bc_lex_next(&p->l); |
| 4518 | if (s) goto err; |
| 4519 | } |
| 4520 | |
| 4521 | comma = p->l.t.t == BC_LEX_COMMA; |
| 4522 | if (comma) { |
| 4523 | s = bc_lex_next(&p->l); |
| 4524 | if (s) goto err; |
| 4525 | } |
| 4526 | |
| 4527 | s = bc_func_insert(p->func, name, var); |
| 4528 | if (s) goto err; |
| 4529 | } |
| 4530 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4531 | if (comma) return bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4532 | |
| 4533 | flags = BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_BODY; |
| 4534 | bc_parse_startBody(p, flags); |
| 4535 | |
| 4536 | s = bc_lex_next(&p->l); |
| 4537 | if (s) return s; |
| 4538 | |
| 4539 | if (p->l.t.t != BC_LEX_LBRACE) |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 4540 | s = bc_POSIX_requires("the left brace be on the same line as the function header"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4541 | |
| 4542 | return s; |
| 4543 | |
| 4544 | err: |
| 4545 | free(name); |
| 4546 | return s; |
| 4547 | } |
| 4548 | |
| 4549 | static BcStatus bc_parse_auto(BcParse *p) |
| 4550 | { |
| 4551 | BcStatus s; |
| 4552 | bool comma, var, one; |
| 4553 | char *name; |
| 4554 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4555 | if (!p->auto_part) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4556 | s = bc_lex_next(&p->l); |
| 4557 | if (s) return s; |
| 4558 | |
| 4559 | p->auto_part = comma = false; |
| 4560 | one = p->l.t.t == BC_LEX_NAME; |
| 4561 | |
| 4562 | while (p->l.t.t == BC_LEX_NAME) { |
| 4563 | |
| 4564 | name = xstrdup(p->l.t.v.v); |
| 4565 | s = bc_lex_next(&p->l); |
| 4566 | if (s) goto err; |
| 4567 | |
| 4568 | var = p->l.t.t != BC_LEX_LBRACKET; |
| 4569 | if (!var) { |
| 4570 | |
| 4571 | s = bc_lex_next(&p->l); |
| 4572 | if (s) goto err; |
| 4573 | |
| 4574 | if (p->l.t.t != BC_LEX_RBRACKET) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4575 | s = bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4576 | goto err; |
| 4577 | } |
| 4578 | |
| 4579 | s = bc_lex_next(&p->l); |
| 4580 | if (s) goto err; |
| 4581 | } |
| 4582 | |
| 4583 | comma = p->l.t.t == BC_LEX_COMMA; |
| 4584 | if (comma) { |
| 4585 | s = bc_lex_next(&p->l); |
| 4586 | if (s) goto err; |
| 4587 | } |
| 4588 | |
| 4589 | s = bc_func_insert(p->func, name, var); |
| 4590 | if (s) goto err; |
| 4591 | } |
| 4592 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4593 | if (comma) return bc_error("bad function definition"); |
Denys Vlasenko | abbc433 | 2018-12-03 21:46:41 +0100 | [diff] [blame] | 4594 | if (!one) return bc_error("no auto variable found"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4595 | |
| 4596 | 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] | 4597 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4598 | |
| 4599 | return bc_lex_next(&p->l); |
| 4600 | |
| 4601 | err: |
| 4602 | free(name); |
| 4603 | return s; |
| 4604 | } |
| 4605 | |
| 4606 | static BcStatus bc_parse_body(BcParse *p, bool brace) |
| 4607 | { |
| 4608 | BcStatus s = BC_STATUS_SUCCESS; |
| 4609 | uint8_t *flag_ptr = bc_vec_top(&p->flags); |
| 4610 | |
| 4611 | *flag_ptr &= ~(BC_PARSE_FLAG_BODY); |
| 4612 | |
| 4613 | if (*flag_ptr & BC_PARSE_FLAG_FUNC_INNER) { |
| 4614 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4615 | if (!brace) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4616 | p->auto_part = p->l.t.t != BC_LEX_KEY_AUTO; |
| 4617 | |
| 4618 | if (!p->auto_part) { |
| 4619 | s = bc_parse_auto(p); |
| 4620 | if (s) return s; |
| 4621 | } |
| 4622 | |
| 4623 | if (p->l.t.t == BC_LEX_NLINE) s = bc_lex_next(&p->l); |
| 4624 | } |
| 4625 | else { |
| 4626 | s = bc_parse_stmt(p); |
| 4627 | if (!s && !brace) s = bc_parse_endBody(p, false); |
| 4628 | } |
| 4629 | |
| 4630 | return s; |
| 4631 | } |
| 4632 | |
| 4633 | static BcStatus bc_parse_stmt(BcParse *p) |
| 4634 | { |
| 4635 | BcStatus s = BC_STATUS_SUCCESS; |
| 4636 | |
| 4637 | switch (p->l.t.t) { |
| 4638 | |
| 4639 | case BC_LEX_NLINE: |
| 4640 | { |
| 4641 | return bc_lex_next(&p->l); |
| 4642 | } |
| 4643 | |
| 4644 | case BC_LEX_KEY_ELSE: |
| 4645 | { |
| 4646 | p->auto_part = false; |
| 4647 | break; |
| 4648 | } |
| 4649 | |
| 4650 | case BC_LEX_LBRACE: |
| 4651 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4652 | if (!BC_PARSE_BODY(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4653 | |
| 4654 | ++p->nbraces; |
| 4655 | s = bc_lex_next(&p->l); |
| 4656 | if (s) return s; |
| 4657 | |
| 4658 | return bc_parse_body(p, true); |
| 4659 | } |
| 4660 | |
| 4661 | case BC_LEX_KEY_AUTO: |
| 4662 | { |
| 4663 | return bc_parse_auto(p); |
| 4664 | } |
| 4665 | |
| 4666 | default: |
| 4667 | { |
| 4668 | p->auto_part = false; |
| 4669 | |
| 4670 | if (BC_PARSE_IF_END(p)) { |
| 4671 | bc_parse_noElse(p); |
| 4672 | return BC_STATUS_SUCCESS; |
| 4673 | } |
| 4674 | else if (BC_PARSE_BODY(p)) |
| 4675 | return bc_parse_body(p, false); |
| 4676 | |
| 4677 | break; |
| 4678 | } |
| 4679 | } |
| 4680 | |
| 4681 | switch (p->l.t.t) { |
| 4682 | |
| 4683 | case BC_LEX_OP_INC: |
| 4684 | case BC_LEX_OP_DEC: |
| 4685 | case BC_LEX_OP_MINUS: |
| 4686 | case BC_LEX_OP_BOOL_NOT: |
| 4687 | case BC_LEX_LPAREN: |
| 4688 | case BC_LEX_NAME: |
| 4689 | case BC_LEX_NUMBER: |
| 4690 | case BC_LEX_KEY_IBASE: |
| 4691 | case BC_LEX_KEY_LAST: |
| 4692 | case BC_LEX_KEY_LENGTH: |
| 4693 | case BC_LEX_KEY_OBASE: |
| 4694 | case BC_LEX_KEY_READ: |
| 4695 | case BC_LEX_KEY_SCALE: |
| 4696 | case BC_LEX_KEY_SQRT: |
| 4697 | { |
| 4698 | s = bc_parse_expr(p, BC_PARSE_PRINT, bc_parse_next_expr); |
| 4699 | break; |
| 4700 | } |
| 4701 | |
| 4702 | case BC_LEX_KEY_ELSE: |
| 4703 | { |
| 4704 | s = bc_parse_else(p); |
| 4705 | break; |
| 4706 | } |
| 4707 | |
| 4708 | case BC_LEX_SCOLON: |
| 4709 | { |
| 4710 | while (!s && p->l.t.t == BC_LEX_SCOLON) s = bc_lex_next(&p->l); |
| 4711 | break; |
| 4712 | } |
| 4713 | |
| 4714 | case BC_LEX_RBRACE: |
| 4715 | { |
| 4716 | s = bc_parse_endBody(p, true); |
| 4717 | break; |
| 4718 | } |
| 4719 | |
| 4720 | case BC_LEX_STR: |
| 4721 | { |
| 4722 | s = bc_parse_string(p, BC_INST_PRINT_STR); |
| 4723 | break; |
| 4724 | } |
| 4725 | |
| 4726 | case BC_LEX_KEY_BREAK: |
| 4727 | case BC_LEX_KEY_CONTINUE: |
| 4728 | { |
| 4729 | s = bc_parse_loopExit(p, p->l.t.t); |
| 4730 | break; |
| 4731 | } |
| 4732 | |
| 4733 | case BC_LEX_KEY_FOR: |
| 4734 | { |
| 4735 | s = bc_parse_for(p); |
| 4736 | break; |
| 4737 | } |
| 4738 | |
| 4739 | case BC_LEX_KEY_HALT: |
| 4740 | { |
| 4741 | bc_parse_push(p, BC_INST_HALT); |
| 4742 | s = bc_lex_next(&p->l); |
| 4743 | break; |
| 4744 | } |
| 4745 | |
| 4746 | case BC_LEX_KEY_IF: |
| 4747 | { |
| 4748 | s = bc_parse_if(p); |
| 4749 | break; |
| 4750 | } |
| 4751 | |
| 4752 | case BC_LEX_KEY_LIMITS: |
| 4753 | { |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 4754 | // "limits" is a compile-time command, |
| 4755 | // the output is produced at _parse time_. |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4756 | s = bc_lex_next(&p->l); |
| 4757 | if (s) return s; |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 4758 | printf( |
| 4759 | "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n" |
| 4760 | "BC_DIM_MAX = "BC_MAX_DIM_STR "\n" |
| 4761 | "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n" |
| 4762 | "BC_STRING_MAX = "BC_MAX_STRING_STR"\n" |
| 4763 | "BC_NAME_MAX = "BC_MAX_NAME_STR "\n" |
| 4764 | "BC_NUM_MAX = "BC_MAX_NUM_STR "\n" |
| 4765 | "MAX Exponent = "BC_MAX_EXP_STR "\n" |
| 4766 | "Number of vars = "BC_MAX_VARS_STR "\n" |
| 4767 | ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4768 | break; |
| 4769 | } |
| 4770 | |
| 4771 | case BC_LEX_KEY_PRINT: |
| 4772 | { |
| 4773 | s = bc_parse_print(p); |
| 4774 | break; |
| 4775 | } |
| 4776 | |
| 4777 | case BC_LEX_KEY_QUIT: |
| 4778 | { |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 4779 | // "quit" is a compile-time command. For example, |
| 4780 | // "if (0 == 1) quit" terminates when parsing the statement, |
| 4781 | // not when it is executed |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 4782 | QUIT_OR_RETURN_TO_MAIN; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4783 | } |
| 4784 | |
| 4785 | case BC_LEX_KEY_RETURN: |
| 4786 | { |
| 4787 | s = bc_parse_return(p); |
| 4788 | break; |
| 4789 | } |
| 4790 | |
| 4791 | case BC_LEX_KEY_WHILE: |
| 4792 | { |
| 4793 | s = bc_parse_while(p); |
| 4794 | break; |
| 4795 | } |
| 4796 | |
| 4797 | default: |
| 4798 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4799 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4800 | break; |
| 4801 | } |
| 4802 | } |
| 4803 | |
| 4804 | return s; |
| 4805 | } |
| 4806 | |
| 4807 | static BcStatus bc_parse_parse(BcParse *p) |
| 4808 | { |
| 4809 | BcStatus s; |
| 4810 | |
| 4811 | if (p->l.t.t == BC_LEX_EOF) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4812 | 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] | 4813 | else if (p->l.t.t == BC_LEX_KEY_DEFINE) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4814 | if (!BC_PARSE_CAN_EXEC(p)) return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4815 | s = bc_parse_func(p); |
| 4816 | } |
| 4817 | else |
| 4818 | s = bc_parse_stmt(p); |
| 4819 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 4820 | if (s || G_interrupt) { |
| 4821 | bc_parse_reset(p); |
| 4822 | s = BC_STATUS_FAILURE; |
| 4823 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4824 | |
| 4825 | return s; |
| 4826 | } |
| 4827 | |
Denys Vlasenko | 050b0fe | 2018-12-05 22:40:44 +0100 | [diff] [blame] | 4828 | static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags, BcParseNext next) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4829 | { |
| 4830 | BcStatus s = BC_STATUS_SUCCESS; |
| 4831 | BcInst prev = BC_INST_PRINT; |
| 4832 | BcLexType top, t = p->l.t.t; |
| 4833 | size_t nexprs = 0, ops_bgn = p->ops.len; |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 4834 | unsigned nparens, nrelops; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4835 | bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last; |
| 4836 | |
| 4837 | paren_first = p->l.t.t == BC_LEX_LPAREN; |
| 4838 | nparens = nrelops = 0; |
| 4839 | paren_expr = rprn = done = get_token = assign = false; |
| 4840 | bin_last = true; |
| 4841 | |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 4842 | 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] | 4843 | switch (t) { |
| 4844 | |
| 4845 | case BC_LEX_OP_INC: |
| 4846 | case BC_LEX_OP_DEC: |
| 4847 | { |
| 4848 | s = bc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags); |
| 4849 | rprn = get_token = bin_last = false; |
| 4850 | break; |
| 4851 | } |
| 4852 | |
| 4853 | case BC_LEX_OP_MINUS: |
| 4854 | { |
| 4855 | s = bc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs); |
| 4856 | rprn = get_token = false; |
| 4857 | bin_last = prev == BC_INST_MINUS; |
| 4858 | break; |
| 4859 | } |
| 4860 | |
| 4861 | case BC_LEX_OP_ASSIGN_POWER: |
| 4862 | case BC_LEX_OP_ASSIGN_MULTIPLY: |
| 4863 | case BC_LEX_OP_ASSIGN_DIVIDE: |
| 4864 | case BC_LEX_OP_ASSIGN_MODULUS: |
| 4865 | case BC_LEX_OP_ASSIGN_PLUS: |
| 4866 | case BC_LEX_OP_ASSIGN_MINUS: |
| 4867 | case BC_LEX_OP_ASSIGN: |
| 4868 | { |
| 4869 | if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM && |
| 4870 | prev != BC_INST_SCALE && prev != BC_INST_IBASE && |
| 4871 | prev != BC_INST_OBASE && prev != BC_INST_LAST) |
| 4872 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4873 | s = bc_error("bad assignment:" |
| 4874 | " left side must be scale," |
| 4875 | " ibase, obase, last, var," |
| 4876 | " or array element" |
| 4877 | ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4878 | break; |
| 4879 | } |
| 4880 | } |
| 4881 | // Fallthrough. |
| 4882 | case BC_LEX_OP_POWER: |
| 4883 | case BC_LEX_OP_MULTIPLY: |
| 4884 | case BC_LEX_OP_DIVIDE: |
| 4885 | case BC_LEX_OP_MODULUS: |
| 4886 | case BC_LEX_OP_PLUS: |
| 4887 | case BC_LEX_OP_REL_EQ: |
| 4888 | case BC_LEX_OP_REL_LE: |
| 4889 | case BC_LEX_OP_REL_GE: |
| 4890 | case BC_LEX_OP_REL_NE: |
| 4891 | case BC_LEX_OP_REL_LT: |
| 4892 | case BC_LEX_OP_REL_GT: |
| 4893 | case BC_LEX_OP_BOOL_NOT: |
| 4894 | case BC_LEX_OP_BOOL_OR: |
| 4895 | case BC_LEX_OP_BOOL_AND: |
| 4896 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4897 | if (((t == BC_LEX_OP_BOOL_NOT) != bin_last) |
| 4898 | || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT) |
| 4899 | ) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4900 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4901 | } |
| 4902 | |
| 4903 | nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT; |
| 4904 | prev = BC_PARSE_TOKEN_INST(t); |
| 4905 | s = bc_parse_operator(p, t, ops_bgn, &nexprs, true); |
| 4906 | rprn = get_token = false; |
| 4907 | bin_last = t != BC_LEX_OP_BOOL_NOT; |
| 4908 | |
| 4909 | break; |
| 4910 | } |
| 4911 | |
| 4912 | case BC_LEX_LPAREN: |
| 4913 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4914 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4915 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4916 | ++nparens; |
| 4917 | paren_expr = rprn = bin_last = false; |
| 4918 | get_token = true; |
| 4919 | bc_vec_push(&p->ops, &t); |
| 4920 | |
| 4921 | break; |
| 4922 | } |
| 4923 | |
| 4924 | case BC_LEX_RPAREN: |
| 4925 | { |
| 4926 | if (bin_last || prev == BC_INST_BOOL_NOT) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4927 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4928 | |
| 4929 | if (nparens == 0) { |
| 4930 | s = BC_STATUS_SUCCESS; |
| 4931 | done = true; |
| 4932 | get_token = false; |
| 4933 | break; |
| 4934 | } |
| 4935 | else if (!paren_expr) |
| 4936 | return BC_STATUS_PARSE_EMPTY_EXP; |
| 4937 | |
| 4938 | --nparens; |
| 4939 | paren_expr = rprn = true; |
| 4940 | get_token = bin_last = false; |
| 4941 | |
| 4942 | s = bc_parse_rightParen(p, ops_bgn, &nexprs); |
| 4943 | |
| 4944 | break; |
| 4945 | } |
| 4946 | |
| 4947 | case BC_LEX_NAME: |
| 4948 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4949 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4950 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4951 | paren_expr = true; |
| 4952 | rprn = get_token = bin_last = false; |
| 4953 | s = bc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL); |
| 4954 | ++nexprs; |
| 4955 | |
| 4956 | break; |
| 4957 | } |
| 4958 | |
| 4959 | case BC_LEX_NUMBER: |
| 4960 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4961 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4962 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4963 | bc_parse_number(p, &prev, &nexprs); |
| 4964 | paren_expr = get_token = true; |
| 4965 | rprn = bin_last = false; |
| 4966 | |
| 4967 | break; |
| 4968 | } |
| 4969 | |
| 4970 | case BC_LEX_KEY_IBASE: |
| 4971 | case BC_LEX_KEY_LAST: |
| 4972 | case BC_LEX_KEY_OBASE: |
| 4973 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4974 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4975 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4976 | prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE); |
| 4977 | bc_parse_push(p, (char) prev); |
| 4978 | |
| 4979 | paren_expr = get_token = true; |
| 4980 | rprn = bin_last = false; |
| 4981 | ++nexprs; |
| 4982 | |
| 4983 | break; |
| 4984 | } |
| 4985 | |
| 4986 | case BC_LEX_KEY_LENGTH: |
| 4987 | case BC_LEX_KEY_SQRT: |
| 4988 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4989 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4990 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4991 | s = bc_parse_builtin(p, t, flags, &prev); |
| 4992 | paren_expr = true; |
| 4993 | rprn = get_token = bin_last = false; |
| 4994 | ++nexprs; |
| 4995 | |
| 4996 | break; |
| 4997 | } |
| 4998 | |
| 4999 | case BC_LEX_KEY_READ: |
| 5000 | { |
| 5001 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5002 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5003 | else if (flags & BC_PARSE_NOREAD) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5004 | s = bc_error_nested_read_call(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5005 | else |
| 5006 | s = bc_parse_read(p); |
| 5007 | |
| 5008 | paren_expr = true; |
| 5009 | rprn = get_token = bin_last = false; |
| 5010 | ++nexprs; |
| 5011 | prev = BC_INST_READ; |
| 5012 | |
| 5013 | break; |
| 5014 | } |
| 5015 | |
| 5016 | case BC_LEX_KEY_SCALE: |
| 5017 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5018 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5019 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5020 | s = bc_parse_scale(p, &prev, flags); |
| 5021 | paren_expr = true; |
| 5022 | rprn = get_token = bin_last = false; |
| 5023 | ++nexprs; |
| 5024 | prev = BC_INST_SCALE; |
| 5025 | |
| 5026 | break; |
| 5027 | } |
| 5028 | |
| 5029 | default: |
| 5030 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5031 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5032 | break; |
| 5033 | } |
| 5034 | } |
| 5035 | |
| 5036 | if (!s && get_token) s = bc_lex_next(&p->l); |
| 5037 | } |
| 5038 | |
| 5039 | if (s) return s; |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 5040 | if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5041 | |
| 5042 | while (p->ops.len > ops_bgn) { |
| 5043 | |
| 5044 | top = BC_PARSE_TOP_OP(p); |
| 5045 | assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN; |
| 5046 | |
| 5047 | if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5048 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5049 | |
| 5050 | bc_parse_push(p, BC_PARSE_TOKEN_INST(top)); |
| 5051 | |
| 5052 | nexprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG; |
| 5053 | bc_vec_pop(&p->ops); |
| 5054 | } |
| 5055 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5056 | if (prev == BC_INST_BOOL_NOT || nexprs != 1) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5057 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5058 | |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 5059 | // next is BcParseNext, byte array of up to 4 BC_LEX's, packed into 32-bit word |
| 5060 | for (;;) { |
| 5061 | if (t == (next & 0x7f)) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5062 | goto ok; |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 5063 | if (next & 0x80) // last element? |
| 5064 | break; |
| 5065 | next >>= 8; |
| 5066 | } |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5067 | return bc_error_bad_expression(); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5068 | ok: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5069 | |
| 5070 | if (!(flags & BC_PARSE_REL) && nrelops) { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 5071 | s = bc_POSIX_does_not_allow("comparison operators outside if or loops"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5072 | if (s) return s; |
| 5073 | } |
| 5074 | else if ((flags & BC_PARSE_REL) && nrelops > 1) { |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 5075 | s = bc_POSIX_requires("exactly one comparison operator per condition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5076 | if (s) return s; |
| 5077 | } |
| 5078 | |
| 5079 | if (flags & BC_PARSE_PRINT) { |
| 5080 | if (paren_first || !assign) bc_parse_push(p, BC_INST_PRINT); |
| 5081 | bc_parse_push(p, BC_INST_POP); |
| 5082 | } |
| 5083 | |
| 5084 | return s; |
| 5085 | } |
| 5086 | |
Denys Vlasenko | 050b0fe | 2018-12-05 22:40:44 +0100 | [diff] [blame] | 5087 | static BcStatus bc_parse_expr(BcParse *p, uint8_t flags, BcParseNext next) |
| 5088 | { |
| 5089 | BcStatus s; |
| 5090 | |
| 5091 | s = bc_parse_expr_empty_ok(p, flags, next); |
| 5092 | if (s == BC_STATUS_PARSE_EMPTY_EXP) |
| 5093 | return bc_error("empty expression"); |
| 5094 | return s; |
| 5095 | } |
| 5096 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5097 | static void bc_parse_init(BcParse *p, size_t func) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5098 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5099 | bc_parse_create(p, func, bc_parse_parse, bc_lex_token); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5100 | } |
| 5101 | |
| 5102 | static BcStatus bc_parse_expression(BcParse *p, uint8_t flags) |
| 5103 | { |
| 5104 | return bc_parse_expr(p, flags, bc_parse_next_read); |
| 5105 | } |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 5106 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5107 | #endif // ENABLE_BC |
| 5108 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5109 | #if ENABLE_DC |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 5110 | |
| 5111 | #define DC_PARSE_BUF_LEN ((int) (sizeof(uint32_t) * CHAR_BIT)) |
| 5112 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5113 | static BcStatus dc_parse_register(BcParse *p) |
| 5114 | { |
| 5115 | BcStatus s; |
| 5116 | char *name; |
| 5117 | |
| 5118 | s = bc_lex_next(&p->l); |
| 5119 | if (s) return s; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5120 | 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] | 5121 | |
| 5122 | name = xstrdup(p->l.t.v.v); |
| 5123 | bc_parse_pushName(p, name); |
| 5124 | |
| 5125 | return s; |
| 5126 | } |
| 5127 | |
| 5128 | static BcStatus dc_parse_string(BcParse *p) |
| 5129 | { |
| 5130 | char *str, *name, b[DC_PARSE_BUF_LEN + 1]; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5131 | size_t idx, len = G.prog.strs.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5132 | |
| 5133 | sprintf(b, "%0*zu", DC_PARSE_BUF_LEN, len); |
| 5134 | name = xstrdup(b); |
| 5135 | |
| 5136 | str = xstrdup(p->l.t.v.v); |
| 5137 | bc_parse_push(p, BC_INST_STR); |
| 5138 | bc_parse_pushIndex(p, len); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5139 | bc_vec_push(&G.prog.strs, &str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5140 | bc_parse_addFunc(p, name, &idx); |
| 5141 | |
| 5142 | return bc_lex_next(&p->l); |
| 5143 | } |
| 5144 | |
| 5145 | static BcStatus dc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store) |
| 5146 | { |
| 5147 | BcStatus s; |
| 5148 | |
| 5149 | bc_parse_push(p, inst); |
| 5150 | if (name) { |
| 5151 | s = dc_parse_register(p); |
| 5152 | if (s) return s; |
| 5153 | } |
| 5154 | |
| 5155 | if (store) { |
| 5156 | bc_parse_push(p, BC_INST_SWAP); |
| 5157 | bc_parse_push(p, BC_INST_ASSIGN); |
| 5158 | bc_parse_push(p, BC_INST_POP); |
| 5159 | } |
| 5160 | |
| 5161 | return bc_lex_next(&p->l); |
| 5162 | } |
| 5163 | |
| 5164 | static BcStatus dc_parse_cond(BcParse *p, uint8_t inst) |
| 5165 | { |
| 5166 | BcStatus s; |
| 5167 | |
| 5168 | bc_parse_push(p, inst); |
| 5169 | bc_parse_push(p, BC_INST_EXEC_COND); |
| 5170 | |
| 5171 | s = dc_parse_register(p); |
| 5172 | if (s) return s; |
| 5173 | |
| 5174 | s = bc_lex_next(&p->l); |
| 5175 | if (s) return s; |
| 5176 | |
| 5177 | if (p->l.t.t == BC_LEX_ELSE) { |
| 5178 | s = dc_parse_register(p); |
| 5179 | if (s) return s; |
| 5180 | s = bc_lex_next(&p->l); |
| 5181 | } |
| 5182 | else |
| 5183 | bc_parse_push(p, BC_PARSE_STREND); |
| 5184 | |
| 5185 | return s; |
| 5186 | } |
| 5187 | |
| 5188 | static BcStatus dc_parse_token(BcParse *p, BcLexType t, uint8_t flags) |
| 5189 | { |
| 5190 | BcStatus s = BC_STATUS_SUCCESS; |
| 5191 | BcInst prev; |
| 5192 | uint8_t inst; |
| 5193 | bool assign, get_token = false; |
| 5194 | |
| 5195 | switch (t) { |
| 5196 | |
| 5197 | case BC_LEX_OP_REL_EQ: |
| 5198 | case BC_LEX_OP_REL_LE: |
| 5199 | case BC_LEX_OP_REL_GE: |
| 5200 | case BC_LEX_OP_REL_NE: |
| 5201 | case BC_LEX_OP_REL_LT: |
| 5202 | case BC_LEX_OP_REL_GT: |
| 5203 | { |
| 5204 | s = dc_parse_cond(p, t - BC_LEX_OP_REL_EQ + BC_INST_REL_EQ); |
| 5205 | break; |
| 5206 | } |
| 5207 | |
| 5208 | case BC_LEX_SCOLON: |
| 5209 | case BC_LEX_COLON: |
| 5210 | { |
| 5211 | s = dc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON); |
| 5212 | break; |
| 5213 | } |
| 5214 | |
| 5215 | case BC_LEX_STR: |
| 5216 | { |
| 5217 | s = dc_parse_string(p); |
| 5218 | break; |
| 5219 | } |
| 5220 | |
| 5221 | case BC_LEX_NEG: |
| 5222 | case BC_LEX_NUMBER: |
| 5223 | { |
| 5224 | if (t == BC_LEX_NEG) { |
| 5225 | s = bc_lex_next(&p->l); |
| 5226 | if (s) return s; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5227 | if (p->l.t.t != BC_LEX_NUMBER) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5228 | return bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5229 | } |
| 5230 | |
| 5231 | bc_parse_number(p, &prev, &p->nbraces); |
| 5232 | |
| 5233 | if (t == BC_LEX_NEG) bc_parse_push(p, BC_INST_NEG); |
| 5234 | get_token = true; |
| 5235 | |
| 5236 | break; |
| 5237 | } |
| 5238 | |
| 5239 | case BC_LEX_KEY_READ: |
| 5240 | { |
| 5241 | if (flags & BC_PARSE_NOREAD) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5242 | s = bc_error_nested_read_call(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5243 | else |
| 5244 | bc_parse_push(p, BC_INST_READ); |
| 5245 | get_token = true; |
| 5246 | break; |
| 5247 | } |
| 5248 | |
| 5249 | case BC_LEX_OP_ASSIGN: |
| 5250 | case BC_LEX_STORE_PUSH: |
| 5251 | { |
| 5252 | assign = t == BC_LEX_OP_ASSIGN; |
| 5253 | inst = assign ? BC_INST_VAR : BC_INST_PUSH_TO_VAR; |
| 5254 | s = dc_parse_mem(p, inst, true, assign); |
| 5255 | break; |
| 5256 | } |
| 5257 | |
| 5258 | case BC_LEX_LOAD: |
| 5259 | case BC_LEX_LOAD_POP: |
| 5260 | { |
| 5261 | inst = t == BC_LEX_LOAD_POP ? BC_INST_PUSH_VAR : BC_INST_LOAD; |
| 5262 | s = dc_parse_mem(p, inst, true, false); |
| 5263 | break; |
| 5264 | } |
| 5265 | |
| 5266 | case BC_LEX_STORE_IBASE: |
| 5267 | case BC_LEX_STORE_SCALE: |
| 5268 | case BC_LEX_STORE_OBASE: |
| 5269 | { |
| 5270 | inst = t - BC_LEX_STORE_IBASE + BC_INST_IBASE; |
| 5271 | s = dc_parse_mem(p, inst, false, true); |
| 5272 | break; |
| 5273 | } |
| 5274 | |
| 5275 | default: |
| 5276 | { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5277 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5278 | get_token = true; |
| 5279 | break; |
| 5280 | } |
| 5281 | } |
| 5282 | |
| 5283 | if (!s && get_token) s = bc_lex_next(&p->l); |
| 5284 | |
| 5285 | return s; |
| 5286 | } |
| 5287 | |
| 5288 | static BcStatus dc_parse_expr(BcParse *p, uint8_t flags) |
| 5289 | { |
| 5290 | BcStatus s = BC_STATUS_SUCCESS; |
| 5291 | BcInst inst; |
| 5292 | BcLexType t; |
| 5293 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5294 | if (flags & BC_PARSE_NOCALL) p->nbraces = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5295 | |
| 5296 | for (t = p->l.t.t; !s && t != BC_LEX_EOF; t = p->l.t.t) { |
| 5297 | |
| 5298 | inst = dc_parse_insts[t]; |
| 5299 | |
| 5300 | if (inst != BC_INST_INVALID) { |
| 5301 | bc_parse_push(p, inst); |
| 5302 | s = bc_lex_next(&p->l); |
| 5303 | } |
| 5304 | else |
| 5305 | s = dc_parse_token(p, t, flags); |
| 5306 | } |
| 5307 | |
| 5308 | if (!s && p->l.t.t == BC_LEX_EOF && (flags & BC_PARSE_NOCALL)) |
| 5309 | bc_parse_push(p, BC_INST_POP_EXEC); |
| 5310 | |
| 5311 | return s; |
| 5312 | } |
| 5313 | |
| 5314 | static BcStatus dc_parse_parse(BcParse *p) |
| 5315 | { |
| 5316 | BcStatus s; |
| 5317 | |
| 5318 | if (p->l.t.t == BC_LEX_EOF) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5319 | s = bc_error("end of file"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5320 | else |
| 5321 | s = dc_parse_expr(p, 0); |
| 5322 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 5323 | if (s || G_interrupt) { |
| 5324 | bc_parse_reset(p); |
| 5325 | s = BC_STATUS_FAILURE; |
| 5326 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5327 | |
| 5328 | return s; |
| 5329 | } |
| 5330 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5331 | static void dc_parse_init(BcParse *p, size_t func) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5332 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5333 | bc_parse_create(p, func, dc_parse_parse, dc_lex_token); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5334 | } |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 5335 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5336 | #endif // ENABLE_DC |
| 5337 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5338 | static void common_parse_init(BcParse *p, size_t func) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5339 | { |
| 5340 | if (IS_BC) { |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 5341 | IF_BC(bc_parse_init(p, func);) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5342 | } else { |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 5343 | IF_DC(dc_parse_init(p, func);) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5344 | } |
| 5345 | } |
| 5346 | |
| 5347 | static BcStatus common_parse_expr(BcParse *p, uint8_t flags) |
| 5348 | { |
| 5349 | if (IS_BC) { |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 5350 | IF_BC(return bc_parse_expression(p, flags);) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5351 | } else { |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 5352 | IF_DC(return dc_parse_expr(p, flags);) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5353 | } |
| 5354 | } |
| 5355 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5356 | static BcVec* bc_program_search(char *id, bool var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5357 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5358 | BcId e, *ptr; |
| 5359 | BcVec *v, *map; |
| 5360 | size_t i; |
| 5361 | BcResultData data; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 5362 | int new; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5363 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5364 | v = var ? &G.prog.vars : &G.prog.arrs; |
| 5365 | map = var ? &G.prog.var_map : &G.prog.arr_map; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5366 | |
| 5367 | e.name = id; |
| 5368 | e.idx = v->len; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 5369 | new = bc_map_insert(map, &e, &i); // 1 if insertion was successful |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5370 | |
| 5371 | if (new) { |
| 5372 | bc_array_init(&data.v, var); |
| 5373 | bc_vec_push(v, &data.v); |
| 5374 | } |
| 5375 | |
| 5376 | ptr = bc_vec_item(map, i); |
| 5377 | if (new) ptr->name = xstrdup(e.name); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5378 | return bc_vec_item(v, ptr->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5379 | } |
| 5380 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5381 | static BcStatus bc_program_num(BcResult *r, BcNum **num, bool hex) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5382 | { |
| 5383 | BcStatus s = BC_STATUS_SUCCESS; |
| 5384 | |
| 5385 | switch (r->t) { |
| 5386 | |
| 5387 | case BC_RESULT_STR: |
| 5388 | case BC_RESULT_TEMP: |
| 5389 | case BC_RESULT_IBASE: |
| 5390 | case BC_RESULT_SCALE: |
| 5391 | case BC_RESULT_OBASE: |
| 5392 | { |
| 5393 | *num = &r->d.n; |
| 5394 | break; |
| 5395 | } |
| 5396 | |
| 5397 | case BC_RESULT_CONSTANT: |
| 5398 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5399 | char **str = bc_vec_item(&G.prog.consts, r->d.id.idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5400 | size_t base_t, len = strlen(*str); |
| 5401 | BcNum *base; |
| 5402 | |
| 5403 | bc_num_init(&r->d.n, len); |
| 5404 | |
| 5405 | hex = hex && len == 1; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5406 | base = hex ? &G.prog.hexb : &G.prog.ib; |
| 5407 | base_t = hex ? BC_NUM_MAX_IBASE : G.prog.ib_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5408 | s = bc_num_parse(&r->d.n, *str, base, base_t); |
| 5409 | |
| 5410 | if (s) { |
| 5411 | bc_num_free(&r->d.n); |
| 5412 | return s; |
| 5413 | } |
| 5414 | |
| 5415 | *num = &r->d.n; |
| 5416 | r->t = BC_RESULT_TEMP; |
| 5417 | |
| 5418 | break; |
| 5419 | } |
| 5420 | |
| 5421 | case BC_RESULT_VAR: |
| 5422 | case BC_RESULT_ARRAY: |
| 5423 | case BC_RESULT_ARRAY_ELEM: |
| 5424 | { |
| 5425 | BcVec *v; |
| 5426 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5427 | 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] | 5428 | |
| 5429 | if (r->t == BC_RESULT_ARRAY_ELEM) { |
| 5430 | v = bc_vec_top(v); |
| 5431 | if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1); |
| 5432 | *num = bc_vec_item(v, r->d.id.idx); |
| 5433 | } |
| 5434 | else |
| 5435 | *num = bc_vec_top(v); |
| 5436 | |
| 5437 | break; |
| 5438 | } |
| 5439 | |
| 5440 | case BC_RESULT_LAST: |
| 5441 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5442 | *num = &G.prog.last; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5443 | break; |
| 5444 | } |
| 5445 | |
| 5446 | case BC_RESULT_ONE: |
| 5447 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5448 | *num = &G.prog.one; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5449 | break; |
| 5450 | } |
| 5451 | } |
| 5452 | |
| 5453 | return s; |
| 5454 | } |
| 5455 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5456 | static BcStatus bc_program_binOpPrep(BcResult **l, BcNum **ln, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5457 | BcResult **r, BcNum **rn, bool assign) |
| 5458 | { |
| 5459 | BcStatus s; |
| 5460 | bool hex; |
| 5461 | BcResultType lt, rt; |
| 5462 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5463 | if (!BC_PROG_STACK(&G.prog.results, 2)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5464 | return bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5465 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5466 | *r = bc_vec_item_rev(&G.prog.results, 0); |
| 5467 | *l = bc_vec_item_rev(&G.prog.results, 1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5468 | |
| 5469 | lt = (*l)->t; |
| 5470 | rt = (*r)->t; |
| 5471 | hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE); |
| 5472 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5473 | s = bc_program_num(*l, ln, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5474 | if (s) return s; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5475 | s = bc_program_num(*r, rn, hex); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5476 | if (s) return s; |
| 5477 | |
| 5478 | // We run this again under these conditions in case any vector has been |
| 5479 | // reallocated out from under the BcNums or arrays we had. |
| 5480 | if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5481 | s = bc_program_num(*l, ln, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5482 | if (s) return s; |
| 5483 | } |
| 5484 | |
| 5485 | if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5486 | return bc_error_variable_is_wrong_type(); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5487 | if (!assign && !BC_PROG_NUM((*r), (*ln))) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5488 | return bc_error_variable_is_wrong_type(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5489 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5490 | return s; |
| 5491 | } |
| 5492 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5493 | static void bc_program_binOpRetire(BcResult *r) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5494 | { |
| 5495 | r->t = BC_RESULT_TEMP; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5496 | bc_vec_pop(&G.prog.results); |
| 5497 | bc_vec_pop(&G.prog.results); |
| 5498 | bc_vec_push(&G.prog.results, r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5499 | } |
| 5500 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5501 | static BcStatus bc_program_prep(BcResult **r, BcNum **n) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5502 | { |
| 5503 | BcStatus s; |
| 5504 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5505 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5506 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5507 | *r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5508 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5509 | s = bc_program_num(*r, n, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5510 | if (s) return s; |
| 5511 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5512 | if (!BC_PROG_NUM((*r), (*n))) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5513 | return bc_error_variable_is_wrong_type(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5514 | |
| 5515 | return s; |
| 5516 | } |
| 5517 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5518 | static void bc_program_retire(BcResult *r, BcResultType t) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5519 | { |
| 5520 | r->t = t; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5521 | bc_vec_pop(&G.prog.results); |
| 5522 | bc_vec_push(&G.prog.results, r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5523 | } |
| 5524 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5525 | static BcStatus bc_program_op(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5526 | { |
| 5527 | BcStatus s; |
| 5528 | BcResult *opd1, *opd2, res; |
| 5529 | BcNum *n1, *n2 = NULL; |
| 5530 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5531 | s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5532 | if (s) return s; |
| 5533 | bc_num_init(&res.d.n, BC_NUM_DEF_SIZE); |
| 5534 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5535 | 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] | 5536 | if (s) goto err; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5537 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5538 | |
| 5539 | return s; |
| 5540 | |
| 5541 | err: |
| 5542 | bc_num_free(&res.d.n); |
| 5543 | return s; |
| 5544 | } |
| 5545 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 5546 | static BcStatus bc_program_read(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5547 | { |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5548 | const char *sv_file; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5549 | BcStatus s; |
| 5550 | BcParse parse; |
| 5551 | BcVec buf; |
| 5552 | BcInstPtr ip; |
| 5553 | size_t i; |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 5554 | BcFunc *f = bc_program_func(BC_PROG_READ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5555 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5556 | for (i = 0; i < G.prog.stack.len; ++i) { |
| 5557 | BcInstPtr *ip_ptr = bc_vec_item(&G.prog.stack, i); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5558 | if (ip_ptr->func == BC_PROG_READ) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5559 | return bc_error_nested_read_call(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5560 | } |
| 5561 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 5562 | bc_vec_pop_all(&f->code); |
| 5563 | bc_char_vec_init(&buf); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5564 | |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5565 | sv_file = G.prog.file; |
| 5566 | G.prog.file = NULL; |
| 5567 | |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 5568 | s = bc_read_line(&buf); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5569 | if (s) goto io_err; |
| 5570 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5571 | common_parse_init(&parse, BC_PROG_READ); |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5572 | bc_lex_file(&parse.l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5573 | |
| 5574 | s = bc_parse_text(&parse, buf.v); |
| 5575 | if (s) goto exec_err; |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 5576 | s = common_parse_expr(&parse, BC_PARSE_NOREAD); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5577 | if (s) goto exec_err; |
| 5578 | |
| 5579 | 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] | 5580 | s = bc_error("bad read() expression"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5581 | goto exec_err; |
| 5582 | } |
| 5583 | |
| 5584 | ip.func = BC_PROG_READ; |
| 5585 | ip.idx = 0; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5586 | ip.len = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5587 | |
| 5588 | // Update this pointer, just in case. |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 5589 | f = bc_program_func(BC_PROG_READ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5590 | |
| 5591 | bc_vec_pushByte(&f->code, BC_INST_POP_EXEC); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5592 | bc_vec_push(&G.prog.stack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5593 | |
| 5594 | exec_err: |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5595 | G.prog.file = sv_file; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5596 | bc_parse_free(&parse); |
| 5597 | io_err: |
| 5598 | bc_vec_free(&buf); |
| 5599 | return s; |
| 5600 | } |
| 5601 | |
| 5602 | static size_t bc_program_index(char *code, size_t *bgn) |
| 5603 | { |
| 5604 | char amt = code[(*bgn)++], i = 0; |
| 5605 | size_t res = 0; |
| 5606 | |
| 5607 | for (; i < amt; ++i, ++(*bgn)) |
| 5608 | res |= (((size_t)((int) code[*bgn]) & UCHAR_MAX) << (i * CHAR_BIT)); |
| 5609 | |
| 5610 | return res; |
| 5611 | } |
| 5612 | |
| 5613 | static char *bc_program_name(char *code, size_t *bgn) |
| 5614 | { |
| 5615 | size_t i; |
| 5616 | char c, *s, *str = code + *bgn, *ptr = strchr(str, BC_PARSE_STREND); |
| 5617 | |
| 5618 | s = xmalloc(ptr - str + 1); |
| 5619 | c = code[(*bgn)++]; |
| 5620 | |
| 5621 | for (i = 0; c != 0 && c != BC_PARSE_STREND; c = code[(*bgn)++], ++i) |
| 5622 | s[i] = c; |
| 5623 | |
| 5624 | s[i] = '\0'; |
| 5625 | |
| 5626 | return s; |
| 5627 | } |
| 5628 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 5629 | static void bc_program_printString(const char *str) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5630 | { |
| 5631 | size_t i, len = strlen(str); |
| 5632 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5633 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5634 | if (len == 0) { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5635 | bb_putchar('\0'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5636 | return; |
| 5637 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5638 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5639 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 5640 | for (i = 0; i < len; ++i, ++G.prog.nchars) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5641 | |
| 5642 | int c = str[i]; |
| 5643 | |
| 5644 | if (c != '\\' || i == len - 1) |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5645 | bb_putchar(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5646 | else { |
| 5647 | |
| 5648 | c = str[++i]; |
| 5649 | |
| 5650 | switch (c) { |
| 5651 | |
| 5652 | case 'a': |
| 5653 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5654 | bb_putchar('\a'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5655 | break; |
| 5656 | } |
| 5657 | |
| 5658 | case 'b': |
| 5659 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5660 | bb_putchar('\b'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5661 | break; |
| 5662 | } |
| 5663 | |
| 5664 | case '\\': |
| 5665 | case 'e': |
| 5666 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5667 | bb_putchar('\\'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5668 | break; |
| 5669 | } |
| 5670 | |
| 5671 | case 'f': |
| 5672 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5673 | bb_putchar('\f'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5674 | break; |
| 5675 | } |
| 5676 | |
| 5677 | case 'n': |
| 5678 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5679 | bb_putchar('\n'); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 5680 | G.prog.nchars = SIZE_MAX; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5681 | break; |
| 5682 | } |
| 5683 | |
| 5684 | case 'r': |
| 5685 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5686 | bb_putchar('\r'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5687 | break; |
| 5688 | } |
| 5689 | |
| 5690 | case 'q': |
| 5691 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5692 | bb_putchar('"'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5693 | break; |
| 5694 | } |
| 5695 | |
| 5696 | case 't': |
| 5697 | { |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5698 | bb_putchar('\t'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5699 | break; |
| 5700 | } |
| 5701 | |
| 5702 | default: |
| 5703 | { |
| 5704 | // Just print the backslash and following character. |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5705 | bb_putchar('\\'); |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 5706 | ++G.prog.nchars; |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5707 | bb_putchar(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5708 | break; |
| 5709 | } |
| 5710 | } |
| 5711 | } |
| 5712 | } |
| 5713 | } |
| 5714 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5715 | static BcStatus bc_program_print(char inst, size_t idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5716 | { |
| 5717 | BcStatus s = BC_STATUS_SUCCESS; |
| 5718 | BcResult *r; |
| 5719 | size_t len, i; |
| 5720 | char *str; |
| 5721 | BcNum *num = NULL; |
| 5722 | bool pop = inst != BC_INST_PRINT; |
| 5723 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5724 | if (!BC_PROG_STACK(&G.prog.results, idx + 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5725 | return bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5726 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5727 | r = bc_vec_item_rev(&G.prog.results, idx); |
| 5728 | s = bc_program_num(r, &num, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5729 | if (s) return s; |
| 5730 | |
| 5731 | if (BC_PROG_NUM(r, num)) { |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 5732 | s = bc_num_print(num, !pop); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5733 | if (!s) bc_num_copy(&G.prog.last, num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5734 | } |
| 5735 | else { |
| 5736 | |
| 5737 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx; |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 5738 | str = *bc_program_str(idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5739 | |
| 5740 | if (inst == BC_INST_PRINT_STR) { |
| 5741 | for (i = 0, len = strlen(str); i < len; ++i) { |
| 5742 | char c = str[i]; |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5743 | bb_putchar(c); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5744 | if (c == '\n') G.prog.nchars = SIZE_MAX; |
| 5745 | ++G.prog.nchars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5746 | } |
| 5747 | } |
| 5748 | else { |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 5749 | bc_program_printString(str); |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5750 | if (inst == BC_INST_PRINT) bb_putchar('\n'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5751 | } |
| 5752 | } |
| 5753 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5754 | if (!s && pop) bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5755 | |
| 5756 | return s; |
| 5757 | } |
| 5758 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5759 | static BcStatus bc_program_negate(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5760 | { |
| 5761 | BcStatus s; |
| 5762 | BcResult res, *ptr; |
| 5763 | BcNum *num = NULL; |
| 5764 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5765 | s = bc_program_prep(&ptr, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5766 | if (s) return s; |
| 5767 | |
| 5768 | bc_num_init(&res.d.n, num->len); |
| 5769 | bc_num_copy(&res.d.n, num); |
| 5770 | if (res.d.n.len) res.d.n.neg = !res.d.n.neg; |
| 5771 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5772 | bc_program_retire(&res, BC_RESULT_TEMP); |
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_logical(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5778 | { |
| 5779 | BcStatus s; |
| 5780 | BcResult *opd1, *opd2, res; |
| 5781 | BcNum *n1, *n2; |
| 5782 | bool cond = 0; |
| 5783 | ssize_t cmp; |
| 5784 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5785 | s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5786 | if (s) return s; |
| 5787 | bc_num_init(&res.d.n, BC_NUM_DEF_SIZE); |
| 5788 | |
| 5789 | if (inst == BC_INST_BOOL_AND) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5790 | 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] | 5791 | else if (inst == BC_INST_BOOL_OR) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5792 | 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] | 5793 | else { |
| 5794 | |
| 5795 | cmp = bc_num_cmp(n1, n2); |
| 5796 | |
| 5797 | switch (inst) { |
| 5798 | |
| 5799 | case BC_INST_REL_EQ: |
| 5800 | { |
| 5801 | cond = cmp == 0; |
| 5802 | break; |
| 5803 | } |
| 5804 | |
| 5805 | case BC_INST_REL_LE: |
| 5806 | { |
| 5807 | cond = cmp <= 0; |
| 5808 | break; |
| 5809 | } |
| 5810 | |
| 5811 | case BC_INST_REL_GE: |
| 5812 | { |
| 5813 | cond = cmp >= 0; |
| 5814 | break; |
| 5815 | } |
| 5816 | |
| 5817 | case BC_INST_REL_NE: |
| 5818 | { |
| 5819 | cond = cmp != 0; |
| 5820 | break; |
| 5821 | } |
| 5822 | |
| 5823 | case BC_INST_REL_LT: |
| 5824 | { |
| 5825 | cond = cmp < 0; |
| 5826 | break; |
| 5827 | } |
| 5828 | |
| 5829 | case BC_INST_REL_GT: |
| 5830 | { |
| 5831 | cond = cmp > 0; |
| 5832 | break; |
| 5833 | } |
| 5834 | } |
| 5835 | } |
| 5836 | |
| 5837 | (cond ? bc_num_one : bc_num_zero)(&res.d.n); |
| 5838 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5839 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5840 | |
| 5841 | return s; |
| 5842 | } |
| 5843 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5844 | #if ENABLE_DC |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5845 | static BcStatus bc_program_assignStr(BcResult *r, BcVec *v, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5846 | bool push) |
| 5847 | { |
| 5848 | BcNum n2; |
| 5849 | BcResult res; |
| 5850 | |
| 5851 | memset(&n2, 0, sizeof(BcNum)); |
| 5852 | n2.rdx = res.d.id.idx = r->d.id.idx; |
| 5853 | res.t = BC_RESULT_STR; |
| 5854 | |
| 5855 | if (!push) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5856 | if (!BC_PROG_STACK(&G.prog.results, 2)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5857 | return bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5858 | bc_vec_pop(v); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5859 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5860 | } |
| 5861 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5862 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5863 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5864 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5865 | bc_vec_push(v, &n2); |
| 5866 | |
| 5867 | return BC_STATUS_SUCCESS; |
| 5868 | } |
| 5869 | #endif // ENABLE_DC |
| 5870 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5871 | static BcStatus bc_program_copyToVar(char *name, bool var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5872 | { |
| 5873 | BcStatus s; |
| 5874 | BcResult *ptr, r; |
| 5875 | BcVec *v; |
| 5876 | BcNum *n; |
| 5877 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5878 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5879 | return bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5880 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5881 | ptr = bc_vec_top(&G.prog.results); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5882 | if ((ptr->t == BC_RESULT_ARRAY) != !var) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5883 | return bc_error_variable_is_wrong_type(); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5884 | v = bc_program_search(name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5885 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5886 | #if ENABLE_DC |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5887 | if (ptr->t == BC_RESULT_STR && !var) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5888 | return bc_error_variable_is_wrong_type(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5889 | 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] | 5890 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5891 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5892 | s = bc_program_num(ptr, &n, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5893 | if (s) return s; |
| 5894 | |
| 5895 | // Do this once more to make sure that pointers were not invalidated. |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5896 | v = bc_program_search(name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5897 | |
| 5898 | if (var) { |
| 5899 | bc_num_init(&r.d.n, BC_NUM_DEF_SIZE); |
| 5900 | bc_num_copy(&r.d.n, n); |
| 5901 | } |
| 5902 | else { |
| 5903 | bc_array_init(&r.d.v, true); |
| 5904 | bc_array_copy(&r.d.v, (BcVec *) n); |
| 5905 | } |
| 5906 | |
| 5907 | bc_vec_push(v, &r.d); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5908 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5909 | |
| 5910 | return s; |
| 5911 | } |
| 5912 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5913 | static BcStatus bc_program_assign(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5914 | { |
| 5915 | BcStatus s; |
| 5916 | BcResult *left, *right, res; |
| 5917 | BcNum *l = NULL, *r = NULL; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5918 | bool assign = inst == BC_INST_ASSIGN, ib, sc; |
| 5919 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5920 | s = bc_program_binOpPrep(&left, &l, &right, &r, assign); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5921 | if (s) return s; |
| 5922 | |
| 5923 | ib = left->t == BC_RESULT_IBASE; |
| 5924 | sc = left->t == BC_RESULT_SCALE; |
| 5925 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5926 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5927 | |
| 5928 | if (right->t == BC_RESULT_STR) { |
| 5929 | |
| 5930 | BcVec *v; |
| 5931 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5932 | if (left->t != BC_RESULT_VAR) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 5933 | return bc_error_variable_is_wrong_type(); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5934 | v = bc_program_search(left->d.id.name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5935 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5936 | return bc_program_assignStr(right, v, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5937 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5938 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5939 | |
| 5940 | if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP) |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5941 | return bc_error("bad assignment:" |
| 5942 | " left side must be scale," |
| 5943 | " ibase, obase, last, var," |
| 5944 | " or array element" |
| 5945 | ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5946 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5947 | #if ENABLE_BC |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5948 | 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] | 5949 | return bc_error("divide by zero"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5950 | |
| 5951 | if (assign) |
| 5952 | bc_num_copy(l, r); |
| 5953 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5954 | 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] | 5955 | |
| 5956 | if (s) return s; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5957 | #else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5958 | bc_num_copy(l, r); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5959 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5960 | |
| 5961 | if (ib || sc || left->t == BC_RESULT_OBASE) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5962 | static const char *const msg[] = { |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 5963 | "bad ibase; must be [2,16]", //BC_RESULT_IBASE |
| 5964 | "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE |
| 5965 | NULL, //can't happen //BC_RESULT_LAST |
| 5966 | NULL, //can't happen //BC_RESULT_CONSTANT |
| 5967 | NULL, //can't happen //BC_RESULT_ONE |
| 5968 | "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5969 | }; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5970 | size_t *ptr; |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 5971 | unsigned long val, max; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5972 | |
| 5973 | s = bc_num_ulong(l, &val); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5974 | if (s) |
| 5975 | return s; |
| 5976 | s = left->t - BC_RESULT_IBASE; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5977 | if (sc) { |
| 5978 | max = BC_MAX_SCALE; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5979 | ptr = &G.prog.scale; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5980 | } |
| 5981 | else { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5982 | if (val < BC_NUM_MIN_BASE) |
| 5983 | return bc_error(msg[s]); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5984 | max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5985 | ptr = ib ? &G.prog.ib_t : &G.prog.ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5986 | } |
| 5987 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5988 | if (val > max) |
| 5989 | return bc_error(msg[s]); |
| 5990 | if (!sc) |
| 5991 | bc_num_copy(ib ? &G.prog.ib : &G.prog.ob, l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5992 | |
| 5993 | *ptr = (size_t) val; |
| 5994 | s = BC_STATUS_SUCCESS; |
| 5995 | } |
| 5996 | |
| 5997 | bc_num_init(&res.d.n, l->len); |
| 5998 | bc_num_copy(&res.d.n, l); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5999 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6000 | |
| 6001 | return s; |
| 6002 | } |
| 6003 | |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6004 | #if !ENABLE_DC |
| 6005 | #define bc_program_pushVar(code, bgn, pop, copy) \ |
| 6006 | bc_program_pushVar(code, bgn) |
| 6007 | // for bc, 'pop' and 'copy' are always false |
| 6008 | #endif |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6009 | static BcStatus bc_program_pushVar(char *code, size_t *bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6010 | bool pop, bool copy) |
| 6011 | { |
| 6012 | BcStatus s = BC_STATUS_SUCCESS; |
| 6013 | BcResult r; |
| 6014 | char *name = bc_program_name(code, bgn); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6015 | |
| 6016 | r.t = BC_RESULT_VAR; |
| 6017 | r.d.id.name = name; |
| 6018 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6019 | #if ENABLE_DC |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6020 | { |
| 6021 | BcVec *v = bc_program_search(name, true); |
| 6022 | BcNum *num = bc_vec_top(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6023 | |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6024 | if (pop || copy) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6025 | |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6026 | if (!BC_PROG_STACK(v, 2 - copy)) { |
| 6027 | free(name); |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6028 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6029 | } |
| 6030 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6031 | free(name); |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 6032 | name = NULL; |
| 6033 | |
| 6034 | if (!BC_PROG_STR(num)) { |
| 6035 | |
| 6036 | r.t = BC_RESULT_TEMP; |
| 6037 | |
| 6038 | bc_num_init(&r.d.n, BC_NUM_DEF_SIZE); |
| 6039 | bc_num_copy(&r.d.n, num); |
| 6040 | } |
| 6041 | else { |
| 6042 | r.t = BC_RESULT_STR; |
| 6043 | r.d.id.idx = num->rdx; |
| 6044 | } |
| 6045 | |
| 6046 | if (!copy) bc_vec_pop(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6047 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6048 | } |
| 6049 | #endif // ENABLE_DC |
| 6050 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6051 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6052 | |
| 6053 | return s; |
| 6054 | } |
| 6055 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6056 | static BcStatus bc_program_pushArray(char *code, size_t *bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6057 | char inst) |
| 6058 | { |
| 6059 | BcStatus s = BC_STATUS_SUCCESS; |
| 6060 | BcResult r; |
| 6061 | BcNum *num; |
| 6062 | |
| 6063 | r.d.id.name = bc_program_name(code, bgn); |
| 6064 | |
| 6065 | if (inst == BC_INST_ARRAY) { |
| 6066 | r.t = BC_RESULT_ARRAY; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6067 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6068 | } |
| 6069 | else { |
| 6070 | |
| 6071 | BcResult *operand; |
| 6072 | unsigned long temp; |
| 6073 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6074 | s = bc_program_prep(&operand, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6075 | if (s) goto err; |
| 6076 | s = bc_num_ulong(num, &temp); |
| 6077 | if (s) goto err; |
| 6078 | |
| 6079 | if (temp > BC_MAX_DIM) { |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 6080 | s = bc_error("array too long; must be [1,"BC_MAX_DIM_STR"]"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6081 | goto err; |
| 6082 | } |
| 6083 | |
| 6084 | r.d.id.idx = (size_t) temp; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6085 | bc_program_retire(&r, BC_RESULT_ARRAY_ELEM); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6086 | } |
| 6087 | |
| 6088 | err: |
| 6089 | if (s) free(r.d.id.name); |
| 6090 | return s; |
| 6091 | } |
| 6092 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6093 | #if ENABLE_BC |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6094 | static BcStatus bc_program_incdec(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6095 | { |
| 6096 | BcStatus s; |
| 6097 | BcResult *ptr, res, copy; |
| 6098 | BcNum *num = NULL; |
| 6099 | char inst2 = inst; |
| 6100 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6101 | s = bc_program_prep(&ptr, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6102 | if (s) return s; |
| 6103 | |
| 6104 | if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) { |
| 6105 | copy.t = BC_RESULT_TEMP; |
| 6106 | bc_num_init(©.d.n, num->len); |
| 6107 | bc_num_copy(©.d.n, num); |
| 6108 | } |
| 6109 | |
| 6110 | res.t = BC_RESULT_ONE; |
| 6111 | inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ? |
| 6112 | BC_INST_ASSIGN_PLUS : |
| 6113 | BC_INST_ASSIGN_MINUS; |
| 6114 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6115 | bc_vec_push(&G.prog.results, &res); |
| 6116 | bc_program_assign(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6117 | |
| 6118 | if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6119 | bc_vec_pop(&G.prog.results); |
| 6120 | bc_vec_push(&G.prog.results, ©); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6121 | } |
| 6122 | |
| 6123 | return s; |
| 6124 | } |
| 6125 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6126 | static BcStatus bc_program_call(char *code, size_t *idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6127 | { |
| 6128 | BcStatus s = BC_STATUS_SUCCESS; |
| 6129 | BcInstPtr ip; |
| 6130 | size_t i, nparams = bc_program_index(code, idx); |
| 6131 | BcFunc *func; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6132 | BcId *a; |
| 6133 | BcResultData param; |
| 6134 | BcResult *arg; |
| 6135 | |
| 6136 | ip.idx = 0; |
| 6137 | ip.func = bc_program_index(code, idx); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6138 | func = bc_program_func(ip.func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6139 | |
Denys Vlasenko | 04a1c76 | 2018-12-03 21:10:57 +0100 | [diff] [blame] | 6140 | if (func->code.len == 0) { |
| 6141 | return bc_error("undefined function"); |
| 6142 | } |
| 6143 | if (nparams != func->nparams) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6144 | 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] | 6145 | } |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6146 | ip.len = G.prog.results.len - nparams; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6147 | |
| 6148 | for (i = 0; i < nparams; ++i) { |
| 6149 | |
| 6150 | a = bc_vec_item(&func->autos, nparams - 1 - i); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6151 | arg = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6152 | |
| 6153 | 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] | 6154 | return bc_error_variable_is_wrong_type(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6155 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6156 | s = bc_program_copyToVar(a->name, a->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6157 | if (s) return s; |
| 6158 | } |
| 6159 | |
| 6160 | for (; i < func->autos.len; ++i) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6161 | BcVec *v; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6162 | |
| 6163 | a = bc_vec_item(&func->autos, i); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6164 | v = bc_program_search(a->name, a->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6165 | |
| 6166 | if (a->idx) { |
| 6167 | bc_num_init(¶m.n, BC_NUM_DEF_SIZE); |
| 6168 | bc_vec_push(v, ¶m.n); |
| 6169 | } |
| 6170 | else { |
| 6171 | bc_array_init(¶m.v, true); |
| 6172 | bc_vec_push(v, ¶m.v); |
| 6173 | } |
| 6174 | } |
| 6175 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6176 | bc_vec_push(&G.prog.stack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6177 | |
| 6178 | return BC_STATUS_SUCCESS; |
| 6179 | } |
| 6180 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6181 | static BcStatus bc_program_return(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6182 | { |
| 6183 | BcStatus s; |
| 6184 | BcResult res; |
| 6185 | BcFunc *f; |
| 6186 | size_t i; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6187 | BcInstPtr *ip = bc_vec_top(&G.prog.stack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6188 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6189 | 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] | 6190 | return bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6191 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6192 | f = bc_program_func(ip->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6193 | res.t = BC_RESULT_TEMP; |
| 6194 | |
| 6195 | if (inst == BC_INST_RET) { |
| 6196 | |
| 6197 | BcNum *num; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6198 | BcResult *operand = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6199 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6200 | s = bc_program_num(operand, &num, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6201 | if (s) return s; |
| 6202 | bc_num_init(&res.d.n, num->len); |
| 6203 | bc_num_copy(&res.d.n, num); |
| 6204 | } |
| 6205 | else { |
| 6206 | bc_num_init(&res.d.n, BC_NUM_DEF_SIZE); |
| 6207 | bc_num_zero(&res.d.n); |
| 6208 | } |
| 6209 | |
| 6210 | // We need to pop arguments as well, so this takes that into account. |
| 6211 | for (i = 0; i < f->autos.len; ++i) { |
| 6212 | |
| 6213 | BcVec *v; |
| 6214 | BcId *a = bc_vec_item(&f->autos, i); |
| 6215 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6216 | v = bc_program_search(a->name, a->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6217 | bc_vec_pop(v); |
| 6218 | } |
| 6219 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6220 | bc_vec_npop(&G.prog.results, G.prog.results.len - ip->len); |
| 6221 | bc_vec_push(&G.prog.results, &res); |
| 6222 | bc_vec_pop(&G.prog.stack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6223 | |
| 6224 | return BC_STATUS_SUCCESS; |
| 6225 | } |
| 6226 | #endif // ENABLE_BC |
| 6227 | |
| 6228 | static unsigned long bc_program_scale(BcNum *n) |
| 6229 | { |
| 6230 | return (unsigned long) n->rdx; |
| 6231 | } |
| 6232 | |
| 6233 | static unsigned long bc_program_len(BcNum *n) |
| 6234 | { |
| 6235 | unsigned long len = n->len; |
| 6236 | size_t i; |
| 6237 | |
| 6238 | if (n->rdx != n->len) return len; |
| 6239 | for (i = n->len - 1; i < n->len && n->num[i] == 0; --len, --i); |
| 6240 | |
| 6241 | return len; |
| 6242 | } |
| 6243 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6244 | static BcStatus bc_program_builtin(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6245 | { |
| 6246 | BcStatus s; |
| 6247 | BcResult *opnd; |
| 6248 | BcNum *num = NULL; |
| 6249 | BcResult res; |
| 6250 | bool len = inst == BC_INST_LENGTH; |
| 6251 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6252 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6253 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6254 | opnd = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6255 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6256 | s = bc_program_num(opnd, &num, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6257 | if (s) return s; |
| 6258 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6259 | #if ENABLE_DC |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6260 | if (!BC_PROG_NUM(opnd, num) && !len) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6261 | return bc_error_variable_is_wrong_type(); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6262 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6263 | |
| 6264 | bc_num_init(&res.d.n, BC_NUM_DEF_SIZE); |
| 6265 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6266 | 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] | 6267 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6268 | else if (len != 0 && opnd->t == BC_RESULT_ARRAY) { |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6269 | bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6270 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6271 | #endif |
| 6272 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6273 | else if (len != 0 && !BC_PROG_NUM(opnd, num)) { |
| 6274 | |
| 6275 | char **str; |
| 6276 | size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx; |
| 6277 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6278 | str = bc_program_str(idx); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6279 | bc_num_ulong2num(&res.d.n, strlen(*str)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6280 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6281 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6282 | else { |
| 6283 | BcProgramBuiltIn f = len ? bc_program_len : bc_program_scale; |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6284 | bc_num_ulong2num(&res.d.n, f(num)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6285 | } |
| 6286 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6287 | bc_program_retire(&res, BC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6288 | |
| 6289 | return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6290 | } |
| 6291 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6292 | #if ENABLE_DC |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6293 | static BcStatus bc_program_divmod(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6294 | { |
| 6295 | BcStatus s; |
| 6296 | BcResult *opd1, *opd2, res, res2; |
| 6297 | BcNum *n1, *n2 = NULL; |
| 6298 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6299 | s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6300 | if (s) return s; |
| 6301 | |
| 6302 | bc_num_init(&res.d.n, BC_NUM_DEF_SIZE); |
| 6303 | bc_num_init(&res2.d.n, n2->len); |
| 6304 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6305 | 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] | 6306 | if (s) goto err; |
| 6307 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6308 | bc_program_binOpRetire(&res2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6309 | res.t = BC_RESULT_TEMP; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6310 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6311 | |
| 6312 | return s; |
| 6313 | |
| 6314 | err: |
| 6315 | bc_num_free(&res2.d.n); |
| 6316 | bc_num_free(&res.d.n); |
| 6317 | return s; |
| 6318 | } |
| 6319 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6320 | static BcStatus bc_program_modexp(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6321 | { |
| 6322 | BcStatus s; |
| 6323 | BcResult *r1, *r2, *r3, res; |
| 6324 | BcNum *n1, *n2, *n3; |
| 6325 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6326 | if (!BC_PROG_STACK(&G.prog.results, 3)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6327 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6328 | s = bc_program_binOpPrep(&r2, &n2, &r3, &n3, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6329 | if (s) return s; |
| 6330 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6331 | r1 = bc_vec_item_rev(&G.prog.results, 2); |
| 6332 | s = bc_program_num(r1, &n1, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6333 | if (s) return s; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6334 | if (!BC_PROG_NUM(r1, n1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6335 | return bc_error_variable_is_wrong_type(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6336 | |
| 6337 | // Make sure that the values have their pointers updated, if necessary. |
| 6338 | if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) { |
| 6339 | |
| 6340 | if (r1->t == r2->t) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6341 | s = bc_program_num(r2, &n2, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6342 | if (s) return s; |
| 6343 | } |
| 6344 | |
| 6345 | if (r1->t == r3->t) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6346 | s = bc_program_num(r3, &n3, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6347 | if (s) return s; |
| 6348 | } |
| 6349 | } |
| 6350 | |
| 6351 | bc_num_init(&res.d.n, n3->len); |
| 6352 | s = bc_num_modexp(n1, n2, n3, &res.d.n); |
| 6353 | if (s) goto err; |
| 6354 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6355 | bc_vec_pop(&G.prog.results); |
| 6356 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6357 | |
| 6358 | return s; |
| 6359 | |
| 6360 | err: |
| 6361 | bc_num_free(&res.d.n); |
| 6362 | return s; |
| 6363 | } |
| 6364 | |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6365 | static void bc_program_stackLen(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6366 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6367 | BcResult res; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6368 | size_t len = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6369 | |
| 6370 | res.t = BC_RESULT_TEMP; |
| 6371 | |
| 6372 | bc_num_init(&res.d.n, BC_NUM_DEF_SIZE); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6373 | bc_num_ulong2num(&res.d.n, len); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6374 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6375 | } |
| 6376 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6377 | static BcStatus bc_program_asciify(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6378 | { |
| 6379 | BcStatus s; |
| 6380 | BcResult *r, res; |
| 6381 | BcNum *num = NULL, n; |
| 6382 | char *str, *str2, c; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6383 | size_t len = G.prog.strs.len, idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6384 | unsigned long val; |
| 6385 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6386 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6387 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6388 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6389 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6390 | s = bc_program_num(r, &num, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6391 | if (s) return s; |
| 6392 | |
| 6393 | if (BC_PROG_NUM(r, num)) { |
| 6394 | |
| 6395 | bc_num_init(&n, BC_NUM_DEF_SIZE); |
| 6396 | bc_num_copy(&n, num); |
| 6397 | bc_num_truncate(&n, n.rdx); |
| 6398 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6399 | s = bc_num_mod(&n, &G.prog.strmb, &n, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6400 | if (s) goto num_err; |
| 6401 | s = bc_num_ulong(&n, &val); |
| 6402 | if (s) goto num_err; |
| 6403 | |
| 6404 | c = (char) val; |
| 6405 | |
| 6406 | bc_num_free(&n); |
| 6407 | } |
| 6408 | else { |
| 6409 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx; |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6410 | str2 = *bc_program_str(idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6411 | c = str2[0]; |
| 6412 | } |
| 6413 | |
| 6414 | str = xmalloc(2); |
| 6415 | str[0] = c; |
| 6416 | str[1] = '\0'; |
| 6417 | |
| 6418 | str2 = xstrdup(str); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6419 | bc_program_addFunc(str2, &idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6420 | |
| 6421 | if (idx != len + BC_PROG_REQ_FUNCS) { |
| 6422 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6423 | for (idx = 0; idx < G.prog.strs.len; ++idx) { |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6424 | if (strcmp(*bc_program_str(idx), str) == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6425 | len = idx; |
| 6426 | break; |
| 6427 | } |
| 6428 | } |
| 6429 | |
| 6430 | free(str); |
| 6431 | } |
| 6432 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6433 | bc_vec_push(&G.prog.strs, &str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6434 | |
| 6435 | res.t = BC_RESULT_STR; |
| 6436 | res.d.id.idx = len; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6437 | bc_vec_pop(&G.prog.results); |
| 6438 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6439 | |
| 6440 | return BC_STATUS_SUCCESS; |
| 6441 | |
| 6442 | num_err: |
| 6443 | bc_num_free(&n); |
| 6444 | return s; |
| 6445 | } |
| 6446 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6447 | static BcStatus bc_program_printStream(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6448 | { |
| 6449 | BcStatus s; |
| 6450 | BcResult *r; |
| 6451 | BcNum *n = NULL; |
| 6452 | size_t idx; |
| 6453 | char *str; |
| 6454 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6455 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6456 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6457 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6458 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6459 | s = bc_program_num(r, &n, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6460 | if (s) return s; |
| 6461 | |
| 6462 | if (BC_PROG_NUM(r, n)) |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 6463 | s = bc_num_stream(n, &G.prog.strmb); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6464 | else { |
| 6465 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx; |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6466 | str = *bc_program_str(idx); |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 6467 | printf("%s", str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6468 | } |
| 6469 | |
| 6470 | return s; |
| 6471 | } |
| 6472 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6473 | static BcStatus bc_program_nquit(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6474 | { |
| 6475 | BcStatus s; |
| 6476 | BcResult *opnd; |
| 6477 | BcNum *num = NULL; |
| 6478 | unsigned long val; |
| 6479 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6480 | s = bc_program_prep(&opnd, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6481 | if (s) return s; |
| 6482 | s = bc_num_ulong(num, &val); |
| 6483 | if (s) return s; |
| 6484 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6485 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6486 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6487 | if (G.prog.stack.len < val) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6488 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 6489 | if (G.prog.stack.len == val) { |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 6490 | QUIT_OR_RETURN_TO_MAIN; |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 6491 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6492 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6493 | bc_vec_npop(&G.prog.stack, val); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6494 | |
| 6495 | return s; |
| 6496 | } |
| 6497 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6498 | static BcStatus bc_program_execStr(char *code, size_t *bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6499 | bool cond) |
| 6500 | { |
| 6501 | BcStatus s = BC_STATUS_SUCCESS; |
| 6502 | BcResult *r; |
| 6503 | char **str; |
| 6504 | BcFunc *f; |
| 6505 | BcParse prs; |
| 6506 | BcInstPtr ip; |
| 6507 | size_t fidx, sidx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6508 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6509 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6510 | return bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6511 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6512 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6513 | |
| 6514 | if (cond) { |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame^] | 6515 | BcNum *n = n; // for compiler |
| 6516 | bool exec; |
| 6517 | char *name; |
| 6518 | char *then_name = bc_program_name(code, bgn); |
| 6519 | char *else_name = NULL; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6520 | |
| 6521 | if (code[*bgn] == BC_PARSE_STREND) |
| 6522 | (*bgn) += 1; |
| 6523 | else |
| 6524 | else_name = bc_program_name(code, bgn); |
| 6525 | |
| 6526 | exec = r->d.n.len != 0; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame^] | 6527 | name = then_name; |
| 6528 | if (!exec && else_name != NULL) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6529 | exec = true; |
| 6530 | name = else_name; |
| 6531 | } |
| 6532 | |
| 6533 | if (exec) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6534 | BcVec *v; |
| 6535 | v = bc_program_search(name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6536 | n = bc_vec_top(v); |
| 6537 | } |
| 6538 | |
| 6539 | free(then_name); |
| 6540 | free(else_name); |
| 6541 | |
| 6542 | if (!exec) goto exit; |
| 6543 | if (!BC_PROG_STR(n)) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6544 | s = bc_error_variable_is_wrong_type(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6545 | goto exit; |
| 6546 | } |
| 6547 | |
| 6548 | sidx = n->rdx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame^] | 6549 | } else { |
| 6550 | if (r->t == BC_RESULT_STR) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6551 | sidx = r->d.id.idx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame^] | 6552 | } else if (r->t == BC_RESULT_VAR) { |
| 6553 | BcNum *n; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6554 | s = bc_program_num(r, &n, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6555 | if (s || !BC_PROG_STR(n)) goto exit; |
| 6556 | sidx = n->rdx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame^] | 6557 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6558 | goto exit; |
| 6559 | } |
| 6560 | |
| 6561 | fidx = sidx + BC_PROG_REQ_FUNCS; |
| 6562 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6563 | str = bc_program_str(sidx); |
| 6564 | f = bc_program_func(fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6565 | |
| 6566 | if (f->code.len == 0) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6567 | common_parse_init(&prs, fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6568 | s = bc_parse_text(&prs, *str); |
| 6569 | if (s) goto err; |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 6570 | s = common_parse_expr(&prs, BC_PARSE_NOCALL); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6571 | if (s) goto err; |
| 6572 | |
| 6573 | if (prs.l.t.t != BC_LEX_EOF) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6574 | s = bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6575 | goto err; |
| 6576 | } |
| 6577 | |
| 6578 | bc_parse_free(&prs); |
| 6579 | } |
| 6580 | |
| 6581 | ip.idx = 0; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6582 | ip.len = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6583 | ip.func = fidx; |
| 6584 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6585 | bc_vec_pop(&G.prog.results); |
| 6586 | bc_vec_push(&G.prog.stack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6587 | |
| 6588 | return BC_STATUS_SUCCESS; |
| 6589 | |
| 6590 | err: |
| 6591 | bc_parse_free(&prs); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6592 | f = bc_program_func(fidx); |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 6593 | bc_vec_pop_all(&f->code); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6594 | exit: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6595 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6596 | return s; |
| 6597 | } |
| 6598 | #endif // ENABLE_DC |
| 6599 | |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6600 | static void bc_program_pushGlobal(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6601 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6602 | BcResult res; |
| 6603 | unsigned long val; |
| 6604 | |
| 6605 | res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE; |
| 6606 | if (inst == BC_INST_IBASE) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6607 | val = (unsigned long) G.prog.ib_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6608 | else if (inst == BC_INST_SCALE) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6609 | val = (unsigned long) G.prog.scale; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6610 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6611 | val = (unsigned long) G.prog.ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6612 | |
| 6613 | bc_num_init(&res.d.n, BC_NUM_DEF_SIZE); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6614 | bc_num_ulong2num(&res.d.n, val); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6615 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6616 | } |
| 6617 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6618 | static void bc_program_addFunc(char *name, size_t *idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6619 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6620 | BcId entry, *entry_ptr; |
| 6621 | BcFunc f; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 6622 | int inserted; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6623 | |
| 6624 | entry.name = name; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6625 | entry.idx = G.prog.fns.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6626 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 6627 | inserted = bc_map_insert(&G.prog.fn_map, &entry, idx); |
| 6628 | if (!inserted) free(name); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6629 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6630 | entry_ptr = bc_vec_item(&G.prog.fn_map, *idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6631 | *idx = entry_ptr->idx; |
| 6632 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 6633 | if (!inserted) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6634 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6635 | BcFunc *func = bc_program_func(entry_ptr->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6636 | |
| 6637 | // We need to reset these, so the function can be repopulated. |
| 6638 | func->nparams = 0; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 6639 | bc_vec_pop_all(&func->autos); |
| 6640 | bc_vec_pop_all(&func->code); |
| 6641 | bc_vec_pop_all(&func->labels); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6642 | } |
| 6643 | else { |
| 6644 | bc_func_init(&f); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6645 | bc_vec_push(&G.prog.fns, &f); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6646 | } |
| 6647 | } |
| 6648 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6649 | static BcStatus bc_program_exec(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6650 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6651 | BcResult r, *ptr; |
| 6652 | BcNum *num; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6653 | BcInstPtr *ip = bc_vec_top(&G.prog.stack); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6654 | BcFunc *func = bc_program_func(ip->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6655 | char *code = func->code.v; |
| 6656 | bool cond = false; |
| 6657 | |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6658 | while (ip->idx < func->code.len) { |
| 6659 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6660 | char inst = code[(ip->idx)++]; |
| 6661 | |
| 6662 | switch (inst) { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6663 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6664 | case BC_INST_JUMP_ZERO: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6665 | s = bc_program_prep(&ptr, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6666 | if (s) return s; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6667 | cond = !bc_num_cmp(num, &G.prog.zero); |
| 6668 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6669 | // Fallthrough. |
| 6670 | case BC_INST_JUMP: { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6671 | size_t *addr; |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6672 | size_t idx = bc_program_index(code, &ip->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6673 | addr = bc_vec_item(&func->labels, idx); |
| 6674 | if (inst == BC_INST_JUMP || cond) ip->idx = *addr; |
| 6675 | break; |
| 6676 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6677 | case BC_INST_CALL: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6678 | s = bc_program_call(code, &ip->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6679 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6680 | case BC_INST_INC_PRE: |
| 6681 | case BC_INST_DEC_PRE: |
| 6682 | case BC_INST_INC_POST: |
| 6683 | case BC_INST_DEC_POST: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6684 | s = bc_program_incdec(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6685 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6686 | case BC_INST_HALT: |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 6687 | QUIT_OR_RETURN_TO_MAIN; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6688 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6689 | case BC_INST_RET: |
| 6690 | case BC_INST_RET0: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6691 | s = bc_program_return(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6692 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6693 | case BC_INST_BOOL_OR: |
| 6694 | case BC_INST_BOOL_AND: |
| 6695 | #endif // ENABLE_BC |
| 6696 | case BC_INST_REL_EQ: |
| 6697 | case BC_INST_REL_LE: |
| 6698 | case BC_INST_REL_GE: |
| 6699 | case BC_INST_REL_NE: |
| 6700 | case BC_INST_REL_LT: |
| 6701 | case BC_INST_REL_GT: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6702 | s = bc_program_logical(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6703 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6704 | case BC_INST_READ: |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6705 | s = bc_program_read(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6706 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6707 | case BC_INST_VAR: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6708 | s = bc_program_pushVar(code, &ip->idx, false, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6709 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6710 | case BC_INST_ARRAY_ELEM: |
| 6711 | case BC_INST_ARRAY: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6712 | s = bc_program_pushArray(code, &ip->idx, inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6713 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6714 | case BC_INST_LAST: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6715 | r.t = BC_RESULT_LAST; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6716 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6717 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6718 | case BC_INST_IBASE: |
| 6719 | case BC_INST_SCALE: |
| 6720 | case BC_INST_OBASE: |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6721 | bc_program_pushGlobal(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6722 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6723 | case BC_INST_SCALE_FUNC: |
| 6724 | case BC_INST_LENGTH: |
| 6725 | case BC_INST_SQRT: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6726 | s = bc_program_builtin(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6727 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6728 | case BC_INST_NUM: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6729 | r.t = BC_RESULT_CONSTANT; |
| 6730 | r.d.id.idx = bc_program_index(code, &ip->idx); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6731 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6732 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6733 | case BC_INST_POP: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6734 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6735 | s = bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6736 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6737 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6738 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6739 | case BC_INST_POP_EXEC: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6740 | bc_vec_pop(&G.prog.stack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6741 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6742 | case BC_INST_PRINT: |
| 6743 | case BC_INST_PRINT_POP: |
| 6744 | case BC_INST_PRINT_STR: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6745 | s = bc_program_print(inst, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6746 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6747 | case BC_INST_STR: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6748 | r.t = BC_RESULT_STR; |
| 6749 | r.d.id.idx = bc_program_index(code, &ip->idx); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6750 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6751 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6752 | case BC_INST_POWER: |
| 6753 | case BC_INST_MULTIPLY: |
| 6754 | case BC_INST_DIVIDE: |
| 6755 | case BC_INST_MODULUS: |
| 6756 | case BC_INST_PLUS: |
| 6757 | case BC_INST_MINUS: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6758 | s = bc_program_op(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6759 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6760 | case BC_INST_BOOL_NOT: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6761 | s = bc_program_prep(&ptr, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6762 | if (s) return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6763 | bc_num_init(&r.d.n, BC_NUM_DEF_SIZE); |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6764 | if (!bc_num_cmp(num, &G.prog.zero)) |
| 6765 | bc_num_one(&r.d.n); |
| 6766 | else |
| 6767 | bc_num_zero(&r.d.n); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6768 | bc_program_retire(&r, BC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6769 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6770 | case BC_INST_NEG: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6771 | s = bc_program_negate(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6772 | break; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6773 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6774 | case BC_INST_ASSIGN_POWER: |
| 6775 | case BC_INST_ASSIGN_MULTIPLY: |
| 6776 | case BC_INST_ASSIGN_DIVIDE: |
| 6777 | case BC_INST_ASSIGN_MODULUS: |
| 6778 | case BC_INST_ASSIGN_PLUS: |
| 6779 | case BC_INST_ASSIGN_MINUS: |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6780 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6781 | case BC_INST_ASSIGN: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6782 | s = bc_program_assign(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6783 | break; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6784 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6785 | case BC_INST_MODEXP: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6786 | s = bc_program_modexp(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6787 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6788 | case BC_INST_DIVMOD: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6789 | s = bc_program_divmod(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6790 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6791 | case BC_INST_EXECUTE: |
| 6792 | case BC_INST_EXEC_COND: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6793 | cond = inst == BC_INST_EXEC_COND; |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6794 | s = bc_program_execStr(code, &ip->idx, cond); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6795 | break; |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6796 | case BC_INST_PRINT_STACK: { |
| 6797 | size_t idx; |
| 6798 | for (idx = 0; idx < G.prog.results.len; ++idx) { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6799 | s = bc_program_print(BC_INST_PRINT, idx); |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6800 | if (s) break; |
| 6801 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6802 | break; |
| 6803 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6804 | case BC_INST_CLEAR_STACK: |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 6805 | bc_vec_pop_all(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6806 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6807 | case BC_INST_STACK_LEN: |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6808 | bc_program_stackLen(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6809 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6810 | case BC_INST_DUPLICATE: |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6811 | if (!BC_PROG_STACK(&G.prog.results, 1)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6812 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6813 | ptr = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6814 | bc_result_copy(&r, ptr); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6815 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6816 | break; |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6817 | case BC_INST_SWAP: { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6818 | BcResult *ptr2; |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6819 | if (!BC_PROG_STACK(&G.prog.results, 2)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6820 | return bc_error_stack_has_too_few_elements(); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6821 | ptr = bc_vec_item_rev(&G.prog.results, 0); |
| 6822 | ptr2 = bc_vec_item_rev(&G.prog.results, 1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6823 | memcpy(&r, ptr, sizeof(BcResult)); |
| 6824 | memcpy(ptr, ptr2, sizeof(BcResult)); |
| 6825 | memcpy(ptr2, &r, sizeof(BcResult)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6826 | break; |
| 6827 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6828 | case BC_INST_ASCIIFY: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6829 | s = bc_program_asciify(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6830 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6831 | case BC_INST_PRINT_STREAM: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6832 | s = bc_program_printStream(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6833 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6834 | case BC_INST_LOAD: |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6835 | case BC_INST_PUSH_VAR: { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6836 | bool copy = inst == BC_INST_LOAD; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6837 | s = bc_program_pushVar(code, &ip->idx, true, copy); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6838 | break; |
| 6839 | } |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6840 | case BC_INST_PUSH_TO_VAR: { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6841 | char *name = bc_program_name(code, &ip->idx); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6842 | s = bc_program_copyToVar(name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6843 | free(name); |
| 6844 | break; |
| 6845 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6846 | case BC_INST_QUIT: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6847 | if (G.prog.stack.len <= 2) |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 6848 | QUIT_OR_RETURN_TO_MAIN; |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 6849 | bc_vec_npop(&G.prog.stack, 2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6850 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6851 | case BC_INST_NQUIT: |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6852 | s = bc_program_nquit(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6853 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6854 | #endif // ENABLE_DC |
| 6855 | } |
| 6856 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6857 | if (s || G_interrupt) { |
| 6858 | bc_program_reset(); |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6859 | return s; |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6860 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6861 | |
| 6862 | // If the stack has changed, pointers may be invalid. |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6863 | ip = bc_vec_top(&G.prog.stack); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6864 | func = bc_program_func(ip->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6865 | code = func->code.v; |
| 6866 | } |
| 6867 | |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6868 | return BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6869 | } |
| 6870 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 6871 | #if ENABLE_BC |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 6872 | static void bc_vm_info(void) |
| 6873 | { |
| 6874 | printf("%s "BB_VER"\n" |
| 6875 | "Copyright (c) 2018 Gavin D. Howard and contributors\n" |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 6876 | , applet_name); |
| 6877 | } |
| 6878 | |
| 6879 | static void bc_args(char **argv) |
| 6880 | { |
| 6881 | unsigned opts; |
| 6882 | int i; |
| 6883 | |
| 6884 | GETOPT_RESET(); |
| 6885 | #if ENABLE_FEATURE_BC_LONG_OPTIONS |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 6886 | opts = option_mask32 |= getopt32long(argv, "wvsqli", |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 6887 | "warn\0" No_argument "w" |
| 6888 | "version\0" No_argument "v" |
| 6889 | "standard\0" No_argument "s" |
| 6890 | "quiet\0" No_argument "q" |
| 6891 | "mathlib\0" No_argument "l" |
| 6892 | "interactive\0" No_argument "i" |
| 6893 | ); |
| 6894 | #else |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 6895 | opts = option_mask32 |= getopt32(argv, "wvsqli"); |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 6896 | #endif |
| 6897 | if (getenv("POSIXLY_CORRECT")) |
| 6898 | option_mask32 |= BC_FLAG_S; |
| 6899 | |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 6900 | if (opts & BC_FLAG_V) { |
| 6901 | bc_vm_info(); |
| 6902 | exit(0); |
| 6903 | } |
| 6904 | |
| 6905 | for (i = optind; argv[i]; ++i) |
| 6906 | bc_vec_push(&G.files, argv + i); |
| 6907 | } |
| 6908 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6909 | static void bc_vm_envArgs(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6910 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6911 | BcVec v; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6912 | char *buf; |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 6913 | char *env_args = getenv("BC_ENV_ARGS"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6914 | |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 6915 | if (!env_args) return; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6916 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6917 | G.env_args = xstrdup(env_args); |
| 6918 | buf = G.env_args; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6919 | |
| 6920 | bc_vec_init(&v, sizeof(char *), NULL); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6921 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6922 | while (*(buf = skip_whitespace(buf)) != '\0') { |
| 6923 | bc_vec_push(&v, &buf); |
| 6924 | buf = skip_non_whitespace(buf); |
| 6925 | if (!*buf) |
| 6926 | break; |
| 6927 | *buf++ = '\0'; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6928 | } |
| 6929 | |
Denys Vlasenko | 54214c3 | 2018-12-06 09:07:06 +0100 | [diff] [blame] | 6930 | // NULL terminate, and pass argv[] so that first arg is argv[1] |
| 6931 | if (sizeof(int) == sizeof(char*)) { |
| 6932 | bc_vec_push(&v, &const_int_0); |
| 6933 | } else { |
| 6934 | static char *const nullptr = NULL; |
| 6935 | bc_vec_push(&v, &nullptr); |
| 6936 | } |
| 6937 | bc_args(((char **)v.v) - 1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6938 | |
| 6939 | bc_vec_free(&v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6940 | } |
| 6941 | #endif // ENABLE_BC |
| 6942 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6943 | static unsigned bc_vm_envLen(const char *var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6944 | { |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6945 | char *lenv; |
| 6946 | unsigned len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6947 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6948 | lenv = getenv(var); |
| 6949 | len = BC_NUM_PRINT_WIDTH; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6950 | if (!lenv) return len; |
| 6951 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6952 | len = bb_strtou(lenv, NULL, 10) - 1; |
| 6953 | if (errno || len < 2 || len >= INT_MAX) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6954 | len = BC_NUM_PRINT_WIDTH; |
| 6955 | |
| 6956 | return len; |
| 6957 | } |
| 6958 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6959 | static BcStatus bc_vm_process(const char *text) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6960 | { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6961 | BcStatus s = bc_parse_text(&G.prs, text); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6962 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6963 | if (s) return s; |
| 6964 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6965 | while (G.prs.l.t.t != BC_LEX_EOF) { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6966 | s = G.prs.parse(&G.prs); |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 6967 | if (s) return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6968 | } |
| 6969 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6970 | if (BC_PARSE_CAN_EXEC(&G.prs)) { |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 6971 | s = bc_program_exec(); |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 6972 | fflush_and_check(); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 6973 | if (s) |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6974 | bc_program_reset(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6975 | } |
| 6976 | |
| 6977 | return s; |
| 6978 | } |
| 6979 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6980 | static BcStatus bc_vm_file(const char *file) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6981 | { |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 6982 | const char *sv_file; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6983 | char *data; |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 6984 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6985 | BcFunc *main_func; |
| 6986 | BcInstPtr *ip; |
| 6987 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6988 | data = bc_read_file(file); |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6989 | if (!data) return bc_error_fmt("file '%s' is not text", file); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6990 | |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 6991 | sv_file = G.prog.file; |
| 6992 | G.prog.file = file; |
| 6993 | bc_lex_file(&G.prs.l); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6994 | s = bc_vm_process(data); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6995 | if (s) goto err; |
| 6996 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6997 | main_func = bc_program_func(BC_PROG_MAIN); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6998 | ip = bc_vec_item(&G.prog.stack, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6999 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 7000 | if (main_func->code.len < ip->idx) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 7001 | s = bc_error_fmt("file '%s' is not executable", file); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7002 | |
| 7003 | err: |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7004 | G.prog.file = sv_file; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7005 | free(data); |
| 7006 | return s; |
| 7007 | } |
| 7008 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7009 | static BcStatus bc_vm_stdin(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7010 | { |
Denys Vlasenko | a0c421c | 2018-12-02 20:16:52 +0100 | [diff] [blame] | 7011 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7012 | BcVec buf, buffer; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7013 | size_t len, i, str = 0; |
Denys Vlasenko | a0c421c | 2018-12-02 20:16:52 +0100 | [diff] [blame] | 7014 | bool comment = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7015 | |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7016 | G.prog.file = NULL; |
| 7017 | bc_lex_file(&G.prs.l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7018 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 7019 | bc_char_vec_init(&buffer); |
| 7020 | bc_char_vec_init(&buf); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 7021 | bc_vec_pushZeroByte(&buffer); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7022 | |
| 7023 | // This loop is complex because the vm tries not to send any lines that end |
| 7024 | // with a backslash to the parser. The reason for that is because the parser |
| 7025 | // treats a backslash+newline combo as whitespace, per the bc spec. In that |
| 7026 | // case, and for strings and comments, the parser will expect more stuff. |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 7027 | while ((s = bc_read_line(&buf)) == BC_STATUS_SUCCESS) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7028 | |
| 7029 | char *string = buf.v; |
| 7030 | |
| 7031 | len = buf.len - 1; |
| 7032 | |
| 7033 | if (len == 1) { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7034 | if (str && buf.v[0] == G.send) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7035 | str -= 1; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7036 | else if (buf.v[0] == G.sbgn) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7037 | str += 1; |
| 7038 | } |
| 7039 | else if (len > 1 || comment) { |
| 7040 | |
| 7041 | for (i = 0; i < len; ++i) { |
| 7042 | |
Denys Vlasenko | a0c421c | 2018-12-02 20:16:52 +0100 | [diff] [blame] | 7043 | bool notend = len > i + 1; |
| 7044 | char c = string[i]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7045 | |
| 7046 | if (i - 1 > len || string[i - 1] != '\\') { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7047 | if (G.sbgn == G.send) |
| 7048 | str ^= c == G.sbgn; |
| 7049 | else if (c == G.send) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7050 | str -= 1; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7051 | else if (c == G.sbgn) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7052 | str += 1; |
| 7053 | } |
| 7054 | |
| 7055 | if (c == '/' && notend && !comment && string[i + 1] == '*') { |
| 7056 | comment = true; |
| 7057 | break; |
| 7058 | } |
| 7059 | else if (c == '*' && notend && comment && string[i + 1] == '/') |
| 7060 | comment = false; |
| 7061 | } |
| 7062 | |
| 7063 | if (str || comment || string[len - 2] == '\\') { |
| 7064 | bc_vec_concat(&buffer, buf.v); |
| 7065 | continue; |
| 7066 | } |
| 7067 | } |
| 7068 | |
| 7069 | bc_vec_concat(&buffer, buf.v); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7070 | s = bc_vm_process(buffer.v); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7071 | if (s) { |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7072 | if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin) { |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 7073 | // Debug config, non-interactive mode: |
| 7074 | // return all the way back to main. |
| 7075 | // Non-debug builds do not come here, they exit. |
| 7076 | break; |
| 7077 | } |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7078 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7079 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 7080 | bc_vec_pop_all(&buffer); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7081 | } |
Denys Vlasenko | f522dd9 | 2018-12-07 16:35:43 +0100 | [diff] [blame] | 7082 | if (s == BC_STATUS_EOF) // input EOF (^D) is not an error |
| 7083 | s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7084 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 7085 | if (str) { |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7086 | s = bc_error("string end could not be found"); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 7087 | } |
| 7088 | else if (comment) { |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7089 | s = bc_error("comment end could not be found"); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 7090 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7091 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7092 | bc_vec_free(&buf); |
| 7093 | bc_vec_free(&buffer); |
| 7094 | return s; |
| 7095 | } |
| 7096 | |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7097 | #if ENABLE_BC |
| 7098 | static const char bc_lib[] = { |
| 7099 | "scale=20" |
| 7100 | "\n" "define e(x){" |
| 7101 | "\n" "auto b,s,n,r,d,i,p,f,v" |
| 7102 | "\n" "b=ibase" |
| 7103 | "\n" "ibase=A" |
| 7104 | "\n" "if(x<0){" |
| 7105 | "\n" "n=1" |
| 7106 | "\n" "x=-x" |
| 7107 | "\n" "}" |
| 7108 | "\n" "s=scale" |
| 7109 | "\n" "r=6+s+0.44*x" |
| 7110 | "\n" "scale=scale(x)+1" |
| 7111 | "\n" "while(x>1){" |
| 7112 | "\n" "d+=1" |
| 7113 | "\n" "x/=2" |
| 7114 | "\n" "scale+=1" |
| 7115 | "\n" "}" |
| 7116 | "\n" "scale=r" |
| 7117 | "\n" "r=x+1" |
| 7118 | "\n" "p=x" |
| 7119 | "\n" "f=v=1" |
| 7120 | "\n" "for(i=2;v!=0;++i){" |
| 7121 | "\n" "p*=x" |
| 7122 | "\n" "f*=i" |
| 7123 | "\n" "v=p/f" |
| 7124 | "\n" "r+=v" |
| 7125 | "\n" "}" |
| 7126 | "\n" "while((d--)!=0)r*=r" |
| 7127 | "\n" "scale=s" |
| 7128 | "\n" "ibase=b" |
| 7129 | "\n" "if(n!=0)return(1/r)" |
| 7130 | "\n" "return(r/1)" |
| 7131 | "\n" "}" |
| 7132 | "\n" "define l(x){" |
| 7133 | "\n" "auto b,s,r,p,a,q,i,v" |
| 7134 | "\n" "b=ibase" |
| 7135 | "\n" "ibase=A" |
| 7136 | "\n" "if(x<=0){" |
| 7137 | "\n" "r=(1-10^scale)/1" |
| 7138 | "\n" "ibase=b" |
| 7139 | "\n" "return(r)" |
| 7140 | "\n" "}" |
| 7141 | "\n" "s=scale" |
| 7142 | "\n" "scale+=6" |
| 7143 | "\n" "p=2" |
| 7144 | "\n" "while(x>=2){" |
| 7145 | "\n" "p*=2" |
| 7146 | "\n" "x=sqrt(x)" |
| 7147 | "\n" "}" |
| 7148 | "\n" "while(x<=0.5){" |
| 7149 | "\n" "p*=2" |
| 7150 | "\n" "x=sqrt(x)" |
| 7151 | "\n" "}" |
| 7152 | "\n" "r=a=(x-1)/(x+1)" |
| 7153 | "\n" "q=a*a" |
| 7154 | "\n" "v=1" |
| 7155 | "\n" "for(i=3;v!=0;i+=2){" |
| 7156 | "\n" "a*=q" |
| 7157 | "\n" "v=a/i" |
| 7158 | "\n" "r+=v" |
| 7159 | "\n" "}" |
| 7160 | "\n" "r*=p" |
| 7161 | "\n" "scale=s" |
| 7162 | "\n" "ibase=b" |
| 7163 | "\n" "return(r/1)" |
| 7164 | "\n" "}" |
| 7165 | "\n" "define s(x){" |
| 7166 | "\n" "auto b,s,r,n,a,q,i" |
| 7167 | "\n" "b=ibase" |
| 7168 | "\n" "ibase=A" |
| 7169 | "\n" "s=scale" |
| 7170 | "\n" "scale=1.1*s+2" |
| 7171 | "\n" "a=a(1)" |
| 7172 | "\n" "if(x<0){" |
| 7173 | "\n" "n=1" |
| 7174 | "\n" "x=-x" |
| 7175 | "\n" "}" |
| 7176 | "\n" "scale=0" |
| 7177 | "\n" "q=(x/a+2)/4" |
| 7178 | "\n" "x=x-4*q*a" |
| 7179 | "\n" "if(q%2!=0)x=-x" |
| 7180 | "\n" "scale=s+2" |
| 7181 | "\n" "r=a=x" |
| 7182 | "\n" "q=-x*x" |
| 7183 | "\n" "for(i=3;a!=0;i+=2){" |
| 7184 | "\n" "a*=q/(i*(i-1))" |
| 7185 | "\n" "r+=a" |
| 7186 | "\n" "}" |
| 7187 | "\n" "scale=s" |
| 7188 | "\n" "ibase=b" |
| 7189 | "\n" "if(n!=0)return(-r/1)" |
| 7190 | "\n" "return(r/1)" |
| 7191 | "\n" "}" |
| 7192 | "\n" "define c(x){" |
| 7193 | "\n" "auto b,s" |
| 7194 | "\n" "b=ibase" |
| 7195 | "\n" "ibase=A" |
| 7196 | "\n" "s=scale" |
| 7197 | "\n" "scale*=1.2" |
| 7198 | "\n" "x=s(2*a(1)+x)" |
| 7199 | "\n" "scale=s" |
| 7200 | "\n" "ibase=b" |
| 7201 | "\n" "return(x/1)" |
| 7202 | "\n" "}" |
| 7203 | "\n" "define a(x){" |
| 7204 | "\n" "auto b,s,r,n,a,m,t,f,i,u" |
| 7205 | "\n" "b=ibase" |
| 7206 | "\n" "ibase=A" |
| 7207 | "\n" "n=1" |
| 7208 | "\n" "if(x<0){" |
| 7209 | "\n" "n=-1" |
| 7210 | "\n" "x=-x" |
| 7211 | "\n" "}" |
| 7212 | "\n" "if(x==1){" |
| 7213 | "\n" "if(scale<65){" |
| 7214 | "\n" "return(.7853981633974483096156608458198757210492923498437764552437361480/n)" |
| 7215 | "\n" "}" |
| 7216 | "\n" "}" |
| 7217 | "\n" "if(x==.2){" |
| 7218 | "\n" "if(scale<65){" |
| 7219 | "\n" "return(.1973955598498807583700497651947902934475851037878521015176889402/n)" |
| 7220 | "\n" "}" |
| 7221 | "\n" "}" |
| 7222 | "\n" "s=scale" |
| 7223 | "\n" "if(x>.2){" |
| 7224 | "\n" "scale+=5" |
| 7225 | "\n" "a=a(.2)" |
| 7226 | "\n" "}" |
| 7227 | "\n" "scale=s+3" |
| 7228 | "\n" "while(x>.2){" |
| 7229 | "\n" "m+=1" |
| 7230 | "\n" "x=(x-.2)/(1+.2*x)" |
| 7231 | "\n" "}" |
| 7232 | "\n" "r=u=x" |
| 7233 | "\n" "f=-x*x" |
| 7234 | "\n" "t=1" |
| 7235 | "\n" "for(i=3;t!=0;i+=2){" |
| 7236 | "\n" "u*=f" |
| 7237 | "\n" "t=u/i" |
| 7238 | "\n" "r+=t" |
| 7239 | "\n" "}" |
| 7240 | "\n" "scale=s" |
| 7241 | "\n" "ibase=b" |
| 7242 | "\n" "return((m*a+r)/n)" |
| 7243 | "\n" "}" |
| 7244 | "\n" "define j(n,x){" |
| 7245 | "\n" "auto b,s,o,a,i,v,f" |
| 7246 | "\n" "b=ibase" |
| 7247 | "\n" "ibase=A" |
| 7248 | "\n" "s=scale" |
| 7249 | "\n" "scale=0" |
| 7250 | "\n" "n/=1" |
| 7251 | "\n" "if(n<0){" |
| 7252 | "\n" "n=-n" |
| 7253 | "\n" "if(n%2==1)o=1" |
| 7254 | "\n" "}" |
| 7255 | "\n" "a=1" |
| 7256 | "\n" "for(i=2;i<=n;++i)a*=i" |
| 7257 | "\n" "scale=1.5*s" |
| 7258 | "\n" "a=(x^n)/2^n/a" |
| 7259 | "\n" "r=v=1" |
| 7260 | "\n" "f=-x*x/4" |
| 7261 | "\n" "scale=scale+length(a)-scale(a)" |
| 7262 | "\n" "for(i=1;v!=0;++i){" |
| 7263 | "\n" "v=v*f/i/(n+i)" |
| 7264 | "\n" "r+=v" |
| 7265 | "\n" "}" |
| 7266 | "\n" "scale=s" |
| 7267 | "\n" "ibase=b" |
| 7268 | "\n" "if(o!=0)a=-a" |
| 7269 | "\n" "return(a*r/1)" |
| 7270 | "\n" "}" |
| 7271 | }; |
| 7272 | #endif // ENABLE_BC |
| 7273 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7274 | static BcStatus bc_vm_exec(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7275 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7276 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7277 | size_t i; |
| 7278 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7279 | #if ENABLE_BC |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 7280 | if (option_mask32 & BC_FLAG_L) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7281 | |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7282 | // We know that internal library is not buggy, |
| 7283 | // thus error checking is normally disabled. |
| 7284 | # define DEBUG_LIB 0 |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7285 | bc_lex_file(&G.prs.l); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7286 | s = bc_parse_text(&G.prs, bc_lib); |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7287 | if (DEBUG_LIB && s) return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7288 | |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7289 | while (G.prs.l.t.t != BC_LEX_EOF) { |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7290 | s = G.prs.parse(&G.prs); |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7291 | if (DEBUG_LIB && s) return s; |
| 7292 | } |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7293 | s = bc_program_exec(); |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7294 | if (DEBUG_LIB && s) return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7295 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7296 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7297 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7298 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7299 | for (i = 0; !s && i < G.files.len; ++i) |
| 7300 | s = bc_vm_file(*((char **) bc_vec_item(&G.files, i))); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7301 | if (ENABLE_FEATURE_CLEAN_UP && s && !G_ttyin) { |
| 7302 | // Debug config, non-interactive mode: |
| 7303 | // return all the way back to main. |
| 7304 | // Non-debug builds do not come here, they exit. |
| 7305 | return s; |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7306 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7307 | |
Denys Vlasenko | ac6ed11 | 2018-12-08 21:39:10 +0100 | [diff] [blame] | 7308 | if (IS_BC || (option_mask32 & BC_FLAG_I)) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7309 | s = bc_vm_stdin(); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7310 | |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7311 | if (!s && !BC_PARSE_CAN_EXEC(&G.prs)) |
| 7312 | s = bc_vm_process(""); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7313 | |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 7314 | return s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7315 | } |
| 7316 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7317 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 7318 | static void bc_program_free(void) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7319 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7320 | bc_num_free(&G.prog.ib); |
| 7321 | bc_num_free(&G.prog.ob); |
| 7322 | bc_num_free(&G.prog.hexb); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7323 | # if ENABLE_DC |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7324 | bc_num_free(&G.prog.strmb); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7325 | # endif |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7326 | bc_vec_free(&G.prog.fns); |
| 7327 | bc_vec_free(&G.prog.fn_map); |
| 7328 | bc_vec_free(&G.prog.vars); |
| 7329 | bc_vec_free(&G.prog.var_map); |
| 7330 | bc_vec_free(&G.prog.arrs); |
| 7331 | bc_vec_free(&G.prog.arr_map); |
| 7332 | bc_vec_free(&G.prog.strs); |
| 7333 | bc_vec_free(&G.prog.consts); |
| 7334 | bc_vec_free(&G.prog.results); |
| 7335 | bc_vec_free(&G.prog.stack); |
| 7336 | bc_num_free(&G.prog.last); |
| 7337 | bc_num_free(&G.prog.zero); |
| 7338 | bc_num_free(&G.prog.one); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7339 | } |
| 7340 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7341 | static void bc_vm_free(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7342 | { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7343 | bc_vec_free(&G.files); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7344 | bc_program_free(); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7345 | bc_parse_free(&G.prs); |
| 7346 | free(G.env_args); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7347 | } |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7348 | #endif |
| 7349 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7350 | static void bc_program_init(void) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7351 | { |
| 7352 | size_t idx; |
| 7353 | BcInstPtr ip; |
| 7354 | |
| 7355 | /* memset(&G.prog, 0, sizeof(G.prog)); - already is */ |
| 7356 | memset(&ip, 0, sizeof(BcInstPtr)); |
| 7357 | |
| 7358 | /* G.prog.nchars = G.prog.scale = 0; - already is */ |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7359 | |
| 7360 | bc_num_init(&G.prog.ib, BC_NUM_DEF_SIZE); |
| 7361 | bc_num_ten(&G.prog.ib); |
| 7362 | G.prog.ib_t = 10; |
| 7363 | |
| 7364 | bc_num_init(&G.prog.ob, BC_NUM_DEF_SIZE); |
| 7365 | bc_num_ten(&G.prog.ob); |
| 7366 | G.prog.ob_t = 10; |
| 7367 | |
| 7368 | bc_num_init(&G.prog.hexb, BC_NUM_DEF_SIZE); |
| 7369 | bc_num_ten(&G.prog.hexb); |
| 7370 | G.prog.hexb.num[0] = 6; |
| 7371 | |
| 7372 | #if ENABLE_DC |
| 7373 | bc_num_init(&G.prog.strmb, BC_NUM_DEF_SIZE); |
| 7374 | bc_num_ulong2num(&G.prog.strmb, UCHAR_MAX + 1); |
| 7375 | #endif |
| 7376 | |
| 7377 | bc_num_init(&G.prog.last, BC_NUM_DEF_SIZE); |
| 7378 | bc_num_zero(&G.prog.last); |
| 7379 | |
| 7380 | bc_num_init(&G.prog.zero, BC_NUM_DEF_SIZE); |
| 7381 | bc_num_zero(&G.prog.zero); |
| 7382 | |
| 7383 | bc_num_init(&G.prog.one, BC_NUM_DEF_SIZE); |
| 7384 | bc_num_one(&G.prog.one); |
| 7385 | |
| 7386 | bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7387 | bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7388 | |
Denys Vlasenko | 1f67e93 | 2018-12-03 00:08:59 +0100 | [diff] [blame] | 7389 | bc_program_addFunc(xstrdup("(main)"), &idx); |
| 7390 | bc_program_addFunc(xstrdup("(read)"), &idx); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7391 | |
| 7392 | bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7393 | bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7394 | |
| 7395 | bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7396 | bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7397 | |
| 7398 | bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free); |
| 7399 | bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free); |
| 7400 | bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free); |
| 7401 | bc_vec_init(&G.prog.stack, sizeof(BcInstPtr), NULL); |
| 7402 | bc_vec_push(&G.prog.stack, &ip); |
| 7403 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7404 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7405 | static int bc_vm_init(const char *env_len) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7406 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7407 | #if ENABLE_FEATURE_EDITING |
| 7408 | G.line_input_state = new_line_input_t(DO_HISTORY); |
| 7409 | #endif |
| 7410 | G.prog.len = bc_vm_envLen(env_len); |
| 7411 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7412 | bc_vec_init(&G.files, sizeof(char *), NULL); |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7413 | if (IS_BC) |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 7414 | IF_BC(bc_vm_envArgs();) |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7415 | bc_program_init(); |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 7416 | if (IS_BC) { |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 7417 | IF_BC(bc_parse_init(&G.prs, BC_PROG_MAIN);) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 7418 | } else { |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 7419 | IF_DC(dc_parse_init(&G.prs, BC_PROG_MAIN);) |
Denys Vlasenko | f6c1da5 | 2018-12-02 17:36:00 +0100 | [diff] [blame] | 7420 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7421 | |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7422 | if (isatty(0)) { |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7423 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7424 | G_ttyin = 1; |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7425 | // With SA_RESTART, most system calls will restart |
| 7426 | // (IOW: they won't fail with EINTR). |
| 7427 | // In particular, this means ^C won't cause |
| 7428 | // stdout to get into "error state" if SIGINT hits |
| 7429 | // within write() syscall. |
| 7430 | // The downside is that ^C while line input is taken |
| 7431 | // will only be handled after [Enter] since read() |
| 7432 | // from stdin is not interrupted by ^C either, |
| 7433 | // it restarts, thus fgetc() does not return on ^C. |
| 7434 | signal_SA_RESTART_empty_mask(SIGINT, record_signo); |
| 7435 | |
| 7436 | // Without SA_RESTART, this exhibits a bug: |
| 7437 | // "while (1) print 1" and try ^C-ing it. |
| 7438 | // Intermittently, instead of returning to input line, |
| 7439 | // you'll get "output error: Interrupted system call" |
| 7440 | // and exit. |
| 7441 | //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo); |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7442 | #endif |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7443 | return 1; // "tty" |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7444 | } |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7445 | return 0; // "not a tty" |
| 7446 | } |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7447 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7448 | static BcStatus bc_vm_run(void) |
| 7449 | { |
| 7450 | BcStatus st = bc_vm_exec(); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7451 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 7452 | if (G_exiting) // it was actually "halt" or "quit" |
| 7453 | st = EXIT_SUCCESS; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7454 | bc_vm_free(); |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 7455 | # if ENABLE_FEATURE_EDITING |
| 7456 | free_line_input_t(G.line_input_state); |
| 7457 | # endif |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 7458 | FREE_G(); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7459 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7460 | return st; |
| 7461 | } |
| 7462 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7463 | #if ENABLE_BC |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7464 | int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7465 | int bc_main(int argc UNUSED_PARAM, char **argv) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7466 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7467 | int is_tty; |
| 7468 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7469 | INIT_G(); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7470 | G.sbgn = G.send = '"'; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7471 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7472 | is_tty = bc_vm_init("BC_LINE_LENGTH"); |
| 7473 | |
| 7474 | bc_args(argv); |
| 7475 | |
| 7476 | if (is_tty && !(option_mask32 & BC_FLAG_Q)) |
| 7477 | bc_vm_info(); |
| 7478 | |
| 7479 | return bc_vm_run(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7480 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7481 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7482 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7483 | #if ENABLE_DC |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7484 | int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7485 | int dc_main(int argc UNUSED_PARAM, char **argv) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7486 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7487 | int noscript; |
| 7488 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7489 | INIT_G(); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7490 | G.sbgn = '['; |
| 7491 | G.send = ']'; |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 7492 | /* |
| 7493 | * TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width |
| 7494 | * 1 char wider than bc from the same package. |
| 7495 | * Both default width, and xC_LINE_LENGTH=N are wider: |
| 7496 | * "DC_LINE_LENGTH=5 dc -e'123456 p'" prints: |
| 7497 | * 1234\ |
| 7498 | * 56 |
| 7499 | * "echo '123456' | BC_LINE_LENGTH=5 bc" prints: |
| 7500 | * 123\ |
| 7501 | * 456 |
| 7502 | * Do the same, or it's a bug? |
| 7503 | */ |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7504 | bc_vm_init("DC_LINE_LENGTH"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7505 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7506 | // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs |
| 7507 | noscript = BC_FLAG_I; |
| 7508 | for (;;) { |
| 7509 | int n = getopt(argc, argv, "e:f:x"); |
| 7510 | if (n <= 0) |
| 7511 | break; |
| 7512 | switch (n) { |
| 7513 | case 'e': |
| 7514 | noscript = 0; |
| 7515 | n = bc_vm_process(optarg); |
| 7516 | if (n) return n; |
| 7517 | break; |
| 7518 | case 'f': |
| 7519 | noscript = 0; |
| 7520 | bc_vm_file(optarg); |
| 7521 | break; |
| 7522 | case 'x': |
| 7523 | option_mask32 |= DC_FLAG_X; |
| 7524 | break; |
| 7525 | default: |
| 7526 | bb_show_usage(); |
| 7527 | } |
| 7528 | } |
| 7529 | argv += optind; |
| 7530 | |
| 7531 | while (*argv) { |
| 7532 | noscript = 0; |
| 7533 | bc_vec_push(&G.files, argv++); |
| 7534 | } |
| 7535 | |
| 7536 | option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin |
| 7537 | |
| 7538 | return bc_vm_run(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7539 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7540 | #endif |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 7541 | |
| 7542 | #endif // not DC_SMALL |