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 |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 13 | //config: (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html). |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 14 | //config: |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 15 | //config: This bc has five differences to the GNU bc: |
| 16 | //config: 1) The period (.) is a shortcut for "last", as in the BSD bc. |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 17 | //config: 2) Arrays are copied before being passed as arguments to |
| 18 | //config: functions. This behavior is required by the bc spec. |
| 19 | //config: 3) Arrays can be passed to the builtin "length" function to get |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 20 | //config: the number of elements in the array. This prints "1": |
| 21 | //config: a[0] = 0; length(a[]) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 22 | //config: 4) The precedence of the boolean "not" operator (!) is equal to |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 23 | //config: that of the unary minus (-) negation operator. This still |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 24 | //config: allows POSIX-compliant scripts to work while somewhat |
| 25 | //config: preserving expected behavior (versus C) and making parsing |
| 26 | //config: easier. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 27 | //config: 5) "read()" accepts expressions, not only numeric literals. |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 28 | //config: |
| 29 | //config: Options: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 30 | //config: -i --interactive force interactive mode |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 31 | //config: -q --quiet don't print version and copyright |
| 32 | //config: -s --standard error if any non-POSIX extensions are used |
| 33 | //config: -w --warn warn if any non-POSIX extensions are used |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 34 | //config: -l --mathlib use predefined math routines: |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 35 | //config: s(expr) sine in radians |
| 36 | //config: c(expr) cosine in radians |
| 37 | //config: a(expr) arctangent, returning radians |
| 38 | //config: l(expr) natural log |
| 39 | //config: e(expr) raises e to the power of expr |
| 40 | //config: j(n, x) Bessel function of integer order n of x |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 41 | //config: |
| 42 | //config:config DC |
| 43 | //config: bool "dc (38 kb; 49 kb when combined with bc)" |
| 44 | //config: default y |
| 45 | //config: help |
| 46 | //config: dc is a reverse-polish notation command-line calculator which |
| 47 | //config: supports unlimited precision arithmetic. See the FreeBSD man page |
| 48 | //config: (https://www.unix.com/man-page/FreeBSD/1/dc/) and GNU dc manual |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 49 | //config: (https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html). |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 50 | //config: |
| 51 | //config: This dc has a few differences from the two above: |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 52 | //config: 1) When printing a byte stream (command "P"), this dc follows what |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 53 | //config: the FreeBSD dc does. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 54 | //config: 2) Implements the GNU extensions for divmod ("~") and |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 55 | //config: modular exponentiation ("|"). |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 56 | //config: 3) Implements all FreeBSD extensions, except for "J" and "M". |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 57 | //config: 4) Like the FreeBSD dc, this dc supports extended registers. |
| 58 | //config: However, they are implemented differently. When it encounters |
| 59 | //config: whitespace where a register should be, it skips the whitespace. |
| 60 | //config: If the character following is not a lowercase letter, an error |
| 61 | //config: is issued. Otherwise, the register name is parsed by the |
| 62 | //config: following regex: |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 63 | //config: [a-z][a-z0-9_]* |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 64 | //config: This generally means that register names will be surrounded by |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 65 | //config: whitespace. Examples: |
| 66 | //config: l idx s temp L index S temp2 < do_thing |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 67 | //config: Also note that, like the FreeBSD dc, extended registers are not |
| 68 | //config: allowed unless the "-x" option is given. |
| 69 | //config: |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 70 | //config:config FEATURE_DC_SMALL |
| 71 | //config: bool "Minimal dc implementation (4.2 kb), not using bc code base" |
| 72 | //config: depends on DC && !BC |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 73 | //config: default n |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 74 | //config: |
| 75 | //config:config FEATURE_DC_LIBM |
| 76 | //config: bool "Enable power and exp functions (requires libm)" |
| 77 | //config: default y |
| 78 | //config: depends on FEATURE_DC_SMALL |
| 79 | //config: help |
| 80 | //config: Enable power and exp functions. |
| 81 | //config: NOTE: This will require libm to be present for linking. |
| 82 | //config: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 83 | //config:config FEATURE_BC_SIGNALS |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 84 | //config: bool "Interactive mode (+4kb)" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 85 | //config: default y |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 86 | //config: depends on (BC || DC) && !FEATURE_DC_SMALL |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 87 | //config: help |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 88 | //config: Enable interactive mode: when started on a tty, |
| 89 | //config: ^C interrupts execution and returns to command line, |
| 90 | //config: errors also return to command line instead of exiting, |
| 91 | //config: line editing with history is available. |
| 92 | //config: |
| 93 | //config: With this option off, input can still be taken from tty, |
| 94 | //config: but all errors are fatal, ^C is fatal, |
| 95 | //config: tty is treated exactly the same as any other |
| 96 | //config: standard input (IOW: no line editing). |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 97 | //config: |
| 98 | //config:config FEATURE_BC_LONG_OPTIONS |
| 99 | //config: bool "Enable bc/dc long options" |
| 100 | //config: default y |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 101 | //config: depends on (BC || DC) && !FEATURE_DC_SMALL |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 102 | //config: help |
| 103 | //config: Enable long options for bc and dc. |
| 104 | |
| 105 | //applet:IF_BC(APPLET(bc, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 106 | //applet:IF_DC(APPLET(dc, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 107 | |
| 108 | //kbuild:lib-$(CONFIG_BC) += bc.o |
| 109 | //kbuild:lib-$(CONFIG_DC) += bc.o |
| 110 | |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 111 | //See www.gnu.org/software/bc/manual/bc.html |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 112 | //usage:#define bc_trivial_usage |
Denys Vlasenko | 09fe0aa | 2018-12-18 16:32:25 +0100 | [diff] [blame] | 113 | //usage: "[-sqlw] FILE..." |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 114 | //usage: |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 115 | //usage:#define bc_full_usage "\n" |
| 116 | //usage: "\nArbitrary precision calculator" |
| 117 | //usage: "\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 118 | ///////: "\n -i Interactive" - has no effect for now |
| 119 | //usage: "\n -q Quiet" |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 120 | //usage: "\n -l Load standard math library" |
| 121 | //usage: "\n -s Be POSIX compatible" |
Denys Vlasenko | 9721f6c | 2018-12-02 20:34:03 +0100 | [diff] [blame] | 122 | //usage: "\n -w Warn if extensions are used" |
| 123 | ///////: "\n -v Version" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 124 | //usage: "\n" |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 125 | //usage: "\n$BC_LINE_LENGTH changes output width" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 126 | //usage: |
| 127 | //usage:#define bc_example_usage |
| 128 | //usage: "3 + 4.129\n" |
| 129 | //usage: "1903 - 2893\n" |
| 130 | //usage: "-129 * 213.28935\n" |
| 131 | //usage: "12 / -1932\n" |
| 132 | //usage: "12 % 12\n" |
| 133 | //usage: "34 ^ 189\n" |
| 134 | //usage: "scale = 13\n" |
| 135 | //usage: "ibase = 2\n" |
| 136 | //usage: "obase = A\n" |
| 137 | //usage: |
| 138 | //usage:#define dc_trivial_usage |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 139 | //usage: IF_NOT_FEATURE_DC_SMALL("[-x] ")"[-eSCRIPT]... [-fFILE]... [FILE]..." |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 140 | //usage: |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 141 | //usage:#define dc_full_usage "\n" |
| 142 | //usage: "\nTiny RPN calculator. Operations:" |
Denys Vlasenko | 6e7c65f | 2018-12-08 19:34:35 +0100 | [diff] [blame] | 143 | //usage: "\n+, -, *, /, %, ~, ^," IF_NOT_FEATURE_DC_SMALL(" |,") |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 144 | //usage: "\np - print top of the stack (without popping)" |
| 145 | //usage: "\nf - print entire stack" |
| 146 | //usage: "\nk - pop the value and set the precision" |
| 147 | //usage: "\ni - pop the value and set input radix" |
| 148 | //usage: "\no - pop the value and set output radix" |
| 149 | //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] | 150 | //usage: |
| 151 | //usage:#define dc_example_usage |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 152 | //usage: "$ dc -e'2 2 + p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 153 | //usage: "4\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 154 | //usage: "$ dc -e'8 8 \\* 2 2 + / p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 155 | //usage: "16\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 156 | //usage: "$ dc -e'0 1 & p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 157 | //usage: "0\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 158 | //usage: "$ dc -e'0 1 | p'\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 159 | //usage: "1\n" |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 160 | //usage: "$ echo '72 9 / 8 * p' | dc\n" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 161 | //usage: "64\n" |
| 162 | |
| 163 | #include "libbb.h" |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 164 | #include "common_bufsiz.h" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 165 | |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 166 | #if ENABLE_FEATURE_DC_SMALL |
| 167 | # include "dc.c" |
| 168 | #else |
| 169 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 170 | #define DEBUG_LEXER 0 |
| 171 | #define DEBUG_COMPILE 0 |
| 172 | #define DEBUG_EXEC 0 |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 173 | // This can be left enabled for production as well: |
| 174 | #define SANITY_CHECKS 1 |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 175 | |
| 176 | #if DEBUG_LEXER |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 177 | static uint8_t lex_indent; |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 178 | #define dbg_lex(...) \ |
| 179 | do { \ |
| 180 | fprintf(stderr, "%*s", lex_indent, ""); \ |
| 181 | bb_error_msg(__VA_ARGS__); \ |
| 182 | } while (0) |
| 183 | #define dbg_lex_enter(...) \ |
| 184 | do { \ |
| 185 | dbg_lex(__VA_ARGS__); \ |
| 186 | lex_indent++; \ |
| 187 | } while (0) |
| 188 | #define dbg_lex_done(...) \ |
| 189 | do { \ |
| 190 | lex_indent--; \ |
| 191 | dbg_lex(__VA_ARGS__); \ |
| 192 | } while (0) |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 193 | #else |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 194 | # define dbg_lex(...) ((void)0) |
| 195 | # define dbg_lex_enter(...) ((void)0) |
| 196 | # define dbg_lex_done(...) ((void)0) |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 197 | #endif |
| 198 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 199 | #if DEBUG_COMPILE |
| 200 | # define dbg_compile(...) bb_error_msg(__VA_ARGS__) |
| 201 | #else |
| 202 | # define dbg_compile(...) ((void)0) |
| 203 | #endif |
| 204 | |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 205 | #if DEBUG_EXEC |
| 206 | # define dbg_exec(...) bb_error_msg(__VA_ARGS__) |
| 207 | #else |
| 208 | # define dbg_exec(...) ((void)0) |
| 209 | #endif |
| 210 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 211 | typedef enum BcStatus { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 212 | BC_STATUS_SUCCESS = 0, |
| 213 | BC_STATUS_FAILURE = 1, |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 214 | BC_STATUS_PARSE_EMPTY_EXP = 2, // bc_parse_expr_empty_ok() uses this |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 215 | } BcStatus; |
| 216 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 217 | #define BC_VEC_INVALID_IDX ((size_t) -1) |
| 218 | #define BC_VEC_START_CAP (1 << 5) |
| 219 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 220 | typedef void (*BcVecFree)(void *) FAST_FUNC; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 221 | |
| 222 | typedef struct BcVec { |
| 223 | char *v; |
| 224 | size_t len; |
| 225 | size_t cap; |
| 226 | size_t size; |
| 227 | BcVecFree dtor; |
| 228 | } BcVec; |
| 229 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 230 | typedef signed char BcDig; |
| 231 | |
| 232 | typedef struct BcNum { |
| 233 | BcDig *restrict num; |
| 234 | size_t rdx; |
| 235 | size_t len; |
| 236 | size_t cap; |
| 237 | bool neg; |
| 238 | } BcNum; |
| 239 | |
Denys Vlasenko | 2fa11b6 | 2018-12-06 12:34:39 +0100 | [diff] [blame] | 240 | #define BC_NUM_MAX_IBASE ((unsigned long) 16) |
| 241 | // larger value might speed up BIGNUM calculations a bit: |
| 242 | #define BC_NUM_DEF_SIZE (16) |
| 243 | #define BC_NUM_PRINT_WIDTH (69) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 244 | |
Denys Vlasenko | 2fa11b6 | 2018-12-06 12:34:39 +0100 | [diff] [blame] | 245 | #define BC_NUM_KARATSUBA_LEN (32) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 246 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 247 | typedef enum BcInst { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 248 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 249 | BC_INST_INC_PRE, |
| 250 | BC_INST_DEC_PRE, |
| 251 | BC_INST_INC_POST, |
| 252 | BC_INST_DEC_POST, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 253 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 254 | |
| 255 | BC_INST_NEG, |
| 256 | |
| 257 | BC_INST_POWER, |
| 258 | BC_INST_MULTIPLY, |
| 259 | BC_INST_DIVIDE, |
| 260 | BC_INST_MODULUS, |
| 261 | BC_INST_PLUS, |
| 262 | BC_INST_MINUS, |
| 263 | |
| 264 | BC_INST_REL_EQ, |
| 265 | BC_INST_REL_LE, |
| 266 | BC_INST_REL_GE, |
| 267 | BC_INST_REL_NE, |
| 268 | BC_INST_REL_LT, |
| 269 | BC_INST_REL_GT, |
| 270 | |
| 271 | BC_INST_BOOL_NOT, |
| 272 | BC_INST_BOOL_OR, |
| 273 | BC_INST_BOOL_AND, |
| 274 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 275 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 276 | BC_INST_ASSIGN_POWER, |
| 277 | BC_INST_ASSIGN_MULTIPLY, |
| 278 | BC_INST_ASSIGN_DIVIDE, |
| 279 | BC_INST_ASSIGN_MODULUS, |
| 280 | BC_INST_ASSIGN_PLUS, |
| 281 | BC_INST_ASSIGN_MINUS, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 282 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 283 | BC_INST_ASSIGN, |
| 284 | |
| 285 | BC_INST_NUM, |
| 286 | BC_INST_VAR, |
| 287 | BC_INST_ARRAY_ELEM, |
| 288 | BC_INST_ARRAY, |
| 289 | |
| 290 | BC_INST_SCALE_FUNC, |
| 291 | BC_INST_IBASE, |
| 292 | BC_INST_SCALE, |
| 293 | BC_INST_LAST, |
| 294 | BC_INST_LENGTH, |
| 295 | BC_INST_READ, |
| 296 | BC_INST_OBASE, |
| 297 | BC_INST_SQRT, |
| 298 | |
| 299 | BC_INST_PRINT, |
| 300 | BC_INST_PRINT_POP, |
| 301 | BC_INST_STR, |
| 302 | BC_INST_PRINT_STR, |
| 303 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 304 | #if ENABLE_BC |
Denys Vlasenko | 39287e0 | 2018-12-22 02:23:08 +0100 | [diff] [blame] | 305 | BC_INST_HALT, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 306 | BC_INST_JUMP, |
| 307 | BC_INST_JUMP_ZERO, |
| 308 | |
| 309 | BC_INST_CALL, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 310 | BC_INST_RET0, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 311 | #endif |
Denys Vlasenko | 39287e0 | 2018-12-22 02:23:08 +0100 | [diff] [blame] | 312 | BC_INST_RET, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 313 | |
| 314 | BC_INST_POP, |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 315 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 316 | BC_INST_POP_EXEC, |
| 317 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 318 | BC_INST_MODEXP, |
| 319 | BC_INST_DIVMOD, |
| 320 | |
| 321 | BC_INST_EXECUTE, |
| 322 | BC_INST_EXEC_COND, |
| 323 | |
| 324 | BC_INST_ASCIIFY, |
| 325 | BC_INST_PRINT_STREAM, |
| 326 | |
| 327 | BC_INST_PRINT_STACK, |
| 328 | BC_INST_CLEAR_STACK, |
| 329 | BC_INST_STACK_LEN, |
| 330 | BC_INST_DUPLICATE, |
| 331 | BC_INST_SWAP, |
| 332 | |
| 333 | BC_INST_LOAD, |
| 334 | BC_INST_PUSH_VAR, |
| 335 | BC_INST_PUSH_TO_VAR, |
| 336 | |
| 337 | BC_INST_QUIT, |
| 338 | BC_INST_NQUIT, |
| 339 | |
| 340 | BC_INST_INVALID = -1, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 341 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 342 | } BcInst; |
| 343 | |
| 344 | typedef struct BcId { |
| 345 | char *name; |
| 346 | size_t idx; |
| 347 | } BcId; |
| 348 | |
| 349 | typedef struct BcFunc { |
| 350 | BcVec code; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 351 | IF_BC(BcVec labels;) |
| 352 | IF_BC(BcVec autos;) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 353 | IF_BC(BcVec strs;) |
| 354 | IF_BC(BcVec consts;) |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 355 | IF_BC(size_t nparams;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 356 | } BcFunc; |
| 357 | |
| 358 | typedef enum BcResultType { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 359 | BC_RESULT_TEMP, |
| 360 | |
| 361 | BC_RESULT_VAR, |
| 362 | BC_RESULT_ARRAY_ELEM, |
| 363 | BC_RESULT_ARRAY, |
| 364 | |
| 365 | BC_RESULT_STR, |
| 366 | |
Denys Vlasenko | 4796a1d | 2018-12-19 12:47:45 +0100 | [diff] [blame] | 367 | //code uses "inst - BC_INST_IBASE + BC_RESULT_IBASE" construct, |
| 368 | BC_RESULT_IBASE, // relative order should match for: BC_INST_IBASE |
| 369 | BC_RESULT_SCALE, // relative order should match for: BC_INST_SCALE |
| 370 | BC_RESULT_LAST, // relative order should match for: BC_INST_LAST |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 371 | BC_RESULT_CONSTANT, |
| 372 | BC_RESULT_ONE, |
Denys Vlasenko | 4796a1d | 2018-12-19 12:47:45 +0100 | [diff] [blame] | 373 | BC_RESULT_OBASE, // relative order should match for: BC_INST_OBASE |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 374 | } BcResultType; |
| 375 | |
| 376 | typedef union BcResultData { |
| 377 | BcNum n; |
| 378 | BcVec v; |
| 379 | BcId id; |
| 380 | } BcResultData; |
| 381 | |
| 382 | typedef struct BcResult { |
| 383 | BcResultType t; |
| 384 | BcResultData d; |
| 385 | } BcResult; |
| 386 | |
| 387 | typedef struct BcInstPtr { |
| 388 | size_t func; |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 389 | size_t inst_idx; |
| 390 | IF_BC(size_t results_len_before_call;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 391 | } BcInstPtr; |
| 392 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 393 | // BC_LEX_NEG is not used in lexing; it is only for parsing. |
| 394 | typedef enum BcLexType { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 395 | BC_LEX_EOF, |
| 396 | BC_LEX_INVALID, |
| 397 | |
| 398 | BC_LEX_OP_INC, |
| 399 | BC_LEX_OP_DEC, |
| 400 | |
| 401 | BC_LEX_NEG, |
| 402 | |
| 403 | BC_LEX_OP_POWER, |
| 404 | BC_LEX_OP_MULTIPLY, |
| 405 | BC_LEX_OP_DIVIDE, |
| 406 | BC_LEX_OP_MODULUS, |
| 407 | BC_LEX_OP_PLUS, |
| 408 | BC_LEX_OP_MINUS, |
| 409 | |
| 410 | BC_LEX_OP_REL_EQ, |
| 411 | BC_LEX_OP_REL_LE, |
| 412 | BC_LEX_OP_REL_GE, |
| 413 | BC_LEX_OP_REL_NE, |
| 414 | BC_LEX_OP_REL_LT, |
| 415 | BC_LEX_OP_REL_GT, |
| 416 | |
| 417 | BC_LEX_OP_BOOL_NOT, |
| 418 | BC_LEX_OP_BOOL_OR, |
| 419 | BC_LEX_OP_BOOL_AND, |
| 420 | |
| 421 | BC_LEX_OP_ASSIGN_POWER, |
| 422 | BC_LEX_OP_ASSIGN_MULTIPLY, |
| 423 | BC_LEX_OP_ASSIGN_DIVIDE, |
| 424 | BC_LEX_OP_ASSIGN_MODULUS, |
| 425 | BC_LEX_OP_ASSIGN_PLUS, |
| 426 | BC_LEX_OP_ASSIGN_MINUS, |
| 427 | BC_LEX_OP_ASSIGN, |
| 428 | |
| 429 | BC_LEX_NLINE, |
| 430 | BC_LEX_WHITESPACE, |
| 431 | |
| 432 | BC_LEX_LPAREN, |
| 433 | BC_LEX_RPAREN, |
| 434 | |
| 435 | BC_LEX_LBRACKET, |
| 436 | BC_LEX_COMMA, |
| 437 | BC_LEX_RBRACKET, |
| 438 | |
Denys Vlasenko | 9dc5d08 | 2018-12-16 18:43:51 +0100 | [diff] [blame] | 439 | BC_LEX_LBRACE, // '{' is 0x7B, '}' is 0x7D, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 440 | BC_LEX_SCOLON, |
Denys Vlasenko | 9dc5d08 | 2018-12-16 18:43:51 +0100 | [diff] [blame] | 441 | BC_LEX_RBRACE, // should be LBRACE+2: code uses (c - '{' + BC_LEX_LBRACE) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 442 | |
| 443 | BC_LEX_STR, |
| 444 | BC_LEX_NAME, |
| 445 | BC_LEX_NUMBER, |
| 446 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 447 | BC_LEX_KEY_1st_keyword, |
| 448 | BC_LEX_KEY_AUTO = BC_LEX_KEY_1st_keyword, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 449 | BC_LEX_KEY_BREAK, |
| 450 | BC_LEX_KEY_CONTINUE, |
| 451 | BC_LEX_KEY_DEFINE, |
| 452 | BC_LEX_KEY_ELSE, |
| 453 | BC_LEX_KEY_FOR, |
| 454 | BC_LEX_KEY_HALT, |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 455 | // code uses "type - BC_LEX_KEY_IBASE + BC_INST_IBASE" construct, |
| 456 | BC_LEX_KEY_IBASE, // relative order should match for: BC_INST_IBASE |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 457 | BC_LEX_KEY_IF, |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 458 | BC_LEX_KEY_LAST, // relative order should match for: BC_INST_LAST |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 459 | BC_LEX_KEY_LENGTH, |
| 460 | BC_LEX_KEY_LIMITS, |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 461 | BC_LEX_KEY_OBASE, // relative order should match for: BC_INST_OBASE |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 462 | BC_LEX_KEY_PRINT, |
| 463 | BC_LEX_KEY_QUIT, |
| 464 | BC_LEX_KEY_READ, |
| 465 | BC_LEX_KEY_RETURN, |
| 466 | BC_LEX_KEY_SCALE, |
| 467 | BC_LEX_KEY_SQRT, |
| 468 | BC_LEX_KEY_WHILE, |
| 469 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 470 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 471 | BC_LEX_EQ_NO_REG, |
| 472 | BC_LEX_OP_MODEXP, |
| 473 | BC_LEX_OP_DIVMOD, |
| 474 | |
| 475 | BC_LEX_COLON, |
| 476 | BC_LEX_ELSE, |
| 477 | BC_LEX_EXECUTE, |
| 478 | BC_LEX_PRINT_STACK, |
| 479 | BC_LEX_CLEAR_STACK, |
| 480 | BC_LEX_STACK_LEVEL, |
| 481 | BC_LEX_DUPLICATE, |
| 482 | BC_LEX_SWAP, |
| 483 | BC_LEX_POP, |
| 484 | |
| 485 | BC_LEX_ASCIIFY, |
| 486 | BC_LEX_PRINT_STREAM, |
| 487 | |
Denys Vlasenko | 4796a1d | 2018-12-19 12:47:45 +0100 | [diff] [blame] | 488 | // code uses "t - BC_LEX_STORE_IBASE + BC_INST_IBASE" construct, |
| 489 | BC_LEX_STORE_IBASE, // relative order should match for: BC_INST_IBASE |
| 490 | BC_LEX_STORE_SCALE, // relative order should match for: BC_INST_SCALE |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 491 | BC_LEX_LOAD, |
| 492 | BC_LEX_LOAD_POP, |
| 493 | BC_LEX_STORE_PUSH, |
Denys Vlasenko | 4796a1d | 2018-12-19 12:47:45 +0100 | [diff] [blame] | 494 | BC_LEX_STORE_OBASE, // relative order should match for: BC_INST_OBASE |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 495 | BC_LEX_PRINT_POP, |
| 496 | BC_LEX_NQUIT, |
| 497 | BC_LEX_SCALE_FACTOR, |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 498 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 499 | } BcLexType; |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 500 | // must match order of BC_LEX_KEY_foo etc above |
| 501 | #if ENABLE_BC |
| 502 | struct BcLexKeyword { |
| 503 | char name8[8]; |
| 504 | }; |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 505 | #define BC_LEX_KW_ENTRY(a, b) \ |
| 506 | { .name8 = a /*, .posix = b */ } |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 507 | static const struct BcLexKeyword bc_lex_kws[20] = { |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 508 | BC_LEX_KW_ENTRY("auto" , 1), // 0 |
| 509 | BC_LEX_KW_ENTRY("break" , 1), // 1 |
| 510 | BC_LEX_KW_ENTRY("continue", 0), // 2 note: this one has no terminating NUL |
| 511 | BC_LEX_KW_ENTRY("define" , 1), // 3 |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 512 | BC_LEX_KW_ENTRY("else" , 0), // 4 |
| 513 | BC_LEX_KW_ENTRY("for" , 1), // 5 |
| 514 | BC_LEX_KW_ENTRY("halt" , 0), // 6 |
| 515 | BC_LEX_KW_ENTRY("ibase" , 1), // 7 |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 516 | BC_LEX_KW_ENTRY("if" , 1), // 8 |
| 517 | BC_LEX_KW_ENTRY("last" , 0), // 9 |
| 518 | BC_LEX_KW_ENTRY("length" , 1), // 10 |
| 519 | BC_LEX_KW_ENTRY("limits" , 0), // 11 |
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 | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 524 | BC_LEX_KW_ENTRY("return" , 1), // 16 |
| 525 | BC_LEX_KW_ENTRY("scale" , 1), // 17 |
| 526 | BC_LEX_KW_ENTRY("sqrt" , 1), // 18 |
| 527 | BC_LEX_KW_ENTRY("while" , 1), // 19 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 528 | }; |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 529 | #undef BC_LEX_KW_ENTRY |
Denys Vlasenko | 59d4ce9 | 2018-12-17 10:42:31 +0100 | [diff] [blame] | 530 | #define STRING_if (bc_lex_kws[8].name8) |
| 531 | #define STRING_else (bc_lex_kws[4].name8) |
| 532 | #define STRING_while (bc_lex_kws[19].name8) |
| 533 | #define STRING_for (bc_lex_kws[5].name8) |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 534 | enum { |
| 535 | POSIX_KWORD_MASK = 0 |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 536 | | (1 << 0) // 0 |
| 537 | | (1 << 1) // 1 |
| 538 | | (0 << 2) // 2 |
| 539 | | (1 << 3) // 3 |
| 540 | | (0 << 4) // 4 |
| 541 | | (1 << 5) // 5 |
| 542 | | (0 << 6) // 6 |
| 543 | | (1 << 7) // 7 |
| 544 | | (1 << 8) // 8 |
| 545 | | (0 << 9) // 9 |
| 546 | | (1 << 10) // 10 |
| 547 | | (0 << 11) // 11 |
| 548 | | (1 << 12) // 12 |
| 549 | | (0 << 13) // 13 |
| 550 | | (1 << 14) // 14 |
| 551 | | (0 << 15) // 15 |
| 552 | | (1 << 16) // 16 |
| 553 | | (1 << 17) // 17 |
| 554 | | (1 << 18) // 18 |
| 555 | | (1 << 19) // 19 |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 556 | }; |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 557 | #define bc_lex_kws_POSIX(i) ((1 << (i)) & POSIX_KWORD_MASK) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 558 | |
| 559 | // This is a bit array that corresponds to token types. An entry is |
| 560 | // true if the token is valid in an expression, false otherwise. |
| 561 | // Used to figure out when expr parsing should stop *without error message* |
| 562 | // - 0 element indicates this condition. 1 means "this token is to be eaten |
| 563 | // as part of the expression", token can them still be determined to be invalid |
| 564 | // by later processing. |
| 565 | enum { |
| 566 | #define EXBITS(a,b,c,d,e,f,g,h) \ |
| 567 | ((uint64_t)((a << 0)+(b << 1)+(c << 2)+(d << 3)+(e << 4)+(f << 5)+(g << 6)+(h << 7))) |
| 568 | BC_PARSE_EXPRS_BITS = 0 |
| 569 | + (EXBITS(0,0,1,1,1,1,1,1) << (0*8)) // 0: eof inval ++ -- - ^ * / |
| 570 | + (EXBITS(1,1,1,1,1,1,1,1) << (1*8)) // 8: % + - == <= >= != < |
| 571 | + (EXBITS(1,1,1,1,1,1,1,1) << (2*8)) // 16: > ! || && ^= *= /= %= |
| 572 | + (EXBITS(1,1,1,0,0,1,1,0) << (3*8)) // 24: += -= = NL WS ( ) [ |
| 573 | + (EXBITS(0,0,0,0,0,0,1,1) << (4*8)) // 32: , ] { ; } STR NAME NUM |
| 574 | + (EXBITS(0,0,0,0,0,0,0,1) << (5*8)) // 40: auto break cont define else for halt ibase |
| 575 | + (EXBITS(0,1,1,1,1,0,0,1) << (6*8)) // 48: if last len limits obase print quit read - bug, why "limits" is allowed? |
| 576 | + (EXBITS(0,1,1,0,0,0,0,0) << (7*8)) // 56: return scale sqrt while |
| 577 | #undef EXBITS |
| 578 | }; |
| 579 | static ALWAYS_INLINE long bc_parse_exprs(unsigned i) |
| 580 | { |
| 581 | #if ULONG_MAX > 0xffffffff |
| 582 | // 64-bit version (will not work correctly for 32-bit longs!) |
| 583 | return BC_PARSE_EXPRS_BITS & (1UL << i); |
| 584 | #else |
| 585 | // 32-bit version |
| 586 | unsigned long m = (uint32_t)BC_PARSE_EXPRS_BITS; |
| 587 | if (i >= 32) { |
| 588 | m = (uint32_t)(BC_PARSE_EXPRS_BITS >> 32); |
| 589 | i &= 31; |
| 590 | } |
| 591 | return m & (1UL << i); |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 592 | #endif |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | // This is an array of data for operators that correspond to |
| 596 | // [BC_LEX_OP_INC...BC_LEX_OP_ASSIGN] token types. |
| 597 | static const uint8_t bc_parse_ops[] = { |
| 598 | #define OP(p,l) ((int)(l) * 0x10 + (p)) |
| 599 | OP(0, false), OP( 0, false ), // inc dec |
| 600 | OP(1, false), // neg |
| 601 | OP(2, false), // pow |
| 602 | OP(3, true ), OP( 3, true ), OP( 3, true ), // mul div mod |
| 603 | OP(4, true ), OP( 4, true ), // + - |
| 604 | OP(6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), OP( 6, true ), // == <= >= != < > |
| 605 | OP(1, false), // not |
| 606 | OP(7, true ), OP( 7, true ), // or and |
| 607 | OP(5, false), OP( 5, false ), OP( 5, false ), OP( 5, false ), OP( 5, false ), // ^= *= /= %= += |
| 608 | OP(5, false), OP( 5, false ), // -= = |
| 609 | #undef OP |
| 610 | }; |
| 611 | #define bc_parse_op_PREC(i) (bc_parse_ops[i] & 0x0f) |
| 612 | #define bc_parse_op_LEFT(i) (bc_parse_ops[i] & 0x10) |
| 613 | #endif // ENABLE_BC |
| 614 | |
| 615 | #if ENABLE_DC |
| 616 | static const //BcInst - should be this type. Using signed narrow type since BC_INST_INVALID is -1 |
| 617 | int8_t |
| 618 | dc_parse_insts[] = { |
| 619 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE, |
| 620 | BC_INST_INVALID, BC_INST_POWER, BC_INST_MULTIPLY, BC_INST_DIVIDE, |
| 621 | BC_INST_MODULUS, BC_INST_PLUS, BC_INST_MINUS, |
| 622 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 623 | BC_INST_INVALID, BC_INST_INVALID, |
| 624 | BC_INST_BOOL_NOT, BC_INST_INVALID, BC_INST_INVALID, |
| 625 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 626 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 627 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GT, BC_INST_INVALID, |
| 628 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_REL_GE, |
| 629 | BC_INST_INVALID, BC_INST_INVALID, |
| 630 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 631 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 632 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_IBASE, |
| 633 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_LENGTH, BC_INST_INVALID, |
| 634 | BC_INST_OBASE, BC_INST_PRINT, BC_INST_QUIT, BC_INST_INVALID, |
| 635 | BC_INST_INVALID, BC_INST_SCALE, BC_INST_SQRT, BC_INST_INVALID, |
| 636 | BC_INST_REL_EQ, BC_INST_MODEXP, BC_INST_DIVMOD, BC_INST_INVALID, |
| 637 | BC_INST_INVALID, BC_INST_EXECUTE, BC_INST_PRINT_STACK, BC_INST_CLEAR_STACK, |
| 638 | BC_INST_STACK_LEN, BC_INST_DUPLICATE, BC_INST_SWAP, BC_INST_POP, |
| 639 | BC_INST_ASCIIFY, BC_INST_PRINT_STREAM, BC_INST_INVALID, BC_INST_INVALID, |
| 640 | BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, |
| 641 | BC_INST_PRINT, BC_INST_NQUIT, BC_INST_SCALE_FUNC, |
| 642 | }; |
| 643 | #endif // ENABLE_DC |
| 644 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 645 | typedef struct BcLex { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 646 | const char *buf; |
| 647 | size_t i; |
| 648 | size_t line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 649 | size_t len; |
| 650 | bool newline; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 651 | struct { |
| 652 | BcLexType t; |
| 653 | BcLexType last; |
| 654 | BcVec v; |
| 655 | } t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 656 | } BcLex; |
| 657 | |
Denys Vlasenko | 4796a1d | 2018-12-19 12:47:45 +0100 | [diff] [blame] | 658 | #define BC_PARSE_STREND (0xff) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 659 | |
Denys Vlasenko | 39287e0 | 2018-12-22 02:23:08 +0100 | [diff] [blame] | 660 | #if ENABLE_BC |
| 661 | # define BC_PARSE_REL (1 << 0) |
| 662 | # define BC_PARSE_PRINT (1 << 1) |
| 663 | # define BC_PARSE_ARRAY (1 << 2) |
| 664 | # define BC_PARSE_NOCALL (1 << 3) |
| 665 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 666 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 667 | typedef struct BcParse { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 668 | BcLex l; |
| 669 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 670 | IF_BC(BcVec exits;) |
| 671 | IF_BC(BcVec conds;) |
| 672 | IF_BC(BcVec ops;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 673 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 674 | BcFunc *func; |
| 675 | size_t fidx; |
| 676 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 677 | IF_BC(size_t in_funcdef;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 678 | } BcParse; |
| 679 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 680 | typedef struct BcProgram { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 681 | size_t len; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 682 | size_t nchars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 683 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 684 | size_t scale; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 685 | size_t ib_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 686 | size_t ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 687 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 688 | BcVec results; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 689 | BcVec exestack; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 690 | |
| 691 | BcVec fns; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 692 | IF_BC(BcVec fn_map;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 693 | |
| 694 | BcVec vars; |
| 695 | BcVec var_map; |
| 696 | |
| 697 | BcVec arrs; |
| 698 | BcVec arr_map; |
| 699 | |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 700 | IF_DC(BcVec strs;) |
| 701 | IF_DC(BcVec consts;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 702 | |
| 703 | const char *file; |
| 704 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 705 | BcNum zero; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 706 | IF_BC(BcNum one;) |
| 707 | IF_BC(BcNum last;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 708 | } BcProgram; |
| 709 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 710 | #define BC_PROG_MAIN (0) |
| 711 | #define BC_PROG_READ (1) |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 712 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 713 | #define BC_PROG_REQ_FUNCS (2) |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 714 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 715 | |
| 716 | #define BC_PROG_STR(n) (!(n)->num && !(n)->cap) |
| 717 | #define BC_PROG_NUM(r, n) \ |
| 718 | ((r)->t != BC_RESULT_ARRAY && (r)->t != BC_RESULT_STR && !BC_PROG_STR(n)) |
| 719 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 720 | #define BC_FLAG_W (1 << 0) |
| 721 | #define BC_FLAG_V (1 << 1) |
| 722 | #define BC_FLAG_S (1 << 2) |
| 723 | #define BC_FLAG_Q (1 << 3) |
| 724 | #define BC_FLAG_L (1 << 4) |
| 725 | #define BC_FLAG_I (1 << 5) |
| 726 | #define DC_FLAG_X (1 << 6) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 727 | |
| 728 | #define BC_MAX(a, b) ((a) > (b) ? (a) : (b)) |
| 729 | #define BC_MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 730 | |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 731 | #define BC_MAX_OBASE ((unsigned) 999) |
| 732 | #define BC_MAX_DIM ((unsigned) INT_MAX) |
| 733 | #define BC_MAX_SCALE ((unsigned) UINT_MAX) |
| 734 | #define BC_MAX_STRING ((unsigned) UINT_MAX - 1) |
| 735 | #define BC_MAX_NUM BC_MAX_STRING |
| 736 | // Unused apart from "limits" message. Just show a "biggish number" there. |
| 737 | //#define BC_MAX_NAME BC_MAX_STRING |
| 738 | //#define BC_MAX_EXP ((unsigned long) LONG_MAX) |
| 739 | //#define BC_MAX_VARS ((unsigned long) SIZE_MAX - 1) |
| 740 | #define BC_MAX_NAME_STR "999999999" |
| 741 | #define BC_MAX_EXP_STR "999999999" |
| 742 | #define BC_MAX_VARS_STR "999999999" |
| 743 | |
| 744 | #define BC_MAX_OBASE_STR "999" |
| 745 | |
| 746 | #if INT_MAX == 2147483647 |
| 747 | # define BC_MAX_DIM_STR "2147483647" |
| 748 | #elif INT_MAX == 9223372036854775807 |
| 749 | # define BC_MAX_DIM_STR "9223372036854775807" |
| 750 | #else |
| 751 | # error Strange INT_MAX |
| 752 | #endif |
| 753 | |
| 754 | #if UINT_MAX == 4294967295 |
| 755 | # define BC_MAX_SCALE_STR "4294967295" |
| 756 | # define BC_MAX_STRING_STR "4294967294" |
| 757 | #elif UINT_MAX == 18446744073709551615 |
| 758 | # define BC_MAX_SCALE_STR "18446744073709551615" |
| 759 | # define BC_MAX_STRING_STR "18446744073709551614" |
| 760 | #else |
| 761 | # error Strange UINT_MAX |
| 762 | #endif |
| 763 | #define BC_MAX_NUM_STR BC_MAX_STRING_STR |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 764 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 765 | struct globals { |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 766 | IF_FEATURE_BC_SIGNALS(smallint ttyin;) |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 767 | IF_FEATURE_CLEAN_UP(smallint exiting;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 768 | |
| 769 | BcParse prs; |
| 770 | BcProgram prog; |
| 771 | |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 772 | // For error messages. Can be set to current parsed line, |
| 773 | // or [TODO] to current executing line (can be before last parsed one) |
| 774 | unsigned err_line; |
| 775 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 776 | BcVec files; |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 777 | BcVec input_buffer; |
| 778 | FILE *input_fp; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 779 | |
| 780 | char *env_args; |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 781 | |
| 782 | #if ENABLE_FEATURE_EDITING |
| 783 | line_input_t *line_input_state; |
| 784 | #endif |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 785 | } FIX_ALIASING; |
| 786 | #define G (*ptr_to_globals) |
| 787 | #define INIT_G() do { \ |
| 788 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 789 | } while (0) |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 790 | #define FREE_G() do { \ |
| 791 | FREE_PTR_TO_GLOBALS(); \ |
| 792 | } while (0) |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 793 | #define G_posix (ENABLE_BC && (option_mask32 & BC_FLAG_S)) |
| 794 | #define G_warn (ENABLE_BC && (option_mask32 & BC_FLAG_W)) |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 795 | #define G_exreg (ENABLE_DC && (option_mask32 & DC_FLAG_X)) |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 796 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | b9c321d | 2018-12-07 12:41:42 +0100 | [diff] [blame] | 797 | # define G_interrupt bb_got_signal |
| 798 | # define G_ttyin G.ttyin |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 799 | #else |
Denys Vlasenko | b9c321d | 2018-12-07 12:41:42 +0100 | [diff] [blame] | 800 | # define G_interrupt 0 |
| 801 | # define G_ttyin 0 |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 802 | #endif |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 803 | #if ENABLE_FEATURE_CLEAN_UP |
| 804 | # define G_exiting G.exiting |
| 805 | #else |
| 806 | # define G_exiting 0 |
| 807 | #endif |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 808 | #define IS_BC (ENABLE_BC && (!ENABLE_DC || applet_name[0] == 'b')) |
Denys Vlasenko | 40534bb | 2018-12-13 16:59:24 +0100 | [diff] [blame] | 809 | #define IS_DC (ENABLE_DC && (!ENABLE_BC || applet_name[0] != 'b')) |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 810 | |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 811 | // In configurations where errors abort instead of propagating error |
| 812 | // return code up the call chain, functions returning BC_STATUS |
| 813 | // actually don't return anything, they always succeed and return "void". |
| 814 | // A macro wrapper is provided, which makes this statement work: |
| 815 | // s = zbc_func(...) |
| 816 | // and makes it visible to the compiler that s is always zero, |
| 817 | // allowing compiler to optimize dead code after the statement. |
| 818 | // |
| 819 | // To make code more readable, each such function has a "z" |
| 820 | // ("always returning zero") prefix, i.e. zbc_foo or zdc_foo. |
| 821 | // |
| 822 | #if ENABLE_FEATURE_BC_SIGNALS || ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 823 | # define ERRORS_ARE_FATAL 0 |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 824 | # define ERRORFUNC /*nothing*/ |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 825 | # define IF_ERROR_RETURN_POSSIBLE(a) a |
| 826 | # define BC_STATUS BcStatus |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 827 | # define RETURN_STATUS(v) return (v) |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 828 | # define COMMA_SUCCESS /*nothing*/ |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 829 | #else |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 830 | # define ERRORS_ARE_FATAL 1 |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 831 | # define ERRORFUNC NORETURN |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 832 | # define IF_ERROR_RETURN_POSSIBLE(a) /*nothing*/ |
| 833 | # define BC_STATUS void |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 834 | # define RETURN_STATUS(v) do { ((void)(v)); return; } while (0) |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 835 | # define COMMA_SUCCESS ,BC_STATUS_SUCCESS |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 836 | #endif |
| 837 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 838 | #define STACK_HAS_MORE_THAN(s, n) ((s)->len > ((size_t)(n))) |
| 839 | #define STACK_HAS_EQUAL_OR_MORE_THAN(s, n) ((s)->len >= ((size_t)(n))) |
| 840 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 841 | #define BC_NUM_NEG(n, neg) ((((ssize_t)(n)) ^ -((ssize_t)(neg))) + (neg)) |
| 842 | #define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1) |
| 843 | #define BC_NUM_INT(n) ((n)->len - (n)->rdx) |
| 844 | //#define BC_NUM_AREQ(a, b) (BC_MAX((a)->rdx, (b)->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1) |
| 845 | static /*ALWAYS_INLINE*/ size_t BC_NUM_AREQ(BcNum *a, BcNum *b) |
| 846 | { |
| 847 | return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1; |
| 848 | } |
| 849 | //#define BC_NUM_MREQ(a, b, scale) (BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX((scale), (a)->rdx + (b)->rdx) + 1) |
| 850 | static /*ALWAYS_INLINE*/ size_t BC_NUM_MREQ(BcNum *a, BcNum *b, size_t scale) |
| 851 | { |
| 852 | return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1; |
| 853 | } |
| 854 | |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 855 | typedef void (*BcNumDigitOp)(size_t, size_t, bool) FAST_FUNC; |
| 856 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 857 | typedef BC_STATUS (*BcNumBinaryOp)(BcNum *, BcNum *, BcNum *, size_t) FAST_FUNC; |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 858 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 859 | static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale, |
| 860 | BcNumBinaryOp op, size_t req); |
| 861 | static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 862 | static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 863 | static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 864 | static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 865 | static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 866 | static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 867 | |
| 868 | static FAST_FUNC BC_STATUS zbc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 869 | { |
| 870 | BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_a : zbc_num_s; |
| 871 | (void) scale; |
| 872 | RETURN_STATUS(zbc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b))); |
| 873 | } |
| 874 | |
| 875 | static FAST_FUNC BC_STATUS zbc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 876 | { |
| 877 | BcNumBinaryOp op = (!a->neg == !b->neg) ? zbc_num_s : zbc_num_a; |
| 878 | (void) scale; |
| 879 | RETURN_STATUS(zbc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b))); |
| 880 | } |
| 881 | |
| 882 | static FAST_FUNC BC_STATUS zbc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 883 | { |
| 884 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 885 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_m, req)); |
| 886 | } |
| 887 | |
| 888 | static FAST_FUNC BC_STATUS zbc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 889 | { |
| 890 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 891 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_d, req)); |
| 892 | } |
| 893 | |
| 894 | static FAST_FUNC BC_STATUS zbc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 895 | { |
| 896 | size_t req = BC_NUM_MREQ(a, b, scale); |
| 897 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_rem, req)); |
| 898 | } |
| 899 | |
| 900 | static FAST_FUNC BC_STATUS zbc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) |
| 901 | { |
| 902 | RETURN_STATUS(zbc_num_binary(a, b, c, scale, zbc_num_p, a->len * b->len + 1)); |
| 903 | } |
Denys Vlasenko | c2d15df | 2018-12-11 17:56:09 +0100 | [diff] [blame] | 904 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 905 | static const BcNumBinaryOp zbc_program_ops[] = { |
| 906 | zbc_num_pow, zbc_num_mul, zbc_num_div, zbc_num_mod, zbc_num_add, zbc_num_sub, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 907 | }; |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 908 | #define zbc_num_add(...) (zbc_num_add(__VA_ARGS__) COMMA_SUCCESS) |
| 909 | #define zbc_num_sub(...) (zbc_num_sub(__VA_ARGS__) COMMA_SUCCESS) |
| 910 | #define zbc_num_mul(...) (zbc_num_mul(__VA_ARGS__) COMMA_SUCCESS) |
| 911 | #define zbc_num_div(...) (zbc_num_div(__VA_ARGS__) COMMA_SUCCESS) |
| 912 | #define zbc_num_mod(...) (zbc_num_mod(__VA_ARGS__) COMMA_SUCCESS) |
| 913 | #define zbc_num_pow(...) (zbc_num_pow(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 914 | |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 915 | static void fflush_and_check(void) |
| 916 | { |
| 917 | fflush_all(); |
| 918 | if (ferror(stdout) || ferror(stderr)) |
| 919 | bb_perror_msg_and_die("output error"); |
| 920 | } |
| 921 | |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 922 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 923 | #define QUIT_OR_RETURN_TO_MAIN \ |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 924 | do { \ |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 925 | 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] | 926 | G_exiting = 1; \ |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 927 | return BC_STATUS_FAILURE; \ |
| 928 | } while (0) |
| 929 | #else |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 930 | #define QUIT_OR_RETURN_TO_MAIN quit() |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 931 | #endif |
| 932 | |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 933 | static void quit(void) NORETURN; |
| 934 | static void quit(void) |
| 935 | { |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 936 | if (ferror(stdin)) |
| 937 | bb_perror_msg_and_die("input error"); |
| 938 | fflush_and_check(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 939 | dbg_exec("quit(): exiting with exitcode SUCCESS"); |
Denys Vlasenko | d4744ad | 2018-12-03 14:28:51 +0100 | [diff] [blame] | 940 | exit(0); |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 941 | } |
| 942 | |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 943 | static void bc_verror_msg(const char *fmt, va_list p) |
| 944 | { |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 945 | const char *sv = sv; // for compiler |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 946 | if (G.prog.file) { |
| 947 | sv = applet_name; |
| 948 | applet_name = xasprintf("%s: %s:%u", applet_name, G.prog.file, G.err_line); |
| 949 | } |
| 950 | bb_verror_msg(fmt, p, NULL); |
| 951 | if (G.prog.file) { |
| 952 | free((char*)applet_name); |
| 953 | applet_name = sv; |
| 954 | } |
| 955 | } |
| 956 | |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 957 | static NOINLINE ERRORFUNC int bc_error_fmt(const char *fmt, ...) |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 958 | { |
| 959 | va_list p; |
| 960 | |
| 961 | va_start(p, fmt); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 962 | bc_verror_msg(fmt, p); |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 963 | va_end(p); |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 964 | |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 965 | if (ENABLE_FEATURE_CLEAN_UP || G_ttyin) |
| 966 | IF_ERROR_RETURN_POSSIBLE(return BC_STATUS_FAILURE); |
| 967 | exit(1); |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 968 | } |
| 969 | |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 970 | #if ENABLE_BC |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 971 | static NOINLINE int bc_posix_error_fmt(const char *fmt, ...) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 972 | { |
| 973 | va_list p; |
| 974 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 975 | // Are non-POSIX constructs totally ok? |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 976 | if (!(option_mask32 & (BC_FLAG_S|BC_FLAG_W))) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 977 | return BC_STATUS_SUCCESS; // yes |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 978 | |
| 979 | va_start(p, fmt); |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 980 | bc_verror_msg(fmt, p); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 981 | va_end(p); |
| 982 | |
| 983 | // Do we treat non-POSIX constructs as errors? |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 984 | if (!(option_mask32 & BC_FLAG_S)) |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 985 | return BC_STATUS_SUCCESS; // no, it's a warning |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 986 | if (ENABLE_FEATURE_CLEAN_UP || G_ttyin) |
| 987 | return BC_STATUS_FAILURE; |
| 988 | exit(1); |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 989 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 990 | #endif |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 991 | |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 992 | // We use error functions with "return bc_error(FMT[, PARAMS])" idiom. |
| 993 | // This idiom begs for tail-call optimization, but for it to work, |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 994 | // function must not have caller-cleaned parameters on stack. |
| 995 | // Unfortunately, vararg function API does exactly that on most arches. |
| 996 | // Thus, use these shims for the cases when we have no vararg PARAMS: |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 997 | static ERRORFUNC int bc_error(const char *msg) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 998 | { |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 999 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("%s", msg); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1000 | } |
| 1001 | static ERRORFUNC int bc_error_bad_character(char c) |
| 1002 | { |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 1003 | if (!c) |
| 1004 | IF_ERROR_RETURN_POSSIBLE(return) bc_error("NUL character"); |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1005 | IF_ERROR_RETURN_POSSIBLE(return) bc_error_fmt("bad character '%c'", c); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1006 | } |
| 1007 | static ERRORFUNC int bc_error_bad_expression(void) |
| 1008 | { |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1009 | IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad expression"); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1010 | } |
| 1011 | static ERRORFUNC int bc_error_bad_token(void) |
| 1012 | { |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1013 | IF_ERROR_RETURN_POSSIBLE(return) bc_error("bad token"); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1014 | } |
| 1015 | static ERRORFUNC int bc_error_stack_has_too_few_elements(void) |
| 1016 | { |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1017 | IF_ERROR_RETURN_POSSIBLE(return) bc_error("stack has too few elements"); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1018 | } |
| 1019 | static ERRORFUNC int bc_error_variable_is_wrong_type(void) |
| 1020 | { |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1021 | IF_ERROR_RETURN_POSSIBLE(return) bc_error("variable is wrong type"); |
Denys Vlasenko | 86e63cd | 2018-12-10 19:46:53 +0100 | [diff] [blame] | 1022 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1023 | #if ENABLE_BC |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 1024 | static int bc_POSIX_requires(const char *msg) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1025 | { |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 1026 | return bc_posix_error_fmt("POSIX requires %s", msg); |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1027 | } |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 1028 | static int bc_POSIX_does_not_allow(const char *msg) |
| 1029 | { |
| 1030 | return bc_posix_error_fmt("%s%s", "POSIX does not allow ", msg); |
| 1031 | } |
| 1032 | static int bc_POSIX_does_not_allow_bool_ops_this_is_bad(const char *msg) |
| 1033 | { |
| 1034 | return bc_posix_error_fmt("%s%s %s", "POSIX does not allow ", "boolean operators; the following is bad:", msg); |
| 1035 | } |
| 1036 | static int bc_POSIX_does_not_allow_empty_X_expression_in_for(const char *msg) |
| 1037 | { |
| 1038 | return bc_posix_error_fmt("%san empty %s expression in a for loop", "POSIX does not allow ", msg); |
| 1039 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1040 | #endif |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 1041 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1042 | static void bc_vec_grow(BcVec *v, size_t n) |
| 1043 | { |
| 1044 | size_t cap = v->cap * 2; |
| 1045 | while (cap < v->len + n) cap *= 2; |
| 1046 | v->v = xrealloc(v->v, v->size * cap); |
| 1047 | v->cap = cap; |
| 1048 | } |
| 1049 | |
| 1050 | static void bc_vec_init(BcVec *v, size_t esize, BcVecFree dtor) |
| 1051 | { |
| 1052 | v->size = esize; |
| 1053 | v->cap = BC_VEC_START_CAP; |
| 1054 | v->len = 0; |
| 1055 | v->dtor = dtor; |
| 1056 | v->v = xmalloc(esize * BC_VEC_START_CAP); |
| 1057 | } |
| 1058 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1059 | static void bc_char_vec_init(BcVec *v) |
| 1060 | { |
| 1061 | bc_vec_init(v, sizeof(char), NULL); |
| 1062 | } |
| 1063 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1064 | static void bc_vec_expand(BcVec *v, size_t req) |
| 1065 | { |
| 1066 | if (v->cap < req) { |
| 1067 | v->v = xrealloc(v->v, v->size * req); |
| 1068 | v->cap = req; |
| 1069 | } |
| 1070 | } |
| 1071 | |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 1072 | static void bc_vec_pop(BcVec *v) |
| 1073 | { |
| 1074 | v->len--; |
| 1075 | if (v->dtor) |
| 1076 | v->dtor(v->v + (v->size * v->len)); |
| 1077 | } |
Denys Vlasenko | 2fa11b6 | 2018-12-06 12:34:39 +0100 | [diff] [blame] | 1078 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1079 | static void bc_vec_npop(BcVec *v, size_t n) |
| 1080 | { |
| 1081 | if (!v->dtor) |
| 1082 | v->len -= n; |
| 1083 | else { |
| 1084 | size_t len = v->len - n; |
| 1085 | while (v->len > len) v->dtor(v->v + (v->size * --v->len)); |
| 1086 | } |
| 1087 | } |
| 1088 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1089 | static void bc_vec_pop_all(BcVec *v) |
| 1090 | { |
| 1091 | bc_vec_npop(v, v->len); |
| 1092 | } |
| 1093 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1094 | static size_t bc_vec_push(BcVec *v, const void *data) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1095 | { |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1096 | size_t len = v->len; |
| 1097 | if (len >= v->cap) bc_vec_grow(v, 1); |
| 1098 | memmove(v->v + (v->size * len), data, v->size); |
| 1099 | v->len++; |
| 1100 | return len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1101 | } |
| 1102 | |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 1103 | // G.prog.results often needs "pop old operand, push result" idiom. |
| 1104 | // Can do this without a few extra ops |
| 1105 | static size_t bc_result_pop_and_push(const void *data) |
| 1106 | { |
| 1107 | BcVec *v = &G.prog.results; |
| 1108 | char *last; |
| 1109 | size_t len = v->len - 1; |
| 1110 | |
| 1111 | last = v->v + (v->size * len); |
| 1112 | if (v->dtor) |
| 1113 | v->dtor(last); |
| 1114 | memmove(last, data, v->size); |
| 1115 | return len; |
| 1116 | } |
| 1117 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1118 | static size_t bc_vec_pushByte(BcVec *v, char data) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1119 | { |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1120 | return bc_vec_push(v, &data); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1121 | } |
| 1122 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1123 | static size_t bc_vec_pushZeroByte(BcVec *v) |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1124 | { |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1125 | //return bc_vec_pushByte(v, '\0'); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1126 | // better: |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 1127 | return bc_vec_push(v, &const_int_0); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1128 | } |
| 1129 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1130 | static void bc_vec_pushAt(BcVec *v, const void *data, size_t idx) |
| 1131 | { |
| 1132 | if (idx == v->len) |
| 1133 | bc_vec_push(v, data); |
| 1134 | else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1135 | char *ptr; |
| 1136 | |
| 1137 | if (v->len == v->cap) bc_vec_grow(v, 1); |
| 1138 | |
| 1139 | ptr = v->v + v->size * idx; |
| 1140 | |
| 1141 | memmove(ptr + v->size, ptr, v->size * (v->len++ - idx)); |
| 1142 | memmove(ptr, data, v->size); |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | static void bc_vec_string(BcVec *v, size_t len, const char *str) |
| 1147 | { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1148 | bc_vec_pop_all(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1149 | bc_vec_expand(v, len + 1); |
| 1150 | memcpy(v->v, str, len); |
| 1151 | v->len = len; |
| 1152 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1153 | bc_vec_pushZeroByte(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1154 | } |
| 1155 | |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 1156 | #if ENABLE_FEATURE_BC_SIGNALS && ENABLE_FEATURE_EDITING |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1157 | static void bc_vec_concat(BcVec *v, const char *str) |
| 1158 | { |
Denys Vlasenko | 8b4cf0d | 2018-12-10 15:12:58 +0100 | [diff] [blame] | 1159 | size_t len, slen; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1160 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 1161 | if (v->len == 0) bc_vec_pushZeroByte(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1162 | |
Denys Vlasenko | 8b4cf0d | 2018-12-10 15:12:58 +0100 | [diff] [blame] | 1163 | slen = strlen(str); |
| 1164 | len = v->len + slen; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1165 | |
Denys Vlasenko | 8b4cf0d | 2018-12-10 15:12:58 +0100 | [diff] [blame] | 1166 | if (v->cap < len) bc_vec_grow(v, slen); |
Denys Vlasenko | 1ff8862 | 2018-12-06 12:06:16 +0100 | [diff] [blame] | 1167 | strcpy(v->v + v->len - 1, str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1168 | |
| 1169 | v->len = len; |
| 1170 | } |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 1171 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1172 | |
| 1173 | static void *bc_vec_item(const BcVec *v, size_t idx) |
| 1174 | { |
| 1175 | return v->v + v->size * idx; |
| 1176 | } |
| 1177 | |
| 1178 | static void *bc_vec_item_rev(const BcVec *v, size_t idx) |
| 1179 | { |
| 1180 | return v->v + v->size * (v->len - idx - 1); |
| 1181 | } |
| 1182 | |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 1183 | static void *bc_vec_top(const BcVec *v) |
| 1184 | { |
| 1185 | return v->v + v->size * (v->len - 1); |
| 1186 | } |
| 1187 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1188 | static FAST_FUNC void bc_vec_free(void *vec) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1189 | { |
| 1190 | BcVec *v = (BcVec *) vec; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 1191 | bc_vec_pop_all(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1192 | free(v->v); |
| 1193 | } |
| 1194 | |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 1195 | static BcFunc* bc_program_func(size_t idx) |
| 1196 | { |
| 1197 | return bc_vec_item(&G.prog.fns, idx); |
| 1198 | } |
| 1199 | // BC_PROG_MAIN is zeroth element, so: |
| 1200 | #define bc_program_func_BC_PROG_MAIN() ((BcFunc*)(G.prog.fns.v)) |
| 1201 | |
| 1202 | #if ENABLE_BC |
| 1203 | static BcFunc* bc_program_current_func(void) |
| 1204 | { |
| 1205 | BcInstPtr *ip = bc_vec_top(&G.prog.exestack); |
| 1206 | BcFunc *func = bc_program_func(ip->func); |
| 1207 | return func; |
| 1208 | } |
| 1209 | #endif |
| 1210 | |
| 1211 | static char** bc_program_str(size_t idx) |
| 1212 | { |
| 1213 | #if ENABLE_BC |
| 1214 | if (IS_BC) { |
| 1215 | BcFunc *func = bc_program_current_func(); |
| 1216 | return bc_vec_item(&func->strs, idx); |
| 1217 | } |
| 1218 | #endif |
| 1219 | IF_DC(return bc_vec_item(&G.prog.strs, idx);) |
| 1220 | } |
| 1221 | |
| 1222 | static char** bc_program_const(size_t idx) |
| 1223 | { |
| 1224 | #if ENABLE_BC |
| 1225 | if (IS_BC) { |
| 1226 | BcFunc *func = bc_program_current_func(); |
| 1227 | return bc_vec_item(&func->consts, idx); |
| 1228 | } |
| 1229 | #endif |
| 1230 | IF_DC(return bc_vec_item(&G.prog.consts, idx);) |
| 1231 | } |
| 1232 | |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 1233 | static int bc_id_cmp(const void *e1, const void *e2) |
| 1234 | { |
| 1235 | return strcmp(((const BcId *) e1)->name, ((const BcId *) e2)->name); |
| 1236 | } |
| 1237 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1238 | static FAST_FUNC void bc_id_free(void *id) |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 1239 | { |
| 1240 | free(((BcId *) id)->name); |
| 1241 | } |
| 1242 | |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 1243 | static size_t bc_map_find_ge(const BcVec *v, const void *ptr) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1244 | { |
| 1245 | size_t low = 0, high = v->len; |
| 1246 | |
| 1247 | while (low < high) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1248 | size_t mid = (low + high) / 2; |
| 1249 | BcId *id = bc_vec_item(v, mid); |
| 1250 | int result = bc_id_cmp(ptr, id); |
| 1251 | |
| 1252 | if (result == 0) |
| 1253 | return mid; |
Denys Vlasenko | e3d3d20 | 2018-12-19 13:19:44 +0100 | [diff] [blame] | 1254 | if (result < 0) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1255 | high = mid; |
| 1256 | else |
| 1257 | low = mid + 1; |
| 1258 | } |
| 1259 | |
| 1260 | return low; |
| 1261 | } |
| 1262 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1263 | 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] | 1264 | { |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 1265 | size_t n = *i = bc_map_find_ge(v, ptr); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1266 | |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1267 | if (n == v->len) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1268 | bc_vec_push(v, ptr); |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1269 | else if (!bc_id_cmp(ptr, bc_vec_item(v, n))) |
| 1270 | return 0; // "was not inserted" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1271 | else |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 1272 | bc_vec_pushAt(v, ptr, n); |
| 1273 | return 1; // "was inserted" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1274 | } |
| 1275 | |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1276 | #if ENABLE_BC |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 1277 | static size_t bc_map_find_exact(const BcVec *v, const void *ptr) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1278 | { |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 1279 | size_t i = bc_map_find_ge(v, ptr); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1280 | if (i >= v->len) return BC_VEC_INVALID_IDX; |
| 1281 | return bc_id_cmp(ptr, bc_vec_item(v, i)) ? BC_VEC_INVALID_IDX : i; |
| 1282 | } |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 1283 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1284 | |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 1285 | static int bad_input_byte(char c) |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1286 | { |
| 1287 | if ((c < ' ' && c != '\t' && c != '\r' && c != '\n') // also allow '\v' '\f'? |
| 1288 | || c > 0x7e |
| 1289 | ) { |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1290 | bc_error_fmt("illegal character 0x%02x", c); |
| 1291 | return 1; |
| 1292 | } |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1293 | return 0; |
| 1294 | } |
| 1295 | |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 1296 | // Note: it _appends_ data from fp to vec. |
| 1297 | static void bc_read_line(BcVec *vec, FILE *fp) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1298 | { |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 1299 | again: |
Denys Vlasenko | 915c72b | 2018-12-13 19:28:41 +0100 | [diff] [blame] | 1300 | fflush_and_check(); |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1301 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1302 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | 915c72b | 2018-12-13 19:28:41 +0100 | [diff] [blame] | 1303 | if (G_interrupt) { // ^C was pressed |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1304 | intr: |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 1305 | if (fp != stdin) { |
| 1306 | // ^C while running a script (bc SCRIPT): die. |
| 1307 | // We do not return to interactive prompt: |
| 1308 | // user might be running us from a shell, |
| 1309 | // and SCRIPT might be intended to terminate |
| 1310 | // (e.g. contain a "halt" stmt). |
| 1311 | // ^C dropping user into a bc prompt instead of |
| 1312 | // the shell would be unexpected. |
| 1313 | xfunc_die(); |
| 1314 | } |
| 1315 | // ^C while interactive input |
Denys Vlasenko | 915c72b | 2018-12-13 19:28:41 +0100 | [diff] [blame] | 1316 | G_interrupt = 0; |
| 1317 | // GNU bc says "interrupted execution." |
| 1318 | // GNU dc says "Interrupt!" |
| 1319 | fputs("\ninterrupted execution\n", stderr); |
| 1320 | } |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 1321 | |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1322 | # if ENABLE_FEATURE_EDITING |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 1323 | if (G_ttyin && fp == stdin) { |
Denys Vlasenko | 915c72b | 2018-12-13 19:28:41 +0100 | [diff] [blame] | 1324 | int n, i; |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1325 | # define line_buf bb_common_bufsiz1 |
Denys Vlasenko | 915c72b | 2018-12-13 19:28:41 +0100 | [diff] [blame] | 1326 | n = read_line_input(G.line_input_state, "", line_buf, COMMON_BUFSIZE); |
| 1327 | if (n <= 0) { // read errors or EOF, or ^D, or ^C |
| 1328 | if (n == 0) // ^C |
| 1329 | goto intr; |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 1330 | bc_vec_pushZeroByte(vec); // ^D or EOF (or error) |
Denys Vlasenko | e755e30 | 2018-12-13 22:25:28 +0100 | [diff] [blame] | 1331 | return; |
Denys Vlasenko | 915c72b | 2018-12-13 19:28:41 +0100 | [diff] [blame] | 1332 | } |
| 1333 | i = 0; |
| 1334 | for (;;) { |
| 1335 | char c = line_buf[i++]; |
| 1336 | if (!c) break; |
| 1337 | if (bad_input_byte(c)) goto again; |
| 1338 | } |
| 1339 | bc_vec_concat(vec, line_buf); |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1340 | # undef line_buf |
Denys Vlasenko | 915c72b | 2018-12-13 19:28:41 +0100 | [diff] [blame] | 1341 | } else |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 1342 | # endif |
Denys Vlasenko | c1c2470 | 2018-12-03 16:06:02 +0100 | [diff] [blame] | 1343 | #endif |
Denys Vlasenko | 915c72b | 2018-12-13 19:28:41 +0100 | [diff] [blame] | 1344 | { |
| 1345 | int c; |
| 1346 | bool bad_chars = 0; |
| 1347 | size_t len = vec->len; |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 1348 | |
Denys Vlasenko | 915c72b | 2018-12-13 19:28:41 +0100 | [diff] [blame] | 1349 | do { |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 1350 | #if ENABLE_FEATURE_BC_SIGNALS |
| 1351 | if (G_interrupt) { |
| 1352 | // ^C was pressed: ignore entire line, get another one |
| 1353 | vec->len = len; |
Denys Vlasenko | 915c72b | 2018-12-13 19:28:41 +0100 | [diff] [blame] | 1354 | goto intr; |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 1355 | } |
Denys Vlasenko | 915c72b | 2018-12-13 19:28:41 +0100 | [diff] [blame] | 1356 | #endif |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 1357 | do c = fgetc(fp); while (c == '\0'); |
Denys Vlasenko | 915c72b | 2018-12-13 19:28:41 +0100 | [diff] [blame] | 1358 | if (c == EOF) { |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 1359 | if (ferror(fp)) |
| 1360 | bb_perror_msg_and_die("input error"); |
| 1361 | // Note: EOF does not append '\n' |
Denys Vlasenko | 915c72b | 2018-12-13 19:28:41 +0100 | [diff] [blame] | 1362 | break; |
| 1363 | } |
| 1364 | bad_chars |= bad_input_byte(c); |
| 1365 | bc_vec_pushByte(vec, (char)c); |
| 1366 | } while (c != '\n'); |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 1367 | |
Denys Vlasenko | 915c72b | 2018-12-13 19:28:41 +0100 | [diff] [blame] | 1368 | if (bad_chars) { |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 1369 | // Bad chars on this line |
| 1370 | if (!G.prog.file) { // stdin |
| 1371 | // ignore entire line, get another one |
| 1372 | vec->len = len; |
| 1373 | goto again; |
| 1374 | } |
| 1375 | bb_perror_msg_and_die("file '%s' is not text", G.prog.file); |
Denys Vlasenko | ed84935 | 2018-12-06 10:26:13 +0100 | [diff] [blame] | 1376 | } |
Denys Vlasenko | 915c72b | 2018-12-13 19:28:41 +0100 | [diff] [blame] | 1377 | bc_vec_pushZeroByte(vec); |
| 1378 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1379 | } |
| 1380 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1381 | static void bc_num_setToZero(BcNum *n, size_t scale) |
| 1382 | { |
| 1383 | n->len = 0; |
| 1384 | n->neg = false; |
| 1385 | n->rdx = scale; |
| 1386 | } |
| 1387 | |
| 1388 | static void bc_num_zero(BcNum *n) |
| 1389 | { |
| 1390 | bc_num_setToZero(n, 0); |
| 1391 | } |
| 1392 | |
| 1393 | static void bc_num_one(BcNum *n) |
| 1394 | { |
| 1395 | bc_num_setToZero(n, 0); |
| 1396 | n->len = 1; |
| 1397 | n->num[0] = 1; |
| 1398 | } |
| 1399 | |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1400 | // Note: this also sets BcNum to zero |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1401 | static void bc_num_init(BcNum *n, size_t req) |
| 1402 | { |
| 1403 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1404 | //memset(n, 0, sizeof(BcNum)); - cleared by assignments below |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1405 | n->num = xmalloc(req); |
| 1406 | n->cap = req; |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 1407 | n->rdx = 0; |
| 1408 | n->len = 0; |
| 1409 | n->neg = false; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1410 | } |
| 1411 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 1412 | static void bc_num_init_DEF_SIZE(BcNum *n) |
| 1413 | { |
| 1414 | bc_num_init(n, BC_NUM_DEF_SIZE); |
| 1415 | } |
| 1416 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1417 | static void bc_num_expand(BcNum *n, size_t req) |
| 1418 | { |
| 1419 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
| 1420 | if (req > n->cap) { |
| 1421 | n->num = xrealloc(n->num, req); |
| 1422 | n->cap = req; |
| 1423 | } |
| 1424 | } |
| 1425 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 1426 | static FAST_FUNC void bc_num_free(void *num) |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1427 | { |
| 1428 | free(((BcNum *) num)->num); |
| 1429 | } |
| 1430 | |
| 1431 | static void bc_num_copy(BcNum *d, BcNum *s) |
| 1432 | { |
| 1433 | if (d != s) { |
| 1434 | bc_num_expand(d, s->cap); |
| 1435 | d->len = s->len; |
| 1436 | d->neg = s->neg; |
| 1437 | d->rdx = s->rdx; |
| 1438 | memcpy(d->num, s->num, sizeof(BcDig) * d->len); |
| 1439 | } |
| 1440 | } |
| 1441 | |
Denys Vlasenko | a9f59db | 2018-12-22 21:52:30 +0100 | [diff] [blame] | 1442 | static BC_STATUS zbc_num_ulong_abs(BcNum *n, unsigned long *result_p) |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1443 | { |
| 1444 | size_t i; |
Denys Vlasenko | 1557b76 | 2018-12-22 21:37:46 +0100 | [diff] [blame] | 1445 | unsigned long result; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1446 | |
Denys Vlasenko | 1557b76 | 2018-12-22 21:37:46 +0100 | [diff] [blame] | 1447 | result = 0; |
| 1448 | i = n->len; |
| 1449 | while (i > n->rdx) { |
| 1450 | unsigned long prev = result; |
| 1451 | result = result * 10 + n->num[--i]; |
| 1452 | // Even overflowed N*10 can still satisfy N*10>=N. For example, |
| 1453 | // 0x1ff00000 * 10 is 0x13f600000, |
| 1454 | // or 0x3f600000 truncated to 32 bits. Which is larger. |
| 1455 | // However, (N*10)/8 < N check is always correct. |
| 1456 | if ((result / 8) < prev) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1457 | RETURN_STATUS(bc_error("overflow")); |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1458 | } |
Denys Vlasenko | ffdcebd | 2018-12-07 15:10:05 +0100 | [diff] [blame] | 1459 | *result_p = result; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1460 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1461 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1462 | } |
Denys Vlasenko | a9f59db | 2018-12-22 21:52:30 +0100 | [diff] [blame] | 1463 | #define zbc_num_ulong_abs(...) (zbc_num_ulong_abs(__VA_ARGS__) COMMA_SUCCESS) |
| 1464 | |
| 1465 | static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p) |
| 1466 | { |
| 1467 | if (n->neg) RETURN_STATUS(bc_error("negative number")); |
| 1468 | |
| 1469 | RETURN_STATUS(zbc_num_ulong_abs(n, result_p)); |
| 1470 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1471 | #define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1472 | |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 1473 | #if ULONG_MAX == 0xffffffffUL // 10 digits: 4294967295 |
| 1474 | # define ULONG_NUM_BUFSIZE (10 > BC_NUM_DEF_SIZE ? 10 : BC_NUM_DEF_SIZE) |
| 1475 | #elif ULONG_MAX == 0xffffffffffffffffULL // 20 digits: 18446744073709551615 |
| 1476 | # define ULONG_NUM_BUFSIZE (20 > BC_NUM_DEF_SIZE ? 20 : BC_NUM_DEF_SIZE) |
| 1477 | #endif |
| 1478 | // minimum BC_NUM_DEF_SIZE, so that bc_num_expand() in bc_num_ulong2num() |
| 1479 | // would not hit realloc() code path - not good if num[] is not malloced |
| 1480 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1481 | static void bc_num_ulong2num(BcNum *n, unsigned long val) |
| 1482 | { |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1483 | BcDig *ptr; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1484 | |
| 1485 | bc_num_zero(n); |
| 1486 | |
| 1487 | if (val == 0) return; |
| 1488 | |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 1489 | bc_num_expand(n, ULONG_NUM_BUFSIZE); |
Denys Vlasenko | b696d9e | 2018-12-10 12:22:15 +0100 | [diff] [blame] | 1490 | |
| 1491 | ptr = n->num; |
| 1492 | for (;;) { |
| 1493 | n->len++; |
| 1494 | *ptr++ = val % 10; |
| 1495 | val /= 10; |
| 1496 | if (val == 0) break; |
| 1497 | } |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 1498 | } |
| 1499 | |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 1500 | static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, size_t len) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1501 | { |
| 1502 | size_t i, j; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1503 | for (i = 0; i < len; ++i) { |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1504 | a[i] -= b[i]; |
| 1505 | for (j = i; a[j] < 0;) { |
| 1506 | a[j++] += 10; |
| 1507 | a[j] -= 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1508 | } |
| 1509 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | static ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len) |
| 1513 | { |
Denys Vlasenko | 4113e1f | 2018-12-18 00:39:24 +0100 | [diff] [blame] | 1514 | size_t i = len; |
| 1515 | for (;;) { |
| 1516 | int c; |
| 1517 | if (i == 0) |
| 1518 | return 0; |
| 1519 | i--; |
| 1520 | c = a[i] - b[i]; |
| 1521 | if (c != 0) { |
| 1522 | i++; |
| 1523 | if (c < 0) |
| 1524 | return -i; |
| 1525 | return i; |
| 1526 | } |
| 1527 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | static ssize_t bc_num_cmp(BcNum *a, BcNum *b) |
| 1531 | { |
| 1532 | size_t i, min, a_int, b_int, diff; |
| 1533 | BcDig *max_num, *min_num; |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1534 | bool a_max, neg; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1535 | ssize_t cmp; |
| 1536 | |
| 1537 | if (a == b) return 0; |
| 1538 | if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg); |
| 1539 | if (b->len == 0) return BC_NUM_NEG(1, a->neg); |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1540 | |
| 1541 | if (a->neg != b->neg) // signs of a and b differ |
| 1542 | // +a,-b = a>b = 1 or -a,+b = a<b = -1 |
| 1543 | return (int)b->neg - (int)a->neg; |
| 1544 | neg = a->neg; // 1 if both negative, 0 if both positive |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1545 | |
| 1546 | a_int = BC_NUM_INT(a); |
| 1547 | b_int = BC_NUM_INT(b); |
| 1548 | a_int -= b_int; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1549 | |
| 1550 | if (a_int != 0) return (ssize_t) a_int; |
| 1551 | |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1552 | a_max = (a->rdx > b->rdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1553 | if (a_max) { |
| 1554 | min = b->rdx; |
| 1555 | diff = a->rdx - b->rdx; |
| 1556 | max_num = a->num + diff; |
| 1557 | min_num = b->num; |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1558 | // neg = (a_max == neg); - NOP (maps 1->1 and 0->0) |
| 1559 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1560 | min = a->rdx; |
| 1561 | diff = b->rdx - a->rdx; |
| 1562 | max_num = b->num + diff; |
| 1563 | min_num = a->num; |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1564 | neg = !neg; // same as "neg = (a_max == neg)" |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | cmp = bc_num_compare(max_num, min_num, b_int + min); |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1568 | if (cmp != 0) return BC_NUM_NEG(cmp, neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1569 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1570 | for (max_num -= diff, i = diff - 1; i < diff; --i) { |
Denys Vlasenko | 251fbb5 | 2018-12-12 11:51:32 +0100 | [diff] [blame] | 1571 | if (max_num[i]) return BC_NUM_NEG(1, neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | return 0; |
| 1575 | } |
| 1576 | |
| 1577 | static void bc_num_truncate(BcNum *n, size_t places) |
| 1578 | { |
| 1579 | if (places == 0) return; |
| 1580 | |
| 1581 | n->rdx -= places; |
| 1582 | |
| 1583 | if (n->len != 0) { |
| 1584 | n->len -= places; |
| 1585 | memmove(n->num, n->num + places, n->len * sizeof(BcDig)); |
| 1586 | } |
| 1587 | } |
| 1588 | |
| 1589 | static void bc_num_extend(BcNum *n, size_t places) |
| 1590 | { |
| 1591 | size_t len = n->len + places; |
| 1592 | |
| 1593 | if (places != 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1594 | if (n->cap < len) bc_num_expand(n, len); |
| 1595 | |
| 1596 | memmove(n->num + places, n->num, sizeof(BcDig) * n->len); |
| 1597 | memset(n->num, 0, sizeof(BcDig) * places); |
| 1598 | |
| 1599 | n->len += places; |
| 1600 | n->rdx += places; |
| 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | static void bc_num_clean(BcNum *n) |
| 1605 | { |
| 1606 | while (n->len > 0 && n->num[n->len - 1] == 0) --n->len; |
| 1607 | if (n->len == 0) |
| 1608 | n->neg = false; |
| 1609 | else if (n->len < n->rdx) |
| 1610 | n->len = n->rdx; |
| 1611 | } |
| 1612 | |
| 1613 | static void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2) |
| 1614 | { |
| 1615 | if (n->rdx < scale) |
| 1616 | bc_num_extend(n, scale - n->rdx); |
| 1617 | else |
| 1618 | bc_num_truncate(n, n->rdx - scale); |
| 1619 | |
| 1620 | bc_num_clean(n); |
| 1621 | if (n->len != 0) n->neg = !neg1 != !neg2; |
| 1622 | } |
| 1623 | |
| 1624 | static void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a, |
| 1625 | BcNum *restrict b) |
| 1626 | { |
| 1627 | if (idx < n->len) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1628 | b->len = n->len - idx; |
| 1629 | a->len = idx; |
| 1630 | a->rdx = b->rdx = 0; |
| 1631 | |
| 1632 | memcpy(b->num, n->num + idx, b->len * sizeof(BcDig)); |
| 1633 | memcpy(a->num, n->num, idx * sizeof(BcDig)); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 1634 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1635 | bc_num_zero(b); |
| 1636 | bc_num_copy(a, n); |
| 1637 | } |
| 1638 | |
| 1639 | bc_num_clean(a); |
| 1640 | bc_num_clean(b); |
| 1641 | } |
| 1642 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1643 | static BC_STATUS zbc_num_shift(BcNum *n, size_t places) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1644 | { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1645 | if (places == 0 || n->len == 0) RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 1646 | |
| 1647 | // This check makes sense only if size_t is (much) larger than BC_MAX_NUM. |
| 1648 | if (SIZE_MAX > (BC_MAX_NUM | 0xff)) { |
| 1649 | if (places + n->len > BC_MAX_NUM) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1650 | RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]")); |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 1651 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1652 | |
| 1653 | if (n->rdx >= places) |
| 1654 | n->rdx -= places; |
| 1655 | else { |
| 1656 | bc_num_extend(n, places - n->rdx); |
| 1657 | n->rdx = 0; |
| 1658 | } |
| 1659 | |
| 1660 | bc_num_clean(n); |
| 1661 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1662 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1663 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1664 | #define zbc_num_shift(...) (zbc_num_shift(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1665 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1666 | static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1667 | { |
| 1668 | BcNum one; |
| 1669 | BcDig num[2]; |
| 1670 | |
| 1671 | one.cap = 2; |
| 1672 | one.num = num; |
| 1673 | bc_num_one(&one); |
| 1674 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1675 | RETURN_STATUS(zbc_num_div(&one, a, b, scale)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1676 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1677 | #define zbc_num_inv(...) (zbc_num_inv(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1678 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1679 | static FAST_FUNC BC_STATUS zbc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1680 | { |
| 1681 | BcDig *ptr, *ptr_a, *ptr_b, *ptr_c; |
| 1682 | size_t i, max, min_rdx, min_int, diff, a_int, b_int; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1683 | unsigned carry; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1684 | |
| 1685 | // Because this function doesn't need to use scale (per the bc spec), |
| 1686 | // I am hijacking it to say whether it's doing an add or a subtract. |
| 1687 | |
| 1688 | if (a->len == 0) { |
| 1689 | bc_num_copy(c, b); |
| 1690 | if (sub && c->len) c->neg = !c->neg; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1691 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1692 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1693 | if (b->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1694 | bc_num_copy(c, a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1695 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1696 | } |
| 1697 | |
| 1698 | c->neg = a->neg; |
| 1699 | c->rdx = BC_MAX(a->rdx, b->rdx); |
| 1700 | min_rdx = BC_MIN(a->rdx, b->rdx); |
| 1701 | c->len = 0; |
| 1702 | |
| 1703 | if (a->rdx > b->rdx) { |
| 1704 | diff = a->rdx - b->rdx; |
| 1705 | ptr = a->num; |
| 1706 | ptr_a = a->num + diff; |
| 1707 | ptr_b = b->num; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1708 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1709 | diff = b->rdx - a->rdx; |
| 1710 | ptr = b->num; |
| 1711 | ptr_a = a->num; |
| 1712 | ptr_b = b->num + diff; |
| 1713 | } |
| 1714 | |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1715 | ptr_c = c->num; |
| 1716 | for (i = 0; i < diff; ++i, ++c->len) |
| 1717 | ptr_c[i] = ptr[i]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1718 | |
| 1719 | ptr_c += diff; |
| 1720 | a_int = BC_NUM_INT(a); |
| 1721 | b_int = BC_NUM_INT(b); |
| 1722 | |
| 1723 | if (a_int > b_int) { |
| 1724 | min_int = b_int; |
| 1725 | max = a_int; |
| 1726 | ptr = ptr_a; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1727 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1728 | min_int = a_int; |
| 1729 | max = b_int; |
| 1730 | ptr = ptr_b; |
| 1731 | } |
| 1732 | |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1733 | carry = 0; |
| 1734 | for (i = 0; i < min_rdx + min_int; ++i) { |
| 1735 | unsigned in = (unsigned)ptr_a[i] + (unsigned)ptr_b[i] + carry; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1736 | carry = in / 10; |
| 1737 | ptr_c[i] = (BcDig)(in % 10); |
| 1738 | } |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1739 | for (; i < max + min_rdx; ++i) { |
| 1740 | unsigned in = (unsigned)ptr[i] + carry; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1741 | carry = in / 10; |
| 1742 | ptr_c[i] = (BcDig)(in % 10); |
| 1743 | } |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1744 | c->len += i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1745 | |
| 1746 | if (carry != 0) c->num[c->len++] = (BcDig) carry; |
| 1747 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1748 | RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary() |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1749 | } |
| 1750 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1751 | static FAST_FUNC BC_STATUS zbc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1752 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1753 | ssize_t cmp; |
| 1754 | BcNum *minuend, *subtrahend; |
| 1755 | size_t start; |
| 1756 | bool aneg, bneg, neg; |
| 1757 | |
| 1758 | // Because this function doesn't need to use scale (per the bc spec), |
| 1759 | // I am hijacking it to say whether it's doing an add or a subtract. |
| 1760 | |
| 1761 | if (a->len == 0) { |
| 1762 | bc_num_copy(c, b); |
| 1763 | if (sub && c->len) c->neg = !c->neg; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1764 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1765 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1766 | if (b->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1767 | bc_num_copy(c, a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1768 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | aneg = a->neg; |
| 1772 | bneg = b->neg; |
| 1773 | a->neg = b->neg = false; |
| 1774 | |
| 1775 | cmp = bc_num_cmp(a, b); |
| 1776 | |
| 1777 | a->neg = aneg; |
| 1778 | b->neg = bneg; |
| 1779 | |
| 1780 | if (cmp == 0) { |
| 1781 | bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx)); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1782 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1783 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1784 | if (cmp > 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1785 | neg = a->neg; |
| 1786 | minuend = a; |
| 1787 | subtrahend = b; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1788 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1789 | neg = b->neg; |
| 1790 | if (sub) neg = !neg; |
| 1791 | minuend = b; |
| 1792 | subtrahend = a; |
| 1793 | } |
| 1794 | |
| 1795 | bc_num_copy(c, minuend); |
| 1796 | c->neg = neg; |
| 1797 | |
| 1798 | if (c->rdx < subtrahend->rdx) { |
| 1799 | bc_num_extend(c, subtrahend->rdx - c->rdx); |
| 1800 | start = 0; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1801 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1802 | start = c->rdx - subtrahend->rdx; |
| 1803 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1804 | bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1805 | |
| 1806 | bc_num_clean(c); |
| 1807 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1808 | RETURN_STATUS(BC_STATUS_SUCCESS); // can't make void, see zbc_num_binary() |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1809 | } |
| 1810 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1811 | static FAST_FUNC BC_STATUS zbc_num_k(BcNum *restrict a, BcNum *restrict b, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1812 | BcNum *restrict c) |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1813 | #define zbc_num_k(...) (zbc_num_k(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1814 | { |
| 1815 | BcStatus s; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1816 | 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] | 1817 | BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1818 | bool aone; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1819 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1820 | if (a->len == 0 || b->len == 0) { |
| 1821 | bc_num_zero(c); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1822 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1823 | } |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1824 | aone = BC_NUM_ONE(a); |
| 1825 | if (aone || BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1826 | bc_num_copy(c, aone ? b : a); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1827 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1828 | } |
| 1829 | |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1830 | if (a->len + b->len < BC_NUM_KARATSUBA_LEN |
| 1831 | || a->len < BC_NUM_KARATSUBA_LEN |
| 1832 | || b->len < BC_NUM_KARATSUBA_LEN |
| 1833 | ) { |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1834 | size_t i, j, len; |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1835 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1836 | bc_num_expand(c, a->len + b->len + 1); |
| 1837 | |
| 1838 | memset(c->num, 0, sizeof(BcDig) * c->cap); |
Denys Vlasenko | b692c2f | 2018-12-05 18:56:14 +0100 | [diff] [blame] | 1839 | c->len = len = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1840 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1841 | for (i = 0; i < b->len; ++i) { |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1842 | unsigned carry = 0; |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 1843 | for (j = 0; j < a->len; ++j) { |
Denys Vlasenko | b3cb901 | 2018-12-05 19:05:32 +0100 | [diff] [blame] | 1844 | unsigned in = c->num[i + j]; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 1845 | in += (unsigned)a->num[j] * (unsigned)b->num[i] + carry; |
Denys Vlasenko | b3cb901 | 2018-12-05 19:05:32 +0100 | [diff] [blame] | 1846 | // note: compilers prefer _unsigned_ div/const |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1847 | carry = in / 10; |
| 1848 | c->num[i + j] = (BcDig)(in % 10); |
| 1849 | } |
| 1850 | |
| 1851 | c->num[i + j] += (BcDig) carry; |
| 1852 | len = BC_MAX(len, i + j + !!carry); |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 1853 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1854 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 1855 | // a=2^1000000 |
| 1856 | // a*a <- without check below, this will not be interruptible |
| 1857 | if (G_interrupt) return BC_STATUS_FAILURE; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1858 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1859 | } |
| 1860 | |
| 1861 | c->len = len; |
| 1862 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1863 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1864 | } |
| 1865 | |
| 1866 | bc_num_init(&l1, max); |
| 1867 | bc_num_init(&h1, max); |
| 1868 | bc_num_init(&l2, max); |
| 1869 | bc_num_init(&h2, max); |
| 1870 | bc_num_init(&m1, max); |
| 1871 | bc_num_init(&m2, max); |
| 1872 | bc_num_init(&z0, max); |
| 1873 | bc_num_init(&z1, max); |
| 1874 | bc_num_init(&z2, max); |
| 1875 | bc_num_init(&temp, max + max); |
| 1876 | |
| 1877 | bc_num_split(a, max2, &l1, &h1); |
| 1878 | bc_num_split(b, max2, &l2, &h2); |
| 1879 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1880 | s = zbc_num_add(&h1, &l1, &m1, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1881 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1882 | s = zbc_num_add(&h2, &l2, &m2, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1883 | if (s) goto err; |
| 1884 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1885 | s = zbc_num_k(&h1, &h2, &z0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1886 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1887 | s = zbc_num_k(&m1, &m2, &z1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1888 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1889 | s = zbc_num_k(&l1, &l2, &z2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1890 | if (s) goto err; |
| 1891 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1892 | s = zbc_num_sub(&z1, &z0, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1893 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1894 | s = zbc_num_sub(&temp, &z2, &z1, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1895 | if (s) goto err; |
| 1896 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1897 | s = zbc_num_shift(&z0, max2 * 2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1898 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1899 | s = zbc_num_shift(&z1, max2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1900 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1901 | s = zbc_num_add(&z0, &z1, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1902 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1903 | s = zbc_num_add(&temp, &z2, c, 0); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 1904 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1905 | bc_num_free(&temp); |
| 1906 | bc_num_free(&z2); |
| 1907 | bc_num_free(&z1); |
| 1908 | bc_num_free(&z0); |
| 1909 | bc_num_free(&m2); |
| 1910 | bc_num_free(&m1); |
| 1911 | bc_num_free(&h2); |
| 1912 | bc_num_free(&l2); |
| 1913 | bc_num_free(&h1); |
| 1914 | bc_num_free(&l1); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1915 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1916 | } |
| 1917 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1918 | static FAST_FUNC BC_STATUS zbc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1919 | { |
| 1920 | BcStatus s; |
| 1921 | BcNum cpa, cpb; |
| 1922 | size_t maxrdx = BC_MAX(a->rdx, b->rdx); |
| 1923 | |
| 1924 | scale = BC_MAX(scale, a->rdx); |
| 1925 | scale = BC_MAX(scale, b->rdx); |
| 1926 | scale = BC_MIN(a->rdx + b->rdx, scale); |
| 1927 | maxrdx = BC_MAX(maxrdx, scale); |
| 1928 | |
| 1929 | bc_num_init(&cpa, a->len); |
| 1930 | bc_num_init(&cpb, b->len); |
| 1931 | |
| 1932 | bc_num_copy(&cpa, a); |
| 1933 | bc_num_copy(&cpb, b); |
| 1934 | cpa.neg = cpb.neg = false; |
| 1935 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1936 | s = zbc_num_shift(&cpa, maxrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1937 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 1938 | s = zbc_num_shift(&cpb, maxrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1939 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 1940 | s = zbc_num_k(&cpa, &cpb, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1941 | if (s) goto err; |
| 1942 | |
| 1943 | maxrdx += scale; |
| 1944 | bc_num_expand(c, c->len + maxrdx); |
| 1945 | |
| 1946 | if (c->len < maxrdx) { |
| 1947 | memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig)); |
| 1948 | c->len += maxrdx; |
| 1949 | } |
| 1950 | |
| 1951 | c->rdx = maxrdx; |
| 1952 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 1953 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1954 | bc_num_free(&cpb); |
| 1955 | bc_num_free(&cpa); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1956 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1957 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 1958 | #define zbc_num_m(...) (zbc_num_m(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1959 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1960 | static FAST_FUNC BC_STATUS zbc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1961 | { |
Denys Vlasenko | 5c0c5ab | 2018-12-18 13:15:55 +0100 | [diff] [blame] | 1962 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1963 | size_t len, end, i; |
| 1964 | BcNum cp; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1965 | |
| 1966 | if (b->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1967 | RETURN_STATUS(bc_error("divide by zero")); |
| 1968 | if (a->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1969 | bc_num_setToZero(c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1970 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1971 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1972 | if (BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1973 | bc_num_copy(c, a); |
| 1974 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 1975 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1976 | } |
| 1977 | |
| 1978 | bc_num_init(&cp, BC_NUM_MREQ(a, b, scale)); |
| 1979 | bc_num_copy(&cp, a); |
| 1980 | len = b->len; |
| 1981 | |
| 1982 | if (len > cp.len) { |
| 1983 | bc_num_expand(&cp, len + 2); |
| 1984 | bc_num_extend(&cp, len - cp.len); |
| 1985 | } |
| 1986 | |
| 1987 | if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx); |
| 1988 | cp.rdx -= b->rdx; |
| 1989 | if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx); |
| 1990 | |
| 1991 | if (b->rdx == b->len) { |
Denys Vlasenko | 71c82d1 | 2018-12-18 12:43:21 +0100 | [diff] [blame] | 1992 | for (;;) { |
| 1993 | if (len == 0) break; |
| 1994 | len--; |
| 1995 | if (b->num[len] != 0) |
| 1996 | break; |
| 1997 | } |
| 1998 | len++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 1999 | } |
| 2000 | |
| 2001 | if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1); |
| 2002 | |
| 2003 | // We want an extra zero in front to make things simpler. |
| 2004 | cp.num[cp.len++] = 0; |
| 2005 | end = cp.len - len; |
| 2006 | |
| 2007 | bc_num_expand(c, cp.len); |
| 2008 | |
| 2009 | bc_num_zero(c); |
| 2010 | memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig)); |
| 2011 | c->rdx = cp.rdx; |
| 2012 | c->len = cp.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2013 | |
Denys Vlasenko | 5c0c5ab | 2018-12-18 13:15:55 +0100 | [diff] [blame] | 2014 | s = BC_STATUS_SUCCESS; |
| 2015 | for (i = end - 1; i < end; --i) { |
| 2016 | BcDig *n, q; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2017 | n = cp.num + i; |
Denys Vlasenko | 5c0c5ab | 2018-12-18 13:15:55 +0100 | [diff] [blame] | 2018 | for (q = 0; n[len] != 0 || bc_num_compare(n, b->num, len) >= 0; ++q) |
| 2019 | bc_num_subArrays(n, b->num, len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2020 | c->num[i] = q; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2021 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 2022 | // a=2^100000 |
| 2023 | // scale=40000 |
| 2024 | // 1/a <- without check below, this will not be interruptible |
| 2025 | if (G_interrupt) { |
| 2026 | s = BC_STATUS_FAILURE; |
| 2027 | break; |
| 2028 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2029 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2030 | } |
| 2031 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2032 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2033 | bc_num_free(&cp); |
| 2034 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2035 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2036 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2037 | #define zbc_num_d(...) (zbc_num_d(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2038 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2039 | static FAST_FUNC BC_STATUS zbc_num_r(BcNum *a, BcNum *b, BcNum *restrict c, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2040 | BcNum *restrict d, size_t scale, size_t ts) |
| 2041 | { |
| 2042 | BcStatus s; |
| 2043 | BcNum temp; |
| 2044 | bool neg; |
| 2045 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2046 | if (b->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2047 | RETURN_STATUS(bc_error("divide by zero")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2048 | |
| 2049 | if (a->len == 0) { |
| 2050 | bc_num_setToZero(d, ts); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2051 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2052 | } |
| 2053 | |
| 2054 | bc_num_init(&temp, d->cap); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2055 | s = zbc_num_d(a, b, c, scale); |
Denys Vlasenko | f381a88 | 2018-12-05 19:21:34 +0100 | [diff] [blame] | 2056 | if (s) goto err; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2057 | |
| 2058 | if (scale != 0) scale = ts; |
| 2059 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2060 | s = zbc_num_m(c, b, &temp, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2061 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2062 | s = zbc_num_sub(a, &temp, d, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2063 | if (s) goto err; |
| 2064 | |
| 2065 | if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx); |
| 2066 | |
| 2067 | neg = d->neg; |
| 2068 | bc_num_retireMul(d, ts, a->neg, b->neg); |
| 2069 | d->neg = neg; |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 2070 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2071 | bc_num_free(&temp); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2072 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2073 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2074 | #define zbc_num_r(...) (zbc_num_r(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2075 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2076 | static FAST_FUNC BC_STATUS zbc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2077 | { |
| 2078 | BcStatus s; |
| 2079 | BcNum c1; |
| 2080 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 2081 | |
| 2082 | bc_num_init(&c1, len); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2083 | s = zbc_num_r(a, b, &c1, c, scale, ts); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2084 | bc_num_free(&c1); |
| 2085 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2086 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2087 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2088 | #define zbc_num_rem(...) (zbc_num_rem(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2089 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2090 | static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2091 | { |
| 2092 | BcStatus s = BC_STATUS_SUCCESS; |
| 2093 | BcNum copy; |
| 2094 | unsigned long pow; |
| 2095 | size_t i, powrdx, resrdx; |
Denys Vlasenko | 6b0fbd1 | 2018-12-18 12:55:40 +0100 | [diff] [blame] | 2096 | bool neg; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2097 | |
Denys Vlasenko | 1acac7f | 2018-12-22 23:14:48 +0100 | [diff] [blame^] | 2098 | // GNU bc does not allow 2^2.0 - we do |
| 2099 | for (i = 0; i < b->rdx; i++) |
| 2100 | if (b->num[i] != 0) |
| 2101 | RETURN_STATUS(bc_error("not an integer")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2102 | |
| 2103 | if (b->len == 0) { |
| 2104 | bc_num_one(c); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2105 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2106 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2107 | if (a->len == 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2108 | bc_num_setToZero(c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2109 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2110 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2111 | if (BC_NUM_ONE(b)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2112 | if (!b->neg) |
| 2113 | bc_num_copy(c, a); |
| 2114 | else |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2115 | s = zbc_num_inv(a, c, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2116 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2117 | } |
| 2118 | |
| 2119 | neg = b->neg; |
Denys Vlasenko | a9f59db | 2018-12-22 21:52:30 +0100 | [diff] [blame] | 2120 | s = zbc_num_ulong_abs(b, &pow); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2121 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 2ea8ddf | 2018-12-22 21:45:18 +0100 | [diff] [blame] | 2122 | // b is not used beyond this point |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2123 | |
| 2124 | bc_num_init(©, a->len); |
| 2125 | bc_num_copy(©, a); |
| 2126 | |
Denys Vlasenko | 2d615fe | 2018-12-07 16:22:45 +0100 | [diff] [blame] | 2127 | if (!neg) { |
| 2128 | if (a->rdx > scale) |
| 2129 | scale = a->rdx; |
| 2130 | if (a->rdx * pow < scale) |
| 2131 | scale = a->rdx * pow; |
| 2132 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2133 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2134 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2135 | for (powrdx = a->rdx; !(pow & 1); pow >>= 1) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2136 | powrdx <<= 1; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2137 | s = zbc_num_mul(©, ©, ©, powrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2138 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2139 | // Not needed: zbc_num_mul() has a check for ^C: |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 2140 | //if (G_interrupt) { |
| 2141 | // s = BC_STATUS_FAILURE; |
| 2142 | // goto err; |
| 2143 | //} |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2144 | } |
| 2145 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2146 | bc_num_copy(c, ©); |
| 2147 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2148 | for (resrdx = powrdx, pow >>= 1; pow != 0; pow >>= 1) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2149 | powrdx <<= 1; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2150 | s = zbc_num_mul(©, ©, ©, powrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2151 | if (s) goto err; |
| 2152 | |
| 2153 | if (pow & 1) { |
| 2154 | resrdx += powrdx; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2155 | s = zbc_num_mul(c, ©, c, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2156 | if (s) goto err; |
| 2157 | } |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2158 | // Not needed: zbc_num_mul() has a check for ^C: |
Denys Vlasenko | 06fa65b | 2018-12-05 19:00:58 +0100 | [diff] [blame] | 2159 | //if (G_interrupt) { |
| 2160 | // s = BC_STATUS_FAILURE; |
| 2161 | // goto err; |
| 2162 | //} |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2163 | } |
| 2164 | |
| 2165 | if (neg) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2166 | s = zbc_num_inv(c, c, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2167 | if (s) goto err; |
| 2168 | } |
| 2169 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2170 | if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale); |
| 2171 | |
| 2172 | // We can't use bc_num_clean() here. |
Denys Vlasenko | 6b0fbd1 | 2018-12-18 12:55:40 +0100 | [diff] [blame] | 2173 | for (i = 0; i < c->len; ++i) |
| 2174 | if (c->num[i] != 0) |
| 2175 | goto skip; |
| 2176 | bc_num_setToZero(c, scale); |
| 2177 | skip: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2178 | |
Denys Vlasenko | 6b0fbd1 | 2018-12-18 12:55:40 +0100 | [diff] [blame] | 2179 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2180 | bc_num_free(©); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2181 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2182 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2183 | #define zbc_num_p(...) (zbc_num_p(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2184 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2185 | static BC_STATUS zbc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2186 | BcNumBinaryOp op, size_t req) |
| 2187 | { |
| 2188 | BcStatus s; |
| 2189 | BcNum num2, *ptr_a, *ptr_b; |
| 2190 | bool init = false; |
| 2191 | |
| 2192 | if (c == a) { |
| 2193 | ptr_a = &num2; |
| 2194 | memcpy(ptr_a, c, sizeof(BcNum)); |
| 2195 | init = true; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2196 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2197 | ptr_a = a; |
| 2198 | |
| 2199 | if (c == b) { |
| 2200 | ptr_b = &num2; |
| 2201 | if (c != a) { |
| 2202 | memcpy(ptr_b, c, sizeof(BcNum)); |
| 2203 | init = true; |
| 2204 | } |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2205 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2206 | ptr_b = b; |
| 2207 | |
| 2208 | if (init) |
| 2209 | bc_num_init(c, req); |
| 2210 | else |
| 2211 | bc_num_expand(c, req); |
| 2212 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2213 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2214 | IF_ERROR_RETURN_POSSIBLE(s =) op(ptr_a, ptr_b, c, scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2215 | |
| 2216 | if (init) bc_num_free(&num2); |
| 2217 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2218 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2219 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2220 | #define zbc_num_binary(...) (zbc_num_binary(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2221 | |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2222 | static bool bc_num_strValid(const char *val, size_t base) |
| 2223 | { |
| 2224 | BcDig b; |
| 2225 | bool radix; |
| 2226 | |
| 2227 | b = (BcDig)(base <= 10 ? base + '0' : base - 10 + 'A'); |
| 2228 | radix = false; |
| 2229 | for (;;) { |
| 2230 | BcDig c = *val++; |
| 2231 | if (c == '\0') |
| 2232 | break; |
| 2233 | if (c == '.') { |
| 2234 | if (radix) return false; |
| 2235 | radix = true; |
| 2236 | continue; |
| 2237 | } |
| 2238 | if (c < '0' || c >= b || (c > '9' && c < 'A')) |
| 2239 | return false; |
| 2240 | } |
| 2241 | return true; |
| 2242 | } |
| 2243 | |
| 2244 | // Note: n is already "bc_num_zero()"ed, |
| 2245 | // leading zeroes in "val" are removed |
| 2246 | static void bc_num_parseDecimal(BcNum *n, const char *val) |
| 2247 | { |
| 2248 | size_t len, i; |
| 2249 | const char *ptr; |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2250 | |
| 2251 | len = strlen(val); |
| 2252 | if (len == 0) |
| 2253 | return; |
| 2254 | |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2255 | bc_num_expand(n, len); |
| 2256 | |
| 2257 | ptr = strchr(val, '.'); |
| 2258 | |
| 2259 | n->rdx = 0; |
| 2260 | if (ptr != NULL) |
| 2261 | n->rdx = (size_t)((val + len) - (ptr + 1)); |
| 2262 | |
Denys Vlasenko | dafbc2c | 2018-12-10 15:38:52 +0100 | [diff] [blame] | 2263 | for (i = 0; val[i]; ++i) { |
| 2264 | if (val[i] != '0' && val[i] != '.') { |
| 2265 | // Not entirely zero value - convert it, and exit |
| 2266 | i = len - 1; |
| 2267 | for (;;) { |
| 2268 | n->num[n->len] = val[i] - '0'; |
| 2269 | ++n->len; |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2270 | skip_dot: |
Denys Vlasenko | 87b49be | 2018-12-14 16:24:01 +0100 | [diff] [blame] | 2271 | if (i == 0) break; |
| 2272 | if (val[--i] == '.') goto skip_dot; |
Denys Vlasenko | dafbc2c | 2018-12-10 15:38:52 +0100 | [diff] [blame] | 2273 | } |
| 2274 | break; |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2275 | } |
| 2276 | } |
Denys Vlasenko | 87b49be | 2018-12-14 16:24:01 +0100 | [diff] [blame] | 2277 | // if for() exits without hitting if(), the value is entirely zero |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2278 | } |
| 2279 | |
| 2280 | // Note: n is already "bc_num_zero()"ed, |
| 2281 | // leading zeroes in "val" are removed |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 2282 | static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t) |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2283 | { |
| 2284 | BcStatus s; |
| 2285 | BcNum temp, mult, result; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 2286 | BcNum base; |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 2287 | BcDig base_digs[ULONG_NUM_BUFSIZE]; |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2288 | BcDig c = '\0'; |
| 2289 | unsigned long v; |
| 2290 | size_t i, digits; |
| 2291 | |
| 2292 | for (i = 0; ; ++i) { |
| 2293 | if (val[i] == '\0') |
| 2294 | return; |
| 2295 | if (val[i] != '.' && val[i] != '0') |
| 2296 | break; |
| 2297 | } |
| 2298 | |
| 2299 | bc_num_init_DEF_SIZE(&temp); |
| 2300 | bc_num_init_DEF_SIZE(&mult); |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 2301 | base.cap = ARRAY_SIZE(base_digs); |
| 2302 | base.num = base_digs; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 2303 | bc_num_ulong2num(&base, base_t); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2304 | |
| 2305 | for (;;) { |
| 2306 | c = *val++; |
| 2307 | if (c == '\0') goto int_err; |
| 2308 | if (c == '.') break; |
| 2309 | |
| 2310 | v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10); |
| 2311 | |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 2312 | s = zbc_num_mul(n, &base, &mult, 0); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2313 | if (s) goto int_err; |
| 2314 | bc_num_ulong2num(&temp, v); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2315 | s = zbc_num_add(&mult, &temp, n, 0); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2316 | if (s) goto int_err; |
| 2317 | } |
| 2318 | |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 2319 | bc_num_init(&result, base.len); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2320 | //bc_num_zero(&result); - already is |
| 2321 | bc_num_one(&mult); |
| 2322 | |
| 2323 | digits = 0; |
| 2324 | for (;;) { |
| 2325 | c = *val++; |
| 2326 | if (c == '\0') break; |
| 2327 | digits++; |
| 2328 | |
| 2329 | v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10); |
| 2330 | |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 2331 | s = zbc_num_mul(&result, &base, &result, 0); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2332 | if (s) goto err; |
| 2333 | bc_num_ulong2num(&temp, v); |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2334 | s = zbc_num_add(&result, &temp, &result, 0); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2335 | if (s) goto err; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 2336 | s = zbc_num_mul(&mult, &base, &mult, 0); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2337 | if (s) goto err; |
| 2338 | } |
| 2339 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2340 | s = zbc_num_div(&result, &mult, &result, digits); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2341 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2342 | s = zbc_num_add(n, &result, n, digits); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2343 | if (s) goto err; |
| 2344 | |
| 2345 | if (n->len != 0) { |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 2346 | if (n->rdx < digits) |
| 2347 | bc_num_extend(n, digits - n->rdx); |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2348 | } else |
| 2349 | bc_num_zero(n); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 2350 | err: |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2351 | bc_num_free(&result); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 2352 | int_err: |
Denys Vlasenko | 9311e01 | 2018-12-10 11:54:18 +0100 | [diff] [blame] | 2353 | bc_num_free(&mult); |
| 2354 | bc_num_free(&temp); |
| 2355 | } |
| 2356 | |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 2357 | static BC_STATUS zbc_num_parse(BcNum *n, const char *val, unsigned base_t) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2358 | { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2359 | if (!bc_num_strValid(val, base_t)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2360 | RETURN_STATUS(bc_error("bad number string")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2361 | |
Denys Vlasenko | 4a024c7 | 2018-12-09 13:21:54 +0100 | [diff] [blame] | 2362 | bc_num_zero(n); |
| 2363 | while (*val == '0') val++; |
| 2364 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2365 | if (base_t == 10) |
| 2366 | bc_num_parseDecimal(n, val); |
| 2367 | else |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 2368 | bc_num_parseBase(n, val, base_t); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2369 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2370 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2371 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2372 | #define zbc_num_parse(...) (zbc_num_parse(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2373 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2374 | static BC_STATUS zbc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2375 | { |
| 2376 | BcStatus s; |
| 2377 | BcNum num1, num2, half, f, fprime, *x0, *x1, *temp; |
| 2378 | size_t pow, len, digs, digs1, resrdx, req, times = 0; |
| 2379 | ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX; |
| 2380 | |
| 2381 | req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1; |
| 2382 | bc_num_expand(b, req); |
| 2383 | |
| 2384 | if (a->len == 0) { |
| 2385 | bc_num_setToZero(b, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2386 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 2387 | } |
| 2388 | if (a->neg) { |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2389 | RETURN_STATUS(bc_error("negative number")); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 2390 | } |
| 2391 | if (BC_NUM_ONE(a)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2392 | bc_num_one(b); |
| 2393 | bc_num_extend(b, scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2394 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2395 | } |
| 2396 | |
| 2397 | scale = BC_MAX(scale, a->rdx) + 1; |
| 2398 | len = a->len + scale; |
| 2399 | |
| 2400 | bc_num_init(&num1, len); |
| 2401 | bc_num_init(&num2, len); |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 2402 | bc_num_init_DEF_SIZE(&half); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2403 | |
| 2404 | bc_num_one(&half); |
| 2405 | half.num[0] = 5; |
| 2406 | half.rdx = 1; |
| 2407 | |
| 2408 | bc_num_init(&f, len); |
| 2409 | bc_num_init(&fprime, len); |
| 2410 | |
| 2411 | x0 = &num1; |
| 2412 | x1 = &num2; |
| 2413 | |
| 2414 | bc_num_one(x0); |
| 2415 | pow = BC_NUM_INT(a); |
| 2416 | |
| 2417 | if (pow) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2418 | if (pow & 1) |
| 2419 | x0->num[0] = 2; |
| 2420 | else |
| 2421 | x0->num[0] = 6; |
| 2422 | |
| 2423 | pow -= 2 - (pow & 1); |
| 2424 | |
| 2425 | bc_num_extend(x0, pow); |
| 2426 | |
| 2427 | // Make sure to move the radix back. |
| 2428 | x0->rdx -= pow; |
| 2429 | } |
| 2430 | |
| 2431 | x0->rdx = digs = digs1 = 0; |
| 2432 | resrdx = scale + 2; |
| 2433 | len = BC_NUM_INT(x0) + resrdx - 1; |
| 2434 | |
Denys Vlasenko | 71e1fc6 | 2018-12-02 19:43:34 +0100 | [diff] [blame] | 2435 | while (cmp != 0 || digs < len) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2436 | s = zbc_num_div(a, x0, &f, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2437 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2438 | s = zbc_num_add(x0, &f, &fprime, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2439 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2440 | s = zbc_num_mul(&fprime, &half, x1, resrdx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2441 | if (s) goto err; |
| 2442 | |
| 2443 | cmp = bc_num_cmp(x1, x0); |
| 2444 | digs = x1->len - (unsigned long long) llabs(cmp); |
| 2445 | |
| 2446 | if (cmp == cmp2 && digs == digs1) |
| 2447 | times += 1; |
| 2448 | else |
| 2449 | times = 0; |
| 2450 | |
| 2451 | resrdx += times > 4; |
| 2452 | |
| 2453 | cmp2 = cmp1; |
| 2454 | cmp1 = cmp; |
| 2455 | digs1 = digs; |
| 2456 | |
| 2457 | temp = x0; |
| 2458 | x0 = x1; |
| 2459 | x1 = temp; |
| 2460 | } |
| 2461 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2462 | bc_num_copy(b, x0); |
| 2463 | scale -= 1; |
| 2464 | if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale); |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2465 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2466 | bc_num_free(&fprime); |
| 2467 | bc_num_free(&f); |
| 2468 | bc_num_free(&half); |
| 2469 | bc_num_free(&num2); |
| 2470 | bc_num_free(&num1); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2471 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2472 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2473 | #define zbc_num_sqrt(...) (zbc_num_sqrt(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2474 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2475 | static BC_STATUS zbc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2476 | size_t scale) |
| 2477 | { |
| 2478 | BcStatus s; |
| 2479 | BcNum num2, *ptr_a; |
| 2480 | bool init = false; |
| 2481 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 2482 | |
| 2483 | if (c == a) { |
| 2484 | memcpy(&num2, c, sizeof(BcNum)); |
| 2485 | ptr_a = &num2; |
| 2486 | bc_num_init(c, len); |
| 2487 | init = true; |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2488 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2489 | ptr_a = a; |
| 2490 | bc_num_expand(c, len); |
| 2491 | } |
| 2492 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2493 | s = zbc_num_r(ptr_a, b, c, d, scale, ts); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2494 | |
| 2495 | if (init) bc_num_free(&num2); |
| 2496 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2497 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2498 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2499 | #define zbc_num_divmod(...) (zbc_num_divmod(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2500 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2501 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 2502 | static BC_STATUS zdc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2503 | { |
| 2504 | BcStatus s; |
| 2505 | BcNum base, exp, two, temp; |
| 2506 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2507 | if (c->len == 0) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2508 | RETURN_STATUS(bc_error("divide by zero")); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2509 | if (a->rdx || b->rdx || c->rdx) |
Denys Vlasenko | 1acac7f | 2018-12-22 23:14:48 +0100 | [diff] [blame^] | 2510 | RETURN_STATUS(bc_error("not an integer")); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 2511 | if (b->neg) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2512 | RETURN_STATUS(bc_error("negative number")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2513 | |
| 2514 | bc_num_expand(d, c->len); |
| 2515 | bc_num_init(&base, c->len); |
| 2516 | bc_num_init(&exp, b->len); |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 2517 | bc_num_init_DEF_SIZE(&two); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2518 | bc_num_init(&temp, b->len); |
| 2519 | |
| 2520 | bc_num_one(&two); |
| 2521 | two.num[0] = 2; |
| 2522 | bc_num_one(d); |
| 2523 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2524 | s = zbc_num_rem(a, c, &base, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2525 | if (s) goto err; |
| 2526 | bc_num_copy(&exp, b); |
| 2527 | |
| 2528 | while (exp.len != 0) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2529 | s = zbc_num_divmod(&exp, &two, &exp, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2530 | if (s) goto err; |
| 2531 | |
| 2532 | if (BC_NUM_ONE(&temp)) { |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2533 | s = zbc_num_mul(d, &base, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2534 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2535 | s = zbc_num_rem(&temp, c, d, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2536 | if (s) goto err; |
| 2537 | } |
| 2538 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2539 | s = zbc_num_mul(&base, &base, &temp, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2540 | if (s) goto err; |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 2541 | s = zbc_num_rem(&temp, c, &base, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2542 | if (s) goto err; |
| 2543 | } |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2544 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2545 | bc_num_free(&temp); |
| 2546 | bc_num_free(&two); |
| 2547 | bc_num_free(&exp); |
| 2548 | bc_num_free(&base); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 2549 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2550 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 2551 | #define zdc_num_modexp(...) (zdc_num_modexp(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2552 | #endif // ENABLE_DC |
| 2553 | |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 2554 | #if ENABLE_BC |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2555 | static BC_STATUS zbc_func_insert(BcFunc *f, char *name, bool var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2556 | { |
Denys Vlasenko | 87888ce | 2018-12-19 17:55:23 +0100 | [diff] [blame] | 2557 | BcId *autoid; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2558 | BcId a; |
| 2559 | size_t i; |
| 2560 | |
Denys Vlasenko | 87888ce | 2018-12-19 17:55:23 +0100 | [diff] [blame] | 2561 | autoid = (void*)f->autos.v; |
| 2562 | for (i = 0; i < f->autos.len; i++, autoid++) { |
| 2563 | if (strcmp(name, autoid->name) == 0) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2564 | RETURN_STATUS(bc_error("function parameter or auto var has the same name as another")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2565 | } |
| 2566 | |
| 2567 | a.idx = var; |
| 2568 | a.name = name; |
| 2569 | |
| 2570 | bc_vec_push(&f->autos, &a); |
| 2571 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2572 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2573 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2574 | #define zbc_func_insert(...) (zbc_func_insert(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 23c2e9f | 2018-12-06 11:43:17 +0100 | [diff] [blame] | 2575 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2576 | |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 2577 | static FAST_FUNC void bc_string_free(void *string) |
| 2578 | { |
| 2579 | free(*(char**)string); |
| 2580 | } |
| 2581 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2582 | static void bc_func_init(BcFunc *f) |
| 2583 | { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 2584 | bc_char_vec_init(&f->code); |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 2585 | IF_BC(bc_vec_init(&f->labels, sizeof(size_t), NULL);) |
| 2586 | IF_BC(bc_vec_init(&f->autos, sizeof(BcId), bc_id_free);) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 2587 | IF_BC(bc_vec_init(&f->strs, sizeof(char *), bc_string_free);) |
| 2588 | IF_BC(bc_vec_init(&f->consts, sizeof(char *), bc_string_free);) |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 2589 | IF_BC(f->nparams = 0;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2590 | } |
| 2591 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2592 | static FAST_FUNC void bc_func_free(void *func) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2593 | { |
| 2594 | BcFunc *f = (BcFunc *) func; |
| 2595 | bc_vec_free(&f->code); |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 2596 | IF_BC(bc_vec_free(&f->labels);) |
| 2597 | IF_BC(bc_vec_free(&f->autos);) |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 2598 | IF_BC(bc_vec_free(&f->strs);) |
| 2599 | IF_BC(bc_vec_free(&f->consts);) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2600 | } |
| 2601 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2602 | static void bc_array_expand(BcVec *a, size_t len); |
| 2603 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2604 | static void bc_array_init(BcVec *a, bool nums) |
| 2605 | { |
| 2606 | if (nums) |
| 2607 | bc_vec_init(a, sizeof(BcNum), bc_num_free); |
| 2608 | else |
| 2609 | bc_vec_init(a, sizeof(BcVec), bc_vec_free); |
| 2610 | bc_array_expand(a, 1); |
| 2611 | } |
| 2612 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2613 | static void bc_array_expand(BcVec *a, size_t len) |
| 2614 | { |
Denys Vlasenko | d6e24bd | 2018-12-18 20:10:48 +0100 | [diff] [blame] | 2615 | if (a->dtor == bc_num_free |
| 2616 | // && a->size == sizeof(BcNum) - always true |
| 2617 | ) { |
| 2618 | BcNum n; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2619 | while (len > a->len) { |
Denys Vlasenko | d6e24bd | 2018-12-18 20:10:48 +0100 | [diff] [blame] | 2620 | bc_num_init_DEF_SIZE(&n); |
| 2621 | bc_vec_push(a, &n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2622 | } |
Denys Vlasenko | e2e6ffd | 2018-12-18 12:23:16 +0100 | [diff] [blame] | 2623 | } else { |
Denys Vlasenko | d6e24bd | 2018-12-18 20:10:48 +0100 | [diff] [blame] | 2624 | BcVec v; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2625 | while (len > a->len) { |
Denys Vlasenko | d6e24bd | 2018-12-18 20:10:48 +0100 | [diff] [blame] | 2626 | bc_array_init(&v, true); |
| 2627 | bc_vec_push(a, &v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2628 | } |
| 2629 | } |
| 2630 | } |
| 2631 | |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2632 | static void bc_array_copy(BcVec *d, const BcVec *s) |
| 2633 | { |
Denys Vlasenko | eac0de5 | 2018-12-19 17:59:30 +0100 | [diff] [blame] | 2634 | BcNum *dnum, *snum; |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2635 | size_t i; |
| 2636 | |
| 2637 | bc_vec_pop_all(d); |
| 2638 | bc_vec_expand(d, s->cap); |
| 2639 | d->len = s->len; |
| 2640 | |
Denys Vlasenko | eac0de5 | 2018-12-19 17:59:30 +0100 | [diff] [blame] | 2641 | dnum = (void*)d->v; |
| 2642 | snum = (void*)s->v; |
| 2643 | for (i = 0; i < s->len; i++, dnum++, snum++) { |
Denys Vlasenko | b0e3761 | 2018-12-05 21:03:16 +0100 | [diff] [blame] | 2644 | bc_num_init(dnum, snum->len); |
| 2645 | bc_num_copy(dnum, snum); |
| 2646 | } |
| 2647 | } |
| 2648 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2649 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 2650 | static void dc_result_copy(BcResult *d, BcResult *src) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2651 | { |
| 2652 | d->t = src->t; |
| 2653 | |
| 2654 | switch (d->t) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2655 | case BC_RESULT_TEMP: |
| 2656 | case BC_RESULT_IBASE: |
| 2657 | case BC_RESULT_SCALE: |
| 2658 | case BC_RESULT_OBASE: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2659 | bc_num_init(&d->d.n, src->d.n.len); |
| 2660 | bc_num_copy(&d->d.n, &src->d.n); |
| 2661 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2662 | case BC_RESULT_VAR: |
| 2663 | case BC_RESULT_ARRAY: |
| 2664 | case BC_RESULT_ARRAY_ELEM: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2665 | d->d.id.name = xstrdup(src->d.id.name); |
| 2666 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2667 | case BC_RESULT_CONSTANT: |
| 2668 | case BC_RESULT_LAST: |
| 2669 | case BC_RESULT_ONE: |
| 2670 | case BC_RESULT_STR: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2671 | memcpy(&d->d.n, &src->d.n, sizeof(BcNum)); |
| 2672 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2673 | } |
| 2674 | } |
| 2675 | #endif // ENABLE_DC |
| 2676 | |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 2677 | static FAST_FUNC void bc_result_free(void *result) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2678 | { |
| 2679 | BcResult *r = (BcResult *) result; |
| 2680 | |
| 2681 | switch (r->t) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2682 | case BC_RESULT_TEMP: |
| 2683 | case BC_RESULT_IBASE: |
| 2684 | case BC_RESULT_SCALE: |
| 2685 | case BC_RESULT_OBASE: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2686 | bc_num_free(&r->d.n); |
| 2687 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2688 | case BC_RESULT_VAR: |
| 2689 | case BC_RESULT_ARRAY: |
| 2690 | case BC_RESULT_ARRAY_ELEM: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2691 | free(r->d.id.name); |
| 2692 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2693 | default: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2694 | // Do nothing. |
| 2695 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2696 | } |
| 2697 | } |
| 2698 | |
| 2699 | static void bc_lex_lineComment(BcLex *l) |
| 2700 | { |
Denys Vlasenko | 55f3cab | 2018-12-18 14:37:16 +0100 | [diff] [blame] | 2701 | // Try: echo -n '#foo' | bc |
| 2702 | size_t i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2703 | l->t.t = BC_LEX_WHITESPACE; |
Denys Vlasenko | 55f3cab | 2018-12-18 14:37:16 +0100 | [diff] [blame] | 2704 | i = l->i; |
| 2705 | while (i < l->len && l->buf[i] != '\n') |
| 2706 | i++; |
| 2707 | l->i = i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2708 | } |
| 2709 | |
| 2710 | static void bc_lex_whitespace(BcLex *l) |
| 2711 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2712 | l->t.t = BC_LEX_WHITESPACE; |
Denys Vlasenko | 89198a9 | 2018-12-13 21:31:29 +0100 | [diff] [blame] | 2713 | for (;;) { |
| 2714 | char c = l->buf[l->i]; |
Denys Vlasenko | 9dc5d08 | 2018-12-16 18:43:51 +0100 | [diff] [blame] | 2715 | if (c == '\n') // this is BC_LEX_NLINE, not BC_LEX_WHITESPACE |
| 2716 | break; |
| 2717 | if (!isspace(c)) |
Denys Vlasenko | 89198a9 | 2018-12-13 21:31:29 +0100 | [diff] [blame] | 2718 | break; |
| 2719 | l->i++; |
| 2720 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2721 | } |
| 2722 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2723 | static BC_STATUS zbc_lex_number(BcLex *l, char start) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2724 | { |
| 2725 | const char *buf = l->buf + l->i; |
Denys Vlasenko | 0f31a5c | 2018-12-18 03:16:48 +0100 | [diff] [blame] | 2726 | size_t len, i, ccnt; |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2727 | bool pt; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2728 | |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2729 | pt = (start == '.'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2730 | l->t.t = BC_LEX_NUMBER; |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2731 | ccnt = i = 0; |
| 2732 | for (;;) { |
| 2733 | char c = buf[i]; |
| 2734 | if (c == '\0') |
| 2735 | break; |
| 2736 | if (c == '\\' && buf[i + 1] == '\n') { |
| 2737 | i += 2; |
Denys Vlasenko | 0f31a5c | 2018-12-18 03:16:48 +0100 | [diff] [blame] | 2738 | //number_of_backslashes++ - see comment below |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2739 | continue; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2740 | } |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2741 | if (!isdigit(c) && (c < 'A' || c > 'F')) { |
| 2742 | if (c != '.') break; |
| 2743 | // if '.' was already seen, stop on second one: |
| 2744 | if (pt) break; |
Denys Vlasenko | 0f31a5c | 2018-12-18 03:16:48 +0100 | [diff] [blame] | 2745 | pt = true; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2746 | } |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2747 | // buf[i] is one of "0-9A-F." |
| 2748 | i++; |
| 2749 | if (c != '.') |
| 2750 | ccnt = i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2751 | } |
Denys Vlasenko | 0f31a5c | 2018-12-18 03:16:48 +0100 | [diff] [blame] | 2752 | //ccnt is the number of chars in the number string, excluding possible |
| 2753 | //trailing "[\<newline>].[\<newline>]" (with any number of \<NL> repetitions). |
| 2754 | //i is buf[i] index of the first not-yet-parsed char after that. |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2755 | l->i += i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2756 | |
Denys Vlasenko | 0f31a5c | 2018-12-18 03:16:48 +0100 | [diff] [blame] | 2757 | // This might overestimate the size, if there are "\<NL>"'s |
| 2758 | // in the number. Subtracting number_of_backslashes*2 correctly |
| 2759 | // is not that easy: consider that in the case of "NNN.\<NL>" |
| 2760 | // loop above will count "\<NL>" before it realizes it is not |
| 2761 | // in fact *inside* the number: |
| 2762 | len = ccnt + 1; // +1 byte for NUL termination |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2763 | |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 2764 | // This check makes sense only if size_t is (much) larger than BC_MAX_NUM. |
| 2765 | if (SIZE_MAX > (BC_MAX_NUM | 0xff)) { |
| 2766 | if (len > BC_MAX_NUM) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2767 | RETURN_STATUS(bc_error("number too long: must be [1,"BC_MAX_NUM_STR"]")); |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 2768 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2769 | |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 2770 | bc_vec_pop_all(&l->t.v); |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2771 | bc_vec_expand(&l->t.v, 1 + len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2772 | bc_vec_push(&l->t.v, &start); |
| 2773 | |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2774 | while (ccnt != 0) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2775 | // If we have hit a backslash, skip it. We don't have |
| 2776 | // to check for a newline because it's guaranteed. |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2777 | if (*buf == '\\') { |
| 2778 | buf += 2; |
| 2779 | ccnt -= 2; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2780 | continue; |
| 2781 | } |
Denys Vlasenko | c355c4a | 2018-12-11 17:36:21 +0100 | [diff] [blame] | 2782 | bc_vec_push(&l->t.v, buf); |
| 2783 | buf++; |
| 2784 | ccnt--; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2785 | } |
| 2786 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 2787 | bc_vec_pushZeroByte(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2788 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 2789 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2790 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2791 | #define zbc_lex_number(...) (zbc_lex_number(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2792 | |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 2793 | static void bc_lex_name(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2794 | { |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 2795 | size_t i; |
| 2796 | const char *buf; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2797 | |
| 2798 | l->t.t = BC_LEX_NAME; |
| 2799 | |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 2800 | i = 0; |
| 2801 | buf = l->buf + l->i - 1; |
| 2802 | for (;;) { |
| 2803 | char c = buf[i]; |
| 2804 | if ((c < 'a' || c > 'z') && !isdigit(c) && c != '_') break; |
| 2805 | i++; |
| 2806 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2807 | |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 2808 | #if 0 // We do not protect against people with gigabyte-long names |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 2809 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
| 2810 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
| 2811 | if (i > BC_MAX_STRING) |
| 2812 | return bc_error("name too long: must be [1,"BC_MAX_STRING_STR"]"); |
| 2813 | } |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 2814 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2815 | bc_vec_string(&l->t.v, i, buf); |
| 2816 | |
| 2817 | // Increment the index. We minus 1 because it has already been incremented. |
| 2818 | l->i += i - 1; |
| 2819 | |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 2820 | //return BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2821 | } |
| 2822 | |
Denys Vlasenko | c2265f5 | 2018-12-12 23:10:08 +0100 | [diff] [blame] | 2823 | static void bc_lex_init(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2824 | { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 2825 | bc_char_vec_init(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2826 | } |
| 2827 | |
| 2828 | static void bc_lex_free(BcLex *l) |
| 2829 | { |
| 2830 | bc_vec_free(&l->t.v); |
| 2831 | } |
| 2832 | |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 2833 | static void bc_lex_file(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2834 | { |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 2835 | G.err_line = l->line = 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2836 | l->newline = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2837 | } |
| 2838 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 2839 | static bool bc_lex_more_input(BcLex *l) |
| 2840 | { |
| 2841 | size_t str; |
| 2842 | bool comment; |
| 2843 | |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2844 | bc_vec_pop_all(&G.input_buffer); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 2845 | |
| 2846 | // This loop is complex because the vm tries not to send any lines that end |
| 2847 | // with a backslash to the parser. The reason for that is because the parser |
| 2848 | // treats a backslash+newline combo as whitespace, per the bc spec. In that |
| 2849 | // case, and for strings and comments, the parser will expect more stuff. |
| 2850 | comment = false; |
| 2851 | str = 0; |
| 2852 | for (;;) { |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2853 | size_t prevlen = G.input_buffer.len; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 2854 | char *string; |
| 2855 | |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2856 | bc_read_line(&G.input_buffer, G.input_fp); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 2857 | // No more input means EOF |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2858 | if (G.input_buffer.len <= prevlen + 1) // (we expect +1 for NUL byte) |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 2859 | break; |
| 2860 | |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2861 | string = G.input_buffer.v + prevlen; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 2862 | while (*string) { |
| 2863 | char c = *string; |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2864 | if (string == G.input_buffer.v || string[-1] != '\\') { |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 2865 | if (IS_BC) |
| 2866 | str ^= (c == '"'); |
| 2867 | else { |
| 2868 | if (c == ']') |
| 2869 | str -= 1; |
| 2870 | else if (c == '[') |
| 2871 | str += 1; |
| 2872 | } |
| 2873 | } |
| 2874 | string++; |
| 2875 | if (c == '/' && *string == '*') { |
| 2876 | comment = true; |
| 2877 | string++; |
| 2878 | continue; |
| 2879 | } |
| 2880 | if (c == '*' && *string == '/') { |
| 2881 | comment = false; |
| 2882 | string++; |
| 2883 | } |
| 2884 | } |
| 2885 | if (str != 0 || comment) { |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2886 | G.input_buffer.len--; // backstep over the trailing NUL byte |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 2887 | continue; |
| 2888 | } |
| 2889 | |
| 2890 | // Check for backslash+newline. |
| 2891 | // we do not check that last char is '\n' - |
| 2892 | // if it is not, then it's EOF, and looping back |
| 2893 | // to bc_read_line() will detect it: |
| 2894 | string -= 2; |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2895 | if (string >= G.input_buffer.v && *string == '\\') { |
| 2896 | G.input_buffer.len--; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 2897 | continue; |
| 2898 | } |
| 2899 | |
| 2900 | break; |
| 2901 | } |
| 2902 | |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2903 | l->buf = G.input_buffer.v; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 2904 | l->i = 0; |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2905 | // bb_error_msg("G.input_buffer.len:%d '%s'", G.input_buffer.len, G.input_buffer.v); |
| 2906 | l->len = G.input_buffer.len - 1; // do not include NUL |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 2907 | |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2908 | return l->len != 0; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 2909 | } |
| 2910 | |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 2911 | IF_BC(static BC_STATUS zbc_lex_token(BcLex *l);) |
| 2912 | IF_DC(static BC_STATUS zdc_lex_token(BcLex *l);) |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 2913 | #define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
| 2914 | #define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 2915 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 2916 | static BC_STATUS zbc_lex_next(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2917 | { |
| 2918 | BcStatus s; |
| 2919 | |
| 2920 | l->t.last = l->t.t; |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 2921 | if (l->t.last == BC_LEX_EOF) RETURN_STATUS(bc_error("end of file")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2922 | |
| 2923 | l->line += l->newline; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 2924 | G.err_line = l->line; |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2925 | l->newline = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2926 | |
| 2927 | // Loop until failure or we don't have whitespace. This |
| 2928 | // is so the parser doesn't get inundated with whitespace. |
Denys Vlasenko | 9dc5d08 | 2018-12-16 18:43:51 +0100 | [diff] [blame] | 2929 | // Comments are also BC_LEX_WHITESPACE tokens and eaten here. |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 2930 | s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2931 | do { |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2932 | if (l->i == l->len) { |
Denys Vlasenko | 55f3cab | 2018-12-18 14:37:16 +0100 | [diff] [blame] | 2933 | l->t.t = BC_LEX_EOF; |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 2934 | if (!G.input_fp) |
| 2935 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 2936 | if (!bc_lex_more_input(l)) { |
| 2937 | G.input_fp = NULL; |
| 2938 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 2939 | } |
| 2940 | // here it's guaranteed that l->i is below l->len |
| 2941 | } |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 2942 | dbg_lex("next string to parse:'%.*s'", |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 2943 | (int)(strchrnul(l->buf + l->i, '\n') - (l->buf + l->i)), |
| 2944 | l->buf + l->i); |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 2945 | if (IS_BC) { |
| 2946 | IF_BC(s = zbc_lex_token(l)); |
| 2947 | } else { |
| 2948 | IF_DC(s = zdc_lex_token(l)); |
| 2949 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2950 | } while (!s && l->t.t == BC_LEX_WHITESPACE); |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame] | 2951 | dbg_lex("l->t.t from string:%d", l->t.t); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2952 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 2953 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2954 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2955 | #define zbc_lex_next(...) (zbc_lex_next(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2956 | |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 2957 | #if ENABLE_BC |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 2958 | static BC_STATUS zbc_lex_skip_if_at_NLINE(BcLex *l) |
| 2959 | { |
| 2960 | if (l->t.t == BC_LEX_NLINE) |
| 2961 | RETURN_STATUS(zbc_lex_next(l)); |
| 2962 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 2963 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2964 | #define zbc_lex_skip_if_at_NLINE(...) (zbc_lex_skip_if_at_NLINE(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 2965 | |
| 2966 | static BC_STATUS zbc_lex_next_and_skip_NLINE(BcLex *l) |
| 2967 | { |
| 2968 | BcStatus s; |
| 2969 | s = zbc_lex_next(l); |
| 2970 | if (s) RETURN_STATUS(s); |
| 2971 | // if(cond)<newline>stmt is accepted too (but not 2+ newlines) |
| 2972 | s = zbc_lex_skip_if_at_NLINE(l); |
| 2973 | RETURN_STATUS(s); |
| 2974 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2975 | #define zbc_lex_next_and_skip_NLINE(...) (zbc_lex_next_and_skip_NLINE(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 2976 | #endif |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 2977 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 2978 | static BC_STATUS zbc_lex_text_init(BcLex *l, const char *text) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2979 | { |
| 2980 | l->buf = text; |
| 2981 | l->i = 0; |
| 2982 | l->len = strlen(text); |
| 2983 | l->t.t = l->t.last = BC_LEX_INVALID; |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 2984 | RETURN_STATUS(zbc_lex_next(l)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2985 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 2986 | #define zbc_lex_text_init(...) (zbc_lex_text_init(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2987 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 2988 | #if ENABLE_BC |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 2989 | static BC_STATUS zbc_lex_identifier(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2990 | { |
| 2991 | BcStatus s; |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 2992 | unsigned i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 2993 | const char *buf = l->buf + l->i - 1; |
| 2994 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 2995 | for (i = 0; i < ARRAY_SIZE(bc_lex_kws); ++i) { |
| 2996 | const char *keyword8 = bc_lex_kws[i].name8; |
| 2997 | unsigned j = 0; |
| 2998 | while (buf[j] != '\0' && buf[j] == keyword8[j]) { |
| 2999 | j++; |
| 3000 | if (j == 8) goto match; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3001 | } |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3002 | if (keyword8[j] != '\0') |
| 3003 | continue; |
| 3004 | match: |
| 3005 | // buf starts with keyword bc_lex_kws[i] |
Denys Vlasenko | 5acd14b | 2018-12-20 16:48:50 +0100 | [diff] [blame] | 3006 | if (isalnum(buf[j]) || buf[j]=='_') |
| 3007 | continue; // "ifz" does not match "if" keyword, "if." does |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3008 | l->t.t = BC_LEX_KEY_1st_keyword + i; |
Denys Vlasenko | d00d2f9 | 2018-12-06 12:59:40 +0100 | [diff] [blame] | 3009 | if (!bc_lex_kws_POSIX(i)) { |
Denys Vlasenko | 0d7e46b | 2018-12-05 18:31:19 +0100 | [diff] [blame] | 3010 | s = bc_posix_error_fmt("%sthe '%.8s' keyword", "POSIX does not allow ", bc_lex_kws[i].name8); |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3011 | IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s)); |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3012 | } |
| 3013 | |
| 3014 | // We minus 1 because the index has already been incremented. |
| 3015 | l->i += j - 1; |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3016 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3017 | } |
| 3018 | |
Denys Vlasenko | 88cfea6 | 2018-12-10 20:26:04 +0100 | [diff] [blame] | 3019 | bc_lex_name(l); |
Denys Vlasenko | 0f31a5c | 2018-12-18 03:16:48 +0100 | [diff] [blame] | 3020 | s = BC_STATUS_SUCCESS; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3021 | |
Denys Vlasenko | 0d7e46b | 2018-12-05 18:31:19 +0100 | [diff] [blame] | 3022 | if (l->t.v.len > 2) { |
| 3023 | // Prevent this: |
| 3024 | // >>> qwe=1 |
| 3025 | // bc: POSIX only allows one character names; the following is bad: 'qwe=1 |
| 3026 | // ' |
| 3027 | unsigned len = strchrnul(buf, '\n') - buf; |
| 3028 | s = bc_posix_error_fmt("POSIX only allows one character names; the following is bad: '%.*s'", len, buf); |
| 3029 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3030 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3031 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3032 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3033 | #define zbc_lex_identifier(...) (zbc_lex_identifier(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3034 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3035 | static BC_STATUS zbc_lex_string(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3036 | { |
Denys Vlasenko | 07597cd | 2018-12-18 14:03:20 +0100 | [diff] [blame] | 3037 | size_t len, nls, i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3038 | |
| 3039 | l->t.t = BC_LEX_STR; |
| 3040 | |
Denys Vlasenko | 07597cd | 2018-12-18 14:03:20 +0100 | [diff] [blame] | 3041 | nls = 0; |
| 3042 | i = l->i; |
| 3043 | for (;;) { |
| 3044 | char c = l->buf[i]; |
| 3045 | if (c == '\0') { |
| 3046 | l->i = i; |
| 3047 | RETURN_STATUS(bc_error("string end could not be found")); |
| 3048 | } |
| 3049 | if (c == '"') |
| 3050 | break; |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3051 | nls += (c == '\n'); |
Denys Vlasenko | 07597cd | 2018-12-18 14:03:20 +0100 | [diff] [blame] | 3052 | i++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3053 | } |
| 3054 | |
| 3055 | len = i - l->i; |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 3056 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
| 3057 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
| 3058 | if (len > BC_MAX_STRING) |
Denys Vlasenko | 9811ad0 | 2018-12-12 23:25:13 +0100 | [diff] [blame] | 3059 | RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]")); |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 3060 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3061 | bc_vec_string(&l->t.v, len, l->buf + l->i); |
| 3062 | |
| 3063 | l->i = i + 1; |
| 3064 | l->line += nls; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3065 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3066 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3067 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3068 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3069 | #define zbc_lex_string(...) (zbc_lex_string(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3070 | |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 3071 | static void bc_lex_assign(BcLex *l, unsigned with_and_without) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3072 | { |
| 3073 | if (l->buf[l->i] == '=') { |
| 3074 | ++l->i; |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 3075 | with_and_without >>= 8; // store "with" value |
| 3076 | } // else store "without" value |
| 3077 | l->t.t = (with_and_without & 0xff); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3078 | } |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 3079 | #define bc_lex_assign(l, with, without) \ |
| 3080 | bc_lex_assign(l, ((with)<<8)|(without)) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3081 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3082 | static BC_STATUS zbc_lex_comment(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3083 | { |
| 3084 | size_t i, nls = 0; |
| 3085 | const char *buf = l->buf; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3086 | |
| 3087 | l->t.t = BC_LEX_WHITESPACE; |
Denys Vlasenko | 9dc5d08 | 2018-12-16 18:43:51 +0100 | [diff] [blame] | 3088 | i = l->i; /* here buf[l->i] is the '*' of opening comment delimiter */ |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3089 | for (;;) { |
Denys Vlasenko | 9dc5d08 | 2018-12-16 18:43:51 +0100 | [diff] [blame] | 3090 | char c = buf[++i]; |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3091 | check_star: |
| 3092 | if (c == '*') { |
| 3093 | c = buf[++i]; |
| 3094 | if (c == '/') |
| 3095 | break; |
| 3096 | goto check_star; |
| 3097 | } |
| 3098 | if (c == '\0') { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3099 | l->i = i; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3100 | RETURN_STATUS(bc_error("comment end could not be found")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3101 | } |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3102 | nls += (c == '\n'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3103 | } |
| 3104 | |
Denys Vlasenko | bc5ce66 | 2018-12-03 19:12:29 +0100 | [diff] [blame] | 3105 | l->i = i + 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3106 | l->line += nls; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3107 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3108 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3109 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3110 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3111 | #define zbc_lex_comment(...) (zbc_lex_comment(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3112 | |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 3113 | #undef zbc_lex_token |
Denys Vlasenko | c2265f5 | 2018-12-12 23:10:08 +0100 | [diff] [blame] | 3114 | static BC_STATUS zbc_lex_token(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3115 | { |
| 3116 | BcStatus s = BC_STATUS_SUCCESS; |
| 3117 | char c = l->buf[l->i++], c2; |
| 3118 | |
| 3119 | // This is the workhorse of the lexer. |
| 3120 | switch (c) { |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 3121 | // case '\0': // probably never reached |
| 3122 | // l->i--; |
| 3123 | // l->t.t = BC_LEX_EOF; |
| 3124 | // l->newline = true; |
| 3125 | // break; |
Denys Vlasenko | 9dc5d08 | 2018-12-16 18:43:51 +0100 | [diff] [blame] | 3126 | case '\n': |
| 3127 | l->t.t = BC_LEX_NLINE; |
| 3128 | l->newline = true; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3129 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3130 | case '\t': |
| 3131 | case '\v': |
| 3132 | case '\f': |
| 3133 | case '\r': |
| 3134 | case ' ': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3135 | bc_lex_whitespace(l); |
| 3136 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3137 | case '!': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3138 | bc_lex_assign(l, BC_LEX_OP_REL_NE, BC_LEX_OP_BOOL_NOT); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3139 | if (l->t.t == BC_LEX_OP_BOOL_NOT) { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3140 | s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("!"); |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3141 | IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3142 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3143 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3144 | case '"': |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3145 | s = zbc_lex_string(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3146 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3147 | case '#': |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3148 | s = bc_POSIX_does_not_allow("'#' script comments"); |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3149 | IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3150 | bc_lex_lineComment(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3151 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3152 | case '%': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3153 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_MODULUS, BC_LEX_OP_MODULUS); |
| 3154 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3155 | case '&': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3156 | c2 = l->buf[l->i]; |
| 3157 | if (c2 == '&') { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3158 | s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("&&"); |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3159 | IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3160 | ++l->i; |
| 3161 | l->t.t = BC_LEX_OP_BOOL_AND; |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3162 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3163 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3164 | s = bc_error_bad_character('&'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3165 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3166 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3167 | case '(': |
| 3168 | case ')': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3169 | l->t.t = (BcLexType)(c - '(' + BC_LEX_LPAREN); |
| 3170 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3171 | case '*': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3172 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_MULTIPLY, BC_LEX_OP_MULTIPLY); |
| 3173 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3174 | case '+': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3175 | c2 = l->buf[l->i]; |
| 3176 | if (c2 == '+') { |
| 3177 | ++l->i; |
| 3178 | l->t.t = BC_LEX_OP_INC; |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3179 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3180 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_PLUS, BC_LEX_OP_PLUS); |
| 3181 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3182 | case ',': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3183 | l->t.t = BC_LEX_COMMA; |
| 3184 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3185 | case '-': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3186 | c2 = l->buf[l->i]; |
| 3187 | if (c2 == '-') { |
| 3188 | ++l->i; |
| 3189 | l->t.t = BC_LEX_OP_DEC; |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3190 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3191 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_MINUS, BC_LEX_OP_MINUS); |
| 3192 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3193 | case '.': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3194 | if (isdigit(l->buf[l->i])) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3195 | s = zbc_lex_number(l, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3196 | else { |
| 3197 | l->t.t = BC_LEX_KEY_LAST; |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3198 | 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] | 3199 | } |
| 3200 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3201 | case '/': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3202 | c2 = l->buf[l->i]; |
| 3203 | if (c2 == '*') |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3204 | s = zbc_lex_comment(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3205 | else |
| 3206 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_DIVIDE, BC_LEX_OP_DIVIDE); |
| 3207 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3208 | case '0': |
| 3209 | case '1': |
| 3210 | case '2': |
| 3211 | case '3': |
| 3212 | case '4': |
| 3213 | case '5': |
| 3214 | case '6': |
| 3215 | case '7': |
| 3216 | case '8': |
| 3217 | case '9': |
| 3218 | case 'A': |
| 3219 | case 'B': |
| 3220 | case 'C': |
| 3221 | case 'D': |
| 3222 | case 'E': |
| 3223 | case 'F': |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3224 | s = zbc_lex_number(l, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3225 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3226 | case ';': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3227 | l->t.t = BC_LEX_SCOLON; |
| 3228 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3229 | case '<': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3230 | bc_lex_assign(l, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_LT); |
| 3231 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3232 | case '=': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3233 | bc_lex_assign(l, BC_LEX_OP_REL_EQ, BC_LEX_OP_ASSIGN); |
| 3234 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3235 | case '>': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3236 | bc_lex_assign(l, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_GT); |
| 3237 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3238 | case '[': |
| 3239 | case ']': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3240 | l->t.t = (BcLexType)(c - '[' + BC_LEX_LBRACKET); |
| 3241 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3242 | case '\\': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3243 | if (l->buf[l->i] == '\n') { |
| 3244 | l->t.t = BC_LEX_WHITESPACE; |
| 3245 | ++l->i; |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3246 | } else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3247 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3248 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3249 | case '^': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3250 | bc_lex_assign(l, BC_LEX_OP_ASSIGN_POWER, BC_LEX_OP_POWER); |
| 3251 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3252 | case 'a': |
| 3253 | case 'b': |
| 3254 | case 'c': |
| 3255 | case 'd': |
| 3256 | case 'e': |
| 3257 | case 'f': |
| 3258 | case 'g': |
| 3259 | case 'h': |
| 3260 | case 'i': |
| 3261 | case 'j': |
| 3262 | case 'k': |
| 3263 | case 'l': |
| 3264 | case 'm': |
| 3265 | case 'n': |
| 3266 | case 'o': |
| 3267 | case 'p': |
| 3268 | case 'q': |
| 3269 | case 'r': |
| 3270 | case 's': |
| 3271 | case 't': |
| 3272 | case 'u': |
| 3273 | case 'v': |
| 3274 | case 'w': |
| 3275 | case 'x': |
| 3276 | case 'y': |
| 3277 | case 'z': |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3278 | s = zbc_lex_identifier(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3279 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3280 | case '{': |
| 3281 | case '}': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3282 | l->t.t = (BcLexType)(c - '{' + BC_LEX_LBRACE); |
| 3283 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3284 | case '|': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3285 | c2 = l->buf[l->i]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3286 | if (c2 == '|') { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 3287 | s = bc_POSIX_does_not_allow_bool_ops_this_is_bad("||"); |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3288 | IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3289 | ++l->i; |
| 3290 | l->t.t = BC_LEX_OP_BOOL_OR; |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3291 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3292 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3293 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3294 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3295 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3296 | default: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3297 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3298 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3299 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3300 | } |
| 3301 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3302 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3303 | } |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 3304 | #define zbc_lex_token(...) (zbc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3305 | #endif // ENABLE_BC |
| 3306 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 3307 | #if ENABLE_DC |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3308 | static BC_STATUS zdc_lex_register(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3309 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3310 | if (isspace(l->buf[l->i - 1])) { |
| 3311 | bc_lex_whitespace(l); |
| 3312 | ++l->i; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 3313 | if (!G_exreg) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3314 | RETURN_STATUS(bc_error("extended register")); |
| 3315 | bc_lex_name(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3316 | } |
| 3317 | else { |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3318 | bc_vec_pop_all(&l->t.v); |
Denys Vlasenko | e55a572 | 2018-12-06 12:47:17 +0100 | [diff] [blame] | 3319 | bc_vec_push(&l->t.v, &l->buf[l->i - 1]); |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 3320 | bc_vec_pushZeroByte(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3321 | l->t.t = BC_LEX_NAME; |
| 3322 | } |
| 3323 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3324 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3325 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3326 | #define zdc_lex_register(...) (zdc_lex_register(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3327 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3328 | static BC_STATUS zdc_lex_string(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3329 | { |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3330 | size_t depth, nls, i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3331 | |
| 3332 | l->t.t = BC_LEX_STR; |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 3333 | bc_vec_pop_all(&l->t.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3334 | |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3335 | nls = 0; |
| 3336 | depth = 1; |
| 3337 | i = l->i; |
| 3338 | for (;;) { |
| 3339 | char c = l->buf[i]; |
| 3340 | if (c == '\0') { |
| 3341 | l->i = i; |
| 3342 | RETURN_STATUS(bc_error("string end could not be found")); |
| 3343 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3344 | nls += (c == '\n'); |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3345 | if (i == l->i || l->buf[i - 1] != '\\') { |
| 3346 | if (c == '[') depth++; |
| 3347 | if (c == ']') |
| 3348 | if (--depth == 0) |
| 3349 | break; |
| 3350 | } |
| 3351 | bc_vec_push(&l->t.v, &l->buf[i]); |
| 3352 | i++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3353 | } |
Denys Vlasenko | ef271da | 2018-12-18 13:48:37 +0100 | [diff] [blame] | 3354 | i++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3355 | |
Denys Vlasenko | 08c033c | 2018-12-05 16:55:08 +0100 | [diff] [blame] | 3356 | bc_vec_pushZeroByte(&l->t.v); |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 3357 | // This check makes sense only if size_t is (much) larger than BC_MAX_STRING. |
| 3358 | if (SIZE_MAX > (BC_MAX_STRING | 0xff)) { |
| 3359 | if (i - l->i > BC_MAX_STRING) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3360 | RETURN_STATUS(bc_error("string too long: must be [1,"BC_MAX_STRING_STR"]")); |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 3361 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3362 | |
| 3363 | l->i = i; |
| 3364 | l->line += nls; |
Denys Vlasenko | 5318f81 | 2018-12-05 17:48:01 +0100 | [diff] [blame] | 3365 | G.err_line = l->line; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3366 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3367 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3368 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3369 | #define zdc_lex_string(...) (zdc_lex_string(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3370 | |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 3371 | #undef zdc_lex_token |
Denys Vlasenko | c2265f5 | 2018-12-12 23:10:08 +0100 | [diff] [blame] | 3372 | static BC_STATUS zdc_lex_token(BcLex *l) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3373 | { |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 3374 | static const //BcLexType - should be this type, but narrower type saves size: |
| 3375 | uint8_t |
| 3376 | dc_lex_regs[] = { |
| 3377 | BC_LEX_OP_REL_EQ, BC_LEX_OP_REL_LE, BC_LEX_OP_REL_GE, BC_LEX_OP_REL_NE, |
| 3378 | BC_LEX_OP_REL_LT, BC_LEX_OP_REL_GT, BC_LEX_SCOLON, BC_LEX_COLON, |
| 3379 | BC_LEX_ELSE, BC_LEX_LOAD, BC_LEX_LOAD_POP, BC_LEX_OP_ASSIGN, |
| 3380 | BC_LEX_STORE_PUSH, |
| 3381 | }; |
| 3382 | static const //BcLexType - should be this type |
| 3383 | uint8_t |
| 3384 | dc_lex_tokens[] = { |
| 3385 | /* %&'( */ |
| 3386 | BC_LEX_OP_MODULUS, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_LPAREN, |
| 3387 | /* )*+, */ |
| 3388 | BC_LEX_INVALID, BC_LEX_OP_MULTIPLY, BC_LEX_OP_PLUS, BC_LEX_INVALID, |
| 3389 | /* -./ */ |
| 3390 | BC_LEX_OP_MINUS, BC_LEX_INVALID, BC_LEX_OP_DIVIDE, |
| 3391 | /* 0123456789 */ |
| 3392 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, |
| 3393 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, |
| 3394 | BC_LEX_INVALID, BC_LEX_INVALID, |
| 3395 | /* :;<=>?@ */ |
| 3396 | BC_LEX_COLON, BC_LEX_SCOLON, BC_LEX_OP_REL_GT, BC_LEX_OP_REL_EQ, |
| 3397 | BC_LEX_OP_REL_LT, BC_LEX_KEY_READ, BC_LEX_INVALID, |
| 3398 | /* ABCDEFGH */ |
| 3399 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, |
| 3400 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_EQ_NO_REG, BC_LEX_INVALID, |
| 3401 | /* IJKLMNOP */ |
| 3402 | BC_LEX_KEY_IBASE, BC_LEX_INVALID, BC_LEX_KEY_SCALE, BC_LEX_LOAD_POP, |
| 3403 | BC_LEX_INVALID, BC_LEX_OP_BOOL_NOT, BC_LEX_KEY_OBASE, BC_LEX_PRINT_STREAM, |
| 3404 | /* QRSTUVWXY */ |
| 3405 | BC_LEX_NQUIT, BC_LEX_POP, BC_LEX_STORE_PUSH, BC_LEX_INVALID, BC_LEX_INVALID, |
| 3406 | BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_SCALE_FACTOR, BC_LEX_INVALID, |
| 3407 | /* Z[\] */ |
| 3408 | BC_LEX_KEY_LENGTH, BC_LEX_INVALID, BC_LEX_INVALID, BC_LEX_INVALID, |
| 3409 | /* ^_` */ |
| 3410 | BC_LEX_OP_POWER, BC_LEX_NEG, BC_LEX_INVALID, |
| 3411 | /* abcdefgh */ |
| 3412 | BC_LEX_ASCIIFY, BC_LEX_INVALID, BC_LEX_CLEAR_STACK, BC_LEX_DUPLICATE, |
| 3413 | BC_LEX_ELSE, BC_LEX_PRINT_STACK, BC_LEX_INVALID, BC_LEX_INVALID, |
| 3414 | /* ijklmnop */ |
| 3415 | BC_LEX_STORE_IBASE, BC_LEX_INVALID, BC_LEX_STORE_SCALE, BC_LEX_LOAD, |
| 3416 | BC_LEX_INVALID, BC_LEX_PRINT_POP, BC_LEX_STORE_OBASE, BC_LEX_KEY_PRINT, |
| 3417 | /* qrstuvwx */ |
| 3418 | BC_LEX_KEY_QUIT, BC_LEX_SWAP, BC_LEX_OP_ASSIGN, BC_LEX_INVALID, |
| 3419 | BC_LEX_INVALID, BC_LEX_KEY_SQRT, BC_LEX_INVALID, BC_LEX_EXECUTE, |
| 3420 | /* yz */ |
| 3421 | BC_LEX_INVALID, BC_LEX_STACK_LEVEL, |
| 3422 | /* {|}~ */ |
| 3423 | BC_LEX_LBRACE, BC_LEX_OP_MODEXP, BC_LEX_INVALID, BC_LEX_OP_DIVMOD, |
| 3424 | }; |
| 3425 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3426 | BcStatus s = BC_STATUS_SUCCESS; |
| 3427 | char c = l->buf[l->i++], c2; |
| 3428 | size_t i; |
| 3429 | |
Denys Vlasenko | 51fb8aa | 2018-12-05 00:22:34 +0100 | [diff] [blame] | 3430 | for (i = 0; i < ARRAY_SIZE(dc_lex_regs); ++i) { |
| 3431 | if (l->t.last == dc_lex_regs[i]) |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3432 | RETURN_STATUS(zdc_lex_register(l)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3433 | } |
| 3434 | |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3435 | if (c >= '%' && c <= '~' |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 3436 | && (l->t.t = dc_lex_tokens[c - '%']) != BC_LEX_INVALID |
Denys Vlasenko | 12b9eaf | 2018-12-11 23:50:14 +0100 | [diff] [blame] | 3437 | ) { |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3438 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3439 | } |
| 3440 | |
| 3441 | // This is the workhorse of the lexer. |
| 3442 | switch (c) { |
Denys Vlasenko | b44a7f1 | 2018-12-17 11:58:20 +0100 | [diff] [blame] | 3443 | // case '\0': // probably never reached |
| 3444 | // l->t.t = BC_LEX_EOF; |
| 3445 | // break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3446 | case '\n': |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 3447 | // '\n' is BC_LEX_NLINE, not BC_LEX_WHITESPACE |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 3448 | // (and "case '\n':" is not just empty here) |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 3449 | // only to allow interactive dc have a way to exit |
| 3450 | // "parse" stage of "parse,execute" loop |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 3451 | // on <enter>, not on _next_ token (which would mean |
| 3452 | // commands are not executed on pressing <enter>). |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 3453 | // IOW: typing "1p<enter>" should print "1" _at once_, |
| 3454 | // not after some more input. |
| 3455 | l->t.t = BC_LEX_NLINE; |
| 3456 | l->newline = true; |
| 3457 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3458 | case '\t': |
| 3459 | case '\v': |
| 3460 | case '\f': |
| 3461 | case '\r': |
| 3462 | case ' ': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3463 | l->newline = (c == '\n'); |
| 3464 | bc_lex_whitespace(l); |
| 3465 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3466 | case '!': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3467 | c2 = l->buf[l->i]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3468 | if (c2 == '=') |
| 3469 | l->t.t = BC_LEX_OP_REL_NE; |
| 3470 | else if (c2 == '<') |
| 3471 | l->t.t = BC_LEX_OP_REL_LE; |
| 3472 | else if (c2 == '>') |
| 3473 | l->t.t = BC_LEX_OP_REL_GE; |
| 3474 | else |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3475 | RETURN_STATUS(bc_error_bad_character(c)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3476 | ++l->i; |
| 3477 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3478 | case '#': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3479 | bc_lex_lineComment(l); |
| 3480 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3481 | case '.': |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3482 | if (isdigit(l->buf[l->i])) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3483 | s = zbc_lex_number(l, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3484 | else |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3485 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3486 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3487 | case '0': |
| 3488 | case '1': |
| 3489 | case '2': |
| 3490 | case '3': |
| 3491 | case '4': |
| 3492 | case '5': |
| 3493 | case '6': |
| 3494 | case '7': |
| 3495 | case '8': |
| 3496 | case '9': |
| 3497 | case 'A': |
| 3498 | case 'B': |
| 3499 | case 'C': |
| 3500 | case 'D': |
| 3501 | case 'E': |
| 3502 | case 'F': |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3503 | s = zbc_lex_number(l, c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3504 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3505 | case '[': |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 3506 | s = zdc_lex_string(l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3507 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3508 | default: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3509 | l->t.t = BC_LEX_INVALID; |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3510 | s = bc_error_bad_character(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3511 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3512 | } |
| 3513 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3514 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3515 | } |
Denys Vlasenko | 5cf0b2d | 2018-12-22 18:24:19 +0100 | [diff] [blame] | 3516 | #define zdc_lex_token(...) (zdc_lex_token(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3517 | #endif // ENABLE_DC |
| 3518 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 3519 | static void bc_parse_push(BcParse *p, char i) |
| 3520 | { |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 3521 | dbg_compile("%s:%d pushing bytecode %zd:%d", __func__, __LINE__, p->func->code.len, i); |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 3522 | bc_vec_pushByte(&p->func->code, i); |
| 3523 | } |
Denys Vlasenko | b23ac51 | 2018-12-06 13:10:56 +0100 | [diff] [blame] | 3524 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3525 | static void bc_parse_pushName(BcParse *p, char *name) |
| 3526 | { |
Denys Vlasenko | e6c40c4 | 2018-12-16 20:32:58 +0100 | [diff] [blame] | 3527 | while (*name) |
| 3528 | bc_parse_push(p, *name++); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3529 | bc_parse_push(p, BC_PARSE_STREND); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3530 | } |
| 3531 | |
| 3532 | static void bc_parse_pushIndex(BcParse *p, size_t idx) |
| 3533 | { |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3534 | size_t mask; |
| 3535 | unsigned amt; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3536 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 3537 | dbg_lex("%s:%d pushing index %zd", __func__, __LINE__, idx); |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3538 | mask = ((size_t)0xff) << (sizeof(idx) * 8 - 8); |
| 3539 | amt = sizeof(idx); |
| 3540 | do { |
| 3541 | if (idx & mask) break; |
| 3542 | mask >>= 8; |
| 3543 | amt--; |
| 3544 | } while (amt != 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3545 | |
| 3546 | bc_parse_push(p, amt); |
Denys Vlasenko | c2da68e | 2018-12-12 16:44:34 +0100 | [diff] [blame] | 3547 | |
| 3548 | while (idx != 0) { |
| 3549 | bc_parse_push(p, (unsigned char)idx); |
| 3550 | idx >>= 8; |
| 3551 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3552 | } |
| 3553 | |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3554 | #if ENABLE_BC |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3555 | static void bc_parse_pushJUMP(BcParse *p, size_t idx) |
| 3556 | { |
| 3557 | bc_parse_push(p, BC_INST_JUMP); |
| 3558 | bc_parse_pushIndex(p, idx); |
| 3559 | } |
| 3560 | |
| 3561 | static void bc_parse_pushJUMP_ZERO(BcParse *p, size_t idx) |
| 3562 | { |
| 3563 | bc_parse_push(p, BC_INST_JUMP_ZERO); |
| 3564 | bc_parse_pushIndex(p, idx); |
| 3565 | } |
| 3566 | |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 3567 | static BC_STATUS zbc_parse_pushSTR(BcParse *p) |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3568 | { |
| 3569 | char *str = xstrdup(p->l.t.v.v); |
| 3570 | |
| 3571 | bc_parse_push(p, BC_INST_STR); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3572 | bc_parse_pushIndex(p, p->func->strs.len); |
| 3573 | bc_vec_push(&p->func->strs, &str); |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3574 | |
| 3575 | RETURN_STATUS(zbc_lex_next(&p->l)); |
| 3576 | } |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 3577 | #define zbc_parse_pushSTR(...) (zbc_parse_pushSTR(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3578 | #endif |
| 3579 | |
| 3580 | static void bc_parse_pushNUM(BcParse *p) |
| 3581 | { |
| 3582 | char *num = xstrdup(p->l.t.v.v); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3583 | #if ENABLE_BC && ENABLE_DC |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 3584 | size_t idx = bc_vec_push(IS_BC ? &p->func->consts : &G.prog.consts, &num); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3585 | #elif ENABLE_BC |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 3586 | size_t idx = bc_vec_push(&p->func->consts, &num); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3587 | #else // DC |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 3588 | size_t idx = bc_vec_push(&G.prog.consts, &num); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3589 | #endif |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3590 | bc_parse_push(p, BC_INST_NUM); |
| 3591 | bc_parse_pushIndex(p, idx); |
| 3592 | } |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 3593 | |
Denys Vlasenko | f86e960 | 2018-12-14 17:01:56 +0100 | [diff] [blame] | 3594 | static BC_STATUS zbc_parse_text_init(BcParse *p, const char *text) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3595 | { |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 3596 | p->func = bc_program_func(p->fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3597 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 3598 | RETURN_STATUS(zbc_lex_text_init(&p->l, text)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3599 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3600 | #define zbc_parse_text_init(...) (zbc_parse_text_init(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3601 | |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3602 | // Called when parsing or execution detects a failure, |
| 3603 | // resets execution structures. |
| 3604 | static void bc_program_reset(void) |
| 3605 | { |
| 3606 | BcFunc *f; |
| 3607 | BcInstPtr *ip; |
| 3608 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 3609 | bc_vec_npop(&G.prog.exestack, G.prog.exestack.len - 1); |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3610 | bc_vec_pop_all(&G.prog.results); |
| 3611 | |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 3612 | f = bc_program_func_BC_PROG_MAIN(); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 3613 | ip = bc_vec_top(&G.prog.exestack); |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 3614 | ip->inst_idx = f->code.len; |
Denys Vlasenko | b6f6086 | 2018-12-06 12:54:26 +0100 | [diff] [blame] | 3615 | } |
| 3616 | |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 3617 | // Called when parsing code detects a failure, |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 3618 | // resets parsing structures. |
| 3619 | static void bc_parse_reset(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3620 | { |
| 3621 | if (p->fidx != BC_PROG_MAIN) { |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 3622 | bc_func_free(p->func); |
| 3623 | bc_func_init(p->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3624 | |
Denys Vlasenko | 65e1046 | 2018-12-19 15:13:14 +0100 | [diff] [blame] | 3625 | p->fidx = BC_PROG_MAIN; |
| 3626 | p->func = bc_program_func_BC_PROG_MAIN(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3627 | } |
| 3628 | |
| 3629 | p->l.i = p->l.len; |
| 3630 | p->l.t.t = BC_LEX_EOF; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3631 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 3632 | IF_BC(bc_vec_pop_all(&p->exits);) |
| 3633 | IF_BC(bc_vec_pop_all(&p->conds);) |
| 3634 | IF_BC(bc_vec_pop_all(&p->ops);) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3635 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 3636 | bc_program_reset(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3637 | } |
| 3638 | |
| 3639 | static void bc_parse_free(BcParse *p) |
| 3640 | { |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 3641 | IF_BC(bc_vec_free(&p->exits);) |
| 3642 | IF_BC(bc_vec_free(&p->conds);) |
| 3643 | IF_BC(bc_vec_free(&p->ops);) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3644 | bc_lex_free(&p->l); |
| 3645 | } |
| 3646 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 3647 | static void bc_parse_create(BcParse *p, size_t fidx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3648 | { |
| 3649 | memset(p, 0, sizeof(BcParse)); |
| 3650 | |
Denys Vlasenko | c2265f5 | 2018-12-12 23:10:08 +0100 | [diff] [blame] | 3651 | bc_lex_init(&p->l); |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 3652 | IF_BC(bc_vec_init(&p->exits, sizeof(size_t), NULL);) |
| 3653 | IF_BC(bc_vec_init(&p->conds, sizeof(size_t), NULL);) |
| 3654 | IF_BC(bc_vec_init(&p->ops, sizeof(BcLexType), NULL);) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3655 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 3656 | p->fidx = fidx; |
| 3657 | p->func = bc_program_func(fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3658 | } |
| 3659 | |
Denys Vlasenko | 0471544 | 2018-12-21 00:10:26 +0100 | [diff] [blame] | 3660 | static void bc_program_add_fn(void) |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3661 | { |
Denys Vlasenko | 0471544 | 2018-12-21 00:10:26 +0100 | [diff] [blame] | 3662 | //size_t idx; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3663 | BcFunc f; |
| 3664 | bc_func_init(&f); |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 3665 | //idx = |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3666 | bc_vec_push(&G.prog.fns, &f); |
Denys Vlasenko | 0471544 | 2018-12-21 00:10:26 +0100 | [diff] [blame] | 3667 | //return idx; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3668 | } |
| 3669 | |
| 3670 | #if ENABLE_BC |
| 3671 | |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3672 | // Note: takes ownership of 'name' (must be malloced) |
| 3673 | static size_t bc_program_addFunc(char *name) |
| 3674 | { |
| 3675 | size_t idx; |
| 3676 | BcId entry, *entry_ptr; |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3677 | int inserted; |
| 3678 | |
| 3679 | entry.name = name; |
| 3680 | entry.idx = G.prog.fns.len; |
| 3681 | |
| 3682 | inserted = bc_map_insert(&G.prog.fn_map, &entry, &idx); |
| 3683 | if (!inserted) free(name); |
| 3684 | |
| 3685 | entry_ptr = bc_vec_item(&G.prog.fn_map, idx); |
| 3686 | idx = entry_ptr->idx; |
| 3687 | |
| 3688 | if (!inserted) { |
Denys Vlasenko | eaa3b00 | 2018-12-19 20:05:50 +0100 | [diff] [blame] | 3689 | // There is already a function with this name. |
| 3690 | // It'll be redefined now, clear old definition. |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3691 | BcFunc *func = bc_program_func(entry_ptr->idx); |
Denys Vlasenko | eaa3b00 | 2018-12-19 20:05:50 +0100 | [diff] [blame] | 3692 | bc_func_free(func); |
| 3693 | bc_func_init(func); |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3694 | } else { |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 3695 | bc_program_add_fn(); |
Denys Vlasenko | 408b7d4 | 2018-12-19 19:43:03 +0100 | [diff] [blame] | 3696 | } |
| 3697 | |
| 3698 | return idx; |
| 3699 | } |
| 3700 | |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 3701 | #define BC_PARSE_TOP_OP(p) (*((BcLexType *) bc_vec_top(&(p)->ops))) |
| 3702 | #define BC_PARSE_LEAF(p, rparen) \ |
| 3703 | (((p) >= BC_INST_NUM && (p) <= BC_INST_SQRT) || (rparen) || \ |
| 3704 | (p) == BC_INST_INC_POST || (p) == BC_INST_DEC_POST) |
| 3705 | |
| 3706 | // We can calculate the conversion between tokens and exprs by subtracting the |
| 3707 | // position of the first operator in the lex enum and adding the position of the |
| 3708 | // first in the expr enum. Note: This only works for binary operators. |
Denys Vlasenko | 0154d78 | 2018-12-14 23:32:51 +0100 | [diff] [blame] | 3709 | #define BC_TOKEN_2_INST(t) ((char) ((t) - BC_LEX_NEG + BC_INST_NEG)) |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 3710 | |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3711 | static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags); |
| 3712 | |
| 3713 | static BC_STATUS zbc_parse_expr(BcParse *p, uint8_t flags) |
| 3714 | { |
| 3715 | BcStatus s; |
| 3716 | |
| 3717 | s = bc_parse_expr_empty_ok(p, flags); |
| 3718 | if (s == BC_STATUS_PARSE_EMPTY_EXP) |
| 3719 | RETURN_STATUS(bc_error("empty expression")); |
| 3720 | RETURN_STATUS(s); |
| 3721 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3722 | #define zbc_parse_expr(...) (zbc_parse_expr(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3723 | |
| 3724 | static BC_STATUS zbc_parse_stmt_possibly_auto(BcParse *p, bool auto_allowed); |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3725 | #define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 3726 | |
| 3727 | static BC_STATUS zbc_parse_stmt(BcParse *p) |
| 3728 | { |
| 3729 | RETURN_STATUS(zbc_parse_stmt_possibly_auto(p, false)); |
| 3730 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3731 | #define zbc_parse_stmt(...) (zbc_parse_stmt(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3732 | |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3733 | static BC_STATUS zbc_parse_stmt_allow_NLINE_before(BcParse *p, const char *after_X) |
Denys Vlasenko | 2e8be02 | 2018-12-16 20:41:32 +0100 | [diff] [blame] | 3734 | { |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3735 | // "if(cond)<newline>stmt" is accepted too, but not 2+ newlines. |
| 3736 | // Same for "else", "while()", "for()". |
| 3737 | BcStatus s = zbc_lex_next_and_skip_NLINE(&p->l); |
| 3738 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 2e8be02 | 2018-12-16 20:41:32 +0100 | [diff] [blame] | 3739 | if (p->l.t.t == BC_LEX_NLINE) |
| 3740 | RETURN_STATUS(bc_error_fmt("no statement after '%s'", after_X)); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 3741 | |
| 3742 | RETURN_STATUS(zbc_parse_stmt(p)); |
Denys Vlasenko | 2e8be02 | 2018-12-16 20:41:32 +0100 | [diff] [blame] | 3743 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3744 | #define zbc_parse_stmt_allow_NLINE_before(...) (zbc_parse_stmt_allow_NLINE_before(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 2e8be02 | 2018-12-16 20:41:32 +0100 | [diff] [blame] | 3745 | |
Denys Vlasenko | 7b1df3d | 2018-12-14 23:12:48 +0100 | [diff] [blame] | 3746 | static void bc_parse_operator(BcParse *p, BcLexType type, size_t start, |
| 3747 | size_t *nexprs) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3748 | { |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 3749 | char l, r = bc_parse_op_PREC(type - BC_LEX_OP_INC); |
| 3750 | bool left = bc_parse_op_LEFT(type - BC_LEX_OP_INC); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3751 | |
| 3752 | while (p->ops.len > start) { |
Denys Vlasenko | 7b1df3d | 2018-12-14 23:12:48 +0100 | [diff] [blame] | 3753 | BcLexType t = BC_PARSE_TOP_OP(p); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3754 | if (t == BC_LEX_LPAREN) break; |
| 3755 | |
Denys Vlasenko | 6543758 | 2018-12-05 19:37:19 +0100 | [diff] [blame] | 3756 | l = bc_parse_op_PREC(t - BC_LEX_OP_INC); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3757 | if (l >= r && (l != r || !left)) break; |
| 3758 | |
Denys Vlasenko | 0154d78 | 2018-12-14 23:32:51 +0100 | [diff] [blame] | 3759 | bc_parse_push(p, BC_TOKEN_2_INST(t)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3760 | bc_vec_pop(&p->ops); |
Denys Vlasenko | 7b1df3d | 2018-12-14 23:12:48 +0100 | [diff] [blame] | 3761 | *nexprs -= (t != BC_LEX_OP_BOOL_NOT && t != BC_LEX_NEG); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3762 | } |
| 3763 | |
| 3764 | bc_vec_push(&p->ops, &type); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3765 | } |
| 3766 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3767 | static BC_STATUS zbc_parse_rightParen(BcParse *p, size_t ops_bgn, size_t *nexs) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3768 | { |
| 3769 | BcLexType top; |
| 3770 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3771 | if (p->ops.len <= ops_bgn) |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3772 | RETURN_STATUS(bc_error_bad_expression()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3773 | top = BC_PARSE_TOP_OP(p); |
| 3774 | |
| 3775 | while (top != BC_LEX_LPAREN) { |
Denys Vlasenko | 0154d78 | 2018-12-14 23:32:51 +0100 | [diff] [blame] | 3776 | bc_parse_push(p, BC_TOKEN_2_INST(top)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3777 | |
| 3778 | bc_vec_pop(&p->ops); |
| 3779 | *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG; |
| 3780 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 3781 | if (p->ops.len <= ops_bgn) |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3782 | RETURN_STATUS(bc_error_bad_expression()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3783 | top = BC_PARSE_TOP_OP(p); |
| 3784 | } |
| 3785 | |
| 3786 | bc_vec_pop(&p->ops); |
| 3787 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3788 | RETURN_STATUS(zbc_lex_next(&p->l)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3789 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3790 | #define zbc_parse_rightParen(...) (zbc_parse_rightParen(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3791 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3792 | static BC_STATUS zbc_parse_params(BcParse *p, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3793 | { |
| 3794 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3795 | size_t nparams; |
| 3796 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 3797 | dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t); |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3798 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; |
| 3799 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3800 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3801 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3802 | |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3803 | nparams = 0; |
| 3804 | if (p->l.t.t != BC_LEX_RPAREN) { |
| 3805 | for (;;) { |
| 3806 | s = zbc_parse_expr(p, flags); |
| 3807 | if (s) RETURN_STATUS(s); |
| 3808 | nparams++; |
| 3809 | if (p->l.t.t != BC_LEX_COMMA) { |
| 3810 | if (p->l.t.t == BC_LEX_RPAREN) |
| 3811 | break; |
| 3812 | RETURN_STATUS(bc_error_bad_token()); |
| 3813 | } |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3814 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3815 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3816 | } |
| 3817 | } |
| 3818 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3819 | bc_parse_push(p, BC_INST_CALL); |
| 3820 | bc_parse_pushIndex(p, nparams); |
| 3821 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3822 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3823 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3824 | #define zbc_parse_params(...) (zbc_parse_params(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3825 | |
Denys Vlasenko | e3d3d20 | 2018-12-19 13:19:44 +0100 | [diff] [blame] | 3826 | // Note: takes ownership of 'name' (must be malloced) |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3827 | static BC_STATUS zbc_parse_call(BcParse *p, char *name, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3828 | { |
| 3829 | BcStatus s; |
| 3830 | BcId entry, *entry_ptr; |
| 3831 | size_t idx; |
| 3832 | |
| 3833 | entry.name = name; |
| 3834 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3835 | s = zbc_parse_params(p, flags); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3836 | if (s) goto err; |
| 3837 | |
| 3838 | if (p->l.t.t != BC_LEX_RPAREN) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3839 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3840 | goto err; |
| 3841 | } |
| 3842 | |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 3843 | idx = bc_map_find_exact(&G.prog.fn_map, &entry); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3844 | |
| 3845 | if (idx == BC_VEC_INVALID_IDX) { |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 3846 | // No such function exists, create an empty one |
Denys Vlasenko | 684d441 | 2018-12-19 14:57:23 +0100 | [diff] [blame] | 3847 | bc_program_addFunc(name); |
Denys Vlasenko | 5aa5483 | 2018-12-19 13:55:53 +0100 | [diff] [blame] | 3848 | idx = bc_map_find_exact(&G.prog.fn_map, &entry); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3849 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3850 | free(name); |
| 3851 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 3852 | entry_ptr = bc_vec_item(&G.prog.fn_map, idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3853 | bc_parse_pushIndex(p, entry_ptr->idx); |
| 3854 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3855 | RETURN_STATUS(zbc_lex_next(&p->l)); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 3856 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3857 | free(name); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3858 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3859 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3860 | #define zbc_parse_call(...) (zbc_parse_call(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3861 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3862 | static BC_STATUS zbc_parse_name(BcParse *p, BcInst *type, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3863 | { |
| 3864 | BcStatus s; |
| 3865 | char *name; |
| 3866 | |
| 3867 | name = xstrdup(p->l.t.v.v); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3868 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3869 | if (s) goto err; |
| 3870 | |
| 3871 | if (p->l.t.t == BC_LEX_LBRACKET) { |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3872 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3873 | if (s) goto err; |
| 3874 | |
| 3875 | if (p->l.t.t == BC_LEX_RBRACKET) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3876 | if (!(flags & BC_PARSE_ARRAY)) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3877 | s = bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3878 | goto err; |
| 3879 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3880 | *type = BC_INST_ARRAY; |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3881 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3882 | *type = BC_INST_ARRAY_ELEM; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3883 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3884 | s = zbc_parse_expr(p, flags); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3885 | if (s) goto err; |
| 3886 | } |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3887 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3888 | if (s) goto err; |
| 3889 | bc_parse_push(p, *type); |
| 3890 | bc_parse_pushName(p, name); |
Denys Vlasenko | e6c40c4 | 2018-12-16 20:32:58 +0100 | [diff] [blame] | 3891 | free(name); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 3892 | } else if (p->l.t.t == BC_LEX_LPAREN) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3893 | if (flags & BC_PARSE_NOCALL) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 3894 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3895 | goto err; |
| 3896 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3897 | *type = BC_INST_CALL; |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3898 | s = zbc_parse_call(p, name, flags); |
| 3899 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3900 | *type = BC_INST_VAR; |
| 3901 | bc_parse_push(p, BC_INST_VAR); |
| 3902 | bc_parse_pushName(p, name); |
Denys Vlasenko | e6c40c4 | 2018-12-16 20:32:58 +0100 | [diff] [blame] | 3903 | free(name); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3904 | } |
| 3905 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3906 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 3907 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3908 | free(name); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3909 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3910 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3911 | #define zbc_parse_name(...) (zbc_parse_name(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3912 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 3913 | static BC_STATUS zbc_parse_read(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3914 | { |
| 3915 | BcStatus s; |
| 3916 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3917 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 3918 | if (s) RETURN_STATUS(s); |
| 3919 | if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3920 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3921 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 3922 | if (s) RETURN_STATUS(s); |
| 3923 | if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3924 | |
| 3925 | bc_parse_push(p, BC_INST_READ); |
| 3926 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 3927 | RETURN_STATUS(zbc_lex_next(&p->l)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3928 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3929 | #define zbc_parse_read(...) (zbc_parse_read(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3930 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3931 | static BC_STATUS zbc_parse_builtin(BcParse *p, BcLexType type, uint8_t flags, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3932 | BcInst *prev) |
| 3933 | { |
| 3934 | BcStatus s; |
| 3935 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3936 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3937 | if (s) RETURN_STATUS(s); |
| 3938 | if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3939 | |
| 3940 | flags = (flags & ~(BC_PARSE_PRINT | BC_PARSE_REL)) | BC_PARSE_ARRAY; |
| 3941 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3942 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3943 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3944 | |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3945 | s = zbc_parse_expr(p, flags); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3946 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3947 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3948 | if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3949 | |
| 3950 | *prev = (type == BC_LEX_KEY_LENGTH) ? BC_INST_LENGTH : BC_INST_SQRT; |
| 3951 | bc_parse_push(p, *prev); |
| 3952 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3953 | RETURN_STATUS(zbc_lex_next(&p->l)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3954 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3955 | #define zbc_parse_builtin(...) (zbc_parse_builtin(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3956 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3957 | static BC_STATUS zbc_parse_scale(BcParse *p, BcInst *type, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3958 | { |
| 3959 | BcStatus s; |
| 3960 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3961 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3962 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3963 | |
| 3964 | if (p->l.t.t != BC_LEX_LPAREN) { |
| 3965 | *type = BC_INST_SCALE; |
| 3966 | bc_parse_push(p, BC_INST_SCALE); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3967 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3968 | } |
| 3969 | |
| 3970 | *type = BC_INST_SCALE_FUNC; |
| 3971 | flags &= ~(BC_PARSE_PRINT | BC_PARSE_REL); |
| 3972 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 3973 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3974 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3975 | |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 3976 | s = zbc_parse_expr(p, flags); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3977 | if (s) RETURN_STATUS(s); |
| 3978 | if (p->l.t.t != BC_LEX_RPAREN) |
| 3979 | RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3980 | bc_parse_push(p, BC_INST_SCALE_FUNC); |
| 3981 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3982 | RETURN_STATUS(zbc_lex_next(&p->l)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3983 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 3984 | #define zbc_parse_scale(...) (zbc_parse_scale(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3985 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 3986 | static BC_STATUS zbc_parse_incdec(BcParse *p, BcInst *prev, bool *paren_expr, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3987 | size_t *nexprs, uint8_t flags) |
| 3988 | { |
| 3989 | BcStatus s; |
| 3990 | BcLexType type; |
| 3991 | char inst; |
| 3992 | BcInst etype = *prev; |
| 3993 | |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 3994 | if (etype == BC_INST_VAR || etype == BC_INST_ARRAY_ELEM |
| 3995 | || etype == BC_INST_SCALE || etype == BC_INST_LAST |
| 3996 | || etype == BC_INST_IBASE || etype == BC_INST_OBASE |
| 3997 | ) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 3998 | *prev = inst = BC_INST_INC_POST + (p->l.t.t != BC_LEX_OP_INC); |
| 3999 | bc_parse_push(p, inst); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4000 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4001 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4002 | *prev = inst = BC_INST_INC_PRE + (p->l.t.t != BC_LEX_OP_INC); |
| 4003 | *paren_expr = true; |
| 4004 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4005 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4006 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4007 | type = p->l.t.t; |
| 4008 | |
| 4009 | // Because we parse the next part of the expression |
| 4010 | // right here, we need to increment this. |
| 4011 | *nexprs = *nexprs + 1; |
| 4012 | |
| 4013 | switch (type) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4014 | case BC_LEX_NAME: |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4015 | s = zbc_parse_name(p, prev, flags | BC_PARSE_NOCALL); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4016 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4017 | case BC_LEX_KEY_IBASE: |
| 4018 | case BC_LEX_KEY_LAST: |
| 4019 | case BC_LEX_KEY_OBASE: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4020 | bc_parse_push(p, type - BC_LEX_KEY_IBASE + BC_INST_IBASE); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4021 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4022 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4023 | case BC_LEX_KEY_SCALE: |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4024 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4025 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4026 | if (p->l.t.t == BC_LEX_LPAREN) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4027 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4028 | else |
| 4029 | bc_parse_push(p, BC_INST_SCALE); |
| 4030 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4031 | default: |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4032 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4033 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4034 | } |
| 4035 | |
| 4036 | if (!s) bc_parse_push(p, inst); |
| 4037 | } |
| 4038 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4039 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4040 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4041 | #define zbc_parse_incdec(...) (zbc_parse_incdec(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4042 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4043 | static BC_STATUS zbc_parse_minus(BcParse *p, BcInst *prev, size_t ops_bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4044 | bool rparen, size_t *nexprs) |
| 4045 | { |
| 4046 | BcStatus s; |
| 4047 | BcLexType type; |
| 4048 | BcInst etype = *prev; |
| 4049 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4050 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4051 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4052 | |
| 4053 | type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST || |
| 4054 | (etype >= BC_INST_NUM && etype <= BC_INST_SQRT) ? |
| 4055 | BC_LEX_OP_MINUS : |
| 4056 | BC_LEX_NEG; |
Denys Vlasenko | 0154d78 | 2018-12-14 23:32:51 +0100 | [diff] [blame] | 4057 | *prev = BC_TOKEN_2_INST(type); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4058 | |
| 4059 | // We can just push onto the op stack because this is the largest |
| 4060 | // precedence operator that gets pushed. Inc/dec does not. |
| 4061 | if (type != BC_LEX_OP_MINUS) |
| 4062 | bc_vec_push(&p->ops, &type); |
| 4063 | else |
Denys Vlasenko | 7b1df3d | 2018-12-14 23:12:48 +0100 | [diff] [blame] | 4064 | bc_parse_operator(p, type, ops_bgn, nexprs); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4065 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4066 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4067 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4068 | #define zbc_parse_minus(...) (zbc_parse_minus(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4069 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4070 | static BC_STATUS zbc_parse_print(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4071 | { |
| 4072 | BcStatus s; |
| 4073 | BcLexType type; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4074 | |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4075 | for (;;) { |
| 4076 | s = zbc_lex_next(&p->l); |
| 4077 | if (s) RETURN_STATUS(s); |
| 4078 | type = p->l.t.t; |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4079 | if (type == BC_LEX_STR) { |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 4080 | s = zbc_parse_pushSTR(p); |
Denys Vlasenko | ebc41c9 | 2018-12-08 23:36:28 +0100 | [diff] [blame] | 4081 | } else { |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 4082 | s = zbc_parse_expr(p, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4083 | } |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4084 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 4085 | bc_parse_push(p, BC_INST_PRINT_POP); |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4086 | if (p->l.t.t != BC_LEX_COMMA) |
| 4087 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4088 | } |
| 4089 | |
Denys Vlasenko | 5d18f6b | 2018-12-16 21:08:30 +0100 | [diff] [blame] | 4090 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4091 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4092 | #define zbc_parse_print(...) (zbc_parse_print(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4093 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4094 | static BC_STATUS zbc_parse_return(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4095 | { |
| 4096 | BcStatus s; |
| 4097 | BcLexType t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4098 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4099 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4100 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4101 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4102 | |
| 4103 | t = p->l.t.t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4104 | if (t == BC_LEX_NLINE || t == BC_LEX_SCOLON) |
| 4105 | bc_parse_push(p, BC_INST_RET0); |
| 4106 | else { |
Denys Vlasenko | e9519e4 | 2018-12-16 17:06:07 +0100 | [diff] [blame] | 4107 | bool paren = (t == BC_LEX_LPAREN); |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 4108 | s = bc_parse_expr_empty_ok(p, 0); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4109 | if (s == BC_STATUS_PARSE_EMPTY_EXP) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4110 | bc_parse_push(p, BC_INST_RET0); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4111 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4112 | } |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4113 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4114 | |
| 4115 | if (!paren || p->l.t.last != BC_LEX_RPAREN) { |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 4116 | s = bc_POSIX_requires("parentheses around return expressions"); |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4117 | IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4118 | } |
| 4119 | |
| 4120 | bc_parse_push(p, BC_INST_RET); |
| 4121 | } |
| 4122 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4123 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4124 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4125 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4126 | #define zbc_parse_return(...) (zbc_parse_return(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4127 | |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4128 | static void rewrite_label_to_current(BcParse *p, size_t idx) |
| 4129 | { |
| 4130 | size_t *label = bc_vec_item(&p->func->labels, idx); |
| 4131 | *label = p->func->code.len; |
| 4132 | } |
| 4133 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4134 | static BC_STATUS zbc_parse_if(BcParse *p) |
| 4135 | { |
| 4136 | BcStatus s; |
Denys Vlasenko | 1585083 | 2018-12-16 21:40:54 +0100 | [diff] [blame] | 4137 | size_t ip_idx; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4138 | |
| 4139 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
| 4140 | s = zbc_lex_next(&p->l); |
| 4141 | if (s) RETURN_STATUS(s); |
| 4142 | if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
| 4143 | |
| 4144 | s = zbc_lex_next(&p->l); |
| 4145 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 4146 | s = zbc_parse_expr(p, BC_PARSE_REL); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4147 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4148 | if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4149 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 4150 | // Encode "if zero, jump to ..." |
| 4151 | // Pushed value (destination of the jump) is uninitialized, |
| 4152 | // will be rewritten to be address of "end of if()" or of "else". |
| 4153 | ip_idx = bc_vec_push(&p->func->labels, &ip_idx); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4154 | bc_parse_pushJUMP_ZERO(p, ip_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4155 | |
Denys Vlasenko | 59d4ce9 | 2018-12-17 10:42:31 +0100 | [diff] [blame] | 4156 | s = zbc_parse_stmt_allow_NLINE_before(p, STRING_if); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4157 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4158 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4159 | dbg_lex("%s:%d in if after stmt: p->l.t.t:%d", __func__, __LINE__, p->l.t.t); |
| 4160 | if (p->l.t.t == BC_LEX_KEY_ELSE) { |
Denys Vlasenko | 1585083 | 2018-12-16 21:40:54 +0100 | [diff] [blame] | 4161 | size_t ip2_idx; |
Denys Vlasenko | 7415633 | 2018-12-16 21:21:27 +0100 | [diff] [blame] | 4162 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 4163 | // Encode "after then_stmt, jump to end of if()" |
| 4164 | ip2_idx = bc_vec_push(&p->func->labels, &ip2_idx); |
| 4165 | dbg_lex("%s:%d after if() then_stmt: BC_INST_JUMP to %zd", __func__, __LINE__, ip2_idx); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4166 | bc_parse_pushJUMP(p, ip2_idx); |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4167 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4168 | dbg_lex("%s:%d rewriting 'if_zero' label to jump to 'else'-> %zd", __func__, __LINE__, p->func->code.len); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4169 | rewrite_label_to_current(p, ip_idx); |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4170 | |
Denys Vlasenko | 1585083 | 2018-12-16 21:40:54 +0100 | [diff] [blame] | 4171 | ip_idx = ip2_idx; |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4172 | |
Denys Vlasenko | 59d4ce9 | 2018-12-17 10:42:31 +0100 | [diff] [blame] | 4173 | s = zbc_parse_stmt_allow_NLINE_before(p, STRING_else); |
Denys Vlasenko | a50576a | 2018-12-16 19:21:57 +0100 | [diff] [blame] | 4174 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4175 | } |
| 4176 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4177 | dbg_lex("%s:%d rewriting label to jump after 'if' body-> %zd", __func__, __LINE__, p->func->code.len); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4178 | rewrite_label_to_current(p, ip_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4179 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4180 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
| 4181 | RETURN_STATUS(s); |
| 4182 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4183 | #define zbc_parse_if(...) (zbc_parse_if(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4184 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4185 | static BC_STATUS zbc_parse_while(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4186 | { |
| 4187 | BcStatus s; |
Denys Vlasenko | de24e9d | 2018-12-16 23:02:22 +0100 | [diff] [blame] | 4188 | size_t cond_idx; |
Denys Vlasenko | 5ebd2a6 | 2018-12-16 23:35:04 +0100 | [diff] [blame] | 4189 | size_t ip_idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4190 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4191 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4192 | if (s) RETURN_STATUS(s); |
| 4193 | if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4194 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4195 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4196 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 4197 | cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len); |
Denys Vlasenko | 5ebd2a6 | 2018-12-16 23:35:04 +0100 | [diff] [blame] | 4198 | ip_idx = cond_idx + 1; |
Denys Vlasenko | de24e9d | 2018-12-16 23:02:22 +0100 | [diff] [blame] | 4199 | bc_vec_push(&p->conds, &cond_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4200 | |
Denys Vlasenko | 5ebd2a6 | 2018-12-16 23:35:04 +0100 | [diff] [blame] | 4201 | bc_vec_push(&p->exits, &ip_idx); |
| 4202 | bc_vec_push(&p->func->labels, &ip_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4203 | |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 4204 | s = zbc_parse_expr(p, BC_PARSE_REL); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4205 | if (s) RETURN_STATUS(s); |
| 4206 | if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 4207 | |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4208 | bc_parse_pushJUMP_ZERO(p, ip_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4209 | |
Denys Vlasenko | 59d4ce9 | 2018-12-17 10:42:31 +0100 | [diff] [blame] | 4210 | s = zbc_parse_stmt_allow_NLINE_before(p, STRING_while); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4211 | if (s) RETURN_STATUS(s); |
| 4212 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4213 | dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, cond_idx); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4214 | bc_parse_pushJUMP(p, cond_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4215 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4216 | dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4217 | rewrite_label_to_current(p, ip_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4218 | |
| 4219 | bc_vec_pop(&p->exits); |
| 4220 | bc_vec_pop(&p->conds); |
| 4221 | |
| 4222 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4223 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4224 | #define zbc_parse_while(...) (zbc_parse_while(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4225 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4226 | static BC_STATUS zbc_parse_for(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4227 | { |
| 4228 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4229 | size_t cond_idx, exit_idx, body_idx, update_idx; |
| 4230 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4231 | dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4232 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4233 | if (s) RETURN_STATUS(s); |
| 4234 | if (p->l.t.t != BC_LEX_LPAREN) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4235 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4236 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4237 | |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4238 | if (p->l.t.t != BC_LEX_SCOLON) { |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 4239 | s = zbc_parse_expr(p, 0); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4240 | bc_parse_push(p, BC_INST_POP); |
| 4241 | if (s) RETURN_STATUS(s); |
| 4242 | } else { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 4243 | s = bc_POSIX_does_not_allow_empty_X_expression_in_for("init"); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4244 | IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s);) |
| 4245 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4246 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4247 | if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4248 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4249 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4250 | |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 4251 | cond_idx = bc_vec_push(&p->func->labels, &p->func->code.len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4252 | update_idx = cond_idx + 1; |
| 4253 | body_idx = update_idx + 1; |
| 4254 | exit_idx = body_idx + 1; |
| 4255 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4256 | if (p->l.t.t != BC_LEX_SCOLON) |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 4257 | s = zbc_parse_expr(p, BC_PARSE_REL); |
Denys Vlasenko | 52caa00 | 2018-12-21 00:35:22 +0100 | [diff] [blame] | 4258 | else { |
Denys Vlasenko | 447dc02 | 2018-12-21 00:39:02 +0100 | [diff] [blame] | 4259 | // Set this for the next call to bc_parse_pushNUM(). |
Denys Vlasenko | 52caa00 | 2018-12-21 00:35:22 +0100 | [diff] [blame] | 4260 | // This is safe to set because the current token is a semicolon, |
| 4261 | // which has no string requirement. |
| 4262 | bc_vec_string(&p->l.t.v, 1, "1"); |
| 4263 | bc_parse_pushNUM(p); |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 4264 | s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition"); |
Denys Vlasenko | 52caa00 | 2018-12-21 00:35:22 +0100 | [diff] [blame] | 4265 | } |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4266 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4267 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4268 | if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4269 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4270 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4271 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4272 | |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4273 | bc_parse_pushJUMP_ZERO(p, exit_idx); |
| 4274 | bc_parse_pushJUMP(p, body_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4275 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4276 | bc_vec_push(&p->conds, &update_idx); |
| 4277 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4278 | |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4279 | if (p->l.t.t != BC_LEX_RPAREN) { |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 4280 | s = zbc_parse_expr(p, 0); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4281 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 6ed7fb0 | 2018-12-21 22:16:17 +0100 | [diff] [blame] | 4282 | if (p->l.t.t != BC_LEX_RPAREN) RETURN_STATUS(bc_error_bad_token()); |
| 4283 | bc_parse_push(p, BC_INST_POP); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4284 | } else { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 4285 | s = bc_POSIX_does_not_allow_empty_X_expression_in_for("update"); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 4286 | IF_ERROR_RETURN_POSSIBLE(if (s) RETURN_STATUS(s);) |
| 4287 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4288 | |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4289 | bc_parse_pushJUMP(p, cond_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4290 | bc_vec_push(&p->func->labels, &p->func->code.len); |
| 4291 | |
Denys Vlasenko | 5ebd2a6 | 2018-12-16 23:35:04 +0100 | [diff] [blame] | 4292 | bc_vec_push(&p->exits, &exit_idx); |
| 4293 | bc_vec_push(&p->func->labels, &exit_idx); |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 4294 | |
Denys Vlasenko | 59d4ce9 | 2018-12-17 10:42:31 +0100 | [diff] [blame] | 4295 | s = zbc_parse_stmt_allow_NLINE_before(p, STRING_for); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4296 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4297 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4298 | dbg_lex("%s:%d BC_INST_JUMP to %zd", __func__, __LINE__, update_idx); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4299 | bc_parse_pushJUMP(p, update_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4300 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4301 | dbg_lex("%s:%d rewriting label-> %zd", __func__, __LINE__, p->func->code.len); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4302 | rewrite_label_to_current(p, exit_idx); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4303 | |
| 4304 | bc_vec_pop(&p->exits); |
| 4305 | bc_vec_pop(&p->conds); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4306 | |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4307 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4308 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4309 | #define zbc_parse_for(...) (zbc_parse_for(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4310 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4311 | static BC_STATUS zbc_parse_break_or_continue(BcParse *p, BcLexType type) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4312 | { |
| 4313 | BcStatus s; |
| 4314 | size_t i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4315 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4316 | if (type == BC_LEX_KEY_BREAK) { |
Denys Vlasenko | 8e7686e | 2018-12-16 23:18:28 +0100 | [diff] [blame] | 4317 | if (p->exits.len == 0) // none of the enclosing blocks is a loop |
| 4318 | RETURN_STATUS(bc_error_bad_token()); |
Denys Vlasenko | 266aa00 | 2018-12-16 23:24:25 +0100 | [diff] [blame] | 4319 | i = *(size_t*)bc_vec_top(&p->exits); |
Denys Vlasenko | 8e7686e | 2018-12-16 23:18:28 +0100 | [diff] [blame] | 4320 | } else { |
Denys Vlasenko | 266aa00 | 2018-12-16 23:24:25 +0100 | [diff] [blame] | 4321 | i = *(size_t*)bc_vec_top(&p->conds); |
Denys Vlasenko | 8e7686e | 2018-12-16 23:18:28 +0100 | [diff] [blame] | 4322 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4323 | |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4324 | bc_parse_pushJUMP(p, i); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4325 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4326 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4327 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4328 | |
| 4329 | if (p->l.t.t != BC_LEX_SCOLON && p->l.t.t != BC_LEX_NLINE) |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4330 | RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4331 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4332 | RETURN_STATUS(zbc_lex_next(&p->l)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4333 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4334 | #define zbc_parse_break_or_continue(...) (zbc_parse_break_or_continue(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4335 | |
Denys Vlasenko | e9519e4 | 2018-12-16 17:06:07 +0100 | [diff] [blame] | 4336 | static BC_STATUS zbc_parse_funcdef(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4337 | { |
| 4338 | BcStatus s; |
| 4339 | bool var, comma = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4340 | char *name; |
| 4341 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4342 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4343 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4344 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4345 | if (p->l.t.t != BC_LEX_NAME) |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4346 | RETURN_STATUS(bc_error("bad function definition")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4347 | |
| 4348 | name = xstrdup(p->l.t.v.v); |
Denys Vlasenko | 684d441 | 2018-12-19 14:57:23 +0100 | [diff] [blame] | 4349 | p->fidx = bc_program_addFunc(name); |
| 4350 | p->func = bc_program_func(p->fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4351 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4352 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4353 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4354 | if (p->l.t.t != BC_LEX_LPAREN) |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4355 | RETURN_STATUS(bc_error("bad function definition")); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4356 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4357 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4358 | |
| 4359 | while (p->l.t.t != BC_LEX_RPAREN) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4360 | if (p->l.t.t != BC_LEX_NAME) |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4361 | RETURN_STATUS(bc_error("bad function definition")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4362 | |
| 4363 | ++p->func->nparams; |
| 4364 | |
| 4365 | name = xstrdup(p->l.t.v.v); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4366 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4367 | if (s) goto err; |
| 4368 | |
| 4369 | var = p->l.t.t != BC_LEX_LBRACKET; |
| 4370 | |
| 4371 | if (!var) { |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4372 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4373 | if (s) goto err; |
| 4374 | |
| 4375 | if (p->l.t.t != BC_LEX_RBRACKET) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4376 | s = bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4377 | goto err; |
| 4378 | } |
| 4379 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4380 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4381 | if (s) goto err; |
| 4382 | } |
| 4383 | |
| 4384 | comma = p->l.t.t == BC_LEX_COMMA; |
| 4385 | if (comma) { |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4386 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4387 | if (s) goto err; |
| 4388 | } |
| 4389 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 4390 | s = zbc_func_insert(p->func, name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4391 | if (s) goto err; |
| 4392 | } |
| 4393 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4394 | if (comma) RETURN_STATUS(bc_error("bad function definition")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4395 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4396 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4397 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4398 | |
| 4399 | if (p->l.t.t != BC_LEX_LBRACE) |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 4400 | 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] | 4401 | |
Denys Vlasenko | 202dd19 | 2018-12-16 17:30:35 +0100 | [diff] [blame] | 4402 | // Prevent "define z()<newline>" from being interpreted as function with empty stmt as body |
| 4403 | s = zbc_lex_skip_if_at_NLINE(&p->l); |
| 4404 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4405 | //GNU bc requires a {} block even if function body has single stmt, enforce this? |
| 4406 | if (p->l.t.t != BC_LEX_LBRACE) |
| 4407 | RETURN_STATUS(bc_error("function { body } expected")); |
Denys Vlasenko | e9519e4 | 2018-12-16 17:06:07 +0100 | [diff] [blame] | 4408 | |
| 4409 | p->in_funcdef++; // to determine whether "return" stmt is allowed, and such |
Denys Vlasenko | 94f72a3 | 2018-12-17 00:07:48 +0100 | [diff] [blame] | 4410 | s = zbc_parse_stmt_possibly_auto(p, true); |
Denys Vlasenko | e9519e4 | 2018-12-16 17:06:07 +0100 | [diff] [blame] | 4411 | p->in_funcdef--; |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4412 | if (s) RETURN_STATUS(s); |
| 4413 | |
| 4414 | bc_parse_push(p, BC_INST_RET0); |
Denys Vlasenko | 65e1046 | 2018-12-19 15:13:14 +0100 | [diff] [blame] | 4415 | |
| 4416 | // Subsequent code generation is into main program |
| 4417 | p->fidx = BC_PROG_MAIN; |
| 4418 | p->func = bc_program_func_BC_PROG_MAIN(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4419 | |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4420 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4421 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4422 | err: |
Denys Vlasenko | f4f1072 | 2018-12-18 02:23:53 +0100 | [diff] [blame] | 4423 | dbg_lex_done("%s:%d done (error)", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4424 | free(name); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4425 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4426 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4427 | #define zbc_parse_funcdef(...) (zbc_parse_funcdef(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4428 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4429 | static BC_STATUS zbc_parse_auto(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4430 | { |
| 4431 | BcStatus s; |
| 4432 | bool comma, var, one; |
| 4433 | char *name; |
| 4434 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4435 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4436 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4437 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4438 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4439 | comma = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4440 | one = p->l.t.t == BC_LEX_NAME; |
| 4441 | |
| 4442 | while (p->l.t.t == BC_LEX_NAME) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4443 | name = xstrdup(p->l.t.v.v); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4444 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4445 | if (s) goto err; |
| 4446 | |
| 4447 | var = p->l.t.t != BC_LEX_LBRACKET; |
| 4448 | if (!var) { |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4449 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4450 | if (s) goto err; |
| 4451 | |
| 4452 | if (p->l.t.t != BC_LEX_RBRACKET) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4453 | s = bc_error("bad function definition"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4454 | goto err; |
| 4455 | } |
| 4456 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4457 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4458 | if (s) goto err; |
| 4459 | } |
| 4460 | |
| 4461 | comma = p->l.t.t == BC_LEX_COMMA; |
| 4462 | if (comma) { |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4463 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4464 | if (s) goto err; |
| 4465 | } |
| 4466 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 4467 | s = zbc_func_insert(p->func, name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4468 | if (s) goto err; |
| 4469 | } |
| 4470 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4471 | if (comma) RETURN_STATUS(bc_error("bad function definition")); |
| 4472 | if (!one) RETURN_STATUS(bc_error("no auto variable found")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4473 | |
| 4474 | if (p->l.t.t != BC_LEX_NLINE && p->l.t.t != BC_LEX_SCOLON) |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4475 | RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4476 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4477 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4478 | RETURN_STATUS(zbc_lex_next(&p->l)); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4479 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4480 | free(name); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4481 | dbg_lex_done("%s:%d done (ERROR)", __func__, __LINE__); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4482 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4483 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4484 | #define zbc_parse_auto(...) (zbc_parse_auto(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4485 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4486 | #undef zbc_parse_stmt_possibly_auto |
| 4487 | static BC_STATUS zbc_parse_stmt_possibly_auto(BcParse *p, bool auto_allowed) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4488 | { |
| 4489 | BcStatus s = BC_STATUS_SUCCESS; |
| 4490 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4491 | dbg_lex_enter("%s:%d entered, p->l.t.t:%d", __func__, __LINE__, p->l.t.t); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4492 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4493 | if (p->l.t.t == BC_LEX_NLINE) { |
| 4494 | dbg_lex_done("%s:%d done (seen BC_LEX_NLINE)", __func__, __LINE__); |
| 4495 | RETURN_STATUS(zbc_lex_next(&p->l)); |
| 4496 | } |
| 4497 | if (p->l.t.t == BC_LEX_SCOLON) { |
| 4498 | dbg_lex_done("%s:%d done (seen BC_LEX_SCOLON)", __func__, __LINE__); |
| 4499 | RETURN_STATUS(zbc_lex_next(&p->l)); |
| 4500 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4501 | |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4502 | if (p->l.t.t == BC_LEX_LBRACE) { |
| 4503 | dbg_lex("%s:%d BC_LEX_LBRACE: (auto_allowed:%d)", __func__, __LINE__, auto_allowed); |
| 4504 | do { |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4505 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4506 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4507 | } while (p->l.t.t == BC_LEX_NLINE); |
| 4508 | if (auto_allowed && p->l.t.t == BC_LEX_KEY_AUTO) { |
| 4509 | dbg_lex("%s:%d calling zbc_parse_auto()", __func__, __LINE__); |
| 4510 | s = zbc_parse_auto(p); |
| 4511 | if (s) RETURN_STATUS(s); |
| 4512 | } |
| 4513 | while (p->l.t.t != BC_LEX_RBRACE) { |
| 4514 | dbg_lex("%s:%d block parsing loop", __func__, __LINE__); |
| 4515 | s = zbc_parse_stmt(p); |
| 4516 | if (s) RETURN_STATUS(s); |
| 4517 | } |
| 4518 | s = zbc_lex_next(&p->l); |
| 4519 | dbg_lex_done("%s:%d done (seen BC_LEX_RBRACE)", __func__, __LINE__); |
| 4520 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4521 | } |
| 4522 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4523 | dbg_lex("%s:%d p->l.t.t:%d", __func__, __LINE__, p->l.t.t); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4524 | switch (p->l.t.t) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4525 | case BC_LEX_OP_INC: |
| 4526 | case BC_LEX_OP_DEC: |
| 4527 | case BC_LEX_OP_MINUS: |
| 4528 | case BC_LEX_OP_BOOL_NOT: |
| 4529 | case BC_LEX_LPAREN: |
| 4530 | case BC_LEX_NAME: |
| 4531 | case BC_LEX_NUMBER: |
| 4532 | case BC_LEX_KEY_IBASE: |
| 4533 | case BC_LEX_KEY_LAST: |
| 4534 | case BC_LEX_KEY_LENGTH: |
| 4535 | case BC_LEX_KEY_OBASE: |
| 4536 | case BC_LEX_KEY_READ: |
| 4537 | case BC_LEX_KEY_SCALE: |
| 4538 | case BC_LEX_KEY_SQRT: |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 4539 | s = zbc_parse_expr(p, BC_PARSE_PRINT); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4540 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4541 | case BC_LEX_STR: |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 4542 | s = zbc_parse_pushSTR(p); |
Denys Vlasenko | 44dbe67 | 2018-12-19 19:10:40 +0100 | [diff] [blame] | 4543 | bc_parse_push(p, BC_INST_PRINT_STR); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4544 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4545 | case BC_LEX_KEY_BREAK: |
| 4546 | case BC_LEX_KEY_CONTINUE: |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4547 | s = zbc_parse_break_or_continue(p, p->l.t.t); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4548 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4549 | case BC_LEX_KEY_FOR: |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4550 | s = zbc_parse_for(p); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4551 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4552 | case BC_LEX_KEY_HALT: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4553 | bc_parse_push(p, BC_INST_HALT); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4554 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4555 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4556 | case BC_LEX_KEY_IF: |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4557 | s = zbc_parse_if(p); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4558 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4559 | case BC_LEX_KEY_LIMITS: |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 4560 | // "limits" is a compile-time command, |
| 4561 | // the output is produced at _parse time_. |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 4562 | printf( |
| 4563 | "BC_BASE_MAX = "BC_MAX_OBASE_STR "\n" |
| 4564 | "BC_DIM_MAX = "BC_MAX_DIM_STR "\n" |
| 4565 | "BC_SCALE_MAX = "BC_MAX_SCALE_STR "\n" |
| 4566 | "BC_STRING_MAX = "BC_MAX_STRING_STR"\n" |
| 4567 | "BC_NAME_MAX = "BC_MAX_NAME_STR "\n" |
| 4568 | "BC_NUM_MAX = "BC_MAX_NUM_STR "\n" |
| 4569 | "MAX Exponent = "BC_MAX_EXP_STR "\n" |
| 4570 | "Number of vars = "BC_MAX_VARS_STR "\n" |
| 4571 | ); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4572 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4573 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4574 | case BC_LEX_KEY_PRINT: |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4575 | s = zbc_parse_print(p); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4576 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4577 | case BC_LEX_KEY_QUIT: |
Denys Vlasenko | cfdc133 | 2018-12-03 14:02:35 +0100 | [diff] [blame] | 4578 | // "quit" is a compile-time command. For example, |
| 4579 | // "if (0 == 1) quit" terminates when parsing the statement, |
| 4580 | // not when it is executed |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 4581 | QUIT_OR_RETURN_TO_MAIN; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4582 | case BC_LEX_KEY_RETURN: |
Denys Vlasenko | e9519e4 | 2018-12-16 17:06:07 +0100 | [diff] [blame] | 4583 | if (!p->in_funcdef) |
| 4584 | RETURN_STATUS(bc_error("'return' not in a function")); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4585 | s = zbc_parse_return(p); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4586 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4587 | case BC_LEX_KEY_WHILE: |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4588 | s = zbc_parse_while(p); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4589 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4590 | default: |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4591 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4592 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4593 | } |
| 4594 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4595 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4596 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4597 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4598 | #define zbc_parse_stmt_possibly_auto(...) (zbc_parse_stmt_possibly_auto(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4599 | |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 4600 | static BC_STATUS zbc_parse_stmt_or_funcdef(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4601 | { |
| 4602 | BcStatus s; |
| 4603 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4604 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4605 | if (p->l.t.t == BC_LEX_EOF) |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 4606 | s = bc_error("end of file"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4607 | else if (p->l.t.t == BC_LEX_KEY_DEFINE) { |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4608 | dbg_lex("%s:%d p->l.t.t:BC_LEX_KEY_DEFINE", __func__, __LINE__); |
Denys Vlasenko | e9519e4 | 2018-12-16 17:06:07 +0100 | [diff] [blame] | 4609 | s = zbc_parse_funcdef(p); |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4610 | } else { |
| 4611 | dbg_lex("%s:%d p->l.t.t:%d (not BC_LEX_KEY_DEFINE)", __func__, __LINE__, p->l.t.t); |
Denys Vlasenko | ae0faf9 | 2018-12-12 15:19:54 +0100 | [diff] [blame] | 4612 | s = zbc_parse_stmt(p); |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4613 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4614 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 4615 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4616 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4617 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4618 | #define zbc_parse_stmt_or_funcdef(...) (zbc_parse_stmt_or_funcdef(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4619 | |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 4620 | // This is not a "z" function: can also return BC_STATUS_PARSE_EMPTY_EXP |
Denys Vlasenko | 4b72aeb | 2018-12-17 16:54:37 +0100 | [diff] [blame] | 4621 | static BcStatus bc_parse_expr_empty_ok(BcParse *p, uint8_t flags) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4622 | { |
| 4623 | BcStatus s = BC_STATUS_SUCCESS; |
| 4624 | BcInst prev = BC_INST_PRINT; |
| 4625 | BcLexType top, t = p->l.t.t; |
| 4626 | size_t nexprs = 0, ops_bgn = p->ops.len; |
Denys Vlasenko | 18c6b54 | 2018-12-07 12:57:32 +0100 | [diff] [blame] | 4627 | unsigned nparens, nrelops; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4628 | bool paren_first, paren_expr, rprn, done, get_token, assign, bin_last; |
| 4629 | |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame] | 4630 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4631 | paren_first = p->l.t.t == BC_LEX_LPAREN; |
| 4632 | nparens = nrelops = 0; |
| 4633 | paren_expr = rprn = done = get_token = assign = false; |
| 4634 | bin_last = true; |
| 4635 | |
Denys Vlasenko | bcb62a7 | 2018-12-05 20:17:48 +0100 | [diff] [blame] | 4636 | for (; !G_interrupt && !s && !done && bc_parse_exprs(t); t = p->l.t.t) { |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 4637 | dbg_lex("%s:%d t:%d", __func__, __LINE__, t); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4638 | switch (t) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4639 | case BC_LEX_OP_INC: |
| 4640 | case BC_LEX_OP_DEC: |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4641 | s = zbc_parse_incdec(p, &prev, &paren_expr, &nexprs, flags); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4642 | rprn = get_token = bin_last = false; |
| 4643 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4644 | case BC_LEX_OP_MINUS: |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4645 | s = zbc_parse_minus(p, &prev, ops_bgn, rprn, &nexprs); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4646 | rprn = get_token = false; |
| 4647 | bin_last = prev == BC_INST_MINUS; |
| 4648 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4649 | case BC_LEX_OP_ASSIGN_POWER: |
| 4650 | case BC_LEX_OP_ASSIGN_MULTIPLY: |
| 4651 | case BC_LEX_OP_ASSIGN_DIVIDE: |
| 4652 | case BC_LEX_OP_ASSIGN_MODULUS: |
| 4653 | case BC_LEX_OP_ASSIGN_PLUS: |
| 4654 | case BC_LEX_OP_ASSIGN_MINUS: |
| 4655 | case BC_LEX_OP_ASSIGN: |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4656 | if (prev != BC_INST_VAR && prev != BC_INST_ARRAY_ELEM |
| 4657 | && prev != BC_INST_SCALE && prev != BC_INST_IBASE |
| 4658 | && prev != BC_INST_OBASE && prev != BC_INST_LAST |
| 4659 | ) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4660 | s = bc_error("bad assignment:" |
Denys Vlasenko | 0154d78 | 2018-12-14 23:32:51 +0100 | [diff] [blame] | 4661 | " left side must be variable" |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4662 | " or array element" |
Denys Vlasenko | 0154d78 | 2018-12-14 23:32:51 +0100 | [diff] [blame] | 4663 | ); // note: shared string |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4664 | break; |
| 4665 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4666 | // Fallthrough. |
| 4667 | case BC_LEX_OP_POWER: |
| 4668 | case BC_LEX_OP_MULTIPLY: |
| 4669 | case BC_LEX_OP_DIVIDE: |
| 4670 | case BC_LEX_OP_MODULUS: |
| 4671 | case BC_LEX_OP_PLUS: |
| 4672 | case BC_LEX_OP_REL_EQ: |
| 4673 | case BC_LEX_OP_REL_LE: |
| 4674 | case BC_LEX_OP_REL_GE: |
| 4675 | case BC_LEX_OP_REL_NE: |
| 4676 | case BC_LEX_OP_REL_LT: |
| 4677 | case BC_LEX_OP_REL_GT: |
| 4678 | case BC_LEX_OP_BOOL_NOT: |
| 4679 | case BC_LEX_OP_BOOL_OR: |
| 4680 | case BC_LEX_OP_BOOL_AND: |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4681 | if (((t == BC_LEX_OP_BOOL_NOT) != bin_last) |
| 4682 | || (t != BC_LEX_OP_BOOL_NOT && prev == BC_INST_BOOL_NOT) |
| 4683 | ) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4684 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4685 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4686 | nrelops += t >= BC_LEX_OP_REL_EQ && t <= BC_LEX_OP_REL_GT; |
Denys Vlasenko | 0154d78 | 2018-12-14 23:32:51 +0100 | [diff] [blame] | 4687 | prev = BC_TOKEN_2_INST(t); |
Denys Vlasenko | 7b1df3d | 2018-12-14 23:12:48 +0100 | [diff] [blame] | 4688 | bc_parse_operator(p, t, ops_bgn, &nexprs); |
| 4689 | s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4690 | rprn = get_token = false; |
| 4691 | bin_last = t != BC_LEX_OP_BOOL_NOT; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4692 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4693 | case BC_LEX_LPAREN: |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4694 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4695 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4696 | ++nparens; |
| 4697 | paren_expr = rprn = bin_last = false; |
| 4698 | get_token = true; |
| 4699 | bc_vec_push(&p->ops, &t); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4700 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4701 | case BC_LEX_RPAREN: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4702 | if (bin_last || prev == BC_INST_BOOL_NOT) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4703 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4704 | if (nparens == 0) { |
| 4705 | s = BC_STATUS_SUCCESS; |
| 4706 | done = true; |
| 4707 | get_token = false; |
| 4708 | break; |
| 4709 | } |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4710 | if (!paren_expr) { |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame] | 4711 | dbg_lex_done("%s:%d done (returning EMPTY_EXP)", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4712 | return BC_STATUS_PARSE_EMPTY_EXP; |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame] | 4713 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4714 | --nparens; |
| 4715 | paren_expr = rprn = true; |
| 4716 | get_token = bin_last = false; |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4717 | s = zbc_parse_rightParen(p, ops_bgn, &nexprs); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4718 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4719 | case BC_LEX_NAME: |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4720 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4721 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4722 | paren_expr = true; |
| 4723 | rprn = get_token = bin_last = false; |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4724 | s = zbc_parse_name(p, &prev, flags & ~BC_PARSE_NOCALL); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4725 | ++nexprs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4726 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4727 | case BC_LEX_NUMBER: |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4728 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4729 | return bc_error_bad_expression(); |
Denys Vlasenko | e3d3d20 | 2018-12-19 13:19:44 +0100 | [diff] [blame] | 4730 | bc_parse_pushNUM(p); |
Denys Vlasenko | 9dc5d08 | 2018-12-16 18:43:51 +0100 | [diff] [blame] | 4731 | nexprs++; |
| 4732 | prev = BC_INST_NUM; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4733 | paren_expr = get_token = true; |
| 4734 | rprn = bin_last = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4735 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4736 | case BC_LEX_KEY_IBASE: |
| 4737 | case BC_LEX_KEY_LAST: |
| 4738 | case BC_LEX_KEY_OBASE: |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4739 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4740 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4741 | prev = (char) (t - BC_LEX_KEY_IBASE + BC_INST_IBASE); |
| 4742 | bc_parse_push(p, (char) prev); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4743 | paren_expr = get_token = true; |
| 4744 | rprn = bin_last = false; |
| 4745 | ++nexprs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4746 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4747 | case BC_LEX_KEY_LENGTH: |
| 4748 | case BC_LEX_KEY_SQRT: |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4749 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4750 | return bc_error_bad_expression(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4751 | s = zbc_parse_builtin(p, t, flags, &prev); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4752 | paren_expr = true; |
| 4753 | rprn = get_token = bin_last = false; |
| 4754 | ++nexprs; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4755 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4756 | case BC_LEX_KEY_READ: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4757 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4758 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4759 | else |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4760 | s = zbc_parse_read(p); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4761 | paren_expr = true; |
| 4762 | rprn = get_token = bin_last = false; |
| 4763 | ++nexprs; |
| 4764 | prev = BC_INST_READ; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4765 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4766 | case BC_LEX_KEY_SCALE: |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4767 | if (BC_PARSE_LEAF(prev, rprn)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4768 | return bc_error_bad_expression(); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 4769 | s = zbc_parse_scale(p, &prev, flags); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4770 | paren_expr = true; |
| 4771 | rprn = get_token = bin_last = false; |
| 4772 | ++nexprs; |
| 4773 | prev = BC_INST_SCALE; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4774 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4775 | default: |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4776 | s = bc_error_bad_token(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4777 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4778 | } |
| 4779 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4780 | if (!s && get_token) s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4781 | } |
| 4782 | |
| 4783 | if (s) return s; |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 4784 | if (G_interrupt) return BC_STATUS_FAILURE; // ^C: stop parsing |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4785 | |
| 4786 | while (p->ops.len > ops_bgn) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4787 | top = BC_PARSE_TOP_OP(p); |
| 4788 | assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN; |
| 4789 | |
| 4790 | if (top == BC_LEX_LPAREN || top == BC_LEX_RPAREN) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4791 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4792 | |
Denys Vlasenko | 0154d78 | 2018-12-14 23:32:51 +0100 | [diff] [blame] | 4793 | bc_parse_push(p, BC_TOKEN_2_INST(top)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4794 | |
| 4795 | nexprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_NEG; |
| 4796 | bc_vec_pop(&p->ops); |
| 4797 | } |
| 4798 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4799 | if (prev == BC_INST_BOOL_NOT || nexprs != 1) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 4800 | return bc_error_bad_expression(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4801 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4802 | if (!(flags & BC_PARSE_REL) && nrelops) { |
Denys Vlasenko | 0064679 | 2018-12-05 18:12:27 +0100 | [diff] [blame] | 4803 | s = bc_POSIX_does_not_allow("comparison operators outside if or loops"); |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4804 | IF_ERROR_RETURN_POSSIBLE(if (s) return s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 4805 | } else if ((flags & BC_PARSE_REL) && nrelops > 1) { |
Denys Vlasenko | a6f84e1 | 2018-12-06 11:10:11 +0100 | [diff] [blame] | 4806 | s = bc_POSIX_requires("exactly one comparison operator per condition"); |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4807 | IF_ERROR_RETURN_POSSIBLE(if (s) return s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4808 | } |
| 4809 | |
| 4810 | if (flags & BC_PARSE_PRINT) { |
| 4811 | if (paren_first || !assign) bc_parse_push(p, BC_INST_PRINT); |
| 4812 | bc_parse_push(p, BC_INST_POP); |
| 4813 | } |
| 4814 | |
Denys Vlasenko | 17df882 | 2018-12-14 23:00:24 +0100 | [diff] [blame] | 4815 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4816 | return s; |
| 4817 | } |
| 4818 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4819 | #endif // ENABLE_BC |
| 4820 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 4821 | #if ENABLE_DC |
Denys Vlasenko | cca79a0 | 2018-12-05 21:15:46 +0100 | [diff] [blame] | 4822 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4823 | static BC_STATUS zdc_parse_register(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4824 | { |
| 4825 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4826 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4827 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4828 | if (s) RETURN_STATUS(s); |
| 4829 | if (p->l.t.t != BC_LEX_NAME) RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4830 | |
Denys Vlasenko | e6c40c4 | 2018-12-16 20:32:58 +0100 | [diff] [blame] | 4831 | bc_parse_pushName(p, p->l.t.v.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4832 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4833 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4834 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4835 | #define zdc_parse_register(...) (zdc_parse_register(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4836 | |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 4837 | static void dc_parse_string(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4838 | { |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4839 | char *str; |
Denys Vlasenko | 684d441 | 2018-12-19 14:57:23 +0100 | [diff] [blame] | 4840 | size_t len = G.prog.strs.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4841 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4842 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4843 | |
| 4844 | str = xstrdup(p->l.t.v.v); |
| 4845 | bc_parse_push(p, BC_INST_STR); |
| 4846 | bc_parse_pushIndex(p, len); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 4847 | bc_vec_push(&G.prog.strs, &str); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4848 | |
| 4849 | // Explanation needed here |
| 4850 | bc_program_add_fn(); |
Denys Vlasenko | 684d441 | 2018-12-19 14:57:23 +0100 | [diff] [blame] | 4851 | p->func = bc_program_func(p->fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4852 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4853 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4854 | } |
| 4855 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4856 | static BC_STATUS zdc_parse_mem(BcParse *p, uint8_t inst, bool name, bool store) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4857 | { |
| 4858 | BcStatus s; |
| 4859 | |
| 4860 | bc_parse_push(p, inst); |
| 4861 | if (name) { |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4862 | s = zdc_parse_register(p); |
| 4863 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4864 | } |
| 4865 | |
| 4866 | if (store) { |
| 4867 | bc_parse_push(p, BC_INST_SWAP); |
| 4868 | bc_parse_push(p, BC_INST_ASSIGN); |
| 4869 | bc_parse_push(p, BC_INST_POP); |
| 4870 | } |
| 4871 | |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 4872 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4873 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4874 | #define zdc_parse_mem(...) (zdc_parse_mem(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4875 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4876 | static BC_STATUS zdc_parse_cond(BcParse *p, uint8_t inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4877 | { |
| 4878 | BcStatus s; |
| 4879 | |
| 4880 | bc_parse_push(p, inst); |
| 4881 | bc_parse_push(p, BC_INST_EXEC_COND); |
| 4882 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4883 | s = zdc_parse_register(p); |
| 4884 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4885 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4886 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4887 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4888 | |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 4889 | // Note that 'else' part can not be on the next line: |
| 4890 | // echo -e '[1p]sa [2p]sb 2 1>a eb' | dc - OK, prints "2" |
| 4891 | // echo -e '[1p]sa [2p]sb 2 1>a\neb' | dc - parse error |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4892 | if (p->l.t.t == BC_LEX_ELSE) { |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4893 | s = zdc_parse_register(p); |
| 4894 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4895 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 4896 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4897 | bc_parse_push(p, BC_PARSE_STREND); |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 4898 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4899 | |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4900 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4901 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4902 | #define zdc_parse_cond(...) (zdc_parse_cond(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4903 | |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 4904 | static BC_STATUS zdc_parse_token(BcParse *p, BcLexType t) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4905 | { |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 4906 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4907 | uint8_t inst; |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 4908 | bool assign, get_token; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4909 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4910 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 4911 | s = BC_STATUS_SUCCESS; |
| 4912 | get_token = true; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4913 | switch (t) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4914 | case BC_LEX_OP_REL_EQ: |
| 4915 | case BC_LEX_OP_REL_LE: |
| 4916 | case BC_LEX_OP_REL_GE: |
| 4917 | case BC_LEX_OP_REL_NE: |
| 4918 | case BC_LEX_OP_REL_LT: |
| 4919 | case BC_LEX_OP_REL_GT: |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4920 | s = zdc_parse_cond(p, t - BC_LEX_OP_REL_EQ + BC_INST_REL_EQ); |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 4921 | get_token = false; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4922 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4923 | case BC_LEX_SCOLON: |
| 4924 | case BC_LEX_COLON: |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4925 | s = zdc_parse_mem(p, BC_INST_ARRAY_ELEM, true, t == BC_LEX_COLON); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4926 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4927 | case BC_LEX_STR: |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4928 | dbg_lex("%s:%d LEX_STR", __func__, __LINE__); |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 4929 | dc_parse_string(p); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4930 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4931 | case BC_LEX_NEG: |
| 4932 | case BC_LEX_NUMBER: |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4933 | dbg_lex("%s:%d LEX_NEG/NUMBER", __func__, __LINE__); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4934 | if (t == BC_LEX_NEG) { |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4935 | s = zbc_lex_next(&p->l); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4936 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 4937 | if (p->l.t.t != BC_LEX_NUMBER) |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4938 | RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4939 | } |
Denys Vlasenko | e3d3d20 | 2018-12-19 13:19:44 +0100 | [diff] [blame] | 4940 | bc_parse_pushNUM(p); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4941 | if (t == BC_LEX_NEG) bc_parse_push(p, BC_INST_NEG); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4942 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4943 | case BC_LEX_KEY_READ: |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 4944 | bc_parse_push(p, BC_INST_READ); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4945 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4946 | case BC_LEX_OP_ASSIGN: |
| 4947 | case BC_LEX_STORE_PUSH: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4948 | assign = t == BC_LEX_OP_ASSIGN; |
| 4949 | inst = assign ? BC_INST_VAR : BC_INST_PUSH_TO_VAR; |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4950 | s = zdc_parse_mem(p, inst, true, assign); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4951 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4952 | case BC_LEX_LOAD: |
| 4953 | case BC_LEX_LOAD_POP: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4954 | inst = t == BC_LEX_LOAD_POP ? BC_INST_PUSH_VAR : BC_INST_LOAD; |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4955 | s = zdc_parse_mem(p, inst, true, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4956 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4957 | case BC_LEX_STORE_IBASE: |
| 4958 | case BC_LEX_STORE_SCALE: |
| 4959 | case BC_LEX_STORE_OBASE: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4960 | inst = t - BC_LEX_STORE_IBASE + BC_INST_IBASE; |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4961 | s = zdc_parse_mem(p, inst, false, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4962 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4963 | default: |
Denys Vlasenko | 5daa1a0 | 2018-12-22 16:40:38 +0100 | [diff] [blame] | 4964 | dbg_lex_done("%s:%d done (bad token)", __func__, __LINE__); |
| 4965 | RETURN_STATUS(bc_error_bad_token()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4966 | } |
| 4967 | |
Denys Vlasenko | 9a34e89 | 2018-12-12 13:58:55 +0100 | [diff] [blame] | 4968 | if (!s && get_token) s = zbc_lex_next(&p->l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4969 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4970 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | 8cd468f | 2018-12-12 14:54:38 +0100 | [diff] [blame] | 4971 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4972 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 4973 | #define zdc_parse_token(...) (zdc_parse_token(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4974 | |
Denys Vlasenko | 39287e0 | 2018-12-22 02:23:08 +0100 | [diff] [blame] | 4975 | static BC_STATUS zdc_parse_expr(BcParse *p) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4976 | { |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 4977 | BcInst inst; |
| 4978 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4979 | |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 4980 | inst = dc_parse_insts[p->l.t.t]; |
| 4981 | if (inst != BC_INST_INVALID) { |
| 4982 | bc_parse_push(p, inst); |
| 4983 | s = zbc_lex_next(&p->l); |
| 4984 | } else { |
| 4985 | s = zdc_parse_token(p, p->l.t.t); |
| 4986 | } |
| 4987 | RETURN_STATUS(s); |
| 4988 | } |
| 4989 | #define zdc_parse_expr(...) (zdc_parse_expr(__VA_ARGS__) COMMA_SUCCESS) |
| 4990 | |
| 4991 | static BC_STATUS zdc_parse_exprs_until_eof(BcParse *p) |
| 4992 | { |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 4993 | dbg_lex_enter("%s:%d entered, p->l.t.t:%d", __func__, __LINE__, p->l.t.t); |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 4994 | while (p->l.t.t != BC_LEX_EOF) { |
| 4995 | BcStatus s = zdc_parse_expr(p); |
Denys Vlasenko | a199cc9 | 2018-12-18 14:11:35 +0100 | [diff] [blame] | 4996 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 4997 | } |
| 4998 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 4999 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | a199cc9 | 2018-12-18 14:11:35 +0100 | [diff] [blame] | 5000 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5001 | } |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5002 | #define zdc_parse_exprs_until_eof(...) (zdc_parse_exprs_until_eof(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5003 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5004 | #endif // ENABLE_DC |
| 5005 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5006 | static BcVec* bc_program_search(char *id, bool var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5007 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5008 | BcId e, *ptr; |
| 5009 | BcVec *v, *map; |
| 5010 | size_t i; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 5011 | int new; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5012 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5013 | v = var ? &G.prog.vars : &G.prog.arrs; |
| 5014 | map = var ? &G.prog.var_map : &G.prog.arr_map; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5015 | |
| 5016 | e.name = id; |
| 5017 | e.idx = v->len; |
Denys Vlasenko | a02f844 | 2018-12-03 20:35:16 +0100 | [diff] [blame] | 5018 | new = bc_map_insert(map, &e, &i); // 1 if insertion was successful |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5019 | |
| 5020 | if (new) { |
Denys Vlasenko | f36a0ad | 2018-12-19 17:15:04 +0100 | [diff] [blame] | 5021 | BcVec v2; |
| 5022 | bc_array_init(&v2, var); |
| 5023 | bc_vec_push(v, &v2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5024 | } |
| 5025 | |
| 5026 | ptr = bc_vec_item(map, i); |
| 5027 | if (new) ptr->name = xstrdup(e.name); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5028 | return bc_vec_item(v, ptr->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5029 | } |
| 5030 | |
Denys Vlasenko | 8287b1c | 2018-12-21 22:43:53 +0100 | [diff] [blame] | 5031 | // 'num' need not be initialized on entry |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5032 | static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5033 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5034 | switch (r->t) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5035 | case BC_RESULT_STR: |
| 5036 | case BC_RESULT_TEMP: |
| 5037 | case BC_RESULT_IBASE: |
| 5038 | case BC_RESULT_SCALE: |
| 5039 | case BC_RESULT_OBASE: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5040 | *num = &r->d.n; |
| 5041 | break; |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5042 | case BC_RESULT_CONSTANT: { |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5043 | BcStatus s; |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 5044 | char *str; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5045 | unsigned base_t; |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 5046 | size_t len; |
| 5047 | |
| 5048 | str = *bc_program_const(r->d.id.idx); |
| 5049 | len = strlen(str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5050 | |
| 5051 | bc_num_init(&r->d.n, len); |
| 5052 | |
| 5053 | hex = hex && len == 1; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5054 | base_t = hex ? 16 : G.prog.ib_t; |
Denys Vlasenko | e3d3d20 | 2018-12-19 13:19:44 +0100 | [diff] [blame] | 5055 | s = zbc_num_parse(&r->d.n, str, base_t); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5056 | if (s) { |
| 5057 | bc_num_free(&r->d.n); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5058 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5059 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5060 | *num = &r->d.n; |
| 5061 | r->t = BC_RESULT_TEMP; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5062 | break; |
| 5063 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5064 | case BC_RESULT_VAR: |
| 5065 | case BC_RESULT_ARRAY: |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5066 | case BC_RESULT_ARRAY_ELEM: { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5067 | BcVec *v; |
| 5068 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5069 | 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] | 5070 | |
| 5071 | if (r->t == BC_RESULT_ARRAY_ELEM) { |
| 5072 | v = bc_vec_top(v); |
| 5073 | if (v->len <= r->d.id.idx) bc_array_expand(v, r->d.id.idx + 1); |
| 5074 | *num = bc_vec_item(v, r->d.id.idx); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5075 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5076 | *num = bc_vec_top(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5077 | break; |
| 5078 | } |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 5079 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5080 | case BC_RESULT_LAST: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5081 | *num = &G.prog.last; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5082 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5083 | case BC_RESULT_ONE: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5084 | *num = &G.prog.one; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5085 | break; |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 5086 | #endif |
| 5087 | default: |
| 5088 | // Testing the theory that dc does not reach LAST/ONE |
| 5089 | bb_error_msg_and_die("BUG:%d", r->t); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5090 | } |
| 5091 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5092 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5093 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5094 | #define zbc_program_num(...) (zbc_program_num(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5095 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5096 | static BC_STATUS zbc_program_binOpPrep(BcResult **l, BcNum **ln, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5097 | BcResult **r, BcNum **rn, bool assign) |
| 5098 | { |
| 5099 | BcStatus s; |
| 5100 | bool hex; |
| 5101 | BcResultType lt, rt; |
| 5102 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5103 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5104 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5105 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5106 | *r = bc_vec_item_rev(&G.prog.results, 0); |
| 5107 | *l = bc_vec_item_rev(&G.prog.results, 1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5108 | |
| 5109 | lt = (*l)->t; |
| 5110 | rt = (*r)->t; |
| 5111 | hex = assign && (lt == BC_RESULT_IBASE || lt == BC_RESULT_OBASE); |
| 5112 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5113 | s = zbc_program_num(*l, ln, false); |
| 5114 | if (s) RETURN_STATUS(s); |
| 5115 | s = zbc_program_num(*r, rn, hex); |
| 5116 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5117 | |
| 5118 | // We run this again under these conditions in case any vector has been |
| 5119 | // reallocated out from under the BcNums or arrays we had. |
| 5120 | if (lt == rt && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM)) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5121 | s = zbc_program_num(*l, ln, false); |
| 5122 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5123 | } |
| 5124 | |
| 5125 | if (!BC_PROG_NUM((*l), (*ln)) && (!assign || (*l)->t != BC_RESULT_VAR)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5126 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5127 | if (!assign && !BC_PROG_NUM((*r), (*ln))) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5128 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5129 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5130 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5131 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5132 | #define zbc_program_binOpPrep(...) (zbc_program_binOpPrep(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5133 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5134 | static void bc_program_binOpRetire(BcResult *r) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5135 | { |
| 5136 | r->t = BC_RESULT_TEMP; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5137 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 5138 | bc_result_pop_and_push(r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5139 | } |
| 5140 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5141 | static BC_STATUS zbc_program_prep(BcResult **r, BcNum **n) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5142 | { |
| 5143 | BcStatus s; |
| 5144 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5145 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5146 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5147 | *r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5148 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5149 | s = zbc_program_num(*r, n, false); |
| 5150 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5151 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5152 | if (!BC_PROG_NUM((*r), (*n))) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5153 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5154 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5155 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5156 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5157 | #define zbc_program_prep(...) (zbc_program_prep(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5158 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5159 | static void bc_program_retire(BcResult *r, BcResultType t) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5160 | { |
| 5161 | r->t = t; |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 5162 | bc_result_pop_and_push(r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5163 | } |
| 5164 | |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5165 | static BC_STATUS zbc_program_op(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5166 | { |
| 5167 | BcStatus s; |
| 5168 | BcResult *opd1, *opd2, res; |
| 5169 | BcNum *n1, *n2 = NULL; |
| 5170 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5171 | s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5172 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5173 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5174 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5175 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5176 | IF_ERROR_RETURN_POSSIBLE(s =) zbc_program_ops[inst - BC_INST_POWER](n1, n2, &res.d.n, G.prog.scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5177 | if (s) goto err; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5178 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5179 | |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5180 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5181 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5182 | bc_num_free(&res.d.n); |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5183 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5184 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5185 | #define zbc_program_op(...) (zbc_program_op(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5186 | |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 5187 | static BC_STATUS zbc_program_read(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5188 | { |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5189 | const char *sv_file; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5190 | BcStatus s; |
| 5191 | BcParse parse; |
| 5192 | BcVec buf; |
| 5193 | BcInstPtr ip; |
Denys Vlasenko | 69171dc | 2018-12-12 00:29:24 +0100 | [diff] [blame] | 5194 | BcFunc *f; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5195 | |
Denys Vlasenko | 69171dc | 2018-12-12 00:29:24 +0100 | [diff] [blame] | 5196 | f = bc_program_func(BC_PROG_READ); |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 5197 | bc_vec_pop_all(&f->code); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5198 | |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5199 | sv_file = G.prog.file; |
| 5200 | G.prog.file = NULL; |
| 5201 | |
Denys Vlasenko | 82ea67f | 2018-12-13 19:23:45 +0100 | [diff] [blame] | 5202 | bc_char_vec_init(&buf); |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 5203 | bc_read_line(&buf, stdin); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5204 | |
Denys Vlasenko | c2265f5 | 2018-12-12 23:10:08 +0100 | [diff] [blame] | 5205 | bc_parse_create(&parse, BC_PROG_READ); |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 5206 | bc_lex_file(&parse.l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5207 | |
Denys Vlasenko | f86e960 | 2018-12-14 17:01:56 +0100 | [diff] [blame] | 5208 | s = zbc_parse_text_init(&parse, buf.v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5209 | if (s) goto exec_err; |
Denys Vlasenko | 514967d | 2018-12-22 03:38:52 +0100 | [diff] [blame] | 5210 | if (IS_BC) { |
| 5211 | IF_BC(s = zbc_parse_expr(&parse, 0)); |
| 5212 | } else { |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 5213 | IF_DC(s = zdc_parse_exprs_until_eof(&parse)); |
Denys Vlasenko | 514967d | 2018-12-22 03:38:52 +0100 | [diff] [blame] | 5214 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5215 | if (s) goto exec_err; |
| 5216 | |
| 5217 | 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] | 5218 | s = bc_error("bad read() expression"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5219 | goto exec_err; |
| 5220 | } |
| 5221 | |
| 5222 | ip.func = BC_PROG_READ; |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 5223 | ip.inst_idx = 0; |
| 5224 | IF_BC(ip.results_len_before_call = G.prog.results.len;) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5225 | |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 5226 | bc_parse_push(&parse, BC_INST_RET); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 5227 | bc_vec_push(&G.prog.exestack, &ip); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5228 | exec_err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5229 | bc_parse_free(&parse); |
Denys Vlasenko | 4dd3652 | 2018-12-11 22:26:38 +0100 | [diff] [blame] | 5230 | G.prog.file = sv_file; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5231 | bc_vec_free(&buf); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 5232 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5233 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5234 | #define zbc_program_read(...) (zbc_program_read(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5235 | |
| 5236 | static size_t bc_program_index(char *code, size_t *bgn) |
| 5237 | { |
Denys Vlasenko | 3f940c9 | 2018-12-18 15:49:42 +0100 | [diff] [blame] | 5238 | unsigned char *bytes = (void*)(code + *bgn); |
| 5239 | unsigned amt; |
| 5240 | unsigned i; |
| 5241 | size_t res; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5242 | |
Denys Vlasenko | 3f940c9 | 2018-12-18 15:49:42 +0100 | [diff] [blame] | 5243 | amt = *bytes++; |
| 5244 | *bgn += amt + 1; |
| 5245 | |
| 5246 | amt *= 8; |
| 5247 | res = 0; |
| 5248 | for (i = 0; i < amt; i += 8) |
| 5249 | res |= (size_t)(*bytes++) << i; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5250 | |
| 5251 | return res; |
| 5252 | } |
| 5253 | |
| 5254 | static char *bc_program_name(char *code, size_t *bgn) |
| 5255 | { |
| 5256 | size_t i; |
Denys Vlasenko | 694d298 | 2018-12-18 19:17:11 +0100 | [diff] [blame] | 5257 | char *s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5258 | |
Denys Vlasenko | 694d298 | 2018-12-18 19:17:11 +0100 | [diff] [blame] | 5259 | code += *bgn; |
| 5260 | s = xmalloc(strchr(code, BC_PARSE_STREND) - code + 1); |
Denys Vlasenko | 6b0fbd1 | 2018-12-18 12:55:40 +0100 | [diff] [blame] | 5261 | i = 0; |
| 5262 | for (;;) { |
Denys Vlasenko | 694d298 | 2018-12-18 19:17:11 +0100 | [diff] [blame] | 5263 | char c = *code++; |
| 5264 | if (c == BC_PARSE_STREND) |
Denys Vlasenko | 6b0fbd1 | 2018-12-18 12:55:40 +0100 | [diff] [blame] | 5265 | break; |
| 5266 | s[i++] = c; |
| 5267 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5268 | s[i] = '\0'; |
Denys Vlasenko | 694d298 | 2018-12-18 19:17:11 +0100 | [diff] [blame] | 5269 | *bgn += i + 1; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5270 | |
| 5271 | return s; |
| 5272 | } |
| 5273 | |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 5274 | static void bc_program_printString(const char *str) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5275 | { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5276 | #if ENABLE_DC |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5277 | if (!str[0]) { |
Denys Vlasenko | 2c6f563 | 2018-12-11 21:21:14 +0100 | [diff] [blame] | 5278 | // Example: echo '[]ap' | dc |
| 5279 | // should print two bytes: 0x00, 0x0A |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5280 | bb_putchar('\0'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5281 | return; |
| 5282 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5283 | #endif |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5284 | while (*str) { |
| 5285 | int c = *str++; |
| 5286 | if (c != '\\' || !*str) |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5287 | bb_putchar(c); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5288 | else { |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5289 | c = *str++; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5290 | switch (c) { |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5291 | case 'a': |
| 5292 | bb_putchar('\a'); |
| 5293 | break; |
| 5294 | case 'b': |
| 5295 | bb_putchar('\b'); |
| 5296 | break; |
| 5297 | case '\\': |
| 5298 | case 'e': |
| 5299 | bb_putchar('\\'); |
| 5300 | break; |
| 5301 | case 'f': |
| 5302 | bb_putchar('\f'); |
| 5303 | break; |
| 5304 | case 'n': |
| 5305 | bb_putchar('\n'); |
| 5306 | G.prog.nchars = SIZE_MAX; |
| 5307 | break; |
| 5308 | case 'r': |
| 5309 | bb_putchar('\r'); |
| 5310 | break; |
| 5311 | case 'q': |
| 5312 | bb_putchar('"'); |
| 5313 | break; |
| 5314 | case 't': |
| 5315 | bb_putchar('\t'); |
| 5316 | break; |
| 5317 | default: |
| 5318 | // Just print the backslash and following character. |
| 5319 | bb_putchar('\\'); |
| 5320 | ++G.prog.nchars; |
| 5321 | bb_putchar(c); |
| 5322 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5323 | } |
| 5324 | } |
Denys Vlasenko | 9f657e0 | 2018-12-11 19:52:25 +0100 | [diff] [blame] | 5325 | ++G.prog.nchars; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5326 | } |
| 5327 | } |
| 5328 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5329 | static void bc_num_printNewline(void) |
| 5330 | { |
| 5331 | if (G.prog.nchars == G.prog.len - 1) { |
| 5332 | bb_putchar('\\'); |
| 5333 | bb_putchar('\n'); |
| 5334 | G.prog.nchars = 0; |
| 5335 | } |
| 5336 | } |
| 5337 | |
| 5338 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5339 | static FAST_FUNC void dc_num_printChar(size_t num, size_t width, bool radix) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5340 | { |
| 5341 | (void) radix; |
| 5342 | bb_putchar((char) num); |
| 5343 | G.prog.nchars += width; |
| 5344 | } |
| 5345 | #endif |
| 5346 | |
| 5347 | static FAST_FUNC void bc_num_printDigits(size_t num, size_t width, bool radix) |
| 5348 | { |
| 5349 | size_t exp, pow; |
| 5350 | |
| 5351 | bc_num_printNewline(); |
| 5352 | bb_putchar(radix ? '.' : ' '); |
| 5353 | ++G.prog.nchars; |
| 5354 | |
| 5355 | bc_num_printNewline(); |
| 5356 | for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10) |
| 5357 | continue; |
| 5358 | |
| 5359 | for (exp = 0; exp < width; pow /= 10, ++G.prog.nchars, ++exp) { |
| 5360 | size_t dig; |
| 5361 | bc_num_printNewline(); |
| 5362 | dig = num / pow; |
| 5363 | num -= dig * pow; |
| 5364 | bb_putchar(((char) dig) + '0'); |
| 5365 | } |
| 5366 | } |
| 5367 | |
| 5368 | static FAST_FUNC void bc_num_printHex(size_t num, size_t width, bool radix) |
| 5369 | { |
| 5370 | if (radix) { |
| 5371 | bc_num_printNewline(); |
| 5372 | bb_putchar('.'); |
Denys Vlasenko | 30a8e0c | 2018-12-18 19:20:04 +0100 | [diff] [blame] | 5373 | G.prog.nchars++; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5374 | } |
| 5375 | |
| 5376 | bc_num_printNewline(); |
| 5377 | bb_putchar(bb_hexdigits_upcase[num]); |
| 5378 | G.prog.nchars += width; |
| 5379 | } |
| 5380 | |
| 5381 | static void bc_num_printDecimal(BcNum *n) |
| 5382 | { |
| 5383 | size_t i, rdx = n->rdx - 1; |
| 5384 | |
Denys Vlasenko | 30a8e0c | 2018-12-18 19:20:04 +0100 | [diff] [blame] | 5385 | if (n->neg) { |
| 5386 | bb_putchar('-'); |
| 5387 | G.prog.nchars++; |
| 5388 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5389 | |
| 5390 | for (i = n->len - 1; i < n->len; --i) |
| 5391 | bc_num_printHex((size_t) n->num[i], 1, i == rdx); |
| 5392 | } |
| 5393 | |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5394 | static BC_STATUS zbc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNumDigitOp print) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5395 | { |
| 5396 | BcStatus s; |
| 5397 | BcVec stack; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5398 | BcNum base; |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 5399 | BcDig base_digs[ULONG_NUM_BUFSIZE]; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5400 | BcNum intp, fracp, digit, frac_len; |
| 5401 | unsigned long dig, *ptr; |
| 5402 | size_t i; |
| 5403 | bool radix; |
| 5404 | |
| 5405 | if (n->len == 0) { |
| 5406 | print(0, width, false); |
| 5407 | RETURN_STATUS(BC_STATUS_SUCCESS); |
| 5408 | } |
| 5409 | |
| 5410 | bc_vec_init(&stack, sizeof(long), NULL); |
| 5411 | bc_num_init(&intp, n->len); |
| 5412 | bc_num_init(&fracp, n->rdx); |
| 5413 | bc_num_init(&digit, width); |
| 5414 | bc_num_init(&frac_len, BC_NUM_INT(n)); |
| 5415 | bc_num_copy(&intp, n); |
| 5416 | bc_num_one(&frac_len); |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 5417 | base.cap = ARRAY_SIZE(base_digs); |
| 5418 | base.num = base_digs; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5419 | bc_num_ulong2num(&base, base_t); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5420 | |
| 5421 | bc_num_truncate(&intp, intp.rdx); |
| 5422 | s = zbc_num_sub(n, &intp, &fracp, 0); |
| 5423 | if (s) goto err; |
| 5424 | |
| 5425 | while (intp.len != 0) { |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5426 | s = zbc_num_divmod(&intp, &base, &intp, &digit, 0); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5427 | if (s) goto err; |
| 5428 | s = zbc_num_ulong(&digit, &dig); |
| 5429 | if (s) goto err; |
| 5430 | bc_vec_push(&stack, &dig); |
| 5431 | } |
| 5432 | |
| 5433 | for (i = 0; i < stack.len; ++i) { |
| 5434 | ptr = bc_vec_item_rev(&stack, i); |
| 5435 | print(*ptr, width, false); |
| 5436 | } |
| 5437 | |
| 5438 | if (!n->rdx) goto err; |
| 5439 | |
| 5440 | for (radix = true; frac_len.len <= n->rdx; radix = false) { |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5441 | s = zbc_num_mul(&fracp, &base, &fracp, n->rdx); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5442 | if (s) goto err; |
| 5443 | s = zbc_num_ulong(&fracp, &dig); |
| 5444 | if (s) goto err; |
| 5445 | bc_num_ulong2num(&intp, dig); |
| 5446 | s = zbc_num_sub(&fracp, &intp, &fracp, 0); |
| 5447 | if (s) goto err; |
| 5448 | print(dig, width, radix); |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5449 | s = zbc_num_mul(&frac_len, &base, &frac_len, 0); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5450 | if (s) goto err; |
| 5451 | } |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5452 | err: |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5453 | bc_num_free(&frac_len); |
| 5454 | bc_num_free(&digit); |
| 5455 | bc_num_free(&fracp); |
| 5456 | bc_num_free(&intp); |
| 5457 | bc_vec_free(&stack); |
| 5458 | RETURN_STATUS(s); |
| 5459 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5460 | #define zbc_num_printNum(...) (zbc_num_printNum(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5461 | |
| 5462 | static BC_STATUS zbc_num_printBase(BcNum *n) |
| 5463 | { |
| 5464 | BcStatus s; |
Denys Vlasenko | d4258dd | 2018-12-18 13:22:23 +0100 | [diff] [blame] | 5465 | size_t width; |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5466 | BcNumDigitOp print; |
| 5467 | bool neg = n->neg; |
| 5468 | |
| 5469 | if (neg) { |
| 5470 | bb_putchar('-'); |
| 5471 | G.prog.nchars++; |
| 5472 | } |
| 5473 | |
| 5474 | n->neg = false; |
| 5475 | |
| 5476 | if (G.prog.ob_t <= BC_NUM_MAX_IBASE) { |
| 5477 | width = 1; |
| 5478 | print = bc_num_printHex; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5479 | } else { |
Denys Vlasenko | d4258dd | 2018-12-18 13:22:23 +0100 | [diff] [blame] | 5480 | unsigned i = G.prog.ob_t - 1; |
| 5481 | width = 0; |
| 5482 | for (;;) { |
| 5483 | width++; |
| 5484 | i /= 10; |
| 5485 | if (i == 0) |
| 5486 | break; |
| 5487 | } |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5488 | print = bc_num_printDigits; |
| 5489 | } |
| 5490 | |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 5491 | s = zbc_num_printNum(n, G.prog.ob_t, width, print); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5492 | n->neg = neg; |
| 5493 | |
| 5494 | RETURN_STATUS(s); |
| 5495 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5496 | #define zbc_num_printBase(...) (zbc_num_printBase(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5497 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5498 | static BC_STATUS zbc_num_print(BcNum *n, bool newline) |
| 5499 | { |
| 5500 | BcStatus s = BC_STATUS_SUCCESS; |
| 5501 | |
| 5502 | bc_num_printNewline(); |
| 5503 | |
| 5504 | if (n->len == 0) { |
| 5505 | bb_putchar('0'); |
| 5506 | ++G.prog.nchars; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5507 | } else if (G.prog.ob_t == 10) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5508 | bc_num_printDecimal(n); |
| 5509 | else |
| 5510 | s = zbc_num_printBase(n); |
| 5511 | |
| 5512 | if (newline) { |
| 5513 | bb_putchar('\n'); |
| 5514 | G.prog.nchars = 0; |
| 5515 | } |
| 5516 | |
| 5517 | RETURN_STATUS(s); |
| 5518 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5519 | #define zbc_num_print(...) (zbc_num_print(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5520 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5521 | static BC_STATUS zbc_program_print(char inst, size_t idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5522 | { |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 5523 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5524 | BcResult *r; |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5525 | BcNum *num; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5526 | bool pop = inst != BC_INST_PRINT; |
| 5527 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5528 | if (!STACK_HAS_MORE_THAN(&G.prog.results, idx)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5529 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5530 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5531 | r = bc_vec_item_rev(&G.prog.results, idx); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5532 | s = zbc_program_num(r, &num, false); |
| 5533 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5534 | |
| 5535 | if (BC_PROG_NUM(r, num)) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5536 | s = zbc_num_print(num, !pop); |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 5537 | #if ENABLE_BC |
| 5538 | if (!s && IS_BC) bc_num_copy(&G.prog.last, num); |
| 5539 | #endif |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5540 | } else { |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5541 | char *str; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5542 | |
| 5543 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx; |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 5544 | str = *bc_program_str(idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5545 | |
| 5546 | if (inst == BC_INST_PRINT_STR) { |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5547 | for (;;) { |
| 5548 | char c = *str++; |
| 5549 | if (c == '\0') break; |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5550 | bb_putchar(c); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5551 | ++G.prog.nchars; |
Denys Vlasenko | 44d79d8 | 2018-12-10 12:33:40 +0100 | [diff] [blame] | 5552 | if (c == '\n') G.prog.nchars = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5553 | } |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5554 | } else { |
Denys Vlasenko | 5f1b90b | 2018-12-08 23:18:06 +0100 | [diff] [blame] | 5555 | bc_program_printString(str); |
Denys Vlasenko | 00d7779 | 2018-11-30 23:13:42 +0100 | [diff] [blame] | 5556 | if (inst == BC_INST_PRINT) bb_putchar('\n'); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5557 | } |
| 5558 | } |
| 5559 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5560 | if (!s && pop) bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5561 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5562 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5563 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5564 | #define zbc_program_print(...) (zbc_program_print(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5565 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5566 | static BC_STATUS zbc_program_negate(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5567 | { |
| 5568 | BcStatus s; |
| 5569 | BcResult res, *ptr; |
| 5570 | BcNum *num = NULL; |
| 5571 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5572 | s = zbc_program_prep(&ptr, &num); |
| 5573 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5574 | |
| 5575 | bc_num_init(&res.d.n, num->len); |
| 5576 | bc_num_copy(&res.d.n, num); |
| 5577 | if (res.d.n.len) res.d.n.neg = !res.d.n.neg; |
| 5578 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5579 | bc_program_retire(&res, BC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5580 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5581 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5582 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5583 | #define zbc_program_negate(...) (zbc_program_negate(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5584 | |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 5585 | static BC_STATUS zbc_program_logical(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5586 | { |
| 5587 | BcStatus s; |
| 5588 | BcResult *opd1, *opd2, res; |
| 5589 | BcNum *n1, *n2; |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5590 | ssize_t cond; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5591 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5592 | s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 5593 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5594 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5595 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5596 | |
| 5597 | if (inst == BC_INST_BOOL_AND) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5598 | 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] | 5599 | else if (inst == BC_INST_BOOL_OR) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5600 | 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] | 5601 | else { |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5602 | cond = bc_num_cmp(n1, n2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5603 | switch (inst) { |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5604 | case BC_INST_REL_EQ: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5605 | cond = (cond == 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5606 | break; |
| 5607 | case BC_INST_REL_LE: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5608 | cond = (cond <= 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5609 | break; |
| 5610 | case BC_INST_REL_GE: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5611 | cond = (cond >= 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5612 | break; |
| 5613 | case BC_INST_REL_LT: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5614 | cond = (cond < 0); |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5615 | break; |
| 5616 | case BC_INST_REL_GT: |
Denys Vlasenko | 16494f5 | 2018-12-12 00:50:23 +0100 | [diff] [blame] | 5617 | cond = (cond > 0); |
| 5618 | break; |
| 5619 | default: // = case BC_INST_REL_NE: |
| 5620 | //cond = (cond != 0); - not needed |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5621 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5622 | } |
| 5623 | } |
| 5624 | |
Denys Vlasenko | 09d8df8 | 2018-12-11 19:29:35 +0100 | [diff] [blame] | 5625 | if (cond) bc_num_one(&res.d.n); |
| 5626 | //else bc_num_zero(&res.d.n); - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5627 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5628 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5629 | |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 5630 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5631 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5632 | #define zbc_program_logical(...) (zbc_program_logical(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5633 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5634 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5635 | static BC_STATUS zdc_program_assignStr(BcResult *r, BcVec *v, bool push) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5636 | { |
| 5637 | BcNum n2; |
| 5638 | BcResult res; |
| 5639 | |
| 5640 | memset(&n2, 0, sizeof(BcNum)); |
| 5641 | n2.rdx = res.d.id.idx = r->d.id.idx; |
| 5642 | res.t = BC_RESULT_STR; |
| 5643 | |
| 5644 | if (!push) { |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5645 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 1)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5646 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5647 | bc_vec_pop(v); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5648 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5649 | } |
| 5650 | |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 5651 | bc_result_pop_and_push(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5652 | bc_vec_push(v, &n2); |
| 5653 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5654 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5655 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5656 | #define zdc_program_assignStr(...) (zdc_program_assignStr(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5657 | #endif // ENABLE_DC |
| 5658 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5659 | static BC_STATUS zbc_program_copyToVar(char *name, bool var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5660 | { |
| 5661 | BcStatus s; |
| 5662 | BcResult *ptr, r; |
| 5663 | BcVec *v; |
| 5664 | BcNum *n; |
| 5665 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5666 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5667 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5668 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5669 | ptr = bc_vec_top(&G.prog.results); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5670 | if ((ptr->t == BC_RESULT_ARRAY) != !var) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5671 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5672 | v = bc_program_search(name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5673 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5674 | #if ENABLE_DC |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5675 | if (ptr->t == BC_RESULT_STR && !var) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5676 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
| 5677 | if (ptr->t == BC_RESULT_STR) |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5678 | RETURN_STATUS(zdc_program_assignStr(ptr, v, true)); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5679 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5680 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5681 | s = zbc_program_num(ptr, &n, false); |
| 5682 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5683 | |
| 5684 | // Do this once more to make sure that pointers were not invalidated. |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5685 | v = bc_program_search(name, var); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5686 | |
| 5687 | if (var) { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5688 | bc_num_init_DEF_SIZE(&r.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5689 | bc_num_copy(&r.d.n, n); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5690 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5691 | bc_array_init(&r.d.v, true); |
| 5692 | bc_array_copy(&r.d.v, (BcVec *) n); |
| 5693 | } |
| 5694 | |
| 5695 | bc_vec_push(v, &r.d); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5696 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5697 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5698 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5699 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5700 | #define zbc_program_copyToVar(...) (zbc_program_copyToVar(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5701 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5702 | static BC_STATUS zbc_program_assign(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5703 | { |
| 5704 | BcStatus s; |
| 5705 | BcResult *left, *right, res; |
| 5706 | BcNum *l = NULL, *r = NULL; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5707 | bool assign = inst == BC_INST_ASSIGN, ib, sc; |
| 5708 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5709 | s = zbc_program_binOpPrep(&left, &l, &right, &r, assign); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5710 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5711 | |
| 5712 | ib = left->t == BC_RESULT_IBASE; |
| 5713 | sc = left->t == BC_RESULT_SCALE; |
| 5714 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5715 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5716 | if (right->t == BC_RESULT_STR) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5717 | BcVec *v; |
| 5718 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5719 | if (left->t != BC_RESULT_VAR) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5720 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5721 | v = bc_program_search(left->d.id.name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5722 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 5723 | RETURN_STATUS(zdc_program_assignStr(right, v, false)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5724 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5725 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5726 | |
| 5727 | if (left->t == BC_RESULT_CONSTANT || left->t == BC_RESULT_TEMP) |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 5728 | RETURN_STATUS(bc_error("bad assignment:" |
Denys Vlasenko | 0154d78 | 2018-12-14 23:32:51 +0100 | [diff] [blame] | 5729 | " left side must be variable" |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5730 | " or array element" |
Denys Vlasenko | 0154d78 | 2018-12-14 23:32:51 +0100 | [diff] [blame] | 5731 | )); // note: shared string |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5732 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5733 | #if ENABLE_BC |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5734 | if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &G.prog.zero)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5735 | RETURN_STATUS(bc_error("divide by zero")); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5736 | |
| 5737 | if (assign) |
| 5738 | bc_num_copy(l, r); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5739 | else { |
| 5740 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5741 | IF_ERROR_RETURN_POSSIBLE(s =) zbc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, G.prog.scale); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5742 | } |
| 5743 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5744 | #else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5745 | bc_num_copy(l, r); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5746 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5747 | |
| 5748 | if (ib || sc || left->t == BC_RESULT_OBASE) { |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5749 | static const char *const msg[] = { |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 5750 | "bad ibase; must be [2,16]", //BC_RESULT_IBASE |
| 5751 | "bad scale; must be [0,"BC_MAX_SCALE_STR"]", //BC_RESULT_SCALE |
| 5752 | NULL, //can't happen //BC_RESULT_LAST |
| 5753 | NULL, //can't happen //BC_RESULT_CONSTANT |
| 5754 | NULL, //can't happen //BC_RESULT_ONE |
| 5755 | "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5756 | }; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5757 | size_t *ptr; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5758 | size_t max; |
| 5759 | unsigned long val; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5760 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5761 | s = zbc_num_ulong(l, &val); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5762 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5763 | s = left->t - BC_RESULT_IBASE; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5764 | if (sc) { |
| 5765 | max = BC_MAX_SCALE; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5766 | ptr = &G.prog.scale; |
Denys Vlasenko | f6e3f85 | 2018-12-17 21:05:09 +0100 | [diff] [blame] | 5767 | } else { |
| 5768 | if (val < 2) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5769 | RETURN_STATUS(bc_error(msg[s])); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5770 | max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5771 | ptr = ib ? &G.prog.ib_t : &G.prog.ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5772 | } |
| 5773 | |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 5774 | if (val > max) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5775 | RETURN_STATUS(bc_error(msg[s])); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5776 | |
| 5777 | *ptr = (size_t) val; |
| 5778 | s = BC_STATUS_SUCCESS; |
| 5779 | } |
| 5780 | |
| 5781 | bc_num_init(&res.d.n, l->len); |
| 5782 | bc_num_copy(&res.d.n, l); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5783 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5784 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5785 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5786 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5787 | #define zbc_program_assign(...) (zbc_program_assign(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5788 | |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 5789 | #if !ENABLE_DC |
| 5790 | #define bc_program_pushVar(code, bgn, pop, copy) \ |
| 5791 | bc_program_pushVar(code, bgn) |
| 5792 | // for bc, 'pop' and 'copy' are always false |
| 5793 | #endif |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 5794 | static BC_STATUS bc_program_pushVar(char *code, size_t *bgn, |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5795 | bool pop, bool copy) |
| 5796 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5797 | BcResult r; |
| 5798 | char *name = bc_program_name(code, bgn); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5799 | |
| 5800 | r.t = BC_RESULT_VAR; |
| 5801 | r.d.id.name = name; |
| 5802 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5803 | #if ENABLE_DC |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 5804 | if (pop || copy) { |
Denys Vlasenko | 416ce76 | 2018-12-02 20:57:17 +0100 | [diff] [blame] | 5805 | BcVec *v = bc_program_search(name, true); |
| 5806 | BcNum *num = bc_vec_top(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5807 | |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 5808 | free(name); |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 5809 | if (!STACK_HAS_MORE_THAN(v, 1 - copy)) { |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 5810 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5811 | } |
Denys Vlasenko | 7b30bc0 | 2018-12-18 17:14:34 +0100 | [diff] [blame] | 5812 | |
| 5813 | if (!BC_PROG_STR(num)) { |
| 5814 | r.t = BC_RESULT_TEMP; |
| 5815 | bc_num_init_DEF_SIZE(&r.d.n); |
| 5816 | bc_num_copy(&r.d.n, num); |
| 5817 | } else { |
| 5818 | r.t = BC_RESULT_STR; |
| 5819 | r.d.id.idx = num->rdx; |
| 5820 | } |
| 5821 | |
| 5822 | if (!copy) bc_vec_pop(v); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5823 | } |
| 5824 | #endif // ENABLE_DC |
| 5825 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5826 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5827 | |
Denys Vlasenko | b402ff8 | 2018-12-11 15:45:15 +0100 | [diff] [blame] | 5828 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5829 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5830 | #define zbc_program_pushVar(...) (bc_program_pushVar(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5831 | |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5832 | static BC_STATUS zbc_program_pushArray(char *code, size_t *bgn, char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5833 | { |
| 5834 | BcStatus s = BC_STATUS_SUCCESS; |
| 5835 | BcResult r; |
| 5836 | BcNum *num; |
| 5837 | |
| 5838 | r.d.id.name = bc_program_name(code, bgn); |
| 5839 | |
| 5840 | if (inst == BC_INST_ARRAY) { |
| 5841 | r.t = BC_RESULT_ARRAY; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5842 | bc_vec_push(&G.prog.results, &r); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5843 | } else { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5844 | BcResult *operand; |
| 5845 | unsigned long temp; |
| 5846 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5847 | s = zbc_program_prep(&operand, &num); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5848 | if (s) goto err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5849 | s = zbc_num_ulong(num, &temp); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5850 | if (s) goto err; |
| 5851 | |
| 5852 | if (temp > BC_MAX_DIM) { |
Denys Vlasenko | 64074a1 | 2018-12-07 15:50:14 +0100 | [diff] [blame] | 5853 | 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] | 5854 | goto err; |
| 5855 | } |
| 5856 | |
| 5857 | r.d.id.idx = (size_t) temp; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5858 | bc_program_retire(&r, BC_RESULT_ARRAY_ELEM); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5859 | } |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5860 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5861 | if (s) free(r.d.id.name); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5862 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5863 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5864 | #define zbc_program_pushArray(...) (zbc_program_pushArray(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5865 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 5866 | #if ENABLE_BC |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5867 | static BC_STATUS zbc_program_incdec(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5868 | { |
| 5869 | BcStatus s; |
| 5870 | BcResult *ptr, res, copy; |
| 5871 | BcNum *num = NULL; |
| 5872 | char inst2 = inst; |
| 5873 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5874 | s = zbc_program_prep(&ptr, &num); |
| 5875 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5876 | |
| 5877 | if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) { |
| 5878 | copy.t = BC_RESULT_TEMP; |
| 5879 | bc_num_init(©.d.n, num->len); |
| 5880 | bc_num_copy(©.d.n, num); |
| 5881 | } |
| 5882 | |
| 5883 | res.t = BC_RESULT_ONE; |
| 5884 | inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ? |
| 5885 | BC_INST_ASSIGN_PLUS : |
| 5886 | BC_INST_ASSIGN_MINUS; |
| 5887 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5888 | bc_vec_push(&G.prog.results, &res); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 5889 | s = zbc_program_assign(inst); |
| 5890 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5891 | |
| 5892 | if (inst2 == BC_INST_INC_POST || inst2 == BC_INST_DEC_POST) { |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 5893 | bc_result_pop_and_push(©); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5894 | } |
| 5895 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5896 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5897 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5898 | #define zbc_program_incdec(...) (zbc_program_incdec(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5899 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5900 | static BC_STATUS zbc_program_call(char *code, size_t *idx) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5901 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5902 | BcInstPtr ip; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 5903 | size_t i, nparams; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5904 | BcFunc *func; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5905 | BcId *a; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5906 | BcResult *arg; |
| 5907 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 5908 | nparams = bc_program_index(code, idx); |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 5909 | ip.inst_idx = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5910 | ip.func = bc_program_index(code, idx); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 5911 | func = bc_program_func(ip.func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5912 | |
Denys Vlasenko | 04a1c76 | 2018-12-03 21:10:57 +0100 | [diff] [blame] | 5913 | if (func->code.len == 0) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5914 | RETURN_STATUS(bc_error("undefined function")); |
Denys Vlasenko | 04a1c76 | 2018-12-03 21:10:57 +0100 | [diff] [blame] | 5915 | } |
| 5916 | if (nparams != func->nparams) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5917 | RETURN_STATUS(bc_error_fmt("function has %u parameters, but called with %u", func->nparams, nparams)); |
Denys Vlasenko | 04a1c76 | 2018-12-03 21:10:57 +0100 | [diff] [blame] | 5918 | } |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 5919 | ip.results_len_before_call = G.prog.results.len - nparams; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5920 | |
| 5921 | for (i = 0; i < nparams; ++i) { |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 5922 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5923 | |
| 5924 | a = bc_vec_item(&func->autos, nparams - 1 - i); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5925 | arg = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5926 | |
| 5927 | if ((!a->idx) != (arg->t == BC_RESULT_ARRAY) || arg->t == BC_RESULT_STR) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5928 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5929 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5930 | s = zbc_program_copyToVar(a->name, a->idx); |
| 5931 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5932 | } |
| 5933 | |
Denys Vlasenko | 87888ce | 2018-12-19 17:55:23 +0100 | [diff] [blame] | 5934 | a = bc_vec_item(&func->autos, i); |
| 5935 | for (; i < func->autos.len; i++, a++) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5936 | BcVec *v; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5937 | |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5938 | v = bc_program_search(a->name, a->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5939 | if (a->idx) { |
Denys Vlasenko | f36a0ad | 2018-12-19 17:15:04 +0100 | [diff] [blame] | 5940 | BcNum n2; |
| 5941 | bc_num_init_DEF_SIZE(&n2); |
| 5942 | bc_vec_push(v, &n2); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5943 | } else { |
Denys Vlasenko | f36a0ad | 2018-12-19 17:15:04 +0100 | [diff] [blame] | 5944 | BcVec v2; |
| 5945 | bc_array_init(&v2, true); |
| 5946 | bc_vec_push(v, &v2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5947 | } |
| 5948 | } |
| 5949 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 5950 | bc_vec_push(&G.prog.exestack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5951 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5952 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5953 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5954 | #define zbc_program_call(...) (zbc_program_call(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5955 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5956 | static BC_STATUS zbc_program_return(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5957 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5958 | BcResult res; |
| 5959 | BcFunc *f; |
Denys Vlasenko | 87888ce | 2018-12-19 17:55:23 +0100 | [diff] [blame] | 5960 | BcId *a; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5961 | size_t i; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 5962 | BcInstPtr *ip = bc_vec_top(&G.prog.exestack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5963 | |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 5964 | if (!STACK_HAS_EQUAL_OR_MORE_THAN(&G.prog.results, ip->results_len_before_call + (inst == BC_INST_RET))) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5965 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5966 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 5967 | f = bc_program_func(ip->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5968 | res.t = BC_RESULT_TEMP; |
| 5969 | |
| 5970 | if (inst == BC_INST_RET) { |
Denys Vlasenko | 628bf1b | 2018-12-10 20:41:05 +0100 | [diff] [blame] | 5971 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5972 | BcNum *num; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5973 | BcResult *operand = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5974 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5975 | s = zbc_program_num(operand, &num, false); |
| 5976 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5977 | bc_num_init(&res.d.n, num->len); |
| 5978 | bc_num_copy(&res.d.n, num); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 5979 | } else { |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 5980 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 5981 | //bc_num_zero(&res.d.n); - already is |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5982 | } |
| 5983 | |
| 5984 | // We need to pop arguments as well, so this takes that into account. |
Denys Vlasenko | 87888ce | 2018-12-19 17:55:23 +0100 | [diff] [blame] | 5985 | a = (void*)f->autos.v; |
| 5986 | for (i = 0; i < f->autos.len; i++, a++) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5987 | BcVec *v; |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 5988 | v = bc_program_search(a->name, a->idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5989 | bc_vec_pop(v); |
| 5990 | } |
| 5991 | |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 5992 | bc_vec_npop(&G.prog.results, G.prog.results.len - ip->results_len_before_call); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 5993 | bc_vec_push(&G.prog.results, &res); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 5994 | bc_vec_pop(&G.prog.exestack); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5995 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 5996 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5997 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 5998 | #define zbc_program_return(...) (zbc_program_return(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 5999 | #endif // ENABLE_BC |
| 6000 | |
| 6001 | static unsigned long bc_program_scale(BcNum *n) |
| 6002 | { |
| 6003 | return (unsigned long) n->rdx; |
| 6004 | } |
| 6005 | |
| 6006 | static unsigned long bc_program_len(BcNum *n) |
| 6007 | { |
Denys Vlasenko | a7f1a36 | 2018-12-10 12:57:01 +0100 | [diff] [blame] | 6008 | size_t len = n->len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6009 | |
Denys Vlasenko | a7f1a36 | 2018-12-10 12:57:01 +0100 | [diff] [blame] | 6010 | if (n->rdx != len) return len; |
| 6011 | for (;;) { |
| 6012 | if (len == 0) break; |
| 6013 | len--; |
| 6014 | if (n->num[len] != 0) break; |
| 6015 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6016 | return len; |
| 6017 | } |
| 6018 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6019 | static BC_STATUS zbc_program_builtin(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6020 | { |
| 6021 | BcStatus s; |
| 6022 | BcResult *opnd; |
| 6023 | BcNum *num = NULL; |
| 6024 | BcResult res; |
| 6025 | bool len = inst == BC_INST_LENGTH; |
| 6026 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6027 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6028 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6029 | opnd = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6030 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6031 | s = zbc_program_num(opnd, &num, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6032 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6033 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6034 | #if ENABLE_DC |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6035 | if (!BC_PROG_NUM(opnd, num) && !len) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6036 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6037 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6038 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6039 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6040 | |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6041 | if (inst == BC_INST_SQRT) |
| 6042 | s = zbc_num_sqrt(num, &res.d.n, G.prog.scale); |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6043 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6044 | else if (len != 0 && opnd->t == BC_RESULT_ARRAY) { |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6045 | bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6046 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6047 | #endif |
| 6048 | #if ENABLE_DC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6049 | else if (len != 0 && !BC_PROG_NUM(opnd, num)) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6050 | char **str; |
| 6051 | size_t idx = opnd->t == BC_RESULT_STR ? opnd->d.id.idx : num->rdx; |
| 6052 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6053 | str = bc_program_str(idx); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6054 | bc_num_ulong2num(&res.d.n, strlen(*str)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6055 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6056 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6057 | else { |
Denys Vlasenko | 5ba55f1 | 2018-12-10 15:37:14 +0100 | [diff] [blame] | 6058 | bc_num_ulong2num(&res.d.n, len ? bc_program_len(num) : bc_program_scale(num)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6059 | } |
| 6060 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6061 | bc_program_retire(&res, BC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6062 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6063 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6064 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 6065 | #define zbc_program_builtin(...) (zbc_program_builtin(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6066 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6067 | #if ENABLE_DC |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6068 | static BC_STATUS zdc_program_divmod(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6069 | { |
| 6070 | BcStatus s; |
| 6071 | BcResult *opd1, *opd2, res, res2; |
| 6072 | BcNum *n1, *n2 = NULL; |
| 6073 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6074 | s = zbc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6075 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6076 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6077 | bc_num_init_DEF_SIZE(&res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6078 | bc_num_init(&res2.d.n, n2->len); |
| 6079 | |
Denys Vlasenko | 1aeacef | 2018-12-11 19:12:13 +0100 | [diff] [blame] | 6080 | s = zbc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6081 | if (s) goto err; |
| 6082 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6083 | bc_program_binOpRetire(&res2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6084 | res.t = BC_RESULT_TEMP; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6085 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6086 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6087 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6088 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6089 | bc_num_free(&res2.d.n); |
| 6090 | bc_num_free(&res.d.n); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6091 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6092 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6093 | #define zdc_program_divmod(...) (zdc_program_divmod(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6094 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6095 | static BC_STATUS zdc_program_modexp(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6096 | { |
| 6097 | BcStatus s; |
| 6098 | BcResult *r1, *r2, *r3, res; |
| 6099 | BcNum *n1, *n2, *n3; |
| 6100 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6101 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 2)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6102 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6103 | s = zbc_program_binOpPrep(&r2, &n2, &r3, &n3, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6104 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6105 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6106 | r1 = bc_vec_item_rev(&G.prog.results, 2); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6107 | s = zbc_program_num(r1, &n1, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6108 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | 60cf747 | 2018-12-04 20:05:28 +0100 | [diff] [blame] | 6109 | if (!BC_PROG_NUM(r1, n1)) |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6110 | RETURN_STATUS(bc_error_variable_is_wrong_type()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6111 | |
| 6112 | // Make sure that the values have their pointers updated, if necessary. |
| 6113 | if (r1->t == BC_RESULT_VAR || r1->t == BC_RESULT_ARRAY_ELEM) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6114 | if (r1->t == r2->t) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6115 | s = zbc_program_num(r2, &n2, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6116 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6117 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6118 | if (r1->t == r3->t) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6119 | s = zbc_program_num(r3, &n3, false); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6120 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6121 | } |
| 6122 | } |
| 6123 | |
| 6124 | bc_num_init(&res.d.n, n3->len); |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6125 | s = zdc_num_modexp(n1, n2, n3, &res.d.n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6126 | if (s) goto err; |
| 6127 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6128 | bc_vec_pop(&G.prog.results); |
| 6129 | bc_program_binOpRetire(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6130 | |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6131 | RETURN_STATUS(s); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6132 | err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6133 | bc_num_free(&res.d.n); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6134 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6135 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6136 | #define zdc_program_modexp(...) (zdc_program_modexp(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6137 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6138 | static void dc_program_stackLen(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6139 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6140 | BcResult res; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6141 | size_t len = G.prog.results.len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6142 | |
| 6143 | res.t = BC_RESULT_TEMP; |
| 6144 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6145 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6146 | bc_num_ulong2num(&res.d.n, len); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6147 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6148 | } |
| 6149 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6150 | static BC_STATUS zdc_program_asciify(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6151 | { |
| 6152 | BcStatus s; |
| 6153 | BcResult *r, res; |
Denys Vlasenko | 4a024c7 | 2018-12-09 13:21:54 +0100 | [diff] [blame] | 6154 | BcNum *num, n; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6155 | char **strs; |
| 6156 | char *str; |
| 6157 | char c; |
| 6158 | size_t idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6159 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6160 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6161 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6162 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6163 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6164 | s = zbc_program_num(r, &num, false); |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6165 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6166 | |
| 6167 | if (BC_PROG_NUM(r, num)) { |
Denys Vlasenko | a9f59db | 2018-12-22 21:52:30 +0100 | [diff] [blame] | 6168 | unsigned long val; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 6169 | BcNum strmb; |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 6170 | BcDig strmb_digs[ULONG_NUM_BUFSIZE]; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 6171 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6172 | bc_num_init_DEF_SIZE(&n); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6173 | bc_num_copy(&n, num); |
| 6174 | bc_num_truncate(&n, n.rdx); |
| 6175 | |
Denys Vlasenko | e8e7bda | 2018-12-21 22:36:04 +0100 | [diff] [blame] | 6176 | strmb.cap = ARRAY_SIZE(strmb_digs); |
| 6177 | strmb.num = strmb_digs; |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 6178 | bc_num_ulong2num(&strmb, 0x100); |
| 6179 | s = zbc_num_mod(&n, &strmb, &n, 0); |
Denys Vlasenko | d340143 | 2018-12-18 17:00:35 +0100 | [diff] [blame] | 6180 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6181 | if (s) goto num_err; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6182 | s = zbc_num_ulong(&n, &val); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6183 | if (s) goto num_err; |
| 6184 | |
| 6185 | c = (char) val; |
| 6186 | |
| 6187 | bc_num_free(&n); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6188 | } else { |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6189 | char *sp; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6190 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : num->rdx; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6191 | sp = *bc_program_str(idx); |
| 6192 | c = sp[0]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6193 | } |
| 6194 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6195 | strs = (void*)G.prog.strs.v; |
| 6196 | for (idx = 0; idx < G.prog.strs.len; idx++) { |
| 6197 | if (strs[idx][0] == c && strs[idx][1] == '\0') { |
| 6198 | goto dup; |
| 6199 | } |
| 6200 | } |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6201 | str = xzalloc(2); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6202 | str[0] = c; |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6203 | //str[1] = '\0'; - already is |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6204 | bc_vec_push(&G.prog.strs, &str); |
| 6205 | dup: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6206 | res.t = BC_RESULT_STR; |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 6207 | res.d.id.idx = idx; |
Denys Vlasenko | 1dc4de9 | 2018-12-21 23:13:48 +0100 | [diff] [blame] | 6208 | bc_result_pop_and_push(&res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6209 | |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6210 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6211 | num_err: |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6212 | bc_num_free(&n); |
Denys Vlasenko | c008a73 | 2018-12-11 20:57:53 +0100 | [diff] [blame] | 6213 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6214 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6215 | #define zdc_program_asciify(...) (zdc_program_asciify(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6216 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6217 | static BC_STATUS zdc_program_printStream(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6218 | { |
| 6219 | BcStatus s; |
| 6220 | BcResult *r; |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6221 | BcNum *n; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6222 | size_t idx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6223 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6224 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6225 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6226 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6227 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6228 | s = zbc_program_num(r, &n, false); |
| 6229 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6230 | |
Denys Vlasenko | 57734c9 | 2018-12-17 21:14:05 +0100 | [diff] [blame] | 6231 | if (BC_PROG_NUM(r, n)) { |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6232 | s = zbc_num_printNum(n, 0x100, 1, dc_num_printChar); |
Denys Vlasenko | 57734c9 | 2018-12-17 21:14:05 +0100 | [diff] [blame] | 6233 | } else { |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6234 | char *str; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6235 | idx = (r->t == BC_RESULT_STR) ? r->d.id.idx : n->rdx; |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6236 | str = *bc_program_str(idx); |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6237 | fputs(str, stdout); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6238 | } |
| 6239 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6240 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6241 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6242 | #define zdc_program_printStream(...) (zdc_program_printStream(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6243 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6244 | static BC_STATUS zdc_program_nquit(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6245 | { |
| 6246 | BcStatus s; |
| 6247 | BcResult *opnd; |
| 6248 | BcNum *num = NULL; |
| 6249 | unsigned long val; |
| 6250 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6251 | s = zbc_program_prep(&opnd, &num); |
| 6252 | if (s) RETURN_STATUS(s); |
| 6253 | s = zbc_num_ulong(num, &val); |
| 6254 | if (s) RETURN_STATUS(s); |
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 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6257 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6258 | if (G.prog.exestack.len < val) |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6259 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6260 | if (G.prog.exestack.len == val) { |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 6261 | QUIT_OR_RETURN_TO_MAIN; |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 6262 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6263 | |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6264 | bc_vec_npop(&G.prog.exestack, val); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6265 | |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6266 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6267 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6268 | #define zdc_program_nquit(...) (zdc_program_nquit(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6269 | |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6270 | static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6271 | { |
| 6272 | BcStatus s = BC_STATUS_SUCCESS; |
| 6273 | BcResult *r; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6274 | BcFunc *f; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6275 | BcInstPtr ip; |
| 6276 | size_t fidx, sidx; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6277 | |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6278 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6279 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6280 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6281 | r = bc_vec_top(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6282 | |
| 6283 | if (cond) { |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6284 | BcNum *n = n; // for compiler |
| 6285 | bool exec; |
| 6286 | char *name; |
| 6287 | char *then_name = bc_program_name(code, bgn); |
| 6288 | char *else_name = NULL; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6289 | |
| 6290 | if (code[*bgn] == BC_PARSE_STREND) |
| 6291 | (*bgn) += 1; |
| 6292 | else |
| 6293 | else_name = bc_program_name(code, bgn); |
| 6294 | |
| 6295 | exec = r->d.n.len != 0; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6296 | name = then_name; |
| 6297 | if (!exec && else_name != NULL) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6298 | exec = true; |
| 6299 | name = else_name; |
| 6300 | } |
| 6301 | |
| 6302 | if (exec) { |
Denys Vlasenko | df51539 | 2018-12-02 19:27:48 +0100 | [diff] [blame] | 6303 | BcVec *v; |
| 6304 | v = bc_program_search(name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6305 | n = bc_vec_top(v); |
| 6306 | } |
| 6307 | |
| 6308 | free(then_name); |
| 6309 | free(else_name); |
| 6310 | |
| 6311 | if (!exec) goto exit; |
| 6312 | if (!BC_PROG_STR(n)) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6313 | s = bc_error_variable_is_wrong_type(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6314 | goto exit; |
| 6315 | } |
| 6316 | |
| 6317 | sidx = n->rdx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6318 | } else { |
| 6319 | if (r->t == BC_RESULT_STR) { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6320 | sidx = r->d.id.idx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6321 | } else if (r->t == BC_RESULT_VAR) { |
| 6322 | BcNum *n; |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6323 | s = zbc_program_num(r, &n, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6324 | if (s || !BC_PROG_STR(n)) goto exit; |
| 6325 | sidx = n->rdx; |
Denys Vlasenko | 5ec4b49 | 2018-12-09 02:54:06 +0100 | [diff] [blame] | 6326 | } else |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6327 | goto exit; |
| 6328 | } |
| 6329 | |
| 6330 | fidx = sidx + BC_PROG_REQ_FUNCS; |
| 6331 | |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6332 | f = bc_program_func(fidx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6333 | |
| 6334 | if (f->code.len == 0) { |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6335 | FILE *sv_input_fp; |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6336 | BcParse prs; |
| 6337 | char *str; |
| 6338 | |
Denys Vlasenko | c2265f5 | 2018-12-12 23:10:08 +0100 | [diff] [blame] | 6339 | bc_parse_create(&prs, fidx); |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6340 | str = *bc_program_str(sidx); |
| 6341 | s = zbc_parse_text_init(&prs, str); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6342 | if (s) goto err; |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6343 | |
| 6344 | sv_input_fp = G.input_fp; |
| 6345 | G.input_fp = NULL; // "do not read from input file when <EOL> reached" |
| 6346 | s = zdc_parse_exprs_until_eof(&prs); |
| 6347 | G.input_fp = sv_input_fp; |
| 6348 | |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6349 | if (s) goto err; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6350 | if (prs.l.t.t != BC_LEX_EOF) { |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6351 | s = bc_error_bad_expression(); |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6352 | err: |
| 6353 | bc_parse_free(&prs); |
| 6354 | bc_vec_pop_all(&f->code); |
| 6355 | goto exit; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6356 | } |
Denys Vlasenko | 39287e0 | 2018-12-22 02:23:08 +0100 | [diff] [blame] | 6357 | bc_parse_push(&prs, BC_INST_POP_EXEC); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6358 | bc_parse_free(&prs); |
| 6359 | } |
| 6360 | |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6361 | ip.inst_idx = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6362 | ip.func = fidx; |
| 6363 | |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6364 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6365 | bc_vec_push(&G.prog.exestack, &ip); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6366 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6367 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Denys Vlasenko | 7f2d59c | 2018-12-18 16:24:07 +0100 | [diff] [blame] | 6368 | exit: |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6369 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6370 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6371 | } |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6372 | #define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6373 | #endif // ENABLE_DC |
| 6374 | |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6375 | static void bc_program_pushGlobal(char inst) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6376 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6377 | BcResult res; |
| 6378 | unsigned long val; |
| 6379 | |
| 6380 | res.t = inst - BC_INST_IBASE + BC_RESULT_IBASE; |
| 6381 | if (inst == BC_INST_IBASE) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6382 | val = (unsigned long) G.prog.ib_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6383 | else if (inst == BC_INST_SCALE) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6384 | val = (unsigned long) G.prog.scale; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6385 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6386 | val = (unsigned long) G.prog.ob_t; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6387 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6388 | bc_num_init_DEF_SIZE(&res.d.n); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6389 | bc_num_ulong2num(&res.d.n, val); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6390 | bc_vec_push(&G.prog.results, &res); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6391 | } |
| 6392 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6393 | static BC_STATUS zbc_program_exec(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6394 | { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6395 | BcResult r, *ptr; |
| 6396 | BcNum *num; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6397 | BcInstPtr *ip = bc_vec_top(&G.prog.exestack); |
Denys Vlasenko | 8fa1e8e | 2018-12-09 00:03:57 +0100 | [diff] [blame] | 6398 | BcFunc *func = bc_program_func(ip->func); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6399 | char *code = func->code.v; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6400 | |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 6401 | dbg_exec("func:%zd bytes:%zd ip:%zd results.len:%d", |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6402 | ip->func, func->code.len, ip->inst_idx, G.prog.results.len); |
| 6403 | while (ip->inst_idx < func->code.len) { |
Denys Vlasenko | fa35e59 | 2018-12-10 20:17:24 +0100 | [diff] [blame] | 6404 | BcStatus s = BC_STATUS_SUCCESS; |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6405 | char inst = code[ip->inst_idx++]; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6406 | |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6407 | dbg_exec("inst at %zd:%d results.len:%d", ip->inst_idx - 1, inst, G.prog.results.len); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6408 | switch (inst) { |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6409 | #if ENABLE_BC |
Denys Vlasenko | fd51e0c | 2018-12-15 15:07:14 +0100 | [diff] [blame] | 6410 | case BC_INST_JUMP_ZERO: { |
| 6411 | bool zero; |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 6412 | dbg_exec("BC_INST_JUMP_ZERO:"); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6413 | s = zbc_program_prep(&ptr, &num); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6414 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | fd51e0c | 2018-12-15 15:07:14 +0100 | [diff] [blame] | 6415 | zero = (bc_num_cmp(num, &G.prog.zero) == 0); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6416 | bc_vec_pop(&G.prog.results); |
Denys Vlasenko | fd51e0c | 2018-12-15 15:07:14 +0100 | [diff] [blame] | 6417 | if (!zero) { |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6418 | bc_program_index(code, &ip->inst_idx); |
Denys Vlasenko | fd51e0c | 2018-12-15 15:07:14 +0100 | [diff] [blame] | 6419 | break; |
| 6420 | } |
| 6421 | // else: fall through |
| 6422 | } |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6423 | case BC_INST_JUMP: { |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6424 | size_t idx = bc_program_index(code, &ip->inst_idx); |
Denys Vlasenko | fd51e0c | 2018-12-15 15:07:14 +0100 | [diff] [blame] | 6425 | size_t *addr = bc_vec_item(&func->labels, idx); |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 6426 | dbg_exec("BC_INST_JUMP: to %ld", (long)*addr); |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6427 | ip->inst_idx = *addr; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6428 | break; |
| 6429 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6430 | case BC_INST_CALL: |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 6431 | dbg_exec("BC_INST_CALL:"); |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6432 | s = zbc_program_call(code, &ip->inst_idx); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6433 | goto read_updated_ip; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6434 | case BC_INST_INC_PRE: |
| 6435 | case BC_INST_DEC_PRE: |
| 6436 | case BC_INST_INC_POST: |
| 6437 | case BC_INST_DEC_POST: |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 6438 | dbg_exec("BC_INST_INCDEC:"); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6439 | s = zbc_program_incdec(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6440 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6441 | case BC_INST_HALT: |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 6442 | dbg_exec("BC_INST_HALT:"); |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 6443 | QUIT_OR_RETURN_TO_MAIN; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6444 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6445 | case BC_INST_RET: |
| 6446 | case BC_INST_RET0: |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 6447 | dbg_exec("BC_INST_RET[0]:"); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6448 | s = zbc_program_return(inst); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6449 | goto read_updated_ip; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6450 | case BC_INST_BOOL_OR: |
| 6451 | case BC_INST_BOOL_AND: |
| 6452 | #endif // ENABLE_BC |
| 6453 | case BC_INST_REL_EQ: |
| 6454 | case BC_INST_REL_LE: |
| 6455 | case BC_INST_REL_GE: |
| 6456 | case BC_INST_REL_NE: |
| 6457 | case BC_INST_REL_LT: |
| 6458 | case BC_INST_REL_GT: |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 6459 | dbg_exec("BC_INST_BOOL:"); |
Denys Vlasenko | 728e7c9 | 2018-12-11 19:37:00 +0100 | [diff] [blame] | 6460 | s = zbc_program_logical(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6461 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6462 | case BC_INST_READ: |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 6463 | dbg_exec("BC_INST_READ:"); |
Denys Vlasenko | 26819db | 2018-12-12 16:08:46 +0100 | [diff] [blame] | 6464 | s = zbc_program_read(); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6465 | goto read_updated_ip; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6466 | case BC_INST_VAR: |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 6467 | dbg_exec("BC_INST_VAR:"); |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6468 | s = zbc_program_pushVar(code, &ip->inst_idx, false, false); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6469 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6470 | case BC_INST_ARRAY_ELEM: |
| 6471 | case BC_INST_ARRAY: |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 6472 | dbg_exec("BC_INST_ARRAY[_ELEM]:"); |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6473 | s = zbc_program_pushArray(code, &ip->inst_idx, inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6474 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6475 | case BC_INST_LAST: |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6476 | //TODO: this can't happen on dc, right? |
| 6477 | dbg_exec("BC_INST_LAST:"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6478 | r.t = BC_RESULT_LAST; |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6479 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6480 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6481 | case BC_INST_IBASE: |
| 6482 | case BC_INST_SCALE: |
| 6483 | case BC_INST_OBASE: |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6484 | dbg_exec("BC_INST_internalvar:"); |
Denys Vlasenko | e3b4f23 | 2018-12-02 18:44:40 +0100 | [diff] [blame] | 6485 | bc_program_pushGlobal(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6486 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6487 | case BC_INST_SCALE_FUNC: |
| 6488 | case BC_INST_LENGTH: |
| 6489 | case BC_INST_SQRT: |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 6490 | dbg_exec("BC_INST_builtin:"); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6491 | s = zbc_program_builtin(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6492 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6493 | case BC_INST_NUM: |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 6494 | dbg_exec("BC_INST_NUM:"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6495 | r.t = BC_RESULT_CONSTANT; |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6496 | r.d.id.idx = bc_program_index(code, &ip->inst_idx); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6497 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6498 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6499 | case BC_INST_POP: |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 6500 | dbg_exec("BC_INST_POP:"); |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6501 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | 24fb2cd | 2018-12-05 16:03:46 +0100 | [diff] [blame] | 6502 | s = bc_error_stack_has_too_few_elements(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6503 | else |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6504 | bc_vec_pop(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6505 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6506 | case BC_INST_PRINT: |
| 6507 | case BC_INST_PRINT_POP: |
| 6508 | case BC_INST_PRINT_STR: |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 6509 | dbg_exec("BC_INST_PRINTxyz:"); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6510 | s = zbc_program_print(inst, 0); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6511 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6512 | case BC_INST_STR: |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 6513 | dbg_exec("BC_INST_STR:"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6514 | r.t = BC_RESULT_STR; |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6515 | r.d.id.idx = bc_program_index(code, &ip->inst_idx); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6516 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6517 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6518 | case BC_INST_POWER: |
| 6519 | case BC_INST_MULTIPLY: |
| 6520 | case BC_INST_DIVIDE: |
| 6521 | case BC_INST_MODULUS: |
| 6522 | case BC_INST_PLUS: |
| 6523 | case BC_INST_MINUS: |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 6524 | dbg_exec("BC_INST_binaryop:"); |
Denys Vlasenko | 259137d | 2018-12-11 19:42:05 +0100 | [diff] [blame] | 6525 | s = zbc_program_op(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6526 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6527 | case BC_INST_BOOL_NOT: |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 6528 | dbg_exec("BC_INST_BOOL_NOT:"); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6529 | s = zbc_program_prep(&ptr, &num); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6530 | if (s) RETURN_STATUS(s); |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 6531 | bc_num_init_DEF_SIZE(&r.d.n); |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6532 | if (!bc_num_cmp(num, &G.prog.zero)) |
| 6533 | bc_num_one(&r.d.n); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 6534 | //else bc_num_zero(&r.d.n); - already is |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6535 | bc_program_retire(&r, BC_RESULT_TEMP); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6536 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6537 | case BC_INST_NEG: |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 6538 | dbg_exec("BC_INST_NEG:"); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6539 | s = zbc_program_negate(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6540 | break; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6541 | #if ENABLE_BC |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6542 | case BC_INST_ASSIGN_POWER: |
| 6543 | case BC_INST_ASSIGN_MULTIPLY: |
| 6544 | case BC_INST_ASSIGN_DIVIDE: |
| 6545 | case BC_INST_ASSIGN_MODULUS: |
| 6546 | case BC_INST_ASSIGN_PLUS: |
| 6547 | case BC_INST_ASSIGN_MINUS: |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6548 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6549 | case BC_INST_ASSIGN: |
Denys Vlasenko | 99b3762 | 2018-12-15 20:06:59 +0100 | [diff] [blame] | 6550 | dbg_exec("BC_INST_ASSIGNxyz:"); |
Denys Vlasenko | 7f4daa4 | 2018-12-11 19:04:44 +0100 | [diff] [blame] | 6551 | s = zbc_program_assign(inst); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6552 | break; |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 6553 | #if ENABLE_DC |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 6554 | case BC_INST_POP_EXEC: |
| 6555 | dbg_exec("BC_INST_POP_EXEC:"); |
| 6556 | bc_vec_pop(&G.prog.exestack); |
| 6557 | goto read_updated_ip; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6558 | case BC_INST_MODEXP: |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6559 | dbg_exec("BC_INST_MODEXP:"); |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6560 | s = zdc_program_modexp(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6561 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6562 | case BC_INST_DIVMOD: |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6563 | dbg_exec("BC_INST_DIVMOD:"); |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6564 | s = zdc_program_divmod(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6565 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6566 | case BC_INST_EXECUTE: |
| 6567 | case BC_INST_EXEC_COND: |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6568 | dbg_exec("BC_INST_EXEC[_COND]:"); |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6569 | s = zdc_program_execStr(code, &ip->inst_idx, inst == BC_INST_EXEC_COND); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6570 | goto read_updated_ip; |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6571 | case BC_INST_PRINT_STACK: { |
| 6572 | size_t idx; |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6573 | dbg_exec("BC_INST_PRINT_STACK:"); |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6574 | for (idx = 0; idx < G.prog.results.len; ++idx) { |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6575 | s = zbc_program_print(BC_INST_PRINT, idx); |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6576 | if (s) break; |
| 6577 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6578 | break; |
| 6579 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6580 | case BC_INST_CLEAR_STACK: |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6581 | dbg_exec("BC_INST_CLEAR_STACK:"); |
Denys Vlasenko | 7d62801 | 2018-12-04 21:46:47 +0100 | [diff] [blame] | 6582 | bc_vec_pop_all(&G.prog.results); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6583 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6584 | case BC_INST_STACK_LEN: |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6585 | dbg_exec("BC_INST_STACK_LEN:"); |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6586 | dc_program_stackLen(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6587 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6588 | case BC_INST_DUPLICATE: |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6589 | dbg_exec("BC_INST_DUPLICATE:"); |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6590 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 0)) |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6591 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6592 | ptr = bc_vec_top(&G.prog.results); |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6593 | dc_result_copy(&r, ptr); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6594 | bc_vec_push(&G.prog.results, &r); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6595 | break; |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6596 | case BC_INST_SWAP: { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6597 | BcResult *ptr2; |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6598 | dbg_exec("BC_INST_SWAP:"); |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6599 | if (!STACK_HAS_MORE_THAN(&G.prog.results, 1)) |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6600 | RETURN_STATUS(bc_error_stack_has_too_few_elements()); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 6601 | ptr = bc_vec_item_rev(&G.prog.results, 0); |
| 6602 | ptr2 = bc_vec_item_rev(&G.prog.results, 1); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6603 | memcpy(&r, ptr, sizeof(BcResult)); |
| 6604 | memcpy(ptr, ptr2, sizeof(BcResult)); |
| 6605 | memcpy(ptr2, &r, sizeof(BcResult)); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6606 | break; |
| 6607 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6608 | case BC_INST_ASCIIFY: |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6609 | dbg_exec("BC_INST_ASCIIFY:"); |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6610 | s = zdc_program_asciify(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6611 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6612 | case BC_INST_PRINT_STREAM: |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6613 | dbg_exec("BC_INST_STREAM:"); |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6614 | s = zdc_program_printStream(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6615 | break; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6616 | case BC_INST_LOAD: |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6617 | case BC_INST_PUSH_VAR: { |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6618 | bool copy = inst == BC_INST_LOAD; |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6619 | s = zbc_program_pushVar(code, &ip->inst_idx, true, copy); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6620 | break; |
| 6621 | } |
Denys Vlasenko | 927a7d6 | 2018-12-09 02:24:14 +0100 | [diff] [blame] | 6622 | case BC_INST_PUSH_TO_VAR: { |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6623 | char *name = bc_program_name(code, &ip->inst_idx); |
Denys Vlasenko | 2930123 | 2018-12-11 15:29:32 +0100 | [diff] [blame] | 6624 | s = zbc_program_copyToVar(name, true); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6625 | free(name); |
| 6626 | break; |
| 6627 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6628 | case BC_INST_QUIT: |
Denys Vlasenko | dfe1dd2 | 2018-12-19 17:09:01 +0100 | [diff] [blame] | 6629 | dbg_exec("BC_INST_QUIT:"); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6630 | if (G.prog.exestack.len <= 2) |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 6631 | QUIT_OR_RETURN_TO_MAIN; |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6632 | bc_vec_npop(&G.prog.exestack, 2); |
Denys Vlasenko | 085b420 | 2018-12-19 14:02:59 +0100 | [diff] [blame] | 6633 | goto read_updated_ip; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6634 | case BC_INST_NQUIT: |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6635 | dbg_exec("BC_INST_NQUIT:"); |
Denys Vlasenko | fa21079 | 2018-12-19 19:35:40 +0100 | [diff] [blame] | 6636 | s = zdc_program_nquit(); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6637 | //goto read_updated_ip; - just fall through to it |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6638 | #endif // ENABLE_DC |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 6639 | read_updated_ip: |
| 6640 | // Instruction stack has changed, read new pointers |
| 6641 | ip = bc_vec_top(&G.prog.exestack); |
| 6642 | func = bc_program_func(ip->func); |
| 6643 | code = func->code.v; |
Denys Vlasenko | 24e4194 | 2018-12-21 23:01:26 +0100 | [diff] [blame] | 6644 | dbg_exec("func:%zd bytes:%zd ip:%zd", ip->func, func->code.len, ip->inst_idx); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6645 | } |
| 6646 | |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6647 | if (s || G_interrupt) { |
| 6648 | bc_program_reset(); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6649 | RETURN_STATUS(s); |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6650 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6651 | |
Denys Vlasenko | c5774a3 | 2018-12-17 01:22:53 +0100 | [diff] [blame] | 6652 | fflush_and_check(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6653 | } |
| 6654 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6655 | RETURN_STATUS(BC_STATUS_SUCCESS); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6656 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 6657 | #define zbc_program_exec(...) (zbc_program_exec(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6658 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6659 | static unsigned bc_vm_envLen(const char *var) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6660 | { |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6661 | char *lenv; |
| 6662 | unsigned len; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6663 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6664 | lenv = getenv(var); |
| 6665 | len = BC_NUM_PRINT_WIDTH; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6666 | if (!lenv) return len; |
| 6667 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 6668 | len = bb_strtou(lenv, NULL, 10) - 1; |
| 6669 | if (errno || len < 2 || len >= INT_MAX) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6670 | len = BC_NUM_PRINT_WIDTH; |
| 6671 | |
| 6672 | return len; |
| 6673 | } |
| 6674 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6675 | static BC_STATUS zbc_vm_process(const char *text) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6676 | { |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 6677 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6678 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 6679 | dbg_lex_enter("%s:%d entered", __func__, __LINE__); |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6680 | s = zbc_parse_text_init(&G.prs, text); // does the first zbc_lex_next() |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6681 | if (s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6682 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 6683 | while (G.prs.l.t.t != BC_LEX_EOF) { |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6684 | BcInstPtr *ip; |
| 6685 | BcFunc *f; |
| 6686 | |
Denys Vlasenko | 39287e0 | 2018-12-22 02:23:08 +0100 | [diff] [blame] | 6687 | dbg_lex("%s:%d G.prs.l.t.t:%d, parsing...", __func__, __LINE__, G.prs.l.t.t); |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 6688 | if (IS_BC) { |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6689 | // FIXME: "eating" of stmt delimiters is coded inconsistently |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 6690 | // (sometimes zbc_parse_stmt() eats the delimiter, sometimes don't), |
| 6691 | // which causes bugs such as "print 1 print 2" erroneously accepted, |
| 6692 | // or "print 1 else 2" detecting parse error only after executing |
| 6693 | // "print 1" part. |
| 6694 | IF_BC(s = zbc_parse_stmt_or_funcdef(&G.prs)); |
| 6695 | } else { |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6696 | IF_DC(s = zdc_parse_expr(&G.prs)); |
Denys Vlasenko | 88fcd5c | 2018-12-22 06:00:25 +0100 | [diff] [blame] | 6697 | } |
| 6698 | if (s || G_interrupt) { |
| 6699 | bc_parse_reset(&G.prs); // includes bc_program_reset() |
| 6700 | RETURN_STATUS(BC_STATUS_FAILURE); |
| 6701 | } |
| 6702 | |
Denys Vlasenko | 39287e0 | 2018-12-22 02:23:08 +0100 | [diff] [blame] | 6703 | dbg_lex("%s:%d executing...", __func__, __LINE__); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6704 | s = zbc_program_exec(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 6705 | if (s) { |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 6706 | bc_program_reset(); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 6707 | break; |
| 6708 | } |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6709 | |
| 6710 | ip = (void*)G.prog.exestack.v; |
| 6711 | #if SANITY_CHECKS |
| 6712 | if (G.prog.exestack.len != 1) // should have only main's IP |
| 6713 | bb_error_msg_and_die("BUG:call stack"); |
| 6714 | if (ip->func != BC_PROG_MAIN) |
| 6715 | bb_error_msg_and_die("BUG:not MAIN"); |
| 6716 | #endif |
| 6717 | f = bc_program_func_BC_PROG_MAIN(); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 6718 | // bc discards strings, constants and code after each |
| 6719 | // top-level statement in the "main program". |
| 6720 | // This prevents "yes 1 | bc" from growing its memory |
| 6721 | // without bound. This can be done because data stack |
| 6722 | // is empty and thus can't hold any references to |
| 6723 | // strings or constants, there is no generated code |
| 6724 | // which can hold references (after we discard one |
| 6725 | // we just executed). Code of functions can have references, |
| 6726 | // but bc stores function strings/constants in per-function |
| 6727 | // storage. |
| 6728 | if (IS_BC) { |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 6729 | #if SANITY_CHECKS |
Denys Vlasenko | 7c1c9dc | 2018-12-22 14:18:47 +0100 | [diff] [blame] | 6730 | if (G.prog.results.len != 0) // should be empty |
| 6731 | bb_error_msg_and_die("BUG:data stack"); |
Denys Vlasenko | 19eee8e | 2018-12-21 20:29:34 +0100 | [diff] [blame] | 6732 | #endif |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 6733 | IF_BC(bc_vec_pop_all(&f->strs);) |
| 6734 | IF_BC(bc_vec_pop_all(&f->consts);) |
Denys Vlasenko | badf683 | 2018-12-22 18:04:08 +0100 | [diff] [blame] | 6735 | } else { |
| 6736 | // Most of dc parsing assumes all whitespace, |
| 6737 | // including '\n', is eaten. |
| 6738 | if (G.prs.l.t.t == BC_LEX_NLINE) { |
| 6739 | s = zbc_lex_next(&G.prs.l); |
| 6740 | if (s) RETURN_STATUS(s); |
| 6741 | } |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6742 | |
| 6743 | if (G.prog.results.len == 0 |
| 6744 | && G.prog.vars.len == 0 |
| 6745 | ) { |
| 6746 | // If stack is empty and no registers exist (TODO: or they are all empty), |
| 6747 | // we can get rid of accumulated strings and constants. |
| 6748 | // In this example dc process should not grow |
| 6749 | // its memory consumption with time: |
| 6750 | // yes 1pc | dc |
| 6751 | IF_DC(bc_vec_pop_all(&G.prog.strs);) |
| 6752 | IF_DC(bc_vec_pop_all(&G.prog.consts);) |
| 6753 | } |
| 6754 | // The code is discarded always (below), thus this example |
| 6755 | // should also not grow its memory consumption with time, |
| 6756 | // even though its data stack is not empty: |
| 6757 | // { echo 1; yes dk; } | dc |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 6758 | } |
Denys Vlasenko | ec74a9c | 2018-12-22 19:23:46 +0100 | [diff] [blame] | 6759 | // We drop generated and executed code for both bc and dc: |
| 6760 | bc_vec_pop_all(&f->code); |
| 6761 | ip->inst_idx = 0; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6762 | } |
| 6763 | |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 6764 | dbg_lex_done("%s:%d done", __func__, __LINE__); |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6765 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6766 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 6767 | #define zbc_vm_process(...) (zbc_vm_process(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6768 | |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 6769 | static BC_STATUS zbc_vm_execute_FILE(FILE *fp, const char *filename) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6770 | { |
Denys Vlasenko | 0fe270e | 2018-12-13 19:58:58 +0100 | [diff] [blame] | 6771 | // So far bc/dc have no way to include a file from another file, |
| 6772 | // therefore we know G.prog.file == NULL on entry |
| 6773 | //const char *sv_file; |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 6774 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6775 | |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 6776 | G.prog.file = filename; |
| 6777 | G.input_fp = fp; |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 6778 | bc_lex_file(&G.prs.l); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6779 | |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 6780 | do { |
| 6781 | s = zbc_vm_process(""); |
| 6782 | // We do not stop looping on errors here if reading stdin. |
| 6783 | // Example: start interactive bc and enter "return". |
| 6784 | // It should say "'return' not in a function" |
| 6785 | // but should not exit. |
| 6786 | } while (G.input_fp == stdin); |
Denys Vlasenko | 0fe270e | 2018-12-13 19:58:58 +0100 | [diff] [blame] | 6787 | G.prog.file = NULL; |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 6788 | RETURN_STATUS(s); |
| 6789 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 6790 | #define zbc_vm_execute_FILE(...) (zbc_vm_execute_FILE(__VA_ARGS__) COMMA_SUCCESS) |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 6791 | |
| 6792 | static BC_STATUS zbc_vm_file(const char *file) |
| 6793 | { |
| 6794 | BcStatus s; |
| 6795 | FILE *fp; |
| 6796 | |
| 6797 | fp = xfopen_for_read(file); |
| 6798 | s = zbc_vm_execute_FILE(fp, file); |
| 6799 | fclose(fp); |
| 6800 | |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 6801 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6802 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 6803 | #define zbc_vm_file(...) (zbc_vm_file(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 6804 | |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6805 | #if ENABLE_BC |
Denys Vlasenko | 57b6918 | 2018-12-14 00:12:13 +0100 | [diff] [blame] | 6806 | static void bc_vm_info(void) |
| 6807 | { |
| 6808 | printf("%s "BB_VER"\n" |
| 6809 | "Copyright (c) 2018 Gavin D. Howard and contributors\n" |
| 6810 | , applet_name); |
| 6811 | } |
| 6812 | |
| 6813 | static void bc_args(char **argv) |
| 6814 | { |
| 6815 | unsigned opts; |
| 6816 | int i; |
| 6817 | |
| 6818 | GETOPT_RESET(); |
| 6819 | #if ENABLE_FEATURE_BC_LONG_OPTIONS |
| 6820 | opts = option_mask32 |= getopt32long(argv, "wvsqli", |
| 6821 | "warn\0" No_argument "w" |
| 6822 | "version\0" No_argument "v" |
| 6823 | "standard\0" No_argument "s" |
| 6824 | "quiet\0" No_argument "q" |
| 6825 | "mathlib\0" No_argument "l" |
| 6826 | "interactive\0" No_argument "i" |
| 6827 | ); |
| 6828 | #else |
| 6829 | opts = option_mask32 |= getopt32(argv, "wvsqli"); |
| 6830 | #endif |
| 6831 | if (getenv("POSIXLY_CORRECT")) |
| 6832 | option_mask32 |= BC_FLAG_S; |
| 6833 | |
| 6834 | if (opts & BC_FLAG_V) { |
| 6835 | bc_vm_info(); |
| 6836 | exit(0); |
| 6837 | } |
| 6838 | |
| 6839 | for (i = optind; argv[i]; ++i) |
| 6840 | bc_vec_push(&G.files, argv + i); |
| 6841 | } |
| 6842 | |
| 6843 | static void bc_vm_envArgs(void) |
| 6844 | { |
| 6845 | BcVec v; |
| 6846 | char *buf; |
| 6847 | char *env_args = getenv("BC_ENV_ARGS"); |
| 6848 | |
| 6849 | if (!env_args) return; |
| 6850 | |
| 6851 | G.env_args = xstrdup(env_args); |
| 6852 | buf = G.env_args; |
| 6853 | |
| 6854 | bc_vec_init(&v, sizeof(char *), NULL); |
| 6855 | |
| 6856 | while (*(buf = skip_whitespace(buf)) != '\0') { |
| 6857 | bc_vec_push(&v, &buf); |
| 6858 | buf = skip_non_whitespace(buf); |
| 6859 | if (!*buf) |
| 6860 | break; |
| 6861 | *buf++ = '\0'; |
| 6862 | } |
| 6863 | |
| 6864 | // NULL terminate, and pass argv[] so that first arg is argv[1] |
| 6865 | if (sizeof(int) == sizeof(char*)) { |
| 6866 | bc_vec_push(&v, &const_int_0); |
| 6867 | } else { |
| 6868 | static char *const nullptr = NULL; |
| 6869 | bc_vec_push(&v, &nullptr); |
| 6870 | } |
| 6871 | bc_args(((char **)v.v) - 1); |
| 6872 | |
| 6873 | bc_vec_free(&v); |
| 6874 | } |
| 6875 | |
| 6876 | static const char bc_lib[] ALIGN1 = { |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6877 | "scale=20" |
| 6878 | "\n" "define e(x){" |
| 6879 | "\n" "auto b,s,n,r,d,i,p,f,v" |
Denys Vlasenko | 203210e | 2018-12-14 09:53:50 +0100 | [diff] [blame] | 6880 | ////////////////"if(x<0)return(1/e(-x))" // and drop 'n' and x<0 logic below |
| 6881 | //^^^^^^^^^^^^^^^^ this would work, and is even more precise than GNU bc: |
| 6882 | //e(-.998896): GNU:.36828580434569428695 |
| 6883 | // above code:.36828580434569428696 |
| 6884 | // actual value:.3682858043456942869594... |
| 6885 | // but for now let's be "GNU compatible" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6886 | "\n" "b=ibase" |
| 6887 | "\n" "ibase=A" |
| 6888 | "\n" "if(x<0){" |
| 6889 | "\n" "n=1" |
| 6890 | "\n" "x=-x" |
| 6891 | "\n" "}" |
| 6892 | "\n" "s=scale" |
Denys Vlasenko | 146a79d | 2018-12-16 21:46:11 +0100 | [diff] [blame] | 6893 | "\n" "r=6+s+.44*x" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6894 | "\n" "scale=scale(x)+1" |
| 6895 | "\n" "while(x>1){" |
| 6896 | "\n" "d+=1" |
| 6897 | "\n" "x/=2" |
| 6898 | "\n" "scale+=1" |
| 6899 | "\n" "}" |
| 6900 | "\n" "scale=r" |
| 6901 | "\n" "r=x+1" |
| 6902 | "\n" "p=x" |
| 6903 | "\n" "f=v=1" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 6904 | "\n" "for(i=2;v;++i){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6905 | "\n" "p*=x" |
| 6906 | "\n" "f*=i" |
| 6907 | "\n" "v=p/f" |
| 6908 | "\n" "r+=v" |
| 6909 | "\n" "}" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 6910 | "\n" "while(d--)r*=r" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6911 | "\n" "scale=s" |
| 6912 | "\n" "ibase=b" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 6913 | "\n" "if(n)return(1/r)" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6914 | "\n" "return(r/1)" |
| 6915 | "\n" "}" |
| 6916 | "\n" "define l(x){" |
| 6917 | "\n" "auto b,s,r,p,a,q,i,v" |
| 6918 | "\n" "b=ibase" |
| 6919 | "\n" "ibase=A" |
| 6920 | "\n" "if(x<=0){" |
| 6921 | "\n" "r=(1-10^scale)/1" |
| 6922 | "\n" "ibase=b" |
| 6923 | "\n" "return(r)" |
| 6924 | "\n" "}" |
| 6925 | "\n" "s=scale" |
| 6926 | "\n" "scale+=6" |
| 6927 | "\n" "p=2" |
| 6928 | "\n" "while(x>=2){" |
| 6929 | "\n" "p*=2" |
| 6930 | "\n" "x=sqrt(x)" |
| 6931 | "\n" "}" |
Denys Vlasenko | 146a79d | 2018-12-16 21:46:11 +0100 | [diff] [blame] | 6932 | "\n" "while(x<=.5){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6933 | "\n" "p*=2" |
| 6934 | "\n" "x=sqrt(x)" |
| 6935 | "\n" "}" |
| 6936 | "\n" "r=a=(x-1)/(x+1)" |
| 6937 | "\n" "q=a*a" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 6938 | "\n" "v=1" |
| 6939 | "\n" "for(i=3;v;i+=2){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6940 | "\n" "a*=q" |
| 6941 | "\n" "v=a/i" |
| 6942 | "\n" "r+=v" |
| 6943 | "\n" "}" |
| 6944 | "\n" "r*=p" |
| 6945 | "\n" "scale=s" |
| 6946 | "\n" "ibase=b" |
| 6947 | "\n" "return(r/1)" |
| 6948 | "\n" "}" |
| 6949 | "\n" "define s(x){" |
Denys Vlasenko | 240d7ee | 2018-12-14 11:27:09 +0100 | [diff] [blame] | 6950 | "\n" "auto b,s,r,a,q,i" |
| 6951 | "\n" "if(x<0)return(-s(-x))" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6952 | "\n" "b=ibase" |
| 6953 | "\n" "ibase=A" |
| 6954 | "\n" "s=scale" |
| 6955 | "\n" "scale=1.1*s+2" |
| 6956 | "\n" "a=a(1)" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6957 | "\n" "scale=0" |
| 6958 | "\n" "q=(x/a+2)/4" |
Denys Vlasenko | 203210e | 2018-12-14 09:53:50 +0100 | [diff] [blame] | 6959 | "\n" "x-=4*q*a" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 6960 | "\n" "if(q%2)x=-x" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6961 | "\n" "scale=s+2" |
| 6962 | "\n" "r=a=x" |
| 6963 | "\n" "q=-x*x" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 6964 | "\n" "for(i=3;a;i+=2){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6965 | "\n" "a*=q/(i*(i-1))" |
| 6966 | "\n" "r+=a" |
| 6967 | "\n" "}" |
| 6968 | "\n" "scale=s" |
| 6969 | "\n" "ibase=b" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6970 | "\n" "return(r/1)" |
| 6971 | "\n" "}" |
| 6972 | "\n" "define c(x){" |
| 6973 | "\n" "auto b,s" |
| 6974 | "\n" "b=ibase" |
| 6975 | "\n" "ibase=A" |
| 6976 | "\n" "s=scale" |
| 6977 | "\n" "scale*=1.2" |
| 6978 | "\n" "x=s(2*a(1)+x)" |
| 6979 | "\n" "scale=s" |
| 6980 | "\n" "ibase=b" |
| 6981 | "\n" "return(x/1)" |
| 6982 | "\n" "}" |
| 6983 | "\n" "define a(x){" |
| 6984 | "\n" "auto b,s,r,n,a,m,t,f,i,u" |
| 6985 | "\n" "b=ibase" |
| 6986 | "\n" "ibase=A" |
| 6987 | "\n" "n=1" |
| 6988 | "\n" "if(x<0){" |
| 6989 | "\n" "n=-1" |
| 6990 | "\n" "x=-x" |
| 6991 | "\n" "}" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 6992 | "\n" "if(scale<65){" |
| 6993 | "\n" "if(x==1)return(.7853981633974483096156608458198757210492923498437764552437361480/n)" |
| 6994 | "\n" "if(x==.2)return(.1973955598498807583700497651947902934475851037878521015176889402/n)" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 6995 | "\n" "}" |
| 6996 | "\n" "s=scale" |
| 6997 | "\n" "if(x>.2){" |
| 6998 | "\n" "scale+=5" |
| 6999 | "\n" "a=a(.2)" |
| 7000 | "\n" "}" |
| 7001 | "\n" "scale=s+3" |
| 7002 | "\n" "while(x>.2){" |
| 7003 | "\n" "m+=1" |
| 7004 | "\n" "x=(x-.2)/(1+.2*x)" |
| 7005 | "\n" "}" |
| 7006 | "\n" "r=u=x" |
| 7007 | "\n" "f=-x*x" |
| 7008 | "\n" "t=1" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7009 | "\n" "for(i=3;t;i+=2){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7010 | "\n" "u*=f" |
| 7011 | "\n" "t=u/i" |
| 7012 | "\n" "r+=t" |
| 7013 | "\n" "}" |
| 7014 | "\n" "scale=s" |
| 7015 | "\n" "ibase=b" |
| 7016 | "\n" "return((m*a+r)/n)" |
| 7017 | "\n" "}" |
| 7018 | "\n" "define j(n,x){" |
| 7019 | "\n" "auto b,s,o,a,i,v,f" |
| 7020 | "\n" "b=ibase" |
| 7021 | "\n" "ibase=A" |
| 7022 | "\n" "s=scale" |
| 7023 | "\n" "scale=0" |
| 7024 | "\n" "n/=1" |
| 7025 | "\n" "if(n<0){" |
| 7026 | "\n" "n=-n" |
Denys Vlasenko | 203210e | 2018-12-14 09:53:50 +0100 | [diff] [blame] | 7027 | "\n" "o=n%2" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7028 | "\n" "}" |
| 7029 | "\n" "a=1" |
| 7030 | "\n" "for(i=2;i<=n;++i)a*=i" |
| 7031 | "\n" "scale=1.5*s" |
| 7032 | "\n" "a=(x^n)/2^n/a" |
| 7033 | "\n" "r=v=1" |
| 7034 | "\n" "f=-x*x/4" |
Denys Vlasenko | c06537d | 2018-12-14 10:10:37 +0100 | [diff] [blame] | 7035 | "\n" "scale+=length(a)-scale(a)" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7036 | "\n" "for(i=1;v;++i){" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7037 | "\n" "v=v*f/i/(n+i)" |
| 7038 | "\n" "r+=v" |
| 7039 | "\n" "}" |
| 7040 | "\n" "scale=s" |
| 7041 | "\n" "ibase=b" |
Denys Vlasenko | 3ac0c21 | 2018-12-14 01:01:01 +0100 | [diff] [blame] | 7042 | "\n" "if(o)a=-a" |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7043 | "\n" "return(a*r/1)" |
| 7044 | "\n" "}" |
| 7045 | }; |
| 7046 | #endif // ENABLE_BC |
| 7047 | |
Denys Vlasenko | 19f1107 | 2018-12-12 22:48:19 +0100 | [diff] [blame] | 7048 | static BC_STATUS zbc_vm_exec(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7049 | { |
Denys Vlasenko | ea5cad2 | 2018-12-19 18:09:31 +0100 | [diff] [blame] | 7050 | char **fname; |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7051 | BcStatus s; |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7052 | size_t i; |
| 7053 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7054 | #if ENABLE_BC |
Denys Vlasenko | d70d4a0 | 2018-12-04 20:58:40 +0100 | [diff] [blame] | 7055 | if (option_mask32 & BC_FLAG_L) { |
Denys Vlasenko | 0ad36c4 | 2018-12-05 16:21:43 +0100 | [diff] [blame] | 7056 | // We know that internal library is not buggy, |
| 7057 | // thus error checking is normally disabled. |
| 7058 | # define DEBUG_LIB 0 |
Denys Vlasenko | 0409ad3 | 2018-12-05 16:39:22 +0100 | [diff] [blame] | 7059 | bc_lex_file(&G.prs.l); |
Denys Vlasenko | 2ea53a4 | 2018-12-14 17:51:17 +0100 | [diff] [blame] | 7060 | s = zbc_vm_process(bc_lib); |
Denys Vlasenko | 19f1107 | 2018-12-12 22:48:19 +0100 | [diff] [blame] | 7061 | if (DEBUG_LIB && s) RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7062 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7063 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7064 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7065 | s = BC_STATUS_SUCCESS; |
Denys Vlasenko | ea5cad2 | 2018-12-19 18:09:31 +0100 | [diff] [blame] | 7066 | fname = (void*)G.files.v; |
| 7067 | for (i = 0; i < G.files.len; i++) { |
| 7068 | s = zbc_vm_file(*fname++); |
| 7069 | if (ENABLE_FEATURE_CLEAN_UP && !G_ttyin && s) { |
| 7070 | // Debug config, non-interactive mode: |
| 7071 | // return all the way back to main. |
| 7072 | // Non-debug builds do not come here |
| 7073 | // in non-interactive mode, they exit. |
| 7074 | RETURN_STATUS(s); |
| 7075 | } |
Denys Vlasenko | 9b70f19 | 2018-12-04 20:51:40 +0100 | [diff] [blame] | 7076 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7077 | |
Denys Vlasenko | 91cde95 | 2018-12-10 20:56:08 +0100 | [diff] [blame] | 7078 | if (IS_BC || (option_mask32 & BC_FLAG_I)) |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 7079 | s = zbc_vm_execute_FILE(stdin, /*filename:*/ NULL); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7080 | |
Denys Vlasenko | 19f1107 | 2018-12-12 22:48:19 +0100 | [diff] [blame] | 7081 | RETURN_STATUS(s); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7082 | } |
Denys Vlasenko | ec60318 | 2018-12-17 10:34:02 +0100 | [diff] [blame] | 7083 | #define zbc_vm_exec(...) (zbc_vm_exec(__VA_ARGS__) COMMA_SUCCESS) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7084 | |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7085 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 7086 | static void bc_program_free(void) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7087 | { |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7088 | bc_vec_free(&G.prog.fns); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7089 | IF_BC(bc_vec_free(&G.prog.fn_map);) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7090 | bc_vec_free(&G.prog.vars); |
| 7091 | bc_vec_free(&G.prog.var_map); |
| 7092 | bc_vec_free(&G.prog.arrs); |
| 7093 | bc_vec_free(&G.prog.arr_map); |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 7094 | IF_DC(bc_vec_free(&G.prog.strs);) |
| 7095 | IF_DC(bc_vec_free(&G.prog.consts);) |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7096 | bc_vec_free(&G.prog.results); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 7097 | bc_vec_free(&G.prog.exestack); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7098 | IF_BC(bc_num_free(&G.prog.last);) |
| 7099 | IF_BC(bc_num_free(&G.prog.zero);) |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 7100 | IF_BC(bc_num_free(&G.prog.one);) |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 7101 | bc_vec_free(&G.input_buffer); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7102 | } |
| 7103 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7104 | static void bc_vm_free(void) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7105 | { |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7106 | bc_vec_free(&G.files); |
Denys Vlasenko | a1d3ca2 | 2018-12-02 18:26:38 +0100 | [diff] [blame] | 7107 | bc_program_free(); |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7108 | bc_parse_free(&G.prs); |
| 7109 | free(G.env_args); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7110 | } |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7111 | #endif |
| 7112 | |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7113 | static void bc_program_init(void) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7114 | { |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7115 | BcInstPtr ip; |
| 7116 | |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7117 | // memset(&G.prog, 0, sizeof(G.prog)); - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7118 | memset(&ip, 0, sizeof(BcInstPtr)); |
| 7119 | |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7120 | // G.prog.nchars = G.prog.scale = 0; - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7121 | G.prog.ib_t = 10; |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7122 | G.prog.ob_t = 10; |
| 7123 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 7124 | IF_BC(bc_num_init_DEF_SIZE(&G.prog.last);) |
| 7125 | //IF_BC(bc_num_zero(&G.prog.last);) - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7126 | |
Denys Vlasenko | e20e00d | 2018-12-09 11:44:20 +0100 | [diff] [blame] | 7127 | bc_num_init_DEF_SIZE(&G.prog.zero); |
Denys Vlasenko | 3129f70 | 2018-12-09 12:04:44 +0100 | [diff] [blame] | 7128 | //bc_num_zero(&G.prog.zero); - already is |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7129 | |
Denys Vlasenko | 503faf9 | 2018-12-20 16:24:18 +0100 | [diff] [blame] | 7130 | IF_BC(bc_num_init_DEF_SIZE(&G.prog.one);) |
| 7131 | IF_BC(bc_num_one(&G.prog.one);) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7132 | |
| 7133 | bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free); |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7134 | IF_BC(bc_vec_init(&G.prog.fn_map, sizeof(BcId), bc_id_free);) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7135 | |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7136 | if (IS_BC) { |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 7137 | // Names are chosen simply to be distinct and never match |
Denys Vlasenko | 0471544 | 2018-12-21 00:10:26 +0100 | [diff] [blame] | 7138 | // a valid function name (and be short) |
| 7139 | IF_BC(bc_program_addFunc(xstrdup(""))); // func #0: main |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 7140 | IF_BC(bc_program_addFunc(xstrdup("1"))); // func #1: for read() |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7141 | } else { |
Denys Vlasenko | 8c1e723 | 2018-12-22 01:34:10 +0100 | [diff] [blame] | 7142 | // in dc, functions have no names |
Denys Vlasenko | 44a99ca | 2018-12-20 20:34:09 +0100 | [diff] [blame] | 7143 | bc_program_add_fn(); |
| 7144 | bc_program_add_fn(); |
| 7145 | } |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7146 | |
| 7147 | bc_vec_init(&G.prog.vars, sizeof(BcVec), bc_vec_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7148 | bc_vec_init(&G.prog.var_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7149 | |
| 7150 | bc_vec_init(&G.prog.arrs, sizeof(BcVec), bc_vec_free); |
Denys Vlasenko | cb9a99f | 2018-12-04 21:54:33 +0100 | [diff] [blame] | 7151 | bc_vec_init(&G.prog.arr_map, sizeof(BcId), bc_id_free); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7152 | |
Denys Vlasenko | 5d57bc4 | 2018-12-21 16:22:26 +0100 | [diff] [blame] | 7153 | IF_DC(bc_vec_init(&G.prog.strs, sizeof(char *), bc_string_free);) |
| 7154 | IF_DC(bc_vec_init(&G.prog.consts, sizeof(char *), bc_string_free);) |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7155 | bc_vec_init(&G.prog.results, sizeof(BcResult), bc_result_free); |
Denys Vlasenko | b80d7aa | 2018-12-19 12:35:27 +0100 | [diff] [blame] | 7156 | bc_vec_init(&G.prog.exestack, sizeof(BcInstPtr), NULL); |
| 7157 | bc_vec_push(&G.prog.exestack, &ip); |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 7158 | |
Denys Vlasenko | e4ba4c4 | 2018-12-17 09:51:43 +0100 | [diff] [blame] | 7159 | bc_char_vec_init(&G.input_buffer); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7160 | } |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7161 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7162 | static int bc_vm_init(const char *env_len) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7163 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7164 | #if ENABLE_FEATURE_EDITING |
| 7165 | G.line_input_state = new_line_input_t(DO_HISTORY); |
| 7166 | #endif |
| 7167 | G.prog.len = bc_vm_envLen(env_len); |
| 7168 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7169 | bc_vec_init(&G.files, sizeof(char *), NULL); |
Denys Vlasenko | 5f263f4 | 2018-12-13 22:49:59 +0100 | [diff] [blame] | 7170 | IF_BC(if (IS_BC) bc_vm_envArgs();) |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7171 | bc_program_init(); |
Denys Vlasenko | c2265f5 | 2018-12-12 23:10:08 +0100 | [diff] [blame] | 7172 | bc_parse_create(&G.prs, BC_PROG_MAIN); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7173 | |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 7174 | //TODO: in GNU bc, the check is (isatty(0) && isatty(1)), |
| 7175 | //-i option unconditionally enables this regardless of isatty(): |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7176 | if (isatty(0)) { |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7177 | #if ENABLE_FEATURE_BC_SIGNALS |
Denys Vlasenko | 1a6a482 | 2018-12-06 09:20:32 +0100 | [diff] [blame] | 7178 | G_ttyin = 1; |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7179 | // With SA_RESTART, most system calls will restart |
| 7180 | // (IOW: they won't fail with EINTR). |
| 7181 | // In particular, this means ^C won't cause |
| 7182 | // stdout to get into "error state" if SIGINT hits |
| 7183 | // within write() syscall. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 7184 | // |
| 7185 | // The downside is that ^C while tty input is taken |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7186 | // will only be handled after [Enter] since read() |
| 7187 | // from stdin is not interrupted by ^C either, |
| 7188 | // it restarts, thus fgetc() does not return on ^C. |
Denys Vlasenko | 89e785a | 2018-12-13 16:35:52 +0100 | [diff] [blame] | 7189 | // (This problem manifests only if line editing is disabled) |
Denys Vlasenko | 17c5472 | 2018-12-04 21:21:32 +0100 | [diff] [blame] | 7190 | signal_SA_RESTART_empty_mask(SIGINT, record_signo); |
| 7191 | |
| 7192 | // Without SA_RESTART, this exhibits a bug: |
| 7193 | // "while (1) print 1" and try ^C-ing it. |
| 7194 | // Intermittently, instead of returning to input line, |
| 7195 | // you'll get "output error: Interrupted system call" |
| 7196 | // and exit. |
| 7197 | //signal_no_SA_RESTART_empty_mask(SIGINT, record_signo); |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7198 | #endif |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7199 | return 1; // "tty" |
Denys Vlasenko | d38af48 | 2018-12-04 19:11:02 +0100 | [diff] [blame] | 7200 | } |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7201 | return 0; // "not a tty" |
| 7202 | } |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7203 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7204 | static BcStatus bc_vm_run(void) |
| 7205 | { |
Denys Vlasenko | 19f1107 | 2018-12-12 22:48:19 +0100 | [diff] [blame] | 7206 | BcStatus st = zbc_vm_exec(); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7207 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | c7a7ce0 | 2018-12-06 23:06:57 +0100 | [diff] [blame] | 7208 | if (G_exiting) // it was actually "halt" or "quit" |
| 7209 | st = EXIT_SUCCESS; |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7210 | bc_vm_free(); |
Denys Vlasenko | 95f93bd | 2018-12-06 10:29:12 +0100 | [diff] [blame] | 7211 | # if ENABLE_FEATURE_EDITING |
| 7212 | free_line_input_t(G.line_input_state); |
| 7213 | # endif |
Denys Vlasenko | e873ff9 | 2018-12-06 00:29:22 +0100 | [diff] [blame] | 7214 | FREE_G(); |
Denys Vlasenko | 785e4b3 | 2018-12-02 17:18:52 +0100 | [diff] [blame] | 7215 | #endif |
Denys Vlasenko | d1d29b4 | 2018-12-16 16:03:03 +0100 | [diff] [blame] | 7216 | dbg_exec("exiting with exitcode %d", st); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7217 | return st; |
| 7218 | } |
| 7219 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7220 | #if ENABLE_BC |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7221 | int bc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7222 | int bc_main(int argc UNUSED_PARAM, char **argv) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7223 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7224 | int is_tty; |
| 7225 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7226 | INIT_G(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7227 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7228 | is_tty = bc_vm_init("BC_LINE_LENGTH"); |
| 7229 | |
| 7230 | bc_args(argv); |
| 7231 | |
| 7232 | if (is_tty && !(option_mask32 & BC_FLAG_Q)) |
| 7233 | bc_vm_info(); |
| 7234 | |
| 7235 | return bc_vm_run(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7236 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7237 | #endif |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7238 | |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7239 | #if ENABLE_DC |
Denys Vlasenko | 5a9fef5 | 2018-12-02 14:35:32 +0100 | [diff] [blame] | 7240 | int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 1ff1c70 | 2018-12-06 00:46:09 +0100 | [diff] [blame] | 7241 | int dc_main(int argc UNUSED_PARAM, char **argv) |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7242 | { |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7243 | int noscript; |
| 7244 | |
Denys Vlasenko | 6d9146a | 2018-12-02 15:48:37 +0100 | [diff] [blame] | 7245 | INIT_G(); |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7246 | |
| 7247 | // TODO: dc (GNU bc 1.07.1) 1.4.1 seems to use width |
| 7248 | // 1 char wider than bc from the same package. |
| 7249 | // Both default width, and xC_LINE_LENGTH=N are wider: |
| 7250 | // "DC_LINE_LENGTH=5 dc -e'123456 p'" prints: |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 7251 | // |1234\ | |
| 7252 | // |56 | |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7253 | // "echo '123456' | BC_LINE_LENGTH=5 bc" prints: |
Denys Vlasenko | 0a23814 | 2018-12-14 16:48:34 +0100 | [diff] [blame] | 7254 | // |123\ | |
| 7255 | // |456 | |
Denys Vlasenko | 8226912 | 2018-12-14 16:30:56 +0100 | [diff] [blame] | 7256 | // Do the same, or it's a bug? |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7257 | bc_vm_init("DC_LINE_LENGTH"); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7258 | |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7259 | // Run -e'SCRIPT' and -fFILE in order of appearance, then handle FILEs |
| 7260 | noscript = BC_FLAG_I; |
| 7261 | for (;;) { |
| 7262 | int n = getopt(argc, argv, "e:f:x"); |
| 7263 | if (n <= 0) |
| 7264 | break; |
| 7265 | switch (n) { |
| 7266 | case 'e': |
| 7267 | noscript = 0; |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 7268 | n = zbc_vm_process(optarg); |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7269 | if (n) return n; |
| 7270 | break; |
| 7271 | case 'f': |
| 7272 | noscript = 0; |
Denys Vlasenko | d6ad366 | 2018-12-12 21:39:10 +0100 | [diff] [blame] | 7273 | n = zbc_vm_file(optarg); |
| 7274 | if (n) return n; |
Denys Vlasenko | 6d0be10 | 2018-12-06 18:41:59 +0100 | [diff] [blame] | 7275 | break; |
| 7276 | case 'x': |
| 7277 | option_mask32 |= DC_FLAG_X; |
| 7278 | break; |
| 7279 | default: |
| 7280 | bb_show_usage(); |
| 7281 | } |
| 7282 | } |
| 7283 | argv += optind; |
| 7284 | |
| 7285 | while (*argv) { |
| 7286 | noscript = 0; |
| 7287 | bc_vec_push(&G.files, argv++); |
| 7288 | } |
| 7289 | |
| 7290 | option_mask32 |= noscript; // set BC_FLAG_I if we need to interpret stdin |
| 7291 | |
| 7292 | return bc_vm_run(); |
Gavin Howard | 01055ba | 2018-11-03 11:00:21 -0600 | [diff] [blame] | 7293 | } |
Denys Vlasenko | ef869ec | 2018-12-02 18:49:16 +0100 | [diff] [blame] | 7294 | #endif |
Denys Vlasenko | 9ca9ef2 | 2018-12-06 11:31:14 +0100 | [diff] [blame] | 7295 | |
| 7296 | #endif // not DC_SMALL |